use of org.apache.webbeans.container.BeanManagerImpl in project tomee by apache.
the class Assembler method postConstructResources.
private void postConstructResources(final Set<String> resourceIds, final ClassLoader classLoader, final Context containerSystemContext, final AppContext appContext) throws NamingException, OpenEJBException {
final Thread thread = Thread.currentThread();
final ClassLoader oldCl = thread.getContextClassLoader();
try {
thread.setContextClassLoader(classLoader);
final List<ResourceInfo> resourceList = config.facilities.resources;
for (final ResourceInfo resourceInfo : resourceList) {
if (!resourceIds.contains(resourceInfo.id)) {
continue;
}
if (isTemplatizedResource(resourceInfo)) {
continue;
}
try {
Class<?> clazz;
try {
clazz = classLoader.loadClass(resourceInfo.className);
} catch (final ClassNotFoundException cnfe) {
// custom classpath
clazz = containerSystemContext.lookup(OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id).getClass();
}
final boolean initialize = "true".equalsIgnoreCase(String.valueOf(resourceInfo.properties.remove("InitializeAfterDeployment")));
final AnnotationFinder finder = Proxy.isProxyClass(clazz) ? null : new AnnotationFinder(new ClassesArchive(ancestors(clazz)));
final List<Method> postConstructs = finder == null ? Collections.<Method>emptyList() : finder.findAnnotatedMethods(PostConstruct.class);
final List<Method> preDestroys = finder == null ? Collections.<Method>emptyList() : finder.findAnnotatedMethods(PreDestroy.class);
CreationalContext<?> creationalContext = null;
Object originalResource = null;
if (!postConstructs.isEmpty() || initialize) {
originalResource = containerSystemContext.lookup(OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id);
Object resource = originalResource;
if (resource instanceof Reference) {
resource = unwrapReference(resource);
this.bindResource(resourceInfo.id, resource, true);
}
try {
// wire up CDI
if (appContext != null && appContext.getWebBeansContext() != null) {
final BeanManagerImpl beanManager = appContext.getWebBeansContext().getBeanManagerImpl();
if (beanManager.isInUse()) {
creationalContext = beanManager.createCreationalContext(null);
OWBInjector.inject(beanManager, resource, creationalContext);
}
}
if (!"none".equals(resourceInfo.postConstruct)) {
if (resourceInfo.postConstruct != null) {
final Method p = clazz.getDeclaredMethod(resourceInfo.postConstruct);
if (!p.isAccessible()) {
SetAccessible.on(p);
}
p.invoke(resource);
}
for (final Method m : postConstructs) {
if (!m.isAccessible()) {
SetAccessible.on(m);
}
m.invoke(resource);
}
}
} catch (final Exception e) {
logger.fatal("Error calling @PostConstruct method on " + resource.getClass().getName());
throw new OpenEJBException(e);
}
}
if (!"none".equals(resourceInfo.preDestroy)) {
if (resourceInfo.preDestroy != null) {
final Method p = clazz.getDeclaredMethod(resourceInfo.preDestroy);
if (!p.isAccessible()) {
SetAccessible.on(p);
}
preDestroys.add(p);
}
if (!preDestroys.isEmpty() || creationalContext != null) {
final String name = OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id;
if (originalResource == null) {
originalResource = containerSystemContext.lookup(name);
}
this.bindResource(resourceInfo.id, new ResourceInstance(name, originalResource, preDestroys, creationalContext), true);
}
}
// log unused now for these resources now we built the resource completely and @PostConstruct can have used injected properties
if (resourceInfo.unsetProperties != null && !isPassthroughType(resourceInfo)) {
final Set<String> unsetKeys = resourceInfo.unsetProperties.stringPropertyNames();
for (final String key : unsetKeys) {
// don't use keySet to auto filter txMgr for instance and not real properties!
unusedProperty(resourceInfo.id, logger, key);
}
}
} catch (final Exception e) {
logger.fatal("Error calling PostConstruct method on " + resourceInfo.id);
logger.fatal("Resource " + resourceInfo.id + " could not be initialized. Application will be undeployed.");
throw new OpenEJBException(e);
}
}
} finally {
thread.setContextClassLoader(oldCl);
}
}
use of org.apache.webbeans.container.BeanManagerImpl in project tomee by apache.
the class OpenEJBEnricher method enrich.
public static void enrich(final Object testInstance, final AppContext appCtx) {
// don't rely on arquillian since this enrichment should absolutely be done before the following ones
new MockitoEnricher().enrich(testInstance);
AppContext ctx = appCtx;
if (ctx == null) {
ctx = AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.AppContextTransformer.INSTANCE);
if (ctx == null) {
return;
}
}
final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(ctx.getId() + "_" + testInstance.getClass().getName());
final WebBeansContext appWBC = ctx.getWebBeansContext();
final BeanManagerImpl bm = appWBC == null ? null : appWBC.getBeanManagerImpl();
boolean ok = false;
for (final WebContext web : ctx.getWebContexts()) {
final WebBeansContext webBeansContext = web.getWebBeansContext();
if (webBeansContext == null) {
continue;
}
final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
if (webBeansContext != appWBC && webAppBm.isInUse()) {
try {
doInject(testInstance, context, webAppBm);
ok = true;
break;
} catch (final Exception e) {
// no-op, try next
}
}
}
if (bm != null && bm.isInUse() && !ok) {
try {
doInject(testInstance, context, bm);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, "Failed injection on: " + testInstance.getClass(), e);
if (RuntimeException.class.isInstance(e)) {
throw RuntimeException.class.cast(e);
}
throw new OpenEJBRuntimeException(e);
}
}
if (context != null) {
final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
final ThreadContext oldContext = ThreadContext.enter(callContext);
try {
final InjectionProcessor processor = new InjectionProcessor<>(testInstance, context.getInjections(), context.getJndiContext());
processor.createInstance();
} catch (final OpenEJBException e) {
// ignored
} finally {
ThreadContext.exit(oldContext);
}
}
}
use of org.apache.webbeans.container.BeanManagerImpl in project tomee by apache.
the class CxfRSService method contextCDIIntegration.
private void contextCDIIntegration(final WebBeansContext wbc) {
if (!enabled) {
return;
}
final BeanManagerImpl beanManagerImpl = wbc.getBeanManagerImpl();
if (!beanManagerImpl.getAdditionalQualifiers().contains(Context.class)) {
beanManagerImpl.addAdditionalQualifier(Context.class);
}
if (!hasBean(beanManagerImpl, SecurityContext.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(SecurityContext.class, ThreadLocalContextManager.SECURITY_CONTEXT));
}
if (!hasBean(beanManagerImpl, UriInfo.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(UriInfo.class, ThreadLocalContextManager.URI_INFO));
}
if (!hasBean(beanManagerImpl, HttpServletResponse.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(HttpServletResponse.class, ThreadLocalContextManager.HTTP_SERVLET_RESPONSE));
}
if (!hasBean(beanManagerImpl, HttpHeaders.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(HttpHeaders.class, ThreadLocalContextManager.HTTP_HEADERS));
}
if (!hasBean(beanManagerImpl, Request.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(Request.class, ThreadLocalContextManager.REQUEST));
}
if (!hasBean(beanManagerImpl, ServletConfig.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(ServletConfig.class, ThreadLocalContextManager.SERVLET_CONFIG));
}
if (!hasBean(beanManagerImpl, Providers.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(Providers.class, ThreadLocalContextManager.PROVIDERS));
}
if (!hasBean(beanManagerImpl, ContextResolver.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(ContextResolver.class, ThreadLocalContextManager.CONTEXT_RESOLVER));
}
if (!hasBean(beanManagerImpl, ResourceInfo.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(ResourceInfo.class, ThreadLocalContextManager.RESOURCE_INFO));
}
if (!hasBean(beanManagerImpl, ResourceContext.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(ResourceContext.class, ThreadLocalContextManager.RESOURCE_CONTEXT));
}
if (!hasBean(beanManagerImpl, HttpServletRequest.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(HttpServletRequest.class, ThreadLocalContextManager.HTTP_SERVLET_REQUEST));
}
if (!hasBean(beanManagerImpl, ServletRequest.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(ServletRequest.class, ThreadLocalContextManager.SERVLET_REQUEST));
}
if (!hasBean(beanManagerImpl, ServletContext.class)) {
beanManagerImpl.addInternalBean(new ContextBean<>(ServletContext.class, ThreadLocalContextManager.SERVLET_CONTEXT));
}
// hasBean() usage can have cached several things
beanManagerImpl.getInjectionResolver().clearCaches();
}
use of org.apache.webbeans.container.BeanManagerImpl in project tomee by apache.
the class CxfRsHttpListener method providers.
private List<Object> providers(final Collection<ServiceInfo> services, final Collection<Object> additionalProviders, final WebBeansContext ctx) {
final List<Object> instances = new ArrayList<>();
final BeanManagerImpl bm = ctx == null ? null : ctx.getBeanManagerImpl();
for (final Object o : additionalProviders) {
if (o instanceof Class<?>) {
final Class<?> clazz = (Class<?>) o;
if (isNotServerProvider(clazz)) {
continue;
}
final String name = clazz.getName();
if (shouldSkipProvider(name)) {
continue;
}
if (bm != null && bm.isInUse()) {
try {
final Set<Bean<?>> beans = bm.getBeans(clazz);
if (beans != null && !beans.isEmpty()) {
final Bean<?> bean = bm.resolve(beans);
final CreationalContextImpl<?> creationalContext = bm.createCreationalContext(bean);
instances.add(bm.getReference(bean, clazz, creationalContext));
toRelease.add(creationalContext);
continue;
}
} catch (final Throwable th) {
LOGGER.info("Can't use CDI to create provider " + name);
}
}
final Collection<Object> instance = ServiceInfos.resolve(services, new String[] { name }, OpenEJBProviderFactory.INSTANCE);
if (instance != null && !instance.isEmpty()) {
instances.add(instance.iterator().next());
} else {
try {
instances.add(newProvider(clazz));
} catch (final Exception e) {
LOGGER.error("can't instantiate " + name, e);
}
}
} else {
final Class<?> clazz = o.getClass();
if (isNotServerProvider(clazz)) {
continue;
}
final String name = clazz.getName();
if (shouldSkipProvider(name)) {
continue;
}
instances.add(o);
}
}
return instances;
}
use of org.apache.webbeans.container.BeanManagerImpl in project tomee by apache.
the class CxfRsHttpListener method findProviderComparator.
private Comparator<?> findProviderComparator(final ServiceConfiguration serviceConfiguration, final WebBeansContext ctx) {
final String comparatorKey = CXF_JAXRS_PREFIX + "provider-comparator";
final String comparatorClass = serviceConfiguration.getProperties().getProperty(comparatorKey, SystemInstance.get().getProperty(comparatorKey));
Comparator<Object> comparator = null;
if (comparatorClass == null) {
// try to rely on CXF behavior otherwise just reactivate DefaultProviderComparator.INSTANCE if it is an issue
return null;
} else {
final BeanManagerImpl bm = ctx == null ? null : ctx.getBeanManagerImpl();
if (bm != null && bm.isInUse()) {
try {
final Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(comparatorClass);
final Set<Bean<?>> beans = bm.getBeans(clazz);
if (beans != null && !beans.isEmpty()) {
final Bean<?> bean = bm.resolve(beans);
final CreationalContextImpl<?> creationalContext = bm.createCreationalContext(bean);
comparator = Comparator.class.cast(bm.getReference(bean, clazz, creationalContext));
toRelease.add(creationalContext);
}
} catch (final Throwable th) {
LOGGER.debug("Can't use CDI to load comparator " + comparatorClass);
}
}
if (comparator == null) {
comparator = Comparator.class.cast(ServiceInfos.resolve(serviceConfiguration.getAvailableServices(), comparatorClass));
}
if (comparator == null) {
try {
comparator = Comparator.class.cast(Thread.currentThread().getContextClassLoader().loadClass(comparatorClass).newInstance());
} catch (final Exception e) {
throw new IllegalArgumentException(e);
}
}
for (final Type itf : comparator.getClass().getGenericInterfaces()) {
if (!ParameterizedType.class.isInstance(itf)) {
continue;
}
final ParameterizedType pt = ParameterizedType.class.cast(itf);
if (Comparator.class == pt.getRawType() && pt.getActualTypeArguments().length > 0) {
final Type t = pt.getActualTypeArguments()[0];
if (Class.class.isInstance(t) && ProviderInfo.class == t) {
return comparator;
}
if (ParameterizedType.class.isInstance(t) && ProviderInfo.class == ParameterizedType.class.cast(t).getRawType()) {
return comparator;
}
}
}
return new ProviderComparatorWrapper(comparator);
}
}
Aggregations