Java Code Examples for com.google.inject.Binder#bindInterceptor()

The following examples show how to use com.google.inject.Binder#bindInterceptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GuiceUtils.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * Binds an interceptor that ensures the main ClassLoader is bound as the thread context
 * {@link ClassLoader} during JNI callbacks from mesos.  Some libraries require a thread
 * context ClassLoader be set and this ensures those libraries work properly.
 *
 * @param binder The binder to use to register an interceptor with.
 * @param wrapInterface Interface whose methods should wrapped.
 */
public static void bindJNIContextClassLoader(Binder binder, Class<?> wrapInterface) {
  final ClassLoader mainClassLoader = GuiceUtils.class.getClassLoader();
  binder.bindInterceptor(
      Matchers.subclassesOf(wrapInterface),
      interfaceMatcher(wrapInterface, false),
      new MethodInterceptor() {
        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
          Thread currentThread = Thread.currentThread();
          ClassLoader prior = currentThread.getContextClassLoader();
          try {
            currentThread.setContextClassLoader(mainClassLoader);
            return invocation.proceed();
          } finally {
            currentThread.setContextClassLoader(prior);
          }
        }
      });
}
 
Example 2
Source File: GuiceUtils.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * Binds an exception trap on all interface methods of all classes bound against an interface.
 * Individual methods may opt out of trapping by annotating with {@link AllowUnchecked}.
 * Only void methods are allowed, any non-void interface methods must explicitly opt out.
 *
 * @param binder The binder to register an interceptor with.
 * @param wrapInterface Interface whose methods should be wrapped.
 * @throws IllegalArgumentException If any of the non-whitelisted interface methods are non-void.
 */
public static void bindExceptionTrap(Binder binder, Class<?> wrapInterface)
    throws IllegalArgumentException {

  Set<Method> disallowed = ImmutableSet.copyOf(Iterables.filter(
      ImmutableList.copyOf(wrapInterface.getMethods()),
      Predicates.and(Predicates.not(IS_WHITELISTED), Predicates.not(VOID_METHOD))));
  Preconditions.checkArgument(disallowed.isEmpty(),
      "Non-void methods must be explicitly whitelisted with @AllowUnchecked: %s", disallowed);

  Matcher<Method> matcher =
      Matchers.not(WHITELIST_MATCHER).and(interfaceMatcher(wrapInterface, false));
  binder.bindInterceptor(Matchers.subclassesOf(wrapInterface), matcher,
      new MethodInterceptor() {
        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
          try {
            return invocation.proceed();
          } catch (RuntimeException e) {
            LOG.warn("Trapped uncaught exception: " + e, e);
            return null;
          }
        }
      });
}
 
Example 3
Source File: AbstractMetricsModule.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
void setAnnotation2Interceptors(Binder binder, String annotationEnable, final Class<? extends Annotation> annotationType, MethodInterceptor... interceptors) {
    String ae = Env.INSTANCE.get(annotationEnable);
    if ("false".equals(ae)) {
        LOGGER.info(annotationEnable + " set to false.");
    } else if ("true".equals(ae)) {
        LOGGER.info(annotationEnable + " set to true.");
        binder.bindInterceptor(any(), annotatedWith(annotationType), interceptors);
    } else {
        LOGGER.info(annotationEnable + " not set or wrong set (set to false by default).");
    }
}
 
Example 4
Source File: NoraUiModule.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void configure(Binder binder) {
    LOGGER.debug("NORAUI interceptors binding");
    binder.bindInterceptor(any(), annotatedWith(Conditioned.class), new ConditionedInterceptor());
    binder.bindInterceptor(Matchers.subclassesOf(Step.class).or(Matchers.subclassesOf(BrowserSteps.class)), any(), new StepInterceptor());

    LOGGER.debug("NORAUI service binding");
    binder.bind(CryptoService.class).to(CryptoServiceImpl.class).asEagerSingleton();
    binder.bind(CucumberExpressionService.class).to(CucumberExpressionServiceImpl.class).asEagerSingleton();
    binder.bind(HttpService.class).to(HttpServiceImpl.class).asEagerSingleton();
    binder.bind(ScreenService.class).to(ScreenServiceImpl.class).asEagerSingleton();
    binder.bind(UserNameService.class).to(UserNameServiceImpl.class).asEagerSingleton();
    binder.bind(TimeService.class).to(TimeServiceImpl.class).asEagerSingleton();
}
 
Example 5
Source File: AOPModule.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(final Binder binder) {
    binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Before.class), new BeforeInterceptor());
    binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(After.class), new AfterInterceptor());
    binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(BeforeAndAfter.class), new BeforeAndAfterInterceptor());

    // Interceptor More
    binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(BeforeMore.class), new BeforeMoreInterceptor());
    binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(AfterMore.class), new AfterMoreInterceptor());
    binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(BeforeAndAfterMore.class), new BeforeAndAfterMoreInterceptor());
}
 
Example 6
Source File: TimedInterceptor.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
/**
 * Installs an interceptor in a guice {@link com.google.inject.Injector}, enabling
 * {@literal @Timed} method interception in guice-provided instances.  Requires that a
 * {@link TimeSeriesRepository} is bound elsewhere.
 *
 * @param binder a guice binder to require bindings against
 */
public static void bind(Binder binder) {
  Preconditions.checkNotNull(binder);

  Bindings.requireBinding(binder, TimeSeriesRepository.class);

  TimedInterceptor interceptor = new TimedInterceptor();
  binder.requestInjection(interceptor);
  binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Timed.class), interceptor);
}
 
Example 7
Source File: AopModule.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
public static void bindThriftDecorator(
    Binder binder,
    Matcher<? super Class<?>> classMatcher,
    MethodInterceptor interceptor) {

  binder.bindInterceptor(
      classMatcher,
      Matchers.returns(Matchers.subclassesOf(Response.class)),
      interceptor);
  binder.requestInjection(interceptor);
}
 
Example 8
Source File: DubboReferenceModule.java    From nano-framework with Apache License 2.0 4 votes vote down vote up
@Override
public void configure(final Binder binder) {
    binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Reference.class), new DubboReferenceInterceptor());
}