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 2012/09/30 12:14:16 UTC

svn commit: r1391977 - in /openejb/trunk/openejb: container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ container/openejb-core/src/main/java/org/apache/openejb/config/ tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/

Author: rmannibucau
Date: Sun Sep 30 10:14:14 2012
New Revision: 1391977

URL: http://svn.apache.org/viewvc?rev=1391977&view=rev
Log:
storing @WebXXX jar/file to get a simpler logic in OpenEJBContextConfig (tomcat integration)

Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ClassListInfo.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/WebAppInfo.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/WebModule.java
    openejb/trunk/openejb/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContextConfig.java

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ClassListInfo.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ClassListInfo.java?rev=1391977&r1=1391976&r2=1391977&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ClassListInfo.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ClassListInfo.java Sun Sep 30 10:14:14 2012
@@ -19,6 +19,8 @@ package org.apache.openejb.assembler.cla
 import java.util.HashSet;
 import java.util.Set;
 
+// just to store a kind of Map<String, Set<String>>
+// TODO: find a better name, key can be a classname, a jar/file path...
 public class ClassListInfo extends InfoObject {
     public String name;
     public final Set<String> list = new HashSet<String>();

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/WebAppInfo.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/WebAppInfo.java?rev=1391977&r1=1391976&r2=1391977&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/WebAppInfo.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/WebAppInfo.java Sun Sep 30 10:14:14 2012
@@ -35,7 +35,7 @@ public class WebAppInfo extends CommonIn
     public final Set<String> restApplications = new TreeSet<String>();
     public final Set<String> ejbWebServices = new TreeSet<String>();
     public final Set<String> ejbRestServices = new TreeSet<String>();
-    public final Set<String> webAnnotatedClasses = new TreeSet<String>();
+    public final Set<ClassListInfo> webAnnotatedClasses = new TreeSet<ClassListInfo>();
     public final List<PortInfo> portInfos = new ArrayList<PortInfo>();
     public final JndiEncInfo jndiEnc = new JndiEncInfo();
     public final List<ServletInfo> servlets = new ArrayList<ServletInfo>();

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java?rev=1391977&r1=1391976&r2=1391977&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java Sun Sep 30 10:14:14 2012
@@ -111,6 +111,7 @@ import org.apache.openejb.jee.Transactio
 import org.apache.openejb.jee.WebApp;
 import org.apache.openejb.jee.WebserviceDescription;
 import org.apache.openejb.jee.oejb3.OpenejbJar;
+import org.apache.openejb.loader.JarLocation;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.util.Classes;
 import org.apache.openejb.util.Join;
@@ -1126,7 +1127,7 @@ public class AnnotationDeployer implemen
                 }
 
                 final List<Annotated<Class<?>>> found = finder.findMetaAnnotatedClasses(clazz);
-                webModule.getWebAnnotatedClasses().addAll(metaToStr(found));
+                webModule.getWebAnnotatedClasses().putAll(metaToStr(found));
             }
 
             return webModule;
@@ -5173,14 +5174,45 @@ public class AnnotationDeployer implemen
         return classes;
     }
 
