in server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java [125:317]
public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) {
if ("false".equalsIgnoreCase(appInfo.properties.getProperty("openejb.jaxrs.on", "true"))) {
return;
}
final WebContext webContext = containerSystem.getWebContextByHost(webApp.moduleId, webApp.host != null ? webApp.host : virtualHost);
if (webContext == null) {
return;
}
if (!deployedWebApps.add(webApp)) {
return;
}
final Map<String, EJBRestServiceInfo> restEjbs = getRestEjbs(appInfo, webApp.moduleId);
final ClassLoader classLoader = getClassLoader(webContext.getClassLoader());
final Collection<Injection> injections = webContext.getInjections();
final WebBeansContext owbCtx;
if (webContext.getWebbeansContext() != null) {
owbCtx = webContext.getWebbeansContext();
} else {
owbCtx = webContext.getAppContext().getWebBeansContext();
}
Context context = webContext.getJndiEnc();
if (context == null) { // usually true since it is set in org.apache.tomee.catalina.TomcatWebAppBuilder.afterStart() and lookup(comp) fails
context = webContext.getAppContext().getAppJndiContext();
}
final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
final Collection<Object> additionalProviders = new HashSet<>();
addAppProvidersIfNeeded(appInfo, webApp, classLoader, additionalProviders);
Collection<IdPropertiesInfo> pojoConfigurations = null; // done lazily
try {
boolean deploymentWithApplication = "true".equalsIgnoreCase(appInfo.properties.getProperty(OPENEJB_USE_APPLICATION_PROPERTY, APPLICATION_DEPLOYMENT));
if (deploymentWithApplication) {
Class<?> appClazz;
for (final String app : webApp.restApplications) {
Application application;
boolean appSkipped = false;
String prefix = "/";
try {
appClazz = classLoader.loadClass(app);
application = Application.class.cast(appClazz.newInstance());
if (owbCtx != null && owbCtx.getBeanManagerImpl().isInUse()) {
try {
webContext.inject(application);
} catch (final Exception e) {
// not important since not required by the spec
}
}
} catch (final Exception e) {
throw new OpenEJBRestRuntimeException("can't create class " + app, e);
}
application = wrapApplication(appInfo, application);
final Set<Class<?>> classes = new HashSet<>(application.getClasses());
final Set<Object> singletons = application.getSingletons();
if (classes.size() + singletons.size() == 0) {
appSkipped = true;
} else {
for (final Class<?> clazz : classes) {
if (isProvider(clazz)) {
additionalProviders.add(clazz);
} else if (!hasEjbAndIsNotAManagedBean(restEjbs, clazz.getName())) {
pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
if (PojoUtil.findConfiguration(pojoConfigurations, clazz.getName()) != null) {
deploymentWithApplication = false;
logOldDeploymentUsage(clazz.getName());
}
}
}
if (deploymentWithApplication) { // don't do it if we detected we should use old deployment
for (final Object o : singletons) {
final Class<?> clazz = o.getClass();
if (isProvider(clazz)) {
additionalProviders.add(o);
} else if (!hasEjbAndIsNotAManagedBean(restEjbs, clazz.getName())) {
pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
if (PojoUtil.findConfiguration(pojoConfigurations, clazz.getName()) != null) {
deploymentWithApplication = false;
logOldDeploymentUsage(clazz.getName());
}
}
}
}
}
if (deploymentWithApplication) { // don't do it if we detected we should use old deployment
final String path = appPrefix(webApp, appClazz);
if (path != null) {
prefix += path + wildcard;
} else {
prefix += wildcard;
}
}
if (deploymentWithApplication) { // don't do it if we detected we should use old deployment
if (appSkipped || application == null) {
application = !InternalApplication.class.isInstance(application) ? new InternalApplication(application) : application;
for (final String clazz : webApp.restClass) {
try {
final Class<?> loaded = classLoader.loadClass(clazz);
if (!isProvider(loaded)) {
pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
if (PojoUtil.findConfiguration(pojoConfigurations, loaded.getName()) != null) {
deploymentWithApplication = false;
logOldDeploymentUsage(loaded.getName());
break;
}
application.getClasses().add(loaded);
} else {
additionalProviders.add(loaded);
}
} catch (final Exception e) {
throw new OpenEJBRestRuntimeException("can't load class " + clazz, e);
}
}
if (deploymentWithApplication) {
addEjbToApplication(application, restEjbs);
if (!prefix.endsWith(wildcard)) {
prefix += wildcard;
}
}
}
if (!application.getClasses().isEmpty() || !application.getSingletons().isEmpty()) {
pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
deployApplication(appInfo, webApp.contextRoot, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations, application, prefix);
}
}
if (!deploymentWithApplication) {
fullServletDeployment(appInfo, webApp, webContext, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations);
}
}
if (webApp.restApplications.isEmpty()) {
final Application application = new InternalApplication(null);
for (final String clazz : webApp.restClass) {
try {
final Class<?> loaded = classLoader.loadClass(clazz);
if (!isProvider(loaded)) {
pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
if (PojoUtil.findConfiguration(pojoConfigurations, loaded.getName()) != null) {
deploymentWithApplication = false;
logOldDeploymentUsage(loaded.getName());
break;
}
application.getClasses().add(loaded);
} else {
additionalProviders.add(loaded);
}
} catch (final Exception e) {
throw new OpenEJBRestRuntimeException("can't load class " + clazz, e);
}
}
addEjbToApplication(application, restEjbs);
if (deploymentWithApplication) {
if (!application.getClasses().isEmpty() || !application.getSingletons().isEmpty()) {
final String path = appPrefix(webApp, application.getClass());
final String prefix;
if (path != null) {
prefix = "/" + path + wildcard;
} else {
prefix = "/" + wildcard;
}
pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
deployApplication(appInfo, webApp.contextRoot, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations, application, prefix);
}
} else {
fullServletDeployment(appInfo, webApp, webContext, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations);
}
}
} else {
fullServletDeployment(appInfo, webApp, webContext, restEjbs, classLoader, injections, owbCtx, context, additionalProviders, pojoConfigurations);
}
} finally {
Thread.currentThread().setContextClassLoader(oldLoader);
}
}