geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1483753 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/ClassLoaders.java main/java/org/apache/xbean/finder/util/Files.java test/java/org/apache/xbean/finder/ClassLoadersTest.java
Date Fri, 17 May 2013 11:56:04 GMT
Author: rmannibucau
Date: Fri May 17 11:56:03 2013
New Revision: 1483753

URL: http://svn.apache.org/r1483753
Log:
XBEAN-248 skipping so

Added:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/util/Files.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java
      - copied, changed from r1483751, 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/ClassLoaders.java

Modified: 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=1483753&r1=1483752&r2=1483753&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
Fri May 17 11:56:03 2013
@@ -16,6 +16,9 @@
  */
 package org.apache.xbean.finder;
 
+import org.apache.xbean.finder.util.Files;
+
+import java.io.File;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -30,29 +33,58 @@ 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();
 
+    private static final boolean UNIX = !System.getProperty("os.name").toLowerCase().contains("win");
+
     public static Set<URL> findUrls(final ClassLoader classLoader) throws IOException
{
         if (classLoader == null || (SYSTEM.getParent() != null && classLoader ==
SYSTEM.getParent())) {
             return Collections.emptySet();
         }
 
-        final Set<URL> urls;
+        final Set<URL> urls =  new HashSet<URL>();
 
         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()));
+                for (final Collection<URL> item : Arrays.asList(
+                        Arrays.asList(URLClassLoader.class.cast(classLoader).getURLs()),
findUrls(classLoader.getParent()))) {
+                    for (final URL url : item) {
+                        addIfNotSo(urls, url);
+                    }
+                }
             } else { // http://jira.codehaus.org/browse/SUREFIRE-928 - we could reuse findUrlFromResources
but this seems faster
-                urls.addAll(fromClassPath());
+                for (final URL url : fromClassPath()) {
+                    urls.add(url);
+                }
             }
         } else {
-            urls = findUrlFromResources(classLoader);
+            for (final URL url : findUrlFromResources(classLoader)) {
+                urls.add(url);
+            }
         }
 
         return urls;
     }
 
+    private static void addIfNotSo(final Set<URL> urls, final URL url) {
+        if (UNIX && isNative(url)) {
+            return;
+        }
+
+        urls.add(url);
+    }
+
+    public static boolean isNative(final URL url) {
+        final File file = Files.toFile(url);
+        if (file != null) {
+            final String name = file.getName();
+            if (!name.endsWith(".jar") && !file.isDirectory()
+                    && name.contains(".so") && file.getAbsolutePath().startsWith("/usr/lib"))
{
+                return true;
+            }
+        }
+        return false;
+    }
+
+
     private static boolean isSurefire(ClassLoader classLoader) {
         return System.getProperty("surefire.real.class.path") != null && classLoader
== SYSTEM;
     }

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/util/Files.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/util/Files.java?rev=1483753&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/util/Files.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/util/Files.java
Fri May 17 11:56:03 2013
@@ -0,0 +1,51 @@
+/**
+ * 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.util;
+
+import org.apache.xbean.finder.archive.FileArchive;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public final class Files {
+    public static File toFile(final URL url) {
+        if ("jar".equals(url.getProtocol())) {
+            try {
+                final String spec = url.getFile();
+                final int separator = spec.indexOf('!');
+                if (separator == -1) {
+                    return null;
+                }
+                return toFile(new URL(spec.substring(0, separator + 1)));
+            } catch (final MalformedURLException e) {
+                return null;
+            }
+        } else if ("file".equals(url.getProtocol())) {
+            String path = FileArchive.decode(url.getFile());
+            if (path.endsWith("!")) {
+                path = path.substring(0, path.length() - 1);
+            }
+            return new File(path);
+        }
+        return null;
+    }
+
+    private Files() {
+        // no-op
+    }
+}

Copied: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java
(from r1483751, 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/ClassLoadersTest.java?p2=geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java&p1=geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersPerformancesTest.java&r1=1483751&r2=1483753&rev=1483753&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersPerformancesTest.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java
Fri May 17 11:56:03 2013
@@ -18,37 +18,20 @@ package org.apache.xbean.finder;
 
 import org.junit.Test;
 
-import java.io.IOException;
-import java.net.URLClassLoader;
-import java.util.concurrent.TimeUnit;
+import java.net.MalformedURLException;
+import java.net.URL;
 
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-public class ClassLoadersPerformancesTest {
-    private static final int MAX = 1000;
-
+public class ClassLoadersTest {
     @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();
+    public void testNative() throws MalformedURLException {
+        final String base = "file:/usr/lib/x86_64-linux-gnu/jni/libatk-wrapper.so.0.0.18";
 
-        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");
+        assertTrue(ClassLoaders.isNative(new URL(base)));
+        assertFalse(ClassLoaders.isNative(new URL(base + ".jar")));
+        assertTrue(ClassLoaders.isNative(new URL("jar:" + base + "!/")));
+        assertFalse(ClassLoaders.isNative(new URL("jar:" + base + ".jar!/")));
     }
 }



Mime
View raw message