Java Code Examples for javax.enterprise.inject.spi.BeanManager#getBeans()
The following examples show how to use
javax.enterprise.inject.spi.BeanManager#getBeans() .
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: CdiTestRunner.java From deltaspike with Apache License 2.0 | 6 votes |
@Override protected Object createTest() throws Exception { BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager(); Class<?> type = getTestClass().getJavaClass(); Set<Bean<?>> beans = beanManager.getBeans(type); Object result; if (!USE_TEST_CLASS_AS_CDI_BEAN || beans == null || beans.isEmpty()) { result = super.createTest(); BeanProvider.injectFields(result); //fallback to simple injection } else { Bean<Object> bean = (Bean<Object>) beanManager.resolve(beans); CreationalContext<Object> creationalContext = beanManager.createCreationalContext(bean); result = beanManager.getReference(bean, type, creationalContext); } return result; }
Example 2
Source File: Authorizer.java From deltaspike with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "unchecked" }) private synchronized void lazyInitTargetBean(BeanManager beanManager) { if (boundAuthorizerBean == null) { Method method = boundAuthorizerMethod.getJavaMember(); Set<Bean<?>> beans = beanManager.getBeans(method.getDeclaringClass()); Bean<?> foundBoundAuthorizerBean = beanManager.resolve(beans); if (foundBoundAuthorizerBean == null) { throw new IllegalStateException("Exception looking up authorizer method bean - " + "no beans found for method [" + method.getDeclaringClass() + "." + method.getName() + "]"); } boundAuthorizerMethodProxy = new InjectableMethod( boundAuthorizerMethod, foundBoundAuthorizerBean, beanManager); boundAuthorizerBean = foundBoundAuthorizerBean; } }
Example 3
Source File: RequestServlet.java From deltaspike with Apache License 2.0 | 6 votes |
private RequestScopedBean getRequestScopedBean() { BeanManager beanManager = CdiContainerLoader.getCdiContainer().getBeanManager(); if (beanManager == null) { return null; } Set<Bean<?>> beans = beanManager.getBeans(RequestScopedBean.class); Bean<RequestScopedBean> reqScpdBean = (Bean<RequestScopedBean>) beanManager.resolve(beans); CreationalContext<RequestScopedBean> reqScpdCC = beanManager.createCreationalContext(reqScpdBean); RequestScopedBean instance = (RequestScopedBean) beanManager.getReference(reqScpdBean, RequestScopedBean.class, reqScpdCC); return instance; }
Example 4
Source File: CdiServiceDiscovery.java From katharsis-framework with Apache License 2.0 | 6 votes |
@Override public <A extends Annotation> List<Object> getInstancesByAnnotation(Class<A> annotationClass) { BeanManager beanManager = CDI.current().getBeanManager(); Set<Bean<?>> beans = beanManager.getBeans(Object.class); List<Object> list = new ArrayList<>(); for (Bean<?> bean : beans) { Class<?> beanClass = bean.getBeanClass(); Optional<A> annotation = ClassUtils.getAnnotation(beanClass, annotationClass); if (annotation.isPresent()) { CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); Object object = beanManager.getReference(bean, beanClass, creationalContext); list.add(object); } } return list; }
Example 5
Source File: CdiTestRunner.java From deltaspike with Apache License 2.0 | 5 votes |
@Override public void evaluate() throws Throwable { BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager(); Class<?> type = this.method.getMethod().getDeclaringClass(); Set<Bean<?>> beans = beanManager.getBeans(type); if (!USE_TEST_CLASS_AS_CDI_BEAN || beans == null || beans.isEmpty()) { if (!ALLOW_INJECTION_POINT_MANIPULATION) { BeanProvider.injectFields(this.originalTarget); //fallback to simple injection } invokeMethod(this.originalTarget); } else { Bean<Object> bean = (Bean<Object>) beanManager.resolve(beans); CreationalContext<Object> creationalContext = beanManager.createCreationalContext(bean); Object target = beanManager.getReference(bean, type, creationalContext); try { invokeMethod(target); } finally { if (bean.getScope().equals(Dependent.class)) { bean.destroy(target, creationalContext); } } } }
Example 6
Source File: RuntimeBeanRepository.java From camel-quarkus with Apache License 2.0 | 5 votes |
private static <T> Set<T> getReferencesByType(BeanManager manager, Class<T> type, Annotation... qualifiers) { Set<T> answer = new HashSet<>(); for (Bean<?> bean : manager.getBeans(type, qualifiers)) { T ref = getReference(manager, type, bean); if (ref != null) { answer.add(ref); } } return answer; }
Example 7
Source File: RepositoryMetadataInitializer.java From deltaspike with Apache License 2.0 | 5 votes |
public RepositoryMetadata init(Class<?> repositoryClass, BeanManager beanManager) { RepositoryMetadata repositoryMetadata = new RepositoryMetadata(repositoryClass); // read from looks for JPA Transactional and EntityManagerConfig to determine attributes // if those are set, don't process old annotations if (!repositoryMetadata.readFrom(repositoryClass, beanManager)) { repositoryMetadata.setEntityManagerResolverClass(extractEntityManagerResolver(repositoryClass)); repositoryMetadata.setEntityManagerFlushMode(extractEntityManagerFlushMode(repositoryClass)); if (repositoryMetadata.getEntityManagerResolverClass() != null) { Set<Bean<?>> beans = beanManager.getBeans(repositoryMetadata.getEntityManagerResolverClass()); Class<? extends Annotation> scope = beanManager.resolve(beans).getScope(); repositoryMetadata.setEntityManagerResolverIsNormalScope(beanManager.isNormalScope(scope)); } else { EntityManagerResolver resolver; if (repositoryMetadata.getQualifiers() != null) { resolver = new QualifierBackedEntityManagerResolver(beanManager, repositoryMetadata.getQualifiers()); } else { resolver = new QualifierBackedEntityManagerResolver(beanManager, Default.class); } repositoryMetadata.setUnmanagedResolver(resolver); repositoryMetadata.setEntityManagerResolverIsNormalScope(false); } } repositoryMetadata.setEntityMetadata(entityMetadataInitializer.init(repositoryMetadata)); initializeMethodsMetadata(repositoryMetadata, beanManager); return repositoryMetadata; }
Example 8
Source File: BeanProvider.java From deltaspike with Apache License 2.0 | 5 votes |
public static <T> DependentProvider<T> getDependent(BeanManager beanManager, Class<T> type, Annotation... qualifiers) { Set<Bean<?>> beans = beanManager.getBeans(type, qualifiers); @SuppressWarnings("unchecked") Bean<T> bean = (Bean<T>) beanManager.resolve(beans); return createDependentProvider(beanManager, type, bean); }
Example 9
Source File: AuditEntityListener.java From deltaspike with Apache License 2.0 | 5 votes |
@PrePersist public void persist(Object entity) { BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager(); Set<Bean<?>> beans = beanManager.getBeans(PrePersistAuditListener.class); for (Bean<?> bean : beans) { PrePersistAuditListener result = (PrePersistAuditListener) beanManager.getReference( bean, PrePersistAuditListener.class, beanManager.createCreationalContext(bean)); result.prePersist(entity); } }
Example 10
Source File: PingCDIBean.java From sample.daytrader7 with Apache License 2.0 | 5 votes |
public int getBeanMangerViaCDICurrent() throws Exception { BeanManager beanManager = CDI.current().getBeanManager(); Set<Bean<?>> beans = beanManager.getBeans(Object.class); if (beans.size() > 0) { return ++getBeanManagerHitCountSPI; } return 0; }
Example 11
Source File: AuditEntityListener.java From deltaspike with Apache License 2.0 | 5 votes |
@PreUpdate public void update(Object entity) { BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager(); Set<Bean<?>> beans = beanManager.getBeans(PreUpdateAuditListener.class); for (Bean<?> bean : beans) { PreUpdateAuditListener result = (PreUpdateAuditListener) beanManager.getReference( bean, PreUpdateAuditListener.class, beanManager.createCreationalContext(bean)); result.preUpdate(entity); } }
Example 12
Source File: ProgrammaticBeanLookup.java From activiti6-boot2 with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) public static Object lookup(String name, BeanManager bm) { Set<Bean<?>> beans = bm.getBeans(name); if (beans.isEmpty()) { throw new IllegalStateException("CDI BeanManager cannot find an instance of requested type '" + name + "'"); } Bean bean = bm.resolve(beans); CreationalContext ctx = bm.createCreationalContext(bean); // select one beantype randomly. A bean has a non-empty set of // beantypes. Type type = (Type) bean.getTypes().iterator().next(); return bm.getReference(bean, type, ctx); }
Example 13
Source File: ProgrammaticBeanLookup.java From camunda-bpm-platform with Apache License 2.0 | 5 votes |
public static <T> T lookup(Class<T> clazz, BeanManager bm, boolean optional) { Set<Bean<?>> beans = bm.getBeans(clazz); T instance = getContextualReference(bm, beans, clazz); if (!optional && instance == null) { throw new IllegalStateException("CDI BeanManager cannot find an instance of requested type '" + clazz.getName() + "'"); } return instance; }
Example 14
Source File: ClassUnwrapperTest.java From cxf with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") private<T> T getBeanReference(Class<T> clazz) { final BeanManager beanManager = lifecycle.getBeanManager(); final Set<Bean<?>> beans = beanManager.getBeans(clazz); final Bean<?> bean = beanManager.resolve(beans); return (T)beanManager.getReference(bean, clazz, beanManager.createCreationalContext(bean)); }
Example 15
Source File: ContainerCtrlTckTest.java From deltaspike with Apache License 2.0 | 4 votes |
/** * Stops and starts: application-, session- and request-scope. * <p/> * application-scoped instance has a ref to * request-scoped instance which has a ref to * session-scoped instance. * <p/> * If the deepest ref has the expected value, all levels in between were resetted correctly. */ @Test public void testRestartContexts() { CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer(); Assert.assertNotNull(cdiContainer); cdiContainer.boot(); cdiContainer.getContextControl().startContexts(); BeanManager beanManager = cdiContainer.getBeanManager(); Assert.assertNotNull(beanManager); Set<Bean<?>> beans = beanManager.getBeans(CarRepair.class); Bean<?> bean = beanManager.resolve(beans); CarRepair carRepair = (CarRepair) beanManager.getReference(bean, CarRepair.class, beanManager.createCreationalContext(bean)); Assert.assertNotNull(carRepair); Car car = carRepair.getCar(); Assert.assertNotNull(car); Assert.assertNotNull(car.getUser()); carRepair.getCar().getUser().setName("tester"); Assert.assertEquals("tester", car.getUser().getName()); Assert.assertFalse(CarRepair.isPreDestroyCalled()); Assert.assertFalse(Car.isPreDestroyCalled()); Assert.assertFalse(TestUser.isPreDestroyCalled()); cdiContainer.getContextControl().stopContexts(); Assert.assertTrue(CarRepair.isPreDestroyCalled()); Assert.assertTrue(Car.isPreDestroyCalled()); Assert.assertTrue(TestUser.isPreDestroyCalled()); try { car.getUser(); // accessing the car should have triggered a ContextNotActiveException Assert.fail(); } catch (ContextNotActiveException e) { //do nothing - exception expected } cdiContainer.getContextControl().startContexts(); carRepair = (CarRepair) beanManager.getReference(bean, CarRepair.class, beanManager.createCreationalContext(bean)); Assert.assertNotNull(carRepair.getCar()); Assert.assertNotNull(carRepair.getCar().getUser()); Assert.assertNull(carRepair.getCar().getUser().getName()); cdiContainer.shutdown(); }
Example 16
Source File: StartWebServer.java From hammock with Apache License 2.0 | 4 votes |
private <T> void processInstances(BeanManager beanManager, Class<T> clazz, Consumer<T> consumer) { Set<Bean<?>> beans = beanManager.getBeans(clazz); beans.stream().map(bean -> (T) beanManager.getReference(bean, clazz, beanManager.createCreationalContext(bean))).forEach(consumer); }
Example 17
Source File: ApplicationLifecycleManager.java From quarkus with Apache License 2.0 | 4 votes |
public static void run(Application application, Class<? extends QuarkusApplication> quarkusApplication, Consumer<Integer> exitCodeHandler, String... args) { stateLock.lock(); //in tests we might pass this method an already started application //in this case we don't shut it down at the end boolean alreadyStarted = application.isStarted(); if (!hooksRegistered) { registerHooks(); hooksRegistered = true; } if (currentApplication != null && !shutdownRequested) { throw new IllegalStateException("Quarkus already running"); } try { exitCode = -1; shutdownRequested = false; currentApplication = application; } finally { stateLock.unlock(); } try { application.start(args); //now we are started, we either run the main application or just wait to exit if (quarkusApplication != null) { BeanManager beanManager = CDI.current().getBeanManager(); Set<Bean<?>> beans = beanManager.getBeans(quarkusApplication, Any.Literal.INSTANCE); Bean<?> bean = null; for (Bean<?> i : beans) { if (i.getBeanClass() == quarkusApplication) { bean = i; break; } } QuarkusApplication instance; if (bean == null) { instance = quarkusApplication.newInstance(); } else { CreationalContext<?> ctx = beanManager.createCreationalContext(bean); instance = (QuarkusApplication) beanManager.getReference(bean, quarkusApplication, ctx); } int result = -1; try { result = instance.run(args);//TODO: argument filtering? } finally { stateLock.lock(); try { //now we exit if (exitCode == -1 && result != -1) { exitCode = result; } shutdownRequested = true; stateCond.signalAll(); } finally { stateLock.unlock(); } } } else { stateLock.lock(); try { while (!shutdownRequested) { Thread.interrupted(); stateCond.await(); } } finally { stateLock.unlock(); } } } catch (Exception e) { Logger.getLogger(Application.class).error("Error running Quarkus application", e); stateLock.lock(); try { shutdownRequested = true; stateCond.signalAll(); } finally { stateLock.unlock(); } application.stop(); (exitCodeHandler == null ? defaultExitCodeHandler : exitCodeHandler).accept(1); return; } if (!alreadyStarted) { application.stop(); //this could have already been called } (exitCodeHandler == null ? defaultExitCodeHandler : exitCodeHandler).accept(getExitCode()); //this may not be called if shutdown was initiated by a signal }
Example 18
Source File: BeanManagerTest.java From quarkus with Apache License 2.0 | 4 votes |
@Test public void testGetInjectableReference() { BeanManager beanManager = Arc.container().beanManager(); Set<Bean<?>> beans = beanManager.getBeans(WithInjectionPointMetadata.class); assertEquals(1, beans.size()); @SuppressWarnings("unchecked") Bean<WithInjectionPointMetadata> bean = (Bean<WithInjectionPointMetadata>) beans.iterator().next(); WithInjectionPointMetadata injectableReference = (WithInjectionPointMetadata) beanManager .getInjectableReference(new InjectionPoint() { @Override public boolean isTransient() { return false; } @Override public boolean isDelegate() { return false; } @Override public Type getType() { return WithInjectionPointMetadata.class; } @Override public Set<Annotation> getQualifiers() { return Collections.singleton(Any.Literal.INSTANCE); } @Override public Member getMember() { return null; } @Override public Bean<?> getBean() { return null; } @Override public Annotated getAnnotated() { return null; } }, beanManager.createCreationalContext(bean)); assertNotNull(injectableReference.injectionPoint); assertEquals(WithInjectionPointMetadata.class, injectableReference.injectionPoint.getType()); assertNull(injectableReference.injectionPoint.getBean()); }
Example 19
Source File: CdiUtils.java From ozark with Apache License 2.0 | 3 votes |
/** * Create a new CDI bean given its class and a bean manager. The bean is created * in the context defined by the scope annotation on the class. * * @param bm The BeanManager. * @param clazz CDI class. * @param <T> class parameter. * @return newly allocated CDI bean. */ public static <T> T newBean(BeanManager bm, Class<T> clazz) { Set<Bean<?>> beans = bm.getBeans(clazz); final Bean<T> bean = (Bean<T>) bm.resolve(beans); final CreationalContext<T> ctx = bm.createCreationalContext(bean); return (T) bm.getReference(bean, clazz, ctx); }
Example 20
Source File: CdiUtils.java From krazo with Apache License 2.0 | 3 votes |
/** * Create a new CDI bean given its class and a bean manager. The bean is created * in the context defined by the scope annotation on the class. * * @param bm The BeanManager. * @param clazz CDI class. * @param <T> class parameter. * @return newly allocated CDI bean. */ public static <T> T newBean(BeanManager bm, Class<T> clazz) { Set<Bean<?>> beans = bm.getBeans(clazz); final Bean<T> bean = (Bean<T>) bm.resolve(beans); final CreationalContext<T> ctx = bm.createCreationalContext(bean); return (T) bm.getReference(bean, clazz, ctx); }