Java Code Examples for javax.enterprise.context.spi.CreationalContext#release()

The following examples show how to use javax.enterprise.context.spi.CreationalContext#release() . 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: RedirectScopeManager.java    From krazo with Apache License 2.0 6 votes vote down vote up
/**
 * Destroy the instance.
 *
 * @param contextual the contextual.
 */
public void destroy(Contextual contextual) {
    String scopeId = (String) request.getAttribute(SCOPE_ID);
    if (null != scopeId) {
        HttpSession session = request.getSession();
        if (contextual instanceof PassivationCapable == false) {
            throw new RuntimeException("Unexpected type for contextual");
        }
        PassivationCapable pc = (PassivationCapable) contextual;
        final String sessionKey = SCOPE_ID + "-" + scopeId;
        Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
        if (null != scopeMap) {
            Object instance = scopeMap.get(INSTANCE + pc.getId());
            CreationalContext<?> creational = (CreationalContext<?>) scopeMap.get(CREATIONAL + pc.getId());
            if (null != instance && null != creational) {
                contextual.destroy(instance, creational);
                creational.release();
            }
        }
    }
}
 
Example 2
Source File: InjectionEnricher.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public Map.Entry<Closeable, Object> get() {
    CreationalContext<?> cc = getBeanManager().createCreationalContext(null);
    return new Map.Entry<Closeable, Object>() {
        @Override
        public Closeable getKey() {
            return new Closeable() {
                @Override
                public void close() throws IOException {
                    cc.release();
                }
            };
        }

        @Override
        public Object getValue() {
            return cc;
        }

        @Override
        public Object setValue(Object value) {
            return null;
        }
    };
}
 
Example 3
Source File: CDIBatchArtifactFactory.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: CDIInstanceManager.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
@Override
public void newInstance(final Object o) throws IllegalAccessException, InvocationTargetException, NamingException {
    if (WebBeansConfigurationListener.class.isInstance(o) || o.getClass().getName().startsWith("org.apache.catalina.servlets.")) {
        return;
    }

    final BeanManager bm = CDI.current().getBeanManager();
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType(o.getClass());
    final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
    final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
    injectionTarget.inject(o, creationalContext);
    try {
        injectionTarget.postConstruct(o);
    } catch (final RuntimeException e) {
        creationalContext.release();
        throw e;
    }
    destroyables.put(o, () -> {
        try {
            injectionTarget.preDestroy(o);
        } finally {
            creationalContext.release();
        }
    });
}
 
Example 5
Source File: InjectRule.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            CreationalContext<?> creationalContext = null;
            try {
                creationalContext = Injector.inject(instance);
                Injector.injectConfig(CDI.current().select(Meecrowave.Builder.class).get(), instance);
                base.evaluate();
            } finally {
                if (creationalContext != null) {
                    creationalContext.release();
                }
            }
        }
    };
}
 
Example 6
Source File: WebContext.java    From tomee with Apache License 2.0 5 votes vote down vote up
public <T> Instance newWeakableInstance(final Class<T> beanClass) throws OpenEJBException {
    final WebBeansContext webBeansContext = getWebBeansContext();
    final ConstructorInjectionBean<Object> beanDefinition = getConstructorInjectionBean(beanClass, webBeansContext);
    CreationalContext<Object> creationalContext;
    final Object o;
    if (webBeansContext == null) {
        creationalContext = null;
        try {
            o = beanClass.newInstance();
        } catch (final InstantiationException | IllegalAccessException e) {
            throw new OpenEJBException(e);
        }
    } else {
        creationalContext = webBeansContext.getBeanManagerImpl().createCreationalContext(beanDefinition);
        o = beanDefinition.create(creationalContext);
    }

    // Create bean instance
    final Context unwrap = InjectionProcessor.unwrap(getInitialContext());
    final InjectionProcessor injectionProcessor = new InjectionProcessor(o, injections, unwrap);

    final Object beanInstance;
    try {
        beanInstance = injectionProcessor.createInstance();

        if (webBeansContext != null) {
            final InjectionTargetBean<Object> bean = InjectionTargetBean.class.cast(beanDefinition);
            bean.getInjectionTarget().inject(beanInstance, creationalContext);
            if (shouldBeReleased(bean.getScope())) {
                creationalContexts.put(beanInstance, creationalContext);
            }
        }
    } catch (final OpenEJBException oejbe) {
        if (creationalContext != null) {
            creationalContext.release();
        }
        throw oejbe;
    }
    return new Instance(beanInstance, creationalContext);
}
 
Example 7
Source File: CreationalContextReleaseListener.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected void release(CreationalContext<?> creationalContext) {
  try {
    creationalContext.release();
  } catch(Exception e) {
    LOG.log(Level.WARNING, "Exception while releasing CDI creational context "+e.getMessage(), e);
  }
}
 
Example 8
Source File: DelegatingContextualLifecycle.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public void destroy(Bean<T> bean, T instance, CreationalContext<T> creationalContext)
{
    try
    {
        injectionTarget.preDestroy(instance);
        creationalContext.release();
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: RestClientCdiTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawTypes"})
public void testCreationalContextsReleasedOnClientClose() throws Exception {
    IMocksControl control = EasyMock.createStrictControl();
    BeanManager mockedBeanMgr = control.createMock(BeanManager.class);
    CreationalContext<?> mockedCreationalCtx = control.createMock(CreationalContext.class);
    Bean<?> mockedBean = control.createMock(Bean.class);
    List<String> stringList = new ArrayList<>(Collections.singleton("abc"));

    EasyMock.expect(mockedBeanMgr.getBeans(List.class))
            .andReturn(Collections.singleton(mockedBean));
    EasyMock.expect(mockedBeanMgr.createCreationalContext(mockedBean))
            .andReturn((CreationalContext) mockedCreationalCtx);
    EasyMock.expect(mockedBeanMgr.getReference(mockedBean, List.class, mockedCreationalCtx))
            .andReturn(stringList);
    EasyMock.expect(mockedBean.getScope())
            .andReturn((Class) ApplicationScoped.class);
    EasyMock.expect(mockedBeanMgr.isNormalScope(ApplicationScoped.class))
            .andReturn(false);
    mockedCreationalCtx.release();
    EasyMock.expectLastCall().once();
    control.replay();

    Bus bus = new ExtensionManagerBus();
    bus.setExtension(mockedBeanMgr, BeanManager.class);

    Instance<List> i = CDIUtils.getInstanceFromCDI(List.class, bus);
    assertEquals(stringList, i.getValue());
    i.release();

    control.verify();
}
 
Example 10
Source File: CdiPasswordCipher.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public String decrypt(final char[] encryptedPassword) {
    final String string = new String(encryptedPassword);
    final BeanManagerImpl mgr;
    try {
        final WebBeansContext wbc = WebBeansContext.currentInstance();
        mgr = wbc.getBeanManagerImpl();
        if (!mgr.isInUse()) { // not yet the time to use CDI, container is not started
            // would be cool to log a warning here but would pollute the logs with false positives
            return "cipher:cdi:" + string;
        }
    } catch (final IllegalStateException ise) { // no cdi
        return "cipher:cdi:" + string;
    }

    final int split = string.indexOf(':');
    final String delegate = string.substring(0, split);
    final String pwdStr = string.substring(split + 1, string.length());
    final char[] pwd = pwdStr.toCharArray();

    try {
        final Class<?> beanType = Thread.currentThread().getContextClassLoader().loadClass(delegate);
        final Bean<?> bean = mgr.resolve(mgr.getBeans(beanType));
        if (bean == null) {
            throw new IllegalArgumentException("No bean for " + delegate);
        }

        final CreationalContext<?> cc = mgr.createCreationalContext(null);
        try {
            return PasswordCipher.class.cast(mgr.getReference(bean, PasswordCipher.class, cc)).decrypt(pwd);
        } finally {
            if (!mgr.isNormalScope(bean.getScope())) {
                cc.release();
            }
        }
    } catch (final ClassNotFoundException e) {
        throw new IllegalArgumentException("Can't find " + delegate, e);
    }
}
 
Example 11
Source File: JAXRSCdiResourceExtension.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Releases created CreationalContext instances
 */
public void release(@Observes final BeforeShutdown event) {
    synchronized (disposableCreationalContexts) {
        for (final CreationalContext<?> disposableCreationalContext: disposableCreationalContexts) {
            disposableCreationalContext.release();
        }
        disposableCreationalContexts.clear();
    }

    disposableLifecycles.forEach(Lifecycle::destroy);
    disposableLifecycles.clear();
}
 
Example 12
Source File: DolphinPlatformContextualLifecycle.java    From dolphin-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void destroy(Bean<T> bean, T instance, CreationalContext<T> creationalContext) {
    Assert.requireNonNull(bean, "bean");
    Assert.requireNonNull(creationalContext, "creationalContext");
    injectionTarget.preDestroy(instance);
    creationalContext.release();
}
 
Example 13
Source File: CdiBusBean.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void destroy(final ExtensionManagerBus instance,
        final CreationalContext< ExtensionManagerBus > ctx) {
    injectionTarget.preDestroy(instance);
    injectionTarget.dispose(instance);
    instance.shutdown();
    ctx.release();
}
 
Example 14
Source File: SyntheticBean.java    From metrics-cdi with Apache License 2.0 4 votes vote down vote up
@Override
public void destroy(T instance, CreationalContext<T> context) {
    target.preDestroy(instance);
    target.dispose(instance);
    context.release();
}
 
Example 15
Source File: MetricCollectorBean.java    From hammock with Apache License 2.0 4 votes vote down vote up
@Override
public void destroy(MetricsCollector instance, CreationalContext<MetricsCollector> creationalContext) {
    creationalContext.release();
}
 
Example 16
Source File: CommonBean.java    From thorntail with Apache License 2.0 4 votes vote down vote up
@Override
public void destroy(T instance, CreationalContext<T> creationalContext) {
    creationalContext.release();
}
 
Example 17
Source File: CDIWebContext.java    From ozark with Apache License 2.0 4 votes vote down vote up
/**
 * Dispose all CDI creational contexts to depdenent scoped beans get disposed.
 * <p>
 * MUST be called after the template has been processed
 */
void close() {
    for (CreationalContext ctx : contexts) {
        ctx.release();
    }
}
 
Example 18
Source File: WebContext.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void destroy(final Object o) {
    final CreationalContext<?> ctx = creationalContexts.remove(o);
    if (ctx != null) {
        ctx.release();
    }
}
 
Example 19
Source File: InjectableBean.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
default void destroy(T instance, CreationalContext<T> creationalContext) {
    creationalContext.release();
}
 
Example 20
Source File: CDIWebContext.java    From krazo with Apache License 2.0 4 votes vote down vote up
/**
 * Dispose all CDI creational contexts to depdenent scoped beans get disposed.
 * <p>
 * MUST be called after the template has been processed
 */
void close() {
    for (CreationalContext ctx : contexts) {
        ctx.release();
    }
}