Repository: tomee Updated Branches: refs/heads/develop aa59ad798 -> 4ff45bf73
TOMEE-1449 support same app on multiple hosts Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/4ff45bf7 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/4ff45bf7 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/4ff45bf7 Branch: refs/heads/develop Commit: 4ff45bf73ade4f561c347f8923035352d80cacab Parents: aa59ad7 Author: Romain Manni-Bucau <[email protected]> Authored: Wed Nov 19 13:21:40 2014 +0100 Committer: Romain Manni-Bucau <[email protected]> Committed: Wed Nov 19 13:21:40 2014 +0100 ---------------------------------------------------------------------- .../apache/openejb/config/AppInfoBuilder.java | 2 +- .../org/apache/openejb/config/AppModule.java | 23 ++++++++++++ .../org/apache/openejb/config/AutoConfig.java | 2 +- .../openejb/config/InitEjbDeployments.java | 27 ++++++++++++++ .../openejb/core/CoreContainerSystem.java | 37 +++++++++++++++++--- .../org/apache/openejb/spi/ContainerSystem.java | 3 ++ .../apache/openejb/server/rest/RESTService.java | 14 ++++++-- .../openejb/server/webservices/WsService.java | 2 +- .../tomee/catalina/TomcatJndiBuilder.java | 11 +++--- .../tomee/webservices/TomcatRsRegistry.java | 4 ++- 10 files changed, 110 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java b/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java index be0657b..032cff3 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java @@ -640,7 +640,7 @@ class AppInfoBuilder { final Persistence persistence = persistenceModule.getPersistence(); for (final PersistenceUnit persistenceUnit : persistence.getPersistenceUnit()) { final PersistenceUnitInfo info = new PersistenceUnitInfo(); - info.id = persistenceUnit.getName() + " " + rootUrl.hashCode(); + info.id = appModule.persistenceUnitId(rootUrl, persistenceUnit.getName()); info.name = persistenceUnit.getName(); info.watchedResources.addAll(persistenceModule.getWatchedResources()); info.persistenceUnitRootUrl = rootUrl; http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java b/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java index c11f6ec..7d71a80 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/AppModule.java @@ -79,6 +79,29 @@ public class AppModule implements DeploymentModule { this(classLoader, jarLocation, null, false); } + // shared between org.apache.openejb.config.AutoConfig.resolvePersistenceRefs() and org.apache.openejb.config.AppInfoBuilder.buildPersistenceModules() + public String persistenceUnitId(final String rootUrl, final String name) { + return name + " " + rootUrl.hashCode() + uniqueHostIfExists(); + } + + public String uniqueHostIfExists() { + final boolean hasWebApps = !getWebModules().isEmpty(); + if (isWebapp() && hasWebApps) { + return getWebModules().iterator().next().getHost(); + } else if (hasWebApps) { + String id = null; + for (final WebModule web : getWebModules()) { + if (id == null) { + id = web.getHost(); + } else if (!id.equals(web.getHost())) { + return ""; // find something better as in org.apache.openejb.config.InitEjbDeployments + } + } + return id; + } + return ""; + } + public <T extends DeploymentModule> AppModule(final T... modules) { final T firstModule = modules[0]; http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java b/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java index bd58150..33064f0 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java @@ -215,7 +215,7 @@ public class AutoConfig implements DynamicDeployer, JndiConstants { for (final PersistenceModule module : appModule.getPersistenceModules()) { final String rootUrl = module.getRootUrl(); for (final PersistenceUnit unit : module.getPersistence().getPersistenceUnit()) { - unit.setId(unit.getName() + " " + rootUrl.hashCode()); + unit.setId(appModule.persistenceUnitId(rootUrl, unit.getName())); persistenceUnits.add(rootUrl, unit.getName(), unit); } } http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/container/openejb-core/src/main/java/org/apache/openejb/config/InitEjbDeployments.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/InitEjbDeployments.java b/container/openejb-core/src/main/java/org/apache/openejb/config/InitEjbDeployments.java index c1f8ee3..1437928 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/config/InitEjbDeployments.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/InitEjbDeployments.java @@ -30,8 +30,10 @@ import org.apache.openejb.util.Logger; import org.apache.openejb.util.Messages; import org.apache.openejb.util.StringTemplate; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -67,6 +69,8 @@ public class InitEjbDeployments implements DynamicDeployer { contextData.put("appId", appModule.getModuleId()); for (final EjbModule ejbModule : appModule.getEjbModules()) { + contextData.put("host", ejbModule.isWebapp() ? findHost(ejbModule.getModuleId(), appModule.getWebModules()) : appModule.uniqueHostIfExists()); + contextData.put("hash", Integer.toString(ejbModule.hashCode())); contextData.put("ejbJarId", ejbModule.getModuleId()); deploy(ejbModule, contextData, abstractSchemaNames); } @@ -74,6 +78,29 @@ public class InitEjbDeployments implements DynamicDeployer { return appModule; } + private String findCommonHost(final Collection<WebModule> webModules) { + String host = null; + for (final WebModule w: webModules) { + final String wHost = w.getHost(); + if (host == null) { + host = wHost; + } else if (!host.equals(wHost)) { + return "lib"; // surely better to do + } + } + return host != null ? host : "localhost"; + } + + private String findHost(final String id, final Collection<WebModule> webModules) { + for (final WebModule w: webModules) { + if (w.getModuleId().equals(id)) { + final String host = w.getHost(); + return host != null ? host : "localhost"; + } + } + return "localhost"; + } + public EjbModule deploy(final EjbModule ejbModule) throws OpenEJBException { return deploy(ejbModule, new HashMap<String, String>(), new HashSet<String>()); } http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/container/openejb-core/src/main/java/org/apache/openejb/core/CoreContainerSystem.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/CoreContainerSystem.java b/container/openejb-core/src/main/java/org/apache/openejb/core/CoreContainerSystem.java index ac59ffa..3b41a3f 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/core/CoreContainerSystem.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/core/CoreContainerSystem.java @@ -27,6 +27,7 @@ import org.apache.openejb.spi.ContainerSystem; import javax.naming.Context; import javax.naming.NamingException; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -39,7 +40,7 @@ public class CoreContainerSystem implements ContainerSystem { private final Map<Object, AppContext> apps = new ConcurrentHashMap<Object, AppContext>(); private final Map<Object, BeanContext> deployments = new ConcurrentHashMap<Object, BeanContext>(); private final Map<Object, Container> containers = new ConcurrentHashMap<Object, Container>(); - private final Map<String, WebContext> webDeployments = new ConcurrentHashMap<String, WebContext>(); + private final Map<String, List<WebContext>> webDeployments = new ConcurrentHashMap<String, List<WebContext>>(); private final Context jndiContext; /** @@ -116,16 +117,44 @@ public class CoreContainerSystem implements ContainerSystem { } @Override + public WebContext getWebContextByHost(final String id, final String host) { + final List<WebContext> webContexts = webDeployments.get(id); + if (webContexts == null || webContexts.isEmpty()) { + return null; + } + if (webContexts.size() == 1 && webContexts.get(0).getHost() == null) { + return webContexts.get(0); + } + for (final WebContext web : webContexts) { + if (web.getHost() != null && web.getHost().equals(host)) { + return web; + } + } + return null; + } + + @Override public WebContext getWebContext(final String id) { - return webDeployments.get(id); + final List<WebContext> webContexts = webDeployments.get(id); + return webContexts != null && !webContexts.isEmpty() ? webContexts.get(0) : null; } public WebContext[] WebDeployments() { - return webDeployments.values().toArray(new WebContext[webDeployments.size()]); + final Collection<WebContext> all = new ArrayList<>(webDeployments.size()); + for (final Collection<WebContext> list : webDeployments.values()) { + all.addAll(list); + } + return all.toArray(new WebContext[all.size()]); } public void addWebContext(final WebContext webDeployment) { - this.webDeployments.put(webDeployment.getId(), webDeployment); + final String id = webDeployment.getId(); + List<WebContext> list = this.webDeployments.get(id); + if (list == null) { + list = new ArrayList<WebContext>(); + this.webDeployments.put(id, list); + } + list.add(webDeployment); } public void removeWebContext(final WebContext info) { http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/container/openejb-core/src/main/java/org/apache/openejb/spi/ContainerSystem.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/spi/ContainerSystem.java b/container/openejb-core/src/main/java/org/apache/openejb/spi/ContainerSystem.java index 9acf4ea..e9bea6f 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/spi/ContainerSystem.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/spi/ContainerSystem.java @@ -35,6 +35,9 @@ public interface ContainerSystem { Container[] containers(); + WebContext getWebContextByHost(String id, String host); + + @Deprecated // user getWebContextByHost WebContext getWebContext(String id); Context getJNDIContext(); http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java ---------------------------------------------------------------------- diff --git a/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java b/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java index c1cfe47..da6bcb8 100644 --- a/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java +++ b/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java @@ -100,7 +100,7 @@ public abstract class RESTService implements ServerService, SelfManaging { private final String wildcard = SystemInstance.get().getProperty("openejb.rest.wildcard", ".*"); // embedded = regex, tomee = servlet public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) { - final WebContext webContext = containerSystem.getWebContext(webApp.moduleId); + final WebContext webContext = containerSystem.getWebContextByHost(webApp.moduleId, webApp.host != null ? webApp.host : virtualHost); if (webContext == null) { return; } @@ -457,7 +457,8 @@ public abstract class RESTService implements ServerService, SelfManaging { } final RsHttpListener listener = createHttpListener(); - final RsRegistry.AddressInfo address = rsRegistry.createRsHttpListener(contextRoot, listener, classLoader, nopath.substring(NOPATH_PREFIX.length() - 1), virtualHost, auth, realm); + final String host = findHost(contextRoot, appInfo.webApps); + final RsRegistry.AddressInfo address = rsRegistry.createRsHttpListener(contextRoot, listener, classLoader, nopath.substring(NOPATH_PREFIX.length() - 1), host, auth, realm); services.add(new DeployedService(address.complete, contextRoot, application.getClass().getName())); listener.deployApplication(application, address.complete.substring(0, address.complete.length() - wildcard.length()), nopath.substring(NOPATH_PREFIX.length(), nopath.length() - wildcard.length()), additionalProviders, restEjbs, // app config @@ -465,6 +466,15 @@ public abstract class RESTService implements ServerService, SelfManaging { new ServiceConfiguration(configuration, appInfo.services)); // deployment config } + private String findHost(final String context, final Collection<WebAppInfo> webs) { + for (final WebAppInfo web : webs) { + if (context.equals(web.contextRoot)) { + return web.host != null ? web.host : virtualHost; + } + } + return virtualHost; + } + private static String appPrefix(final WebAppInfo info, final Class<?> appClazz) { StringBuilder builder = null; http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/server/openejb-webservices/src/main/java/org/apache/openejb/server/webservices/WsService.java ---------------------------------------------------------------------- diff --git a/server/openejb-webservices/src/main/java/org/apache/openejb/server/webservices/WsService.java b/server/openejb-webservices/src/main/java/org/apache/openejb/server/webservices/WsService.java index ba0a571..4aab10d 100644 --- a/server/openejb-webservices/src/main/java/org/apache/openejb/server/webservices/WsService.java +++ b/server/openejb-webservices/src/main/java/org/apache/openejb/server/webservices/WsService.java @@ -346,7 +346,7 @@ public abstract class WsService implements ServerService, SelfManaging { } public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) { - final WebContext webContext = containerSystem.getWebContext(webApp.moduleId); + final WebContext webContext = containerSystem.getWebContextByHost(webApp.moduleId, webApp.host != null ? webApp.host : virtualHost); if (webContext == null) return; http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatJndiBuilder.java ---------------------------------------------------------------------- diff --git a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatJndiBuilder.java b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatJndiBuilder.java index 3c360b4..8baa4e4 100644 --- a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatJndiBuilder.java +++ b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatJndiBuilder.java @@ -164,9 +164,10 @@ public class TomcatJndiBuilder { } // classical deployment - needed because can be overriden through META-INF/context.xml + final String hostname = org.apache.tomee.catalina.Contexts.getHostname(standardContext); String path = standardContext.findParameter(TomcatWebAppBuilder.OPENEJB_WEBAPP_MODULE_ID); if (path == null) { // standardContext not created by OpenEJB - path = org.apache.tomee.catalina.Contexts.getHostname(standardContext); + path = hostname; if (standardContext.getPath().startsWith("/")) { path += standardContext.getPath(); } else { @@ -174,11 +175,11 @@ public class TomcatJndiBuilder { } } - WebContext webContext = cs.getWebContext(path); + WebContext webContext = cs.getWebContextByHost(path, hostname); if (webContext == null) { // tomee-embedded deployment - webContext = cs.getWebContext(standardContext.getPath().replaceFirst("/", "")); + webContext = cs.getWebContextByHost(standardContext.getPath().replaceFirst("/", ""), hostname); if (webContext == null) { - webContext = cs.getWebContext(standardContext.getPath()); + webContext = cs.getWebContextByHost(standardContext.getPath(), hostname); } } @@ -189,7 +190,7 @@ public class TomcatJndiBuilder { if (webContext == null && contextInfo != null && contextInfo.appInfo != null) { // can happen if deployed from apps/ for (final WebAppInfo webAppInfo : contextInfo.appInfo.webApps) { if (webAppInfo.path != null && webAppInfo.path.replace(File.separatorChar, '/').equals(standardContext.getDocBase())) { - webContext = cs.getWebContext(webAppInfo.moduleId); + webContext = cs.getWebContextByHost(webAppInfo.moduleId, hostname); if (webContext != null) { break; } http://git-wip-us.apache.org/repos/asf/tomee/blob/4ff45bf7/tomee/tomee-jaxrs/src/main/java/org/apache/tomee/webservices/TomcatRsRegistry.java ---------------------------------------------------------------------- diff --git a/tomee/tomee-jaxrs/src/main/java/org/apache/tomee/webservices/TomcatRsRegistry.java b/tomee/tomee-jaxrs/src/main/java/org/apache/tomee/webservices/TomcatRsRegistry.java index 3b09bab..774c270 100644 --- a/tomee/tomee-jaxrs/src/main/java/org/apache/tomee/webservices/TomcatRsRegistry.java +++ b/tomee/tomee-jaxrs/src/main/java/org/apache/tomee/webservices/TomcatRsRegistry.java @@ -147,13 +147,15 @@ public class TomcatRsRegistry implements RsRegistry { if (webContext == null) { return completePath; } - return completePath.substring(webContext.length()); + return completePath.substring((webContext.length() > 0 && !webContext.startsWith("/") ? 1 : 0) + webContext.length()); } private static Context findContext(final Container host, final String webContext) { Context webapp = Context.class.cast(host.findChild(webContext)); if (webapp == null && "/".equals(webContext)) { // ROOT webapp = Context.class.cast(host.findChild("")); + } else if (webapp == null && webContext.length() > 0 && !webContext.startsWith("/")) { + webapp = Context.class.cast(host.findChild("/" + webContext)); } return webapp; }