-    private static Collection<String> metaToStr(List<Annotated<Class<?>>> found) {
-        final Collection<String> classes = new ArrayList<String>(found.size());
+    private static Map<String, Set<String>> metaToStr(final List<Annotated<Class<?>>> found) {
+        final Map<String, Set<String>> classes = new HashMap<String, Set<String>>(found.size());
         for (Annotated<Class<?>> clazz : found) {
-            classes.add(clazz.get().getName());
+            final Class<?> loadedClass = clazz.get();
+            final URL url = classLocation(loadedClass);
+            Set<String> list = classes.get(url);
+            if (list == null) {
+                list = new HashSet<String>();
+                classes.put(url.toExternalForm(), list);
+            }
+            list.add(loadedClass.getName());
         }
         return classes;
     }
 
+    public static URL classLocation(Class clazz) {
+        try {
+            String classFileName = clazz.getName().replace(".", "/") + ".class";
+
+            ClassLoader loader = clazz.getClassLoader();
+            URL url;
+            if (loader != null) {
+                url = loader.getResource(classFileName);
+            } else {
+                url = clazz.getResource(classFileName);
+            }
+
+            if (url == null) {
+                throw new IllegalStateException("classloader.getResource(classFileName) returned a null URL");
+            }
+
+            return url;
+        } catch (RuntimeException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
     public EnvEntriesPropertiesDeployer getEnvEntriesPropertiesDeployer() {
         return envEntriesPropertiesDeployer;
     }

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java?rev=1391977&r1=1391976&r2=1391977&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java Sun Sep 30 10:14:14 2012
@@ -360,9 +360,15 @@ class AppInfoBuilder {
             webAppInfo.restClass.addAll(webModule.getRestClasses());
             webAppInfo.ejbWebServices.addAll(webModule.getEjbWebServices());
             webAppInfo.ejbRestServices.addAll(webModule.getEjbRestServices());
-            webAppInfo.webAnnotatedClasses.addAll(webModule.getWebAnnotatedClasses());
             webAppInfo.jaxRsProviders.addAll(webModule.getJaxrsProviders());
 
+            for (Map.Entry<String, Set<String>> entry : webModule.getWebAnnotatedClasses().entrySet()) {
+                final ClassListInfo info = new ClassListInfo();
+                info.name = entry.getKey();
+                info.list.addAll(entry.getValue());
+                webAppInfo.webAnnotatedClasses.add(info);
+            }
+
             for (Map.Entry<String, Set<String>> entry : webModule.getJsfAnnotatedClasses().entrySet()) {
                 final ClassListInfo info = new ClassListInfo();
                 info.name = entry.getKey();

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/WebModule.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/WebModule.java?rev=1391977&r1=1391976&r2=1391977&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/WebModule.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/WebModule.java Sun Sep 30 10:14:14 2012
@@ -53,7 +53,7 @@ public class WebModule extends Module im
     private final Set<String> jaxrsProviders = new TreeSet<String>();
     private final Set<String> restApplications = new TreeSet<String>();
     private final Map<String, Set<String>> jsfAnnotatedClasses = new HashMap<String, Set<String>>();
-    private final Set<String> webAnnotatedClasses = new TreeSet<String>();
+    private final Map<String, Set<String>> webAnnotatedClasses = new HashMap<String, Set<String>>();
 
     private ID id;
     
@@ -205,7 +205,7 @@ public class WebModule extends Module im
         return jsfAnnotatedClasses;
     }
 
-    public Set<String> getWebAnnotatedClasses() {
+    public Map<String, Set<String>> getWebAnnotatedClasses() {
         return webAnnotatedClasses;
     }
 

Modified: openejb/trunk/openejb/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContextConfig.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContextConfig.java?rev=1391977&r1=1391976&r2=1391977&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContextConfig.java (original)
+++ openejb/trunk/openejb/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContextConfig.java Sun Sep 30 10:14:14 2012
@@ -20,19 +20,22 @@ import org.apache.catalina.Context;
 import org.apache.catalina.core.StandardContext;
 import org.apache.catalina.deploy.WebXml;
 import org.apache.catalina.startup.ContextConfig;
+import org.apache.openejb.assembler.classic.ClassListInfo;
 import org.apache.openejb.assembler.classic.WebAppBuilder;
 import org.apache.openejb.assembler.classic.WebAppInfo;
+import org.apache.openejb.loader.IO;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
+import org.apache.openejb.util.URLs;
 import org.xml.sax.InputSource;
 
 import javax.servlet.ServletContainerInitializer;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.URLClassLoader;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -44,9 +47,6 @@ public class OpenEJBContextConfig extend
     private static final String MYFACES_TOMEEM_CONTAINER_INITIALIZER = "org.apache.tomee.myfaces.TomEEMyFacesContainerInitializer";
     private static final String TOMEE_MYFACES_CONTEXT_LISTENER = "org.apache.tomee.myfaces.TomEEMyFacesContextListener";
 
-    private static final String CLASSES = "classes";
-    private static final String WEB_INF = "WEB-INF";
-
     private TomcatWebAppBuilder.StandardContextInfo info;
 
     public OpenEJBContextConfig(TomcatWebAppBuilder.StandardContextInfo standardContextInfo) {
@@ -128,137 +128,78 @@ public class OpenEJBContextConfig extend
     }
 
     @Override
-    protected void processAnnotationsFile(File file, WebXml fragment,
-            boolean handlesTypesOnly) {
-        logger.debug("processAnnotationsFile {0}", file.getAbsolutePath() );
-        try {
-            final WebAppInfo webAppInfo = info.get();
-
-            if (webAppInfo == null) {
-                logger.warning("WebAppInfo not found. " + info);
-                super.processAnnotationsFile(file, fragment, handlesTypesOnly);
-                return;
-            }
-
-            logger.debug("Optimized Scan of File {0}", file.getAbsolutePath());
-
-            // TODO We should just remember which jars each class came from
-            // then we wouldn't need to lookup the class from the URL in this
-            // way to guarantee we only add classes from this URL.
-            final URLClassLoader loader = new URLClassLoader(new URL[]{file.toURI().toURL()});
-            for (String webAnnotatedClassName : webAppInfo.webAnnotatedClasses) {
-
-                final String includedPackage = getSubPackage(file);
-                if (includedPackage == null || !webAnnotatedClassName.startsWith(includedPackage)) {
-                    continue;
-                }
-
-                final String classFile = webAnnotatedClassName.substring(includedPackage.length()).replace('.', '/') + ".class";
-                final URL classUrl = loader.getResource(classFile);
+    protected void processAnnotationsFile(File file, WebXml fragment, boolean handlesTypesOnly) {
+        final WebAppInfo webAppInfo = info.get();
+        if (webAppInfo == null) {
+            super.processAnnotationsFile(file, fragment, handlesTypesOnly);
+            return;
+        }
 
-                if (classUrl == null) {
-                    logger.debug("Not present " + webAnnotatedClassName);
+        for (ClassListInfo webAnnotated : webAppInfo.webAnnotatedClasses) {
+            try {
+                final URL url = new URL(webAnnotated.name);
+                final File classAsFile = URLs.toFile(new URL(webAnnotated.name));
+                if (!isIncludedIn(file, classAsFile)) {
                     continue;
                 }
 
-                logger.debug("Found {0}", webAnnotatedClassName);
-
-                final InputStream inputStream = classUrl.openStream();
-                try {
-                    processAnnotationsStream(inputStream, fragment, handlesTypesOnly);
-                    logger.debug("Succeeded {0}", webAnnotatedClassName);
-                } catch (IOException e) {
-                    e.printStackTrace();
-                } finally {
-                    inputStream.close();
-                }
-
+                internalProcessAnnotationsStream(url, fragment, handlesTypesOnly);
+            } catch (MalformedURLException e) {
+                throw new IllegalArgumentException(e);
+            } catch (IOException e) {
+                throw new IllegalArgumentException(e);
             }
-        } catch (Exception e) {
-            logger.error("OpenEJBContextConfig.processAnnotationsFile: failed.", e);
         }
     }
 
-    // because we don't always get WEB-INF/classes folder, simply get the already appended subpackage
-    private static String getSubPackage(final File file) {
-        File current = file.getParentFile();
-        if (current == null) {
-            return "";
+    @Override
+    protected void processAnnotationsUrl(URL currentUrl, WebXml fragment, boolean handlesTypeOnly) {
+        final WebAppInfo webAppInfo = info.get();
+        if (webAppInfo == null) {
+            super.processAnnotationsUrl(currentUrl, fragment, handlesTypeOnly);
+            return;
         }
 
-        File previous = file;
-        while (current.getParentFile() != null) {
-            if (CLASSES.equals(previous.getName()) && WEB_INF.equals(current.getName())) {
-                String path = file.getAbsolutePath().substring(previous.getAbsolutePath().length());
-                if (path.startsWith(File.separator)) {
-                    path = path.substring(File.separator.length());
-                }
-                if (path.endsWith(File.separator)) {
-                    path = path.substring(0, path.length() - 1);
-                }
-
-                if (path.isEmpty()) {
-                    return path;
+        for (ClassListInfo webAnnotated : webAppInfo.webAnnotatedClasses) {
+            try {
+                final URL url = new URL(webAnnotated.name);
+                final File classAsFile = URLs.toFile(new URL(webAppInfo.webAnnotatedClasses.iterator().next().name));
+                final File currentUrlAsFile = URLs.toFile(currentUrl);
+                if (!currentUrlAsFile.equals(classAsFile)) {
+                    continue;
                 }
 
-                return path + ".";
+                internalProcessAnnotationsStream(url, fragment, handlesTypeOnly);
+            } catch (MalformedURLException e) {
+                throw new IllegalArgumentException(e);
+            } catch (IOException e) {
+                throw new IllegalArgumentException(e);
             }
-
-            previous = current;
-            current = current.getParentFile();
         }
-
-        return ""; // no subpackage found
     }
 
-    @Override
-    protected void processAnnotationsUrl(URL url, WebXml fragment, boolean handlesTypeOnly) {
-        logger.debug("processAnnotationsUrl " + url);
-        if (SystemInstance.get().getOptions().get("tomee.tomcat.scan", false)) {
-            super.processAnnotationsUrl(url, fragment, handlesTypeOnly);
-            return;
-        }
-
+    private void internalProcessAnnotationsStream(final URL url, final WebXml fragment, final boolean handlesTypeOnly) {
+        InputStream is = null;
         try {
-            final WebAppInfo webAppInfo = info.get();
-
-            if (webAppInfo == null) {
-                logger.warning("WebAppInfo not found. " + info);
-                super.processAnnotationsUrl(url, fragment, handlesTypeOnly);
-                return;
-            }
-
-            logger.debug("Optimized Scan of URL " + url);
-
-            // TODO We should just remember which jars each class came from
-            // then we wouldn't need to lookup the class from the URL in this
-            // way to guarantee we only add classes from this URL.
-            final URLClassLoader loader = new URLClassLoader(new URL[]{url});
-            for (String webAnnotatedClassName : webAppInfo.webAnnotatedClasses) {
-
-                final String classFile = webAnnotatedClassName.replace('.', '/') + ".class";
-                final URL classUrl = loader.getResource(classFile);
-
-                if (classUrl == null) {
-                    logger.debug("Not present " + webAnnotatedClassName);
-                    continue;
-                }
-
-                logger.debug("Found " + webAnnotatedClassName);
-
-                final InputStream inputStream = classUrl.openStream();
-                try {
-                    processAnnotationsStream(inputStream, fragment, handlesTypeOnly);
-                    logger.debug("Succeeded " + webAnnotatedClassName);
-                } catch (IOException e) {
-                    e.printStackTrace();
-                } finally {
-                    inputStream.close();
-                }
+            is = url.openStream();
+            processAnnotationsStream(is, fragment, handlesTypeOnly);
+        } catch (MalformedURLException e) {
+            throw new IllegalArgumentException(e);
+        } catch (IOException e) {
+            throw new IllegalArgumentException(e);
+        } finally {
+            IO.close(is);
+        }
+    }
 
+    private static boolean isIncludedIn(final File file, final File classAsFile) {
+        File current = classAsFile;
+        while (current != null && current.exists()) {
+            if (current.equals(file)) {
+                return true;
             }
-        } catch (Exception e) {
-            logger.error("OpenEJBContextConfig.processAnnotationsUrl: failed.", e);
+            current = current.getParentFile();
         }
+        return false;
     }
 }