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();
}
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;
}
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;
}
}
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());
}
Aggregations