use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class ThreadSingletonServiceImpl method get.
/**
* Generally contexts.get() is enough since we set the current context from a request (see webbeanslistener)
* but sometimes matching the classloader is better (manager webapps of tomcat deploys for instance)
* so here the algorithm:
* 1) try to match with the classloader
* 2) if not matched try to use the threadlocal
* 3) (shouldn't happen) simply return the biggest webbeancontext
*
* @param cl the key (generally TCCL)
* @return the webbeancontext matching the current context
*/
public static WebBeansContext get(final ClassLoader cl) {
WebBeansContext context = contextByClassLoader.get(cl);
if (context != null) {
return context;
}
context = AppFinder.findAppContextOrWeb(cl, AppFinder.WebBeansContextTransformer.INSTANCE);
if (context == null) {
context = contexts.get();
if (context == null) {
// any "guess" algortithm there would break prod apps cause AppFinder failed already, let's try to not try to be more clever than we can
throw new IllegalStateException("On a thread without an initialized context nor a classloader mapping a deployed app");
}
} else {
// some cache to avoid to browse each app each time
contextByClassLoader.put(cl, context);
}
return context;
}
use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class ThreadSingletonServiceImpl method initialize.
@Override
public void initialize(final StartupObject startupObject) {
if (lazyInit == null) {
// done here cause Cdibuilder trigger this class loading and that's from Warmup so we can't init too early config
synchronized (this) {
if (lazyInit == null) {
lazyInit = new Object();
cachedApplicationScoped = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cdi.applicationScope.cached", "true").trim());
cachedRequestScoped = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cdi.requestScope.cached", "true").trim());
cachedSessionScoped = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cdi.sessionScope.cached", "true").trim());
}
}
}
final AppContext appContext = startupObject.getAppContext();
appContext.setCdiEnabled(hasBeans(startupObject.getAppInfo()));
//initialize owb context, cf geronimo's OpenWebBeansGBean
final Properties properties = new Properties();
properties.setProperty(OpenWebBeansConfiguration.APPLICATION_IS_JSP, "true");
properties.setProperty(OpenWebBeansConfiguration.USE_EJB_DISCOVERY, "true");
//from CDI builder
properties.setProperty(OpenWebBeansConfiguration.INTERCEPTOR_FORCE_NO_CHECKED_EXCEPTIONS, "false");
properties.setProperty(SecurityService.class.getName(), ManagedSecurityService.class.getName());
properties.setProperty(OpenWebBeansConfiguration.CONVERSATION_PERIODIC_DELAY, "1800000");
properties.setProperty(OpenWebBeansConfiguration.APPLICATION_SUPPORTS_CONVERSATION, "true");
properties.setProperty(OpenWebBeansConfiguration.IGNORED_INTERFACES, "org.apache.aries.proxy.weaving.WovenProxy");
final boolean tomee = SystemInstance.get().getProperty("openejb.loader", "foo").startsWith("tomcat");
final String defaultNormalScopeHandlerClass = NormalScopedBeanInterceptorHandler.class.getName();
properties.setProperty("org.apache.webbeans.proxy.mapping.javax.enterprise.context.ApplicationScoped", cachedApplicationScoped ? ApplicationScopedBeanInterceptorHandler.class.getName() : defaultNormalScopeHandlerClass);
properties.setProperty("org.apache.webbeans.proxy.mapping.javax.enterprise.context.RequestScoped", tomee && cachedRequestScoped ? RequestScopedBeanInterceptorHandler.class.getName() : defaultNormalScopeHandlerClass);
properties.setProperty("org.apache.webbeans.proxy.mapping.javax.enterprise.context.SessionScoped", tomee && cachedSessionScoped ? SessionScopedBeanInterceptorHandler.class.getName() : defaultNormalScopeHandlerClass);
properties.put(OpenWebBeansConfiguration.PRODUCER_INTERCEPTION_SUPPORT, SystemInstance.get().getProperty("openejb.cdi.producer.interception", "true"));
properties.putAll(appContext.getProperties());
// services needing WBC as constructor param
properties.put(ContextsService.class.getName(), CdiAppContextsService.class.getName());
properties.put(ResourceInjectionService.class.getName(), CdiResourceInjectionService.class.getName());
properties.put(TransactionService.class.getName(), OpenEJBTransactionService.class.getName());
// NOTE: ensure user can extend/override all the services = set it only if not present in properties, see WebBeansContext#getService()
final Map<Class<?>, Object> services = new HashMap<>();
services.put(AppContext.class, appContext);
if (!properties.containsKey(ApplicationBoundaryService.class.getName())) {
services.put(ApplicationBoundaryService.class, new DefaultApplicationBoundaryService());
}
if (!properties.containsKey(ScannerService.class.getName())) {
services.put(ScannerService.class, new CdiScanner());
}
if (!properties.containsKey(JNDIService.class.getName())) {
services.put(JNDIService.class, new OpenEJBJndiService());
}
if (!properties.containsKey(BeanArchiveService.class.getName())) {
services.put(BeanArchiveService.class, new OpenEJBBeanInfoService());
}
if (!properties.containsKey(ELAdaptor.class.getName())) {
try {
services.put(ELAdaptor.class, new CustomELAdapter(appContext));
} catch (final NoClassDefFoundError noClassDefFoundError) {
// no-op: no javax.el
}
}
if (!properties.containsKey(LoaderService.class.getName())) {
final LoaderService loaderService = SystemInstance.get().getComponent(LoaderService.class);
if (loaderService == null && !properties.containsKey(LoaderService.class.getName())) {
services.put(LoaderService.class, new OptimizedLoaderService(appContext.getProperties()));
} else if (loaderService != null) {
services.put(LoaderService.class, loaderService);
}
}
final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
final ClassLoader cl;
if (oldClassLoader != ThreadSingletonServiceImpl.class.getClassLoader() && ThreadSingletonServiceImpl.class.getClassLoader() != oldClassLoader.getParent()) {
cl = new MultipleClassLoader(oldClassLoader, ThreadSingletonServiceImpl.class.getClassLoader());
} else {
cl = oldClassLoader;
}
Thread.currentThread().setContextClassLoader(cl);
final WebBeansContext webBeansContext;
Object old = null;
try {
if (startupObject.getWebContext() == null) {
webBeansContext = new WebBeansContext(services, properties);
appContext.set(WebBeansContext.class, webBeansContext);
} else {
webBeansContext = new WebappWebBeansContext(services, properties, appContext.getWebBeansContext());
startupObject.getWebContext().setWebbeansContext(webBeansContext);
}
// we want the same reference as the ContextsService if that's our impl
if (webBeansContext.getOpenWebBeansConfiguration().supportsConversation() && "org.apache.webbeans.jsf.DefaultConversationService".equals(webBeansContext.getOpenWebBeansConfiguration().getProperty(ConversationService.class.getName()))) {
webBeansContext.registerService(ConversationService.class, ConversationService.class.cast(webBeansContext.getService(ContextsService.class)));
}
final BeanManagerImpl beanManagerImpl = webBeansContext.getBeanManagerImpl();
beanManagerImpl.addContext(new TransactionContext());
webBeansContext.getInterceptorsManager().addInterceptorBindingType(Transactional.class);
SystemInstance.get().fireEvent(new WebBeansContextCreated(webBeansContext));
old = contextEntered(webBeansContext);
setConfiguration(webBeansContext.getOpenWebBeansConfiguration());
try {
webBeansContext.getService(ContainerLifecycle.class).startApplication(startupObject);
} catch (final Exception e) {
throw new DeploymentException("couldn't start owb context", e);
}
} finally {
contextExited(old);
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class OWBContextThreadListener method contextEntered.
@Override
public void contextEntered(final ThreadContext oldContext, final ThreadContext newContext) {
final BeanContext beanContext = newContext.getBeanContext();
if (beanContext == null) {
// OWBContextHolder will be null so calling contextExited will throw a NPE
return;
}
final ModuleContext moduleContext = beanContext.getModuleContext();
//TODO its not clear what the scope for one of these context should be: ejb, module, or app
//For now, go with the attachment of the BeanManager to AppContext
final AppContext appContext = moduleContext.getAppContext();
final WebBeansContext owbContext = appContext.getWebBeansContext();
final Object oldOWBContext;
if (owbContext != null) {
oldOWBContext = singletonService.contextEntered(owbContext);
} else {
oldOWBContext = null;
}
final OWBContextHolder holder = new OWBContextHolder(oldOWBContext);
newContext.set(OWBContextHolder.class, holder);
}
use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class CDILoginModule method initialize.
@Override
public void initialize(final Subject subject, final CallbackHandler callbackHandler, final Map<String, ?> sharedState, final Map<String, ?> options) {
final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
final BeanManagerImpl bm = webBeansContext.getBeanManagerImpl();
if (!bm.isInUse()) {
throw new OpenEJBRuntimeException("CDI not activated");
}
String delegate = String.valueOf(options.get("delegate"));
if ("null".equals(delegate)) {
final String app = findAppName(webBeansContext);
delegate = String.valueOf(options.get(app));
if ("null".equals(delegate)) {
throw new OpenEJBRuntimeException("Please specify a delegate class");
}
}
final Class<?> clazz;
try {
clazz = Thread.currentThread().getContextClassLoader().loadClass(delegate);
} catch (final ClassNotFoundException e) {
throw new OpenEJBRuntimeException(e.getMessage(), e);
}
cc = bm.createCreationalContext(null);
final String cdiName = String.valueOf(options.get("cdiName"));
if ("true".equals(String.valueOf(options.get("loginModuleAsCdiBean")))) {
final Set<Bean<?>> beans;
if ("null".equals(cdiName)) {
beans = bm.getBeans(clazz);
} else {
beans = bm.getBeans(cdiName);
}
loginModule = LoginModule.class.cast(bm.getReference(bm.resolve(beans), clazz, cc));
} else {
try {
loginModule = LoginModule.class.cast(clazz.newInstance());
OWBInjector.inject(bm, loginModule, cc);
} catch (final Exception e) {
throw new OpenEJBRuntimeException("Can't inject into delegate class " + loginModule, e);
}
}
loginModule.initialize(subject, callbackHandler, sharedState, options);
}
use of org.apache.webbeans.config.WebBeansContext in project tomee by apache.
the class StatefulConversationScopedTOMEE1138Test method startConversation.
@Before
public void startConversation() {
final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
webBeansContext.registerService(ConversationService.class, new ConversationService() {
@Override
public String getConversationId() {
return "conversation-test";
}
@Override
public String generateConversationId() {
return "cid_1";
}
});
webBeansContext.getService(ContextsService.class).startContext(ConversationScoped.class, null);
}
Aggregations