Repository: tomee Updated Branches: refs/heads/tomee-1.7.x 0f4e4d45d -> d98242efe
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/d98242ef Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/d98242ef Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/d98242ef Branch: refs/heads/tomee-1.7.x Commit: d98242efe436541a386aed52e44f28d11a6e8266 Parents: 0f4e4d4 Author: Romain Manni-Bucau <[email protected]> Authored: Wed Nov 19 13:22:21 2014 +0100 Committer: Romain Manni-Bucau <[email protected]> Committed: Wed Nov 19 13:22:21 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 | 14 ++++++++ .../openejb/core/CoreContainerSystem.java | 38 +++++++++++++++++--- .../org/apache/openejb/spi/ContainerSystem.java | 3 ++ .../apache/openejb/server/rest/RESTService.java | 14 ++++++-- .../openejb/server/webservices/WsService.java | 2 +- .../tomee/catalina/TomcatJndiBuilder.java | 9 ++--- .../tomee/catalina/TomcatWebAppBuilder.java | 2 +- .../tomee/webservices/TomcatRsRegistry.java | 4 ++- 11 files changed, 98 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/d98242ef/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 b7c4347..9c574be 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/d98242ef/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/d98242ef/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 009aecd..874f844 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 @@ -192,7 +192,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/d98242ef/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..196f21c 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,16 @@ public class InitEjbDeployments implements DynamicDeployer { return appModule; } + 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/d98242ef/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..ea32607 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,18 +117,47 @@ 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<WebContext>(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) { this.webDeployments.remove(info.getId()); } http://git-wip-us.apache.org/repos/asf/tomee/blob/d98242ef/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/d98242ef/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 00f37a8..7e2e1bd 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/d98242ef/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/d98242ef/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 c100649..f27ee29 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 @@ -171,11 +171,12 @@ public class TomcatJndiBuilder { } } - WebContext webContext = cs.getWebContext(path); + final String hostname = org.apache.tomee.catalina.Contexts.getHostname(standardContext); + 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); } } @@ -186,7 +187,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/d98242ef/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatWebAppBuilder.java ---------------------------------------------------------------------- diff --git a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatWebAppBuilder.java b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatWebAppBuilder.java index 747e526..a24e6f0 100644 --- a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatWebAppBuilder.java +++ b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatWebAppBuilder.java @@ -1612,7 +1612,7 @@ public class TomcatWebAppBuilder implements WebAppBuilder, ContextListener, Pare // add context to WebDeploymentInfo if (currentWebAppInfo != null) { - final WebContext webContext = getContainerSystem().getWebContext(currentWebAppInfo.moduleId); + final WebContext webContext = getContainerSystem().getWebContextByHost(currentWebAppInfo.moduleId, Contexts.getHostname(standardContext)); if (webContext != null) { webContext.setJndiEnc(comp); } http://git-wip-us.apache.org/repos/asf/tomee/blob/d98242ef/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 adf5cc0..8f185cb 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 @@ -163,13 +163,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; }
