geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1475801 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/ test/java/org/apache/xbean/finder/
Date Thu, 25 Apr 2013 14:42:20 GMT
Author: rmannibucau
Date: Thu Apr 25 14:42:20 2013
New Revision: 1475801

URL: http://svn.apache.org/r1475801
Log:
XBEAN-243 reworking getURLs to find really all jars with a workarounf for surefire (sadly)
and a system property to go back to old behavior (if needed)

Added:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersPerformancesTest.java
Modified:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java?rev=1475801&r1=1475800&r2=1475801&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java
Thu Apr 25 14:42:20 2013
@@ -20,6 +20,18 @@
 
 package org.apache.xbean.finder;
 
+import org.apache.xbean.finder.archive.Archive;
+import org.apache.xbean.finder.util.Classes;
+import org.apache.xbean.finder.util.SingleLinkedList;
+import org.objectweb.asm.AnnotationVisitor;
+import org.objectweb.asm.Attribute;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Type;
+import org.objectweb.asm.commons.EmptyVisitor;
+import org.objectweb.asm.signature.SignatureVisitor;
+
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -41,18 +53,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.xbean.finder.archive.Archive;
-import org.apache.xbean.finder.util.Classes;
-import org.apache.xbean.finder.util.SingleLinkedList;
-import org.objectweb.asm.AnnotationVisitor;
-import org.objectweb.asm.Attribute;
-import org.objectweb.asm.ClassReader;
-import org.objectweb.asm.FieldVisitor;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Type;
-import org.objectweb.asm.commons.EmptyVisitor;
-import org.objectweb.asm.signature.SignatureVisitor;
-
 /**
  * ClassFinder searches the classpath of the specified classloader for
  * packages, classes, constructors, methods, or fields with specific annotations.
@@ -628,6 +628,8 @@ public class AnnotationFinder implements
                         }
                     } catch (ClassNotFoundException e) {
                         classesNotLoaded.add(classInfo.getName());
+                    } catch (ClassCircularityError cce) {
+                        classesNotLoaded.add(classInfo.getName());
                     }
                 } else {
                     try {
@@ -923,6 +925,8 @@ public class AnnotationFinder implements
                         }
                     } catch (ClassNotFoundException e) {
                         classesNotLoaded.add(classInfo.getName());
+                    } catch (NoClassDefFoundError ncdfe) {
+                        classesNotLoaded.add(classInfo.getName());
                     }
                 } else {
                     try {

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java?rev=1475801&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
Thu Apr 25 14:42:20 2013
@@ -0,0 +1,90 @@
+/**
+ * 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.xbean.finder;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+public final class ClassLoaders {
+    private static final boolean DONT_USE_GET_URLS = Boolean.getBoolean("xbean.finder.use.get-resources");
+    private static final ClassLoader SYSTEM = ClassLoader.getSystemClassLoader();
+
+    public static Set<URL> findUrls(final ClassLoader classLoader) throws IOException
{
+        if (classLoader == null) {
+            return Collections.emptySet();
+        }
+
+        final Set<URL> urls;
+
+        if (URLClassLoader.class.isInstance(classLoader) && !DONT_USE_GET_URLS) {
+            urls =  new HashSet<URL>();
+
+            if (!isSurefire(classLoader)) {
+                urls.addAll(Arrays.asList(URLClassLoader.class.cast(classLoader).getURLs()));
+                urls.addAll(findUrls(classLoader.getParent()));
+            } else { // http://jira.codehaus.org/browse/SUREFIRE-928 - we could reuse findUrlFromResources
but this seems faster
+                urls.addAll(fromClassPath());
+            }
+        } else {
+            urls = findUrlFromResources(classLoader);
+        }
+
+        return urls;
+    }
+
+    private static boolean isSurefire(ClassLoader classLoader) {
+        return System.getProperty("surefire.real.class.path") != null && classLoader
== SYSTEM;
+    }
+
+    private static Collection<URL> fromClassPath() {
+        final String[] cp = System.getProperty("java.class.path").split(System.getProperty("path.separator",
":"));
+        final Set<URL> urls = new HashSet<URL>();
+        for (final String path : cp) {
+            try {
+                if (path.endsWith(".jar")) {
+                    urls.add(new URL("jar:file://" + path + "!/"));
+                } else {
+                    urls.add(new URL("file://" + path));
+                }
+            } catch (final MalformedURLException e) {
+                // ignore
+            }
+        }
+        return urls;
+    }
+
+    public static Set<URL> findUrlFromResources(final ClassLoader classLoader) throws
IOException {
+        final Set<URL> set = new HashSet<URL>();
+        for (final URL url : Collections.list(classLoader.getResources("META-INF"))) {
+            final String externalForm = url.toExternalForm();
+            set.add(new URL(externalForm.substring(0, externalForm.lastIndexOf("META-INF"))));
+        }
+        set.addAll(Collections.list(classLoader.getResources("")));
+        return set;
+    }
+
+    private ClassLoaders() {
+        // no-op
+    }
+}

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java?rev=1475801&r1=1475800&r2=1475801&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java Thu
Apr 25 14:42:20 2013
@@ -20,6 +20,7 @@ import org.apache.xbean.finder.filter.Fi
 
 import java.net.URL;
 import java.net.MalformedURLException;
+import java.net.URLClassLoader;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
@@ -42,7 +43,7 @@ public class UrlSet implements Iterable<
     private final Map<String,URL> urls;
 
     public UrlSet(ClassLoader classLoader) throws IOException {
-        this(getUrls(classLoader));
+        this(ClassLoaders.findUrls(classLoader));
     }
 
     public UrlSet(URL... urls){
@@ -200,20 +201,6 @@ public class UrlSet implements Iterable<
         return getUrls().iterator();
     }
 
-    private static List<URL> getUrls(ClassLoader classLoader) throws IOException {
-        List<URL> list = new ArrayList<URL>();
-        ArrayList<URL> urls = Collections.list(classLoader.getResources("META-INF"));
-        for (URL url : urls) {
-            String externalForm = url.toExternalForm();
-            int i = externalForm.lastIndexOf("META-INF");
-            externalForm = externalForm.substring(0, i);
-            url = new URL(externalForm);
-            list.add(url);
-        }
-        list.addAll(Collections.list(classLoader.getResources("")));
-        return list;
-    }
-
     @Override
     public String toString() {
         return super.toString() + "[" + urls.size() + "]";

Modified: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java?rev=1475801&r1=1475800&r2=1475801&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java
Thu Apr 25 14:42:20 2013
@@ -44,6 +44,7 @@ import org.acme.foo.Red;
 import org.acme.foo.StringGenericHoliday;
 import org.acme.foo.Thanksgiving;
 import org.acme.foo.ValentinesDay;
+import org.apache.xbean.finder.filter.Filter;
 
 /**
  * @author David Blevins
@@ -63,6 +64,11 @@ public class ClassFinderTest extends Tes
         }
 
         urlSet = urlSet.excludeJavaHome();
+        urlSet = urlSet.filter(new Filter() { // keep only test classes to avoid to scan
asm etc
+            public boolean accept(final String name) {
+                return name.contains("test-classes");
+            }
+        });
 
         classFinder = new ClassFinder(classLoader, urlSet.getUrls());
     }

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersPerformancesTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersPerformancesTest.java?rev=1475801&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersPerformancesTest.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersPerformancesTest.java
Thu Apr 25 14:42:20 2013
@@ -0,0 +1,54 @@
+/**
+ * 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.xbean.finder;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.URLClassLoader;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertTrue;
+
+public class ClassLoadersPerformancesTest {
+    private static final int MAX = 1000;
+
+    @Test
+    public void perfs() throws IOException {
+        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        if (!URLClassLoader.class.isInstance(loader)) {
+            return; // skip test
+        }
+
+        final long startURL = System.nanoTime();
+        for (int i = 0; i < MAX; i++) {
+            ClassLoaders.findUrls(loader).size();
+        }
+        final long endURL = System.nanoTime();
+
+        final long startFind = System.nanoTime();
+        for (int i = 0; i < MAX; i++) {
+            ClassLoaders.findUrlFromResources(loader).size();
+        }
+        final long endFind = System.nanoTime();
+
+        final long urlTime = endURL - startURL;
+        final long findTime = endFind - startFind;
+        assertTrue(TimeUnit.NANOSECONDS.toMillis(urlTime) + " < " + TimeUnit.NANOSECONDS.toMillis(findTime),
urlTime < findTime);
+        System.out.println("getURLs => " + TimeUnit.NANOSECONDS.toMillis(urlTime) + "ms
- getResources => " + TimeUnit.NANOSECONDS.toMillis(findTime) + "ms");
+    }
+}



Mime
View raw message