Java Code Examples for javax.enterprise.inject.spi.BeanManager#resolve()
The following examples show how to use
javax.enterprise.inject.spi.BeanManager#resolve() .
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: ContainerCtrlTckTest.java From deltaspike with Apache License 2.0 | 6 votes |
@Test public void testContainerBoot() { CdiContainer cc = CdiContainerLoader.getCdiContainer(); Assert.assertNotNull(cc); cc.boot(); cc.getContextControl().startContexts(); BeanManager bm = cc.getBeanManager(); Assert.assertNotNull(bm); Set<Bean<?>> beans = bm.getBeans(CarRepair.class); Bean<?> bean = bm.resolve(beans); CarRepair carRepair = (CarRepair) bm.getReference(bean, CarRepair.class, bm.createCreationalContext(bean)); Assert.assertNotNull(carRepair); Assert.assertNotNull(carRepair.getCar()); Assert.assertNotNull(carRepair.getCar().getUser()); cc.shutdown(); }
Example 2
Source File: CDIBatchArtifactFactory.java From incubator-batchee with Apache License 2.0 | 6 votes |
@Override public Instance load(final String batchId) { final BeanManager bm = getBeanManager(); if (bm == null) { return super.load(batchId); } final Set<Bean<?>> beans = bm.getBeans(batchId); final Bean<?> bean = bm.resolve(beans); if (bean == null) { // fallback to try to instantiate it from TCCL as per the spec return super.load(batchId); } final Class<?> clazz = bean.getBeanClass(); final CreationalContext creationalContext = bm.createCreationalContext(bean); final Object artifactInstance = bm.getReference(bean, clazz, creationalContext); if (Dependent.class.equals(bean.getScope()) || !bm.isNormalScope(bean.getScope())) { // need to be released return new Instance(artifactInstance, new Closeable() { @Override public void close() throws IOException { creationalContext.release(); } }); } return new Instance(artifactInstance, null); }
Example 3
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 4
Source File: BeanProvider.java From deltaspike with Apache License 2.0 | 5 votes |
/** * Internal helper method to resolve the right bean and resolve the contextual reference. * * @param type the type of the bean in question * @param beanManager current bean-manager * @param beans beans in question * @param <T> target type * @return the contextual reference */ private static <T> T getContextualReference(Class<T> type, BeanManager beanManager, Set<Bean<?>> beans) { Bean<?> bean = beanManager.resolve(beans); logWarningIfDependent(bean); CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); @SuppressWarnings({ "unchecked", "UnnecessaryLocalVariable" }) T result = (T) beanManager.getReference(bean, type, creationalContext); return result; }
Example 5
Source File: RepositoryRefreshAgent.java From HotswapAgent with GNU General Public License v2.0 | 5 votes |
private static <T> Bean<T> resolve(BeanManager beanManager, Class<T> beanClass) { Set<Bean<?>> beans = beanManager.getBeans(beanClass); for (Bean<?> bean : beans) { if (bean.getBeanClass() == beanClass) { return (Bean<T>) beanManager.resolve(Collections.<Bean<?>> singleton(bean)); } } return (Bean<T>) beanManager.resolve(beans); }
Example 6
Source File: EntityManagerBean.java From openwebbeans-meecrowave with Apache License 2.0 | 5 votes |
private Object findValidatorFactory(final BeanManager bm) { try { final Class<?> type = Thread.currentThread().getContextClassLoader().loadClass("javax.validation.ValidatorFactory"); final Bean<?> bean = bm.resolve(bm.getBeans(type)); if (bean == null || !bm.isNormalScope(bean.getScope())) { return null; } return bm.getReference(bean, type, bm.createCreationalContext(null)); } catch (final NoClassDefFoundError | ClassNotFoundException e) { return null; } }
Example 7
Source File: ContainerCtrlTckTest.java From deltaspike with Apache License 2.0 | 5 votes |
private <T> T resolveInstance(BeanManager beanManager, Class<T> beanClass) { Set<Bean<?>> beans = beanManager.getBeans(beanClass); Bean<?> bean = beanManager.resolve(beans); return (T) beanManager.getReference(bean, beanClass, beanManager.createCreationalContext(bean)); }
Example 8
Source File: PortletCDIExtension.java From portals-pluto with Apache License 2.0 | 5 votes |
/** * After the bean container has validated the deployment from its point of view, * do some checking from the portlet point of view. Activate the portlet deployment * by passing in a bean manager in order to create the required portlet bean instances. * * @param adv * @param bm * @throws InvalidAnnotationException */ void afterDeploymentValidation(@Observes AfterDeploymentValidation adv, BeanManager bm) throws InvalidAnnotationException { // Done processing the annotations, so put the resulting configuration in an // application scoped bean to pass it to the servlet LOG.trace("Now attempting to get the AnnotatedConfigBean ..."); Set<Bean<?>> beans = bm.getBeans(AnnotatedConfigBean.class); @SuppressWarnings("unchecked") Bean<AnnotatedConfigBean> bean = (Bean<AnnotatedConfigBean>) bm.resolve(beans); if (bean != null) { LOG.trace("Got AnnotatedConfigBean bean: " + bean.getBeanClass().getCanonicalName()); try { CreationalContext<AnnotatedConfigBean> cc = bm.createCreationalContext(bean); acb = (AnnotatedConfigBean) bm.getReference(bean, AnnotatedConfigBean.class, cc); LOG.trace("Got AnnotatedConfigBean instance."); acb.setMethodStore(ams); acb.setSummary(summary); acb.setRedirectScopedConfig(par.getRedirectScopedConfig()); acb.setStateScopedConfig(par.getStateScopedConfig()); acb.setSessionScopedConfig(par.getSessionScopedConfig()); } catch (Exception e) { StringBuilder txt = new StringBuilder(128); txt.append("Exception getting AnnotatedConfigBean bean instance: "); txt.append(e.toString()); LOG.warn(txt.toString()); } } else { LOG.warn("AnnotatedConfigBean bean was null."); } LOG.info("Portlet CDI Extension complete. Config bean: " + acb); }
Example 9
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 10
Source File: StartWebServer.java From hammock with Apache License 2.0 | 5 votes |
private WebServer resolveWebServer(BeanManager beanManager) { Set<Bean<?>> beans = beanManager.getBeans(WebServer.class); if (beans.size() > 1) { StringJoiner foundInstances = new StringJoiner(",", "[", "]"); beans.iterator().forEachRemaining(ws -> foundInstances.add(ws.toString())); throw new RuntimeException("Multiple web server implementations found on the classpath " + foundInstances); } if (beans.isEmpty()) { throw new RuntimeException("No web server implementations found on the classpath"); } Bean<?> bean = beanManager.resolve(beans); CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); return (WebServer) beanManager.getReference(bean, WebServer.class, creationalContext); }
Example 11
Source File: OwbPluginTest.java From HotswapAgent with GNU General Public License v2.0 | 5 votes |
public static <T> T getReference(Class<T> beanClass) { BeanManager beanManager = CDI.current().getBeanManager(); Bean<T> bean = (Bean<T>) beanManager.resolve(beanManager.getBeans(beanClass)); if (bean != null) { return (T) beanManager.getReference(bean, beanClass, beanManager.createCreationalContext(bean)); } return null; }
Example 12
Source File: OpenEJBScripter.java From tomee with Apache License 2.0 | 4 votes |
private <T> T instance(final BeanManager bm, final Set<Bean<?>> beans, final Class<T> clazz) { final Bean<?> bean = bm.resolve(beans); return (T) bm.getReference(bean, clazz, bm.createCreationalContext(bean)); }
Example 13
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 14
Source File: ContainerCtrlTckTest.java From deltaspike with Apache License 2.0 | 4 votes |
@LockedCDIImplementation(versions = { @LockedVersionRange(implementation = CdiImplementation.WELD11, versionRange = "[1.1.14,1.2)"), @LockedVersionRange(implementation = CdiImplementation.WELD20, versionRange = "[2.0.1.Final,2.1)") }) @Test public void testShutdownWithInactiveContexts() { CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer(); Assert.assertNotNull(cdiContainer); cdiContainer.boot(); cdiContainer.getContextControl().startContexts(); // now do some random stuff 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()); cdiContainer.shutdown(); }
Example 15
Source File: WeldPluginTest.java From HotswapAgent with GNU General Public License v2.0 | 4 votes |
public <T> T getBeanInstance(Class<T> beanClass) { BeanManager beanManager = CDI.current().getBeanManager(); Bean<T> bean = (Bean<T>) beanManager.resolve(beanManager.getBeans(beanClass)); T result = beanManager.getContext(bean.getScope()).get(bean, beanManager.createCreationalContext(bean)); return result; }
Example 16
Source File: OwbPluginTest.java From HotswapAgent with GNU General Public License v2.0 | 4 votes |
public <T> T getBeanInstance(Class<T> beanClass) { BeanManager beanManager = CDI.current().getBeanManager(); Bean<T> bean = (Bean<T>) beanManager.resolve(beanManager.getBeans(beanClass)); T result = beanManager.getContext(bean.getScope()).get(bean, beanManager.createCreationalContext(bean)); return result; }
Example 17
Source File: ContainerCtrlTckTest.java From deltaspike with Apache License 2.0 | 4 votes |
@Test public void testParallelThreadExecution() throws Exception { final CdiContainer cc = CdiContainerLoader.getCdiContainer(); Assert.assertNotNull(cc); cc.boot(); cc.getContextControl().startContexts(); final BeanManager bm = cc.getBeanManager(); Assert.assertNotNull(bm); final AtomicInteger numErrors = new AtomicInteger(0); final ContextControl contextControl = cc.getContextControl(); Runnable runnable = new Runnable() { @Override public void run() { try { contextControl.startContext(SessionScoped.class); contextControl.startContext(RequestScoped.class); Set<Bean<?>> beans = bm.getBeans(CarRepair.class); Bean<?> bean = bm.resolve(beans); CarRepair carRepair = (CarRepair) bm.getReference(bean, CarRepair.class, bm.createCreationalContext(bean)); Assert.assertNotNull(carRepair); for (int i = 0; i < 100000; i++) { // we need the threads doing something ;) Assert.assertNotNull(carRepair.getCar()); Assert.assertNotNull(carRepair.getCar().getUser()); Assert.assertNull(carRepair.getCar().getUser().getName()); } contextControl.stopContext(RequestScoped.class); contextControl.stopContext(SessionScoped.class); } catch (Throwable e) { log.log(Level.SEVERE, "An exception happened on a new worker thread", e); numErrors.incrementAndGet(); } } }; Thread[] threads = new Thread[NUM_THREADS]; for (int i = 0 ; i < NUM_THREADS; i++) { threads[i] = new Thread(runnable); } for (int i = 0 ; i < NUM_THREADS; i++) { threads[i].start(); } for (int i = 0 ; i < NUM_THREADS; i++) { threads[i].join(); } Assert.assertEquals("An error happened while executing parallel threads", 0, numErrors.get()); cc.shutdown(); }
Example 18
Source File: WeldVertxObserversTest.java From weld-vertx with Apache License 2.0 | 4 votes |
@Test public void testContextBean() { BeanManager beanManager = CDI.current().getBeanManager(); Bean<?> bean = beanManager.resolve(beanManager.getBeans(Context.class)); assertTrue(bean.getTypes().contains(Context.class)); }
Example 19
Source File: WebappMultipleModuleTest.java From tomee with Apache License 2.0 | 4 votes |
private void end(@Observes final AfterDeploymentValidation ignored , final BeanManager manager) { final Bean<?> bean = manager.resolve(manager.getBeans(Marker.class)); assertNotNull(manager.getReference(bean, Marker.class, manager.createCreationalContext(bean))); CALLED.set(true); }
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); }