use of org.apache.openejb.AppContext in project tomee by apache.
the class AppFinder method findAppContextOrWeb.
public static <T> T findAppContextOrWeb(final ClassLoader cl, final Transformer<T> transformer) {
final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
for (final AppContext appContext : containerSystem.getAppContexts()) {
final ClassLoader appContextClassLoader = appContext.getClassLoader();
boolean found = false;
if (appContextClassLoader.equals(cl) || (cl != null && cl.equals(appContextClassLoader))) {
// CxfContainerLoader is not symmetric
final T from = transformer.from(appContext);
found = true;
if (from != null) {
return from;
}
}
for (final WebContext web : appContext.getWebContexts()) {
final ClassLoader webClassLoader = web.getClassLoader();
if (webClassLoader.equals(cl) || (cl != null && cl.equals(webClassLoader))) {
return transformer.from(web);
}
}
if (found) {
// for cases where app and webapp share the same classloader
break;
}
}
return null;
}
use of org.apache.openejb.AppContext in project tomee by apache.
the class ApplicationPropertiesTest method assertContexts.
private void assertContexts(final ContainerSystem containerSystem) {
final BeanContext beanContext = containerSystem.getBeanContext("WidgetBean");
final ModuleContext moduleContext = beanContext.getModuleContext();
final AppContext appContext = moduleContext.getAppContext();
{
// Assert as Properties
// AppContext should have color property
assertProperty(appContext.getProperties(), "color", "orange");
// BeanContext and ModuleContext should not
assertNoProperty(beanContext.getProperties(), "color");
assertNoProperty(moduleContext.getProperties(), "color");
// Try all the above again with mixed case
assertProperty(appContext.getProperties(), "coLOr", "orange");
assertNoProperty(beanContext.getProperties(), "coLOr");
assertNoProperty(moduleContext.getProperties(), "coLOr");
}
{
// Assert as Options
// AppContext should have color option
assertOption(appContext.getOptions(), "color", "orange");
// BeanContext and ModuleContext should inherit AppContext color
assertOption(beanContext.getOptions(), "color", "orange");
assertOption(moduleContext.getOptions(), "color", "orange");
// Try all the above again using mixed case
assertOption(appContext.getOptions(), "coLoR", "orange");
assertOption(moduleContext.getOptions(), "coLoR", "orange");
assertOption(beanContext.getOptions(), "coLoR", "orange");
}
}
use of org.apache.openejb.AppContext in project tomee by apache.
the class TestObserver method beanContext.
private BeanContext beanContext() {
final TestClass tc = testClass.get();
if (tc == null) {
return null;
}
final String className = tc.getName();
final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
for (final AppContext app : containerSystem.getAppContexts()) {
final BeanContext context = containerSystem.getBeanContext(app.getId() + "_" + className);
if (context != null) {
return context;
}
}
return null;
}
use of org.apache.openejb.AppContext in project tomee by apache.
the class OpenEJBDeployableContainer method undeploy.
@Override
public void undeploy(final Archive<?> archive) throws DeploymentException {
final Closeables cl = closeables.get();
if (cl != null) {
try {
cl.close();
} catch (final IOException e) {
// no-op
}
}
// reset classloader for next text
// otherwise if it was closed something can fail
classLoader.set(OpenEJBDeployableContainer.class.getClassLoader());
final AppContext ctx = appContext.get();
if (ctx == null) {
return;
} else {
// release all references of the previous one - classloaders whatever arquillian Instance impl is etc
appContextProducer.set(NO_APP_CTX);
}
try {
if (!configuration.isSingleDeploymentByArchiveName(archive.getName())) {
assembler.destroyApplication(info.get().path);
}
if (configuration.isStartDefaultScopes() && ctx.getWebBeansContext() != null) {
stopContexts(ctx.getWebBeansContext().getContextsService(), servletContext.get(), session.get());
}
} catch (final Exception e) {
throw new DeploymentException("can't undeploy " + archive.getName(), e);
}
}
use of org.apache.openejb.AppContext in project tomee by apache.
the class LightweightWebAppBuilder method deployWebApps.
@Override
public void deployWebApps(final AppInfo appInfo, final ClassLoader appClassLoader) throws Exception {
final CoreContainerSystem cs = (CoreContainerSystem) SystemInstance.get().getComponent(ContainerSystem.class);
final AppContext appContext = cs.getAppContext(appInfo.appId);
if (appContext == null) {
throw new OpenEJBRuntimeException("Can't find app context for " + appInfo.appId);
}
for (final WebAppInfo webAppInfo : appInfo.webApps) {
ClassLoader classLoader = loaderByWebContext.get(webAppInfo.moduleId);
if (classLoader == null) {
classLoader = appClassLoader;
}
final Set<Injection> injections = new HashSet<Injection>(appContext.getInjections());
injections.addAll(new InjectionBuilder(classLoader).buildInjections(webAppInfo.jndiEnc));
final List<BeanContext> beanContexts;
if (!appInfo.webAppAlone) {
// add module bindings in app
final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
beanContexts = assembler.initEjbs(classLoader, appInfo, appContext, injections, new ArrayList<BeanContext>(), webAppInfo.moduleId);
appContext.getBeanContexts().addAll(beanContexts);
} else {
beanContexts = null;
}
final Map<String, Object> bindings = new HashMap<>();
bindings.putAll(appContext.getBindings());
bindings.putAll(new JndiEncBuilder(webAppInfo.jndiEnc, injections, webAppInfo.moduleId, "Bean", null, webAppInfo.uniqueId, classLoader, appInfo.properties).buildBindings(JndiEncBuilder.JndiScope.comp));
final WebContext webContext = new WebContext(appContext);
webContext.setBindings(bindings);
webContext.getBindings().putAll(new JndiEncBuilder(webAppInfo.jndiEnc, injections, webAppInfo.moduleId, "Bean", null, webAppInfo.uniqueId, classLoader, appInfo.properties).buildBindings(JndiEncBuilder.JndiScope.comp));
webContext.setJndiEnc(WebInitialContext.create(bindings, appContext.getGlobalJndiContext()));
webContext.setClassLoader(classLoader);
webContext.setId(webAppInfo.moduleId);
webContext.setContextRoot(webAppInfo.contextRoot);
webContext.setHost(webAppInfo.host);
webContext.getInjections().addAll(injections);
webContext.setInitialContext(new EmbeddedInitialContext(webContext.getJndiEnc(), webContext.getBindings()));
final ServletContext component = SystemInstance.get().getComponent(ServletContext.class);
final ServletContextEvent sce = component == null ? new MockServletContextEvent() : new ServletContextEvent(new LightServletContext(component, webContext.getClassLoader()));
servletContextEvents.put(webAppInfo, sce);
webContext.setServletContext(sce.getServletContext());
SystemInstance.get().fireEvent(new EmbeddedServletContextCreated(sce.getServletContext()));
appContext.getWebContexts().add(webContext);
cs.addWebContext(webContext);
if (!appInfo.webAppAlone && hasCdi(appInfo)) {
final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
new CdiBuilder().build(appInfo, appContext, beanContexts, webContext);
assembler.startEjbs(true, beanContexts);
}
// listeners
for (final ListenerInfo listener : webAppInfo.listeners) {
final Class<?> clazz = webContext.getClassLoader().loadClass(listener.classname);
final Object instance = webContext.newInstance(clazz);
if (ServletContextListener.class.isInstance(instance)) {
switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {
@Override
public void run() {
((ServletContextListener) instance).contextInitialized(sce);
}
});
}
List<Object> list = listeners.get(webAppInfo);
if (list == null) {
list = new ArrayList<Object>();
listeners.put(webAppInfo, list);
}
list.add(instance);
}
for (final ClassListInfo info : webAppInfo.webAnnotatedClasses) {
final String url = info.name;
for (final String filterPath : info.list) {
final Class<?> clazz = loadFromUrls(webContext.getClassLoader(), url, filterPath);
final WebListener annotation = clazz.getAnnotation(WebListener.class);
if (annotation != null) {
final Object instance = webContext.newInstance(clazz);
if (ServletContextListener.class.isInstance(instance)) {
switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {
@Override
public void run() {
((ServletContextListener) instance).contextInitialized(sce);
}
});
}
List<Object> list = listeners.get(webAppInfo);
if (list == null) {
list = new ArrayList<Object>();
listeners.put(webAppInfo, list);
}
list.add(instance);
}
}
}
final DeployedWebObjects deployedWebObjects = new DeployedWebObjects();
deployedWebObjects.webContext = webContext;
servletDeploymentInfo.put(webAppInfo, deployedWebObjects);
if (webContext.getWebBeansContext() != null && webContext.getWebBeansContext().getBeanManagerImpl().isInUse()) {
final Thread thread = Thread.currentThread();
final ClassLoader old = thread.getContextClassLoader();
thread.setContextClassLoader(webContext.getClassLoader());
try {
OpenEJBLifecycle.class.cast(webContext.getWebBeansContext().getService(ContainerLifecycle.class)).startServletContext(sce.getServletContext());
} finally {
thread.setContextClassLoader(old);
}
}
if (addServletMethod == null) {
// can't manage filter/servlets
continue;
}
// register filters
for (final FilterInfo info : webAppInfo.filters) {
switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {
@Override
public void run() {
for (final String mapping : info.mappings) {
final FilterConfig config = new SimpleFilterConfig(sce.getServletContext(), info.name, info.initParams);
try {
addFilterMethod.invoke(null, info.classname, webContext, mapping, config);
deployedWebObjects.filterMappings.add(mapping);
} catch (final Exception e) {
LOGGER.warning(e.getMessage(), e);
}
}
}
});
}
for (final ClassListInfo info : webAppInfo.webAnnotatedClasses) {
final String url = info.name;
for (final String filterPath : info.list) {
final Class<?> clazz = loadFromUrls(webContext.getClassLoader(), url, filterPath);
final WebFilter annotation = clazz.getAnnotation(WebFilter.class);
if (annotation != null) {
final Properties initParams = new Properties();
for (final WebInitParam param : annotation.initParams()) {
initParams.put(param.name(), param.value());
}
final FilterConfig config = new SimpleFilterConfig(sce.getServletContext(), info.name, initParams);
for (final String[] mappings : asList(annotation.urlPatterns(), annotation.value())) {
switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {
@Override
public void run() {
for (final String mapping : mappings) {
try {
addFilterMethod.invoke(null, clazz.getName(), webContext, mapping, config);
deployedWebObjects.filterMappings.add(mapping);
} catch (final Exception e) {
LOGGER.warning(e.getMessage(), e);
}
}
}
});
}
}
}
}
final Map<String, PortInfo> ports = new TreeMap<String, PortInfo>();
for (final PortInfo port : webAppInfo.portInfos) {
ports.put(port.serviceLink, port);
}
// register servlets
for (final ServletInfo info : webAppInfo.servlets) {
if ("true".equalsIgnoreCase(appInfo.properties.getProperty("openejb.jaxrs.on", "true"))) {
// skip jaxrs servlets
boolean skip = false;
for (final ParamValueInfo pvi : info.initParams) {
if ("javax.ws.rs.Application".equals(pvi.name) || Application.class.getName().equals(pvi.name)) {
skip = true;
}
}
if (skip) {
continue;
}
if (info.servletClass == null) {
try {
if (Application.class.isAssignableFrom(classLoader.loadClass(info.servletName))) {
continue;
}
} catch (final Exception e) {
// no-op
}
}
}
// If POJO web services, it will be overriden with WsServlet
if (ports.containsKey(info.servletName) || ports.containsKey(info.servletClass)) {
continue;
}
// deploy
for (final String mapping : info.mappings) {
switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {
@Override
public void run() {
try {
addServletMethod.invoke(null, info.servletClass, webContext, mapping);
deployedWebObjects.mappings.add(mapping);
} catch (final Exception e) {
LOGGER.warning(e.getMessage(), e);
}
}
});
}
}
for (final ClassListInfo info : webAppInfo.webAnnotatedClasses) {
final String url = info.name;
for (final String servletPath : info.list) {
final Class<?> clazz = loadFromUrls(webContext.getClassLoader(), url, servletPath);
final WebServlet annotation = clazz.getAnnotation(WebServlet.class);
if (annotation != null) {
for (final String[] mappings : asList(annotation.urlPatterns(), annotation.value())) {
switchServletContextIfNeeded(sce.getServletContext(), new Runnable() {
@Override
public void run() {
for (final String mapping : mappings) {
try {
addServletMethod.invoke(null, clazz.getName(), webContext, mapping);
deployedWebObjects.mappings.add(mapping);
} catch (final Exception e) {
LOGGER.warning(e.getMessage(), e);
}
}
}
});
}
}
}
}
if (addDefaults != null && tryJsp()) {
addDefaults.invoke(null, webContext);
deployedWebObjects.mappings.add("*\\.jsp");
}
}
}
Aggregations