You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/10/21 19:38:52 UTC

git commit: TOMEE-1418 getting loader config from inplace loader and not fom reading a file - context.xml

Repository: tomee
Updated Branches:
  refs/heads/tomee-1.7.x 0abe4a593 -> 400c886a6


TOMEE-1418 getting loader config from inplace loader and not fom reading a file - context.xml


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/400c886a
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/400c886a
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/400c886a

Branch: refs/heads/tomee-1.7.x
Commit: 400c886a604c1f843506216cd9e8bc2e369f241e
Parents: 0abe4a5
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Tue Oct 21 19:38:09 2014 +0200
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Tue Oct 21 19:38:44 2014 +0200

----------------------------------------------------------------------
 .../apache/openejb/config/DeploymentLoader.java | 43 ++++++++--
 .../openejb/config/QuickContextXmlParser.java   | 87 --------------------
 .../apache/tomee/catalina/LazyStopLoader.java   |  4 +
 .../tomee/catalina/TomcatWebAppBuilder.java     | 37 ++++++++-
 4 files changed, 73 insertions(+), 98 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/400c886a/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java b/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
index 5abd332..0a7514b 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
@@ -110,7 +110,12 @@ public class DeploymentLoader implements DeploymentFilterable {
 
     private volatile List<URL> containerUrls;
 
+    @Deprecated
     public AppModule load(final File jarFile) throws OpenEJBException {
+        return load(jarFile, null);
+    }
+
+    public AppModule load(final File jarFile, final ExternalConfiguration config) throws OpenEJBException {
         // verify we have a valid file
         final String jarPath;
         try {
@@ -210,7 +215,7 @@ public class DeploymentLoader implements DeploymentFilterable {
                 final File file = URLs.toFile(baseUrl);
 
                 // Standalone Web Module
-                final WebModule webModule = createWebModule(file.getAbsolutePath(), baseUrl, getOpenEJBClassLoader(), getContextRoot(), getModuleName());
+                final WebModule webModule = createWebModule(file.getAbsolutePath(), baseUrl, getOpenEJBClassLoader(), getContextRoot(), getModuleName(), config);
                 // important to use the webapp classloader here otherwise each time we'll check something using loadclass it will fail (=== empty classloader)
                 final AppModule appModule = new AppModule(webModule.getClassLoader(), file.getAbsolutePath(), new Application(), true);
                 addWebModule(webModule, appModule);
@@ -775,12 +780,12 @@ public class DeploymentLoader implements DeploymentFilterable {
         return ejbModule;
     }
 
-    private WebModule createWebModule(final String jar, final URL warUrl, final ClassLoader parentClassLoader, final String contextRoot, final String moduleName) throws OpenEJBException {
-        return createWebModule(jar, URLs.toFilePath(warUrl), parentClassLoader, contextRoot, moduleName);
+    private WebModule createWebModule(final String jar, final URL warUrl, final ClassLoader parentClassLoader, final String contextRoot, final String moduleName, final ExternalConfiguration config) throws OpenEJBException {
+        return createWebModule(jar, URLs.toFilePath(warUrl), parentClassLoader, contextRoot, moduleName, config);
     }
 
     public void addWebModule(final AppModule appModule, final URL warUrl, final ClassLoader parentClassLoader, final String contextRoot, final String moduleName) throws OpenEJBException {
-        final WebModule webModule = createWebModule(appModule.getJarLocation(), URLs.toFilePath(warUrl), parentClassLoader, contextRoot, moduleName);
+        final WebModule webModule = createWebModule(appModule.getJarLocation(), URLs.toFilePath(warUrl), parentClassLoader, contextRoot, moduleName, null);
         addWebModule(webModule, appModule);
     }
 
@@ -894,7 +899,7 @@ public class DeploymentLoader implements DeploymentFilterable {
 
     }
 
-    public WebModule createWebModule(final String appId, final String warPath, final ClassLoader parentClassLoader, final String contextRoot, final String moduleName) throws OpenEJBException {
+    public WebModule createWebModule(final String appId, final String warPath, final ClassLoader parentClassLoader, final String contextRoot, final String moduleName, final ExternalConfiguration config) throws OpenEJBException {
         File warFile = new File(warPath);
         if (!warFile.isDirectory()) {
             warFile = unpack(warFile);
@@ -989,10 +994,18 @@ public class DeploymentLoader implements DeploymentFilterable {
         webUrls.addAll(addedUrls);
 
         // context.xml can define some additional libraries
-        final File contextXml = new File(warFile, "META-INF/context.xml");
-        if (contextXml.exists()) {
-            final QuickContextXmlParser parser = QuickContextXmlParser.parse(contextXml);
-            webUrls.addAll(parser.getAdditionalURLs());
+        if (config != null) { // we don't test all !=null inline to show that config will get extra params in the future and that it is hierarchic
+            if (config.getClasspath() != null) {
+                final Set<URL> contextXmlUrls = new LinkedHashSet<URL>();
+                for (final String location : config.getClasspath()) {
+                    try {
+                        webUrls.add(new File(location).toURI().toURL());
+                    } catch (final MalformedURLException e) {
+                        throw new IllegalArgumentException(e);
+                    }
+                }
+                webUrls.addAll(contextXmlUrls);
+            }
         }
 
         final ClassLoaderConfigurer configurer = QuickJarsTxtParser.parse(new File(warFile, "WEB-INF/" + QuickJarsTxtParser.FILE_NAME));
@@ -2049,4 +2062,16 @@ public class DeploymentLoader implements DeploymentFilterable {
     public static void reloadAltDD() {
         ALTDD = SystemInstance.get().getOptions().get(OPENEJB_ALTDD_PREFIX, (String) null);
     }
+
+    public static class ExternalConfiguration {
+        private final String[] classpath;
+
+        public ExternalConfiguration(final String[] classpath) {
+            this.classpath = classpath;
+        }
+
+        public String[] getClasspath() {
+            return classpath;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/400c886a/container/openejb-core/src/main/java/org/apache/openejb/config/QuickContextXmlParser.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/QuickContextXmlParser.java b/container/openejb-core/src/main/java/org/apache/openejb/config/QuickContextXmlParser.java
deleted file mode 100644
index 0a8edf0..0000000
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/QuickContextXmlParser.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.openejb.config;
-
-import org.apache.openejb.loader.Files;
-import org.apache.openejb.loader.ProvisioningUtil;
-import org.apache.openejb.util.PropertyPlaceHolderHelper;
-import org.xml.sax.Attributes;
-import org.xml.sax.SAXException;
-import org.xml.sax.helpers.DefaultHandler;
-
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-import java.io.File;
-import java.net.URL;
-import java.util.Collection;
-import java.util.LinkedHashSet;
-import java.util.Set;
-import java.util.StringTokenizer;
-
-public class QuickContextXmlParser extends DefaultHandler {
-    private static final SAXParserFactory FACTORY = SAXParserFactory.newInstance();
-
-    static {
-        FACTORY.setNamespaceAware(true);
-        FACTORY.setValidating(false);
-    }
-
-    private String virtualClasspath = "";
-
-    @Override
-    public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
-        if ("Loader".equalsIgnoreCase(localName)) {
-            final String className = attributes.getValue("className");
-            if (className != null) {
-                if ("org.apache.catalina.loader.VirtualWebappLoader".equals(className)
-                    || "org.apache.tomee.catalina.ProvisioningWebappLoader".equals(className)) {
-                    virtualClasspath = attributes.getValue("virtualClasspath");
-                }
-            }
-        }
-    }
-
-    public Collection<URL> getAdditionalURLs() {
-        final Set<URL> set = new LinkedHashSet<URL>();
-
-        if (virtualClasspath != null) {
-            final StringTokenizer tkn = new StringTokenizer(virtualClasspath, ";");
-            while (tkn.hasMoreTokens()) {
-                final String token = tkn.nextToken().trim();
-                if (token.isEmpty()) {
-                    continue;
-                }
-
-                set.addAll(Files.listJars(ProvisioningUtil.realLocation(PropertyPlaceHolderHelper.simpleValue(token))));
-            }
-        }
-
-        return set;
-    }
-
-    public static QuickContextXmlParser parse(final File contextXml) {
-        final QuickContextXmlParser handler = new QuickContextXmlParser();
-        try {
-            final SAXParser parser = FACTORY.newSAXParser();
-            parser.parse(contextXml, handler);
-        } catch (final Exception e) {
-            // no-op: not parseable so ignoring
-        }
-        return handler;
-    }
-}

http://git-wip-us.apache.org/repos/asf/tomee/blob/400c886a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/LazyStopLoader.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/LazyStopLoader.java b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/LazyStopLoader.java
index 5e31b29..49fecd1 100644
--- a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/LazyStopLoader.java
+++ b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/LazyStopLoader.java
@@ -173,4 +173,8 @@ public class LazyStopLoader implements Loader, Lifecycle {
     public ClassLoader getStopClassLoader() {
         return classLoader;
     }
+
+    public Loader getDelegateLoader() {
+        return delegate;
+    }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/400c886a/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 fdf6420..9c7fd00 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
@@ -105,6 +105,7 @@ import org.apache.openejb.core.ivm.naming.SystemComponentReference;
 import org.apache.openejb.jee.EnvEntry;
 import org.apache.openejb.jee.WebApp;
 import org.apache.openejb.loader.IO;
+import org.apache.openejb.loader.ProvisioningUtil;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.server.httpd.BeginWebBeansListener;
 import org.apache.openejb.server.httpd.EndWebBeansListener;
@@ -160,6 +161,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -2037,7 +2039,7 @@ public class TomcatWebAppBuilder implements WebAppBuilder, ContextListener, Pare
                     final AppInfo appInfo;
                     try {
                         file = file.getCanonicalFile().getAbsoluteFile();
-                        final AppModule appModule = deploymentLoader.load(file);
+                        final AppModule appModule = deploymentLoader.load(file, null);
 
                         // Ignore any standalone web modules - this happens when the app is unpaked and doesn't have a WEB-INF dir
                         if (appModule.getDeploymentModule().size() == 1 && appModule.getWebModules().size() == 1) {
@@ -2126,6 +2128,37 @@ public class TomcatWebAppBuilder implements WebAppBuilder, ContextListener, Pare
         return file;
     }
 
+    private static DeploymentLoader.ExternalConfiguration configuredClasspath(final StandardContext standardContext) {
+        Loader loader = standardContext.getLoader();
+        if (loader != null && LazyStopLoader.class.isInstance(loader)) {
+            loader = LazyStopLoader.class.cast(loader).getDelegateLoader();
+        }
+        if (loader != null) {
+            final ClassLoader cl = standardContext.getLoader().getClassLoader();
+            if (cl == null) {
+                return null;
+            }
+
+            final Collection<String> cp = new LinkedList<String>();
+
+            final String name = loader.getClass().getName();
+            if ("org.apache.catalina.loader.VirtualWebappLoader".equals(name)
+                    || "org.apache.tomee.catalina.ProvisioningWebappLoader".equals(name)) {
+                final Object virtualClasspath = Reflections.get(loader, "virtualClasspath");
+                if (virtualClasspath != null) {
+                    for (final String str : virtualClasspath.toString().split(";")) {
+                        cp.add(ProvisioningUtil.realLocation(str));
+                    }
+                }
+            }
+
+            if (!cp.isEmpty()) {
+                return new DeploymentLoader.ExternalConfiguration(cp.toArray(new String[cp.size()]));
+            }
+        }
+        return null;
+    }
+
     /**
      * Creates an openejb {@link AppModule} instance
      * from given tomcat context.
@@ -2145,7 +2178,7 @@ public class TomcatWebAppBuilder implements WebAppBuilder, ContextListener, Pare
         final TomcatDeploymentLoader tomcatDeploymentLoader = new TomcatDeploymentLoader(standardContext, id);
         final AppModule appModule;
         try {
-            appModule = tomcatDeploymentLoader.load(Contexts.warPath(standardContext));
+            appModule = tomcatDeploymentLoader.load(Contexts.warPath(standardContext), configuredClasspath(standardContext));
         } catch (final OpenEJBException e) {
             throw new TomEERuntimeException(e);
         }