com.google.inject.matcher.Matchers Java Examples
The following examples show how to use
com.google.inject.matcher.Matchers.
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: MetadataServiceTest.java From conductor with Apache License 2.0 | 6 votes |
@Before public void before() { metadataDAO = Mockito.mock(MetadataDAO.class); eventHandlerDAO = Mockito.mock(EventHandlerDAO.class); eventQueues = Mockito.mock(EventQueues.class); configuration = Mockito.mock(Configuration.class); when(configuration.isOwnerEmailMandatory()).thenReturn(true); Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(MetadataDAO.class).toInstance(metadataDAO); bind(EventHandlerDAO.class).toInstance(eventHandlerDAO); bind(EventQueues.class).toInstance(eventQueues); bind(Configuration.class).toInstance(configuration); install(new ValidationModule()); bindInterceptor( Matchers.any(), Matchers.annotatedWith(Service.class), new ServiceInterceptor(getProvider(Validator.class))); } }); metadataService = injector.getInstance(MetadataServiceImpl.class); }
Example #2
Source File: SecurityAopModule.java From seed with Mozilla Public License 2.0 | 6 votes |
private void bindCrudInterceptor() { Multibinder<CrudActionResolver> crudActionResolverMultibinder = Multibinder.newSetBinder(binder(), CrudActionResolver.class); for (Class<? extends CrudActionResolver> crudActionResolverClass : crudActionResolverClasses) { crudActionResolverMultibinder.addBinding().to(crudActionResolverClass); } RequiresCrudPermissionsInterceptor requiresCrudPermissionsInterceptor = new RequiresCrudPermissionsInterceptor(); requestInjection(requiresCrudPermissionsInterceptor); // Allows a single annotation at class level, or multiple annotations / one per method bindInterceptor(Matchers.annotatedWith(RequiresCrudPermissions.class), Matchers.any(), requiresCrudPermissionsInterceptor); bindInterceptor(Matchers.any(), Matchers.annotatedWith(RequiresCrudPermissions.class), requiresCrudPermissionsInterceptor); }
Example #3
Source File: ContextualProviderModule.java From ProjectAres with GNU Affero General Public License v3.0 | 6 votes |
@Override protected void configure() { bindListener(Matchers.any(), new ProvisionListener() { @Override public <T> void onProvision(ProvisionInvocation<T> provision) { final ProvisionListener.ProvisionInvocation<?> prior = ContextualProvider.provisionInvocation.get(); ContextualProvider.provisionInvocation.set(provision); try { provision.provision(); } finally { if(prior != null) { ContextualProvider.provisionInvocation.set(prior); } else { ContextualProvider.provisionInvocation.remove(); } } } }); }
Example #4
Source File: GuiceUtils.java From attic-aurora with Apache License 2.0 | 6 votes |
/** * 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 #5
Source File: TestHandlerInterception.java From ja-micro with Apache License 2.0 | 6 votes |
@Test public void test_interceptor() throws ClassNotFoundException { Injector injector = Guice.createInjector((Module) binder -> binder.bindInterceptor( Matchers.identicalTo(TestedRpcHandler.class), Matchers.any(), new RpcHandlerMethodInterceptor() )); TestedRpcHandler methodHandler = injector.getInstance(TestedRpcHandler.class); methodHandler.handleRequest(RpcEnvelope.Request.newBuilder().build(), new OrangeContext()); SameGuiceModuleAssertionOnInterceptorInvokation sameGuiceModuleAssertionOnInterceptorInvokation = injector.getInstance(SameGuiceModuleAssertionOnInterceptorInvokation.class); sameGuiceModuleAssertionOnInterceptorInvokation.test_that_intercepted_rpc_handler_still_verified(); assertThat(ReflectionUtil.findSubClassParameterType(methodHandler, 0)). isEqualTo(RpcEnvelope.Request.class); assertThat(ReflectionUtil.findSubClassParameterType(methodHandler, 1)). isEqualTo(RpcEnvelope.Response.class); }
Example #6
Source File: DropwizardModule.java From dropwizard-guicier with Apache License 2.0 | 6 votes |
@Override public void configure(final Binder binder) { binder.bindListener(Matchers.any(), new ProvisionListener() { @Override public <T> void onProvision(ProvisionInvocation<T> provision) { Object obj = provision.provision(); if (obj instanceof Managed) { handle((Managed) obj); } if (obj instanceof Task) { handle((Task) obj); } if (obj instanceof HealthCheck) { handle((HealthCheck) obj); } if (obj instanceof ServerLifecycleListener) { handle((ServerLifecycleListener) obj); } } }); }
Example #7
Source File: GuiceUtils.java From attic-aurora with Apache License 2.0 | 6 votes |
/** * 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 #8
Source File: ServerInfoInterceptorTest.java From attic-aurora with Apache License 2.0 | 6 votes |
@Before public void setUp() { interceptor = new ServerInfoInterceptor(); realThrift = createMock(AnnotatedAuroraAdmin.class); Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { MockDecoratedThrift.bindForwardedMock(binder(), realThrift); bind(IServerInfo.class).toInstance(SERVER_INFO); AopModule.bindThriftDecorator( binder(), Matchers.annotatedWith(DecoratedThrift.class), interceptor); } }); decoratedThrift = injector.getInstance(AnnotatedAuroraAdmin.class); }
Example #9
Source File: ContainerEventBusModule.java From titus-control-plane with Apache License 2.0 | 6 votes |
@Override protected void configure() { bind(ContainerEventBus.class).to(DefaultContainerEventBus.class); // Activation lifecycle ActivationProvisionListener activationProvisionListener = new ActivationProvisionListener(); bind(ActivationLifecycle.class).toInstance(activationProvisionListener); bindListener(Matchers.any(), activationProvisionListener); // Proxies bindInterceptor( Matchers.annotatedWith(ProxyConfiguration.class), Matchers.any(), new ProxyMethodInterceptor(getProvider(ActivationLifecycle.class), getProvider(TitusRuntime.class)) ); }
Example #10
Source File: JndiModule.java From seed with Mozilla Public License 2.0 | 6 votes |
@Override protected void configure() { requestStaticInjection(JndiContext.class); // Bind default context Key<Context> defaultContextKey = Key.get(Context.class, Names.named("defaultContext")); bind(defaultContextKey).toInstance(this.defaultContext); bind(Context.class).to(defaultContextKey); // Bind additional contexts for (Map.Entry<String, Context> jndiContextToBeBound : jndiContextsToBeBound.entrySet()) { Key<Context> key = Key.get(Context.class, Names.named(jndiContextToBeBound.getKey())); bind(key).toInstance(jndiContextToBeBound.getValue()); } bindListener(Matchers.any(), new ResourceTypeListener(this.defaultContext, this.jndiContextsToBeBound)); }
Example #11
Source File: TaskServiceTest.java From conductor with Apache License 2.0 | 6 votes |
@Before public void before() { executionService = Mockito.mock(ExecutionService.class); queueDAO = Mockito.mock(QueueDAO.class); Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(ExecutionService.class).toInstance(executionService); bind(QueueDAO.class).toInstance(queueDAO); install(new ValidationModule()); bindInterceptor(Matchers.any(), Matchers.annotatedWith(Service.class), new ServiceInterceptor(getProvider(Validator.class))); } }); taskService = injector.getInstance(TaskServiceImpl.class); }
Example #12
Source File: EventServiceTest.java From conductor with Apache License 2.0 | 6 votes |
@Before public void before() { metadataService = Mockito.mock(MetadataService.class); eventProcessor = Mockito.mock(SimpleEventProcessor.class); eventQueues = Mockito.mock(EventQueues.class); Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(MetadataService.class).toInstance(metadataService); bind(EventProcessor.class).toInstance(eventProcessor); bind(EventQueues.class).toInstance(eventQueues); install(new ValidationModule()); bindInterceptor(Matchers.any(), Matchers.annotatedWith(Service.class), new ServiceInterceptor(getProvider(Validator.class))); } }); eventService = injector.getInstance(EventServiceImpl.class); }
Example #13
Source File: IndexedCorpusModule.java From termsuite-core with Apache License 2.0 | 6 votes |
@Override protected void configure() { bind(new TypeLiteral<Optional<TermHistory>>(){}).toInstance(history); bind(FilterResource.class).to(DefaultFilterResource.class); bind(Terminology.class).toInstance(corpus.getTerminology()); bind(IndexedCorpus.class).toInstance(corpus); bind(Lang.class).toInstance(corpus.getTerminology().getLang()); bind(OccurrenceStore.class).toInstance(corpus.getOccurrenceStore()); bind(TerminologyService.class).toInstance(new TerminologyService(corpus.getTerminology())); bind(PipelineService.class).in(Singleton.class); bind(IndexService.class).toInstance(new IndexService(corpus.getTerminology())); bind(GroovyService.class).in(Singleton.class); bind(PipelineStats.class).in(Singleton.class); bind(PipelineListener.class).toInstance(listener); bindListener(Matchers.any(), new Slf4JTypeListener()); }
Example #14
Source File: CasesModule.java From dropwizard-guicey with MIT License | 6 votes |
@Override protected void configure() { bindListener(new AbstractMatcher<TypeLiteral<?>>() { @Override public boolean matches(TypeLiteral<?> typeLiteral) { return false; } }, new CustomTypeListener()); bindListener(new AbstractMatcher<Object>() { @Override public boolean matches(Object o) { return false; } }, new CustomProvisionListener()); bindInterceptor(Matchers.subclassesOf(AopedService.class), Matchers.any(), new CustomAop()); bind(AopedService.class); bind(BindService.class).to(OverriddenService.class); bind(BindService2.class).toInstance(new BindService2() {}); }
Example #15
Source File: DestroyModule.java From che with Eclipse Public License 2.0 | 6 votes |
@Override protected void configure() { final Destroyer destroyer = new Destroyer(errorHandler); bind(Destroyer.class).toInstance(destroyer); bindListener( Matchers.any(), new TypeListener() { @Override public <T> void hear(TypeLiteral<T> type, TypeEncounter<T> encounter) { encounter.register( new InjectionListener<T>() { @Override public void afterInjection(T injectee) { final Method[] methods = get(injectee.getClass(), annotationType); if (methods.length > 0) { // copy array when pass it outside final Method[] copy = new Method[methods.length]; System.arraycopy(methods, 0, copy, 0, methods.length); destroyer.add(injectee, copy); } } }); } }); }
Example #16
Source File: BackupServiceModule.java From EDDI with Apache License 2.0 | 6 votes |
@Override protected void configure() { registerConfigFiles(this.configFiles); bind(IRestExportService.class).to(RestExportService.class).in(Scopes.SINGLETON); bind(IRestImportService.class).to(RestImportService.class).in(Scopes.SINGLETON); bind(IRestGitBackupService.class).to(RestGitBackupService.class).in(Scopes.SINGLETON); // AutoGit Injection - all methods that are annotated with @ConfigurationUpdate and start with // update or delete trigger a new commit/push bindInterceptor(Matchers.any(), new AbstractMatcher<>() { @Override public boolean matches(Method method) { return method.isAnnotationPresent(IResourceStore.ConfigurationUpdate.class) && !method.isSynthetic(); } }, new GitConfigurationUpdateService(getProvider(IRestGitBackupService.class), getProvider(IBotStore.class), getProvider(IDeploymentStore.class), getProvider(IPackageStore.class), getProvider(IJsonSerialization.class))); bind(IZipArchive.class).to(ZipArchive.class).in(Scopes.SINGLETON); }
Example #17
Source File: LifecycleModule.java From hivemq-community-edition with Apache License 2.0 | 6 votes |
@Override protected void configure() { final boolean newRegistry = lifecycleRegistry.compareAndSet(null, new LifecycleRegistry()); bind(LifecycleRegistry.class).toInstance(lifecycleRegistry.get()); if (newRegistry) { bind(LifecycleShutdownRegistration.class).asEagerSingleton(); } bindListener(Matchers.any(), new TypeListener() { @Override public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { executePostConstruct(encounter, type.getRawType(), lifecycleRegistry.get()); } }); }
Example #18
Source File: ShardModule.java From flux with Apache License 2.0 | 6 votes |
/** * Performs concrete bindings for interfaces * * @see com.google.inject.AbstractModule#configure() */ @Override protected void configure() { //bind entity classes bind(AuditDAO.class).to(AuditDAOImpl.class).in(Singleton.class); bind(EventsDAO.class).to(EventsDAOImpl.class).in(Singleton.class); bind(StateMachinesDAO.class).to(StateMachinesDAOImpl.class).in(Singleton.class); bind(StatesDAO.class).to(StatesDAOImpl.class).in(Singleton.class); bind(ClientElbDAO.class).to(ClientElbDAOImpl.class).in(Singleton.class); //bind Transactional Interceptor to intercept methods which are annotated with javax.transaction.Transactional Provider<SessionFactoryContext> provider = getProvider(Key.get(SessionFactoryContext.class, Names.named("fluxSessionFactoriesContext"))); final TransactionInterceptor transactionInterceptor = new TransactionInterceptor(provider); // Weird way of getting a package but java.lang.Package.getName(<String>) was no working for some reason. // todo [yogesh] dig deeper and fix this ^ bindInterceptor(Matchers.not(Matchers.inPackage(MessageDao.class.getPackage())), Matchers.annotatedWith(Transactional.class), transactionInterceptor); bindInterceptor(Matchers.not(Matchers.inPackage(ScheduledMessage.class.getPackage())), Matchers.annotatedWith(Transactional.class), transactionInterceptor); }
Example #19
Source File: RepositoryModule.java From guice-persist-orient with MIT License | 6 votes |
/** * Configures repository annotations interceptor. */ protected void configureAop() { final RepositoryMethodInterceptor proxy = new RepositoryMethodInterceptor(); requestInjection(proxy); // repository specific method annotations (query, function, delegate, etc.) bindInterceptor(Matchers.any(), new AbstractMatcher<Method>() { @Override public boolean matches(final Method method) { // this will throw error if two or more annotations specified (fail fast) try { return ExtUtils.findMethodAnnotation(method) != null; } catch (Exception ex) { throw new MethodDefinitionException(String.format("Error declaration on method %s", RepositoryUtils.methodToString(method)), ex); } } }, proxy); }
Example #20
Source File: OldScoresModule.java From examples-javafx-repos1 with Apache License 2.0 | 6 votes |
@Override protected void configure() { String mathRecenteredJSONFile = MATH_RECENTERED_JSON_FILE; String verbalRecenteredJSONFile = VERBAL_RECENTERED_JSON_FILE; String settingsFileName = SETTINGS_FILE_NAME; bind(BuilderFactory.class).to(JavaFXBuilderFactory.class); bind(String.class).annotatedWith(Names.named("mathRecenteredJSONFile")).toInstance(mathRecenteredJSONFile); bind(String.class).annotatedWith(Names.named("verbalRecenteredJSONFile")).toInstance(verbalRecenteredJSONFile); bind(String.class).annotatedWith(Names.named("settingsFileName")).toInstance(settingsFileName); bind(SettingsDAO.class).to(SettingsDAOImpl.class).asEagerSingleton(); bind(RecenteredDAO.class).to(RecenteredDAOImpl.class).asEagerSingleton(); bindInterceptor(Matchers.subclassesOf(ManagedDataSource.class), Matchers.any(), new ManagedDataSourceInterceptor()); }
Example #21
Source File: OldScoresModule.java From examples-javafx-repos1 with Apache License 2.0 | 6 votes |
@Override protected void configure() { String mathRecenteredJSONFile = MATH_RECENTERED_JSON_FILE; String verbalRecenteredJSONFile = VERBAL_RECENTERED_JSON_FILE; String settingsFileName = SETTINGS_FILE_NAME; bind(BuilderFactory.class).to(JavaFXBuilderFactory.class); bind(String.class).annotatedWith(Names.named("mathRecenteredJSONFile")).toInstance(mathRecenteredJSONFile); bind(String.class).annotatedWith(Names.named("verbalRecenteredJSONFile")).toInstance(verbalRecenteredJSONFile); bind(String.class).annotatedWith(Names.named("settingsFileName")).toInstance(settingsFileName); bind(SettingsDAO.class).to(SettingsDAOImpl.class).asEagerSingleton(); bind(RecenteredDAO.class).to(RecenteredDAOImpl.class).asEagerSingleton(); bindInterceptor(Matchers.subclassesOf(ManagedDataSource.class), Matchers.any(), new ManagedDataSourceInterceptor()); }
Example #22
Source File: TracingModule.java From che with Eclipse Public License 2.0 | 5 votes |
@Override protected void configure() { bind(Tracer.class).toProvider(TracerProvider.class); TracingInterceptor traceInterceptor = new TracingInterceptor(); requestInjection(traceInterceptor); bindInterceptor(Matchers.any(), Matchers.annotatedWith(Traced.class), traceInterceptor); }
Example #23
Source File: PrivateGuiceTest.java From nano-framework with Apache License 2.0 | 5 votes |
@Override protected void configure() { TestInterceptor interceptor = new TestInterceptor(); requestInjection(interceptor); bindInterceptor(Matchers.any(), Matchers.annotatedWith(TestAnno.class), interceptor); }
Example #24
Source File: ValidationModule.java From guice-validator with MIT License | 5 votes |
@SuppressWarnings({"unchecked", "PMD.CompareObjectsWithEquals"}) protected Matcher<? super Method> getMethodMatcher(final Class<? extends Annotation> annotation) { final Matcher<AnnotatedElement> res = Matchers.annotatedWith(annotation); return methodMatcher == DECLARED_METHOD_MATCHER // combine custom filter with annotation ? res : res.and((Matcher<? super AnnotatedElement>) methodMatcher); }
Example #25
Source File: MonitoringGuiceModule.java From javamelody with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override protected void configure() { // for annotated methods (of implementations) with MonitoredWithGuice final MonitoringGuiceInterceptor monitoringGuiceInterceptor = new MonitoringGuiceInterceptor(); bindInterceptor(Matchers.any(), Matchers.annotatedWith(MonitoredWithGuice.class), monitoringGuiceInterceptor); // and for annotated classes (of implementations) with MonitoredWithGuice bindInterceptor(Matchers.annotatedWith(MonitoredWithGuice.class), Matchers.any(), monitoringGuiceInterceptor); }
Example #26
Source File: TracerSpanModule.java From cloud-trace-java with Apache License 2.0 | 5 votes |
@Override protected void configure() { bindInterceptor(Matchers.any(), Matchers.annotatedWith(Span.class), new TracerSpanInterceptor(getProvider(Tracer.class), getProvider(Key.get(new TypeLiteral<Map<String, Labeler>>(){})), labelHost)); MapBinder<String, Labeler> mapBinder = MapBinder.newMapBinder(binder(), String.class, Labeler.class); }
Example #27
Source File: FieldInjectModule.java From nano-framework with Apache License 2.0 | 5 votes |
@Override public void configure(final Binder binder) { binder.bindListener(Matchers.any(), new TypeListener() { @Override public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { final List<Field> fields = fields(Lists.newArrayList(), type.getRawType()); fields.stream().filter(field -> field.isAnnotationPresent(FieldInject.class)).forEach(field -> { final FieldInject inject = field.getAnnotation(FieldInject.class); encounter.register(ReflectUtils.newInstance(inject.value(), field)); }); } }); }
Example #28
Source File: AOPModule.java From nano-framework with Apache License 2.0 | 5 votes |
@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 #29
Source File: ServerLifeCycleModule.java From digdag with Apache License 2.0 | 5 votes |
@Override public void configure(Binder binder) { binder.disableCircularProxies(); binder.bindListener(Matchers.any(), new TypeListener() { @Override public <T> void hear(TypeLiteral<T> type, TypeEncounter<T> encounter) { encounter.register(new InjectionListener<T>() { @Override public void afterInjection(T obj) { ServerLifeCycleManager initialized = lifeCycleManagerRef.get(); if (initialized == null) { earlyInjected.add(obj); } else { try { initialized.manageInstance(obj); } catch (Exception e) { // really nothing we can do here throw new Error(e); } } } }); } }); }
Example #30
Source File: ValidationModule.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
@Override protected void configure() { final MethodInterceptor interceptor = new ValidationInterceptor(); bindInterceptor(Matchers.any(), Matchers.annotatedWith(Validate.class), interceptor); requestInjection(interceptor); bind(ConstraintValidatorFactory.class).to(GuiceConstraintValidatorFactory.class); }