Search in sources :

Example 71 with Bean

use of javax.enterprise.inject.spi.Bean in project Payara by payara.

the class ServerSentEventServlet method doGet.

@Override
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // TODO needs to be injected
    if (extension == null) {
        throw new RuntimeException("Enable CDI by including empty WEB-INF/beans.xml");
    }
    Map<String, ServerSentEventApplication> applicationMap = extension.getApplicationMap();
    ServerSentEventApplication sseApp = applicationMap.get(req.getServletPath());
    Class<?> clazz = sseApp.getHandlerClass();
    ServerSentEventHandler sseh;
    CreationalContext cc;
    // Check if SSE handler can be instantiated via CDI
    Iterator<Bean<?>> it = bm.getBeans(clazz).iterator();
    if (it.hasNext()) {
        Bean bean = it.next();
        cc = bm.createCreationalContext(bean);
        sseh = (ServerSentEventHandler) bean.create(cc);
    } else {
        throw new RuntimeException("Cannot create ServerSentEventHandler using CDI");
    }
    ServerSentEventHandler.Status status = sseh.onConnecting(req);
    if (status == ServerSentEventHandler.Status.DONT_RECONNECT) {
        resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
        cc.release();
        return;
    }
    if (status != ServerSentEventHandler.Status.OK) {
        throw new RuntimeException("Internal Error: need to handle status " + status);
    }
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.setContentType("text/event-stream");
    // writes status code and headers
    resp.flushBuffer();
    AsyncContext ac = req.startAsync(req, resp);
    // no timeout. need config ?
    ac.setTimeout(0);
    ServerSentEventConnectionImpl con = sseApp.createConnection(req, sseh, cc, ac);
    ac.addListener(con);
    con.init();
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) ServerSentEventHandler(org.glassfish.sse.api.ServerSentEventHandler) AsyncContext(javax.servlet.AsyncContext) Bean(javax.enterprise.inject.spi.Bean)

Example 72 with Bean

use of javax.enterprise.inject.spi.Bean in project Payara by payara.

the class CDISecondChanceResolver method justInTimeResolution.

/* (non-Javadoc)
     * @see org.glassfish.hk2.api.JustInTimeInjectionResolver#justInTimeResolution(org.glassfish.hk2.api.Injectee)
     */
@SuppressWarnings({ "unchecked" })
@Override
public boolean justInTimeResolution(Injectee failedInjectionPoint) {
    Type requiredType = failedInjectionPoint.getRequiredType();
    Set<Annotation> setQualifiers = failedInjectionPoint.getRequiredQualifiers();
    Annotation[] qualifiers = setQualifiers.toArray(new Annotation[setQualifiers.size()]);
    BeanManager manager = getCurrentBeanManager();
    if (manager == null)
        return false;
    Set<Bean<?>> beans = manager.getBeans(requiredType, qualifiers);
    if (beans == null || beans.isEmpty()) {
        return false;
    }
    DynamicConfiguration config = ServiceLocatorUtilities.createDynamicConfiguration(locator);
    for (Bean<?> bean : beans) {
        // Add a bean to the service locator
        CDIHK2Descriptor<Object> descriptor = new CDIHK2Descriptor<Object>(manager, (Bean<Object>) bean, requiredType);
        config.addActiveDescriptor(descriptor);
    }
    config.commit();
    return true;
}
Also used : Type(java.lang.reflect.Type) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) BeanManager(javax.enterprise.inject.spi.BeanManager) Annotation(java.lang.annotation.Annotation) Bean(javax.enterprise.inject.spi.Bean)

Example 73 with Bean

use of javax.enterprise.inject.spi.Bean in project drools by kiegroup.

the class KieBusinessScopeContext method get.

// Return an existing instance of certain contextual type or create a new instance by calling
// javax.enterprise.context.spi.Contextual.create(CreationalContext) and return the new instance.
@Override
@SuppressWarnings("unchecked")
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    Bean bean = (Bean) contextual;
    // you can store the bean somewhere
    if (somewhere.containsKey(bean.getName())) {
        return (T) somewhere.get(bean.getName());
    } else {
        T t = (T) bean.create(creationalContext);
        somewhere.put(bean.getName(), t);
        return t;
    }
}
Also used : Bean(javax.enterprise.inject.spi.Bean)

Example 74 with Bean

use of javax.enterprise.inject.spi.Bean in project drools by kiegroup.

the class CDIScopeTest method testKieBaseScope.

@Test
public void testKieBaseScope() {
    Set<Bean<?>> beans = beanManager.getBeans(KieBase.class, new KBaseLiteral("org.kie.kbase2"));
    assertEquals(1, beans.size());
    Bean bean1 = beans.toArray(new Bean[1])[0];
    assertEquals(ApplicationScoped.class, bean1.getScope());
    beans = beanManager.getBeans(KieBase.class, new KBaseLiteral("org.kie.kbase3"));
    assertEquals(1, beans.size());
    bean1 = beans.toArray(new Bean[1])[0];
    assertEquals(SessionScoped.class, bean1.getScope());
}
Also used : KieBase(org.kie.api.KieBase) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)74 BeanManager (javax.enterprise.inject.spi.BeanManager)33 Annotation (java.lang.annotation.Annotation)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)8 CreationalContext (javax.enterprise.context.spi.CreationalContext)7 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)7 Test (org.junit.Test)7 Set (java.util.Set)5 ProcessBean (javax.enterprise.inject.spi.ProcessBean)5 ScopedInstance (org.eclipse.jetty.cdi.core.ScopedInstance)5 IOException (java.io.IOException)4 Type (java.lang.reflect.Type)4 Map (java.util.Map)4 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)4 HashMap (java.util.HashMap)3 ServletException (javax.servlet.ServletException)3 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)3 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)3 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)3