geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r482959 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/UrlSet.java test/java/org/apache/xbean/finder/UrlSetTest.java
Date Wed, 06 Dec 2006 08:12:41 GMT
Author: dblevins
Date: Wed Dec  6 00:12:40 2006
New Revision: 482959

URL: http://svn.apache.org/viewvc?view=rev&rev=482959
Log:
Object that can hold a set of urls and filter them via excludes and includes

Added:
    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/UrlSetTest.java

Added: 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?view=auto&rev=482959
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java Wed
Dec  6 00:12:40 2006
@@ -0,0 +1,176 @@
+/**
+ * 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.net.URL;
+import java.net.JarURLConnection;
+import java.net.MalformedURLException;
+import java.util.Collection;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Arrays;
+import java.io.IOException;
+import java.io.File;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class UrlSet {
+
+    private final Map<String,URL> urls;
+
+    public UrlSet(ClassLoader classLoader) throws IOException {
+        this(getUrls(classLoader));
+    }
+
+    public UrlSet(URL... urls){
+        this(Arrays.asList(urls));
+    }
+    /**
+     * Ignores all URLs that are not "jar" or "file"
+     * @param urls
+     */
+    public UrlSet(Collection<URL> urls){
+        this.urls = new HashMap<String,URL>();
+        for (URL location : urls) {
+            try {
+//                if (location.getProtocol().equals("file")) {
+//                    try {
+//                        // See if it's actually a jar
+//                        URL jarUrl = new URL("jar", "", location.toExternalForm() + "!/");
+//                        JarURLConnection juc = (JarURLConnection) jarUrl.openConnection();
+//                        juc.getJarFile();
+//                        location = jarUrl;
+//                    } catch (IOException e) {
+//                    }
+//                    this.urls.put(location.toExternalForm(), location);
+//                }
+                this.urls.put(location.toExternalForm(), location);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    private UrlSet(Map<String, URL> urls) {
+        this.urls = urls;
+    }
+
+    public UrlSet include(UrlSet urlSet){
+        Map<String, URL> urls = new HashMap<String, URL>(this.urls);
+        urls.putAll(urlSet.urls);
+        return new UrlSet(urls);
+    }
+
+    public UrlSet exclude(UrlSet urlSet) {
+        Map<String, URL> urls = new HashMap<String, URL>(this.urls);
+        Map<String, URL> parentUrls = urlSet.urls;
+        for (String url : parentUrls.keySet()) {
+            urls.remove(url);
+        }
+        return new UrlSet(urls);
+    }
+
+    public UrlSet exclude(ClassLoader parent) throws IOException {
+        return exclude(new UrlSet(parent));
+    }
+
+    public UrlSet exclude(File file) throws MalformedURLException {
+        return exclude(relative(file));
+    }
+
+    public UrlSet exclude(String pattern) throws MalformedURLException {
+        return exclude(matching(pattern));
+    }
+
+    /**
+     * Calls excludePaths(System.getProperty("java.ext.dirs"))
+     * @return
+     * @throws MalformedURLException
+     */
+    public UrlSet excludeJavaExtDirs() throws MalformedURLException {
+        return excludePaths(System.getProperty("java.ext.dirs"));
+    }
+
+    /**
+     * Calls excludePaths(System.getProperty("java.endorsed.dirs"))
+     * @return
+     * @throws MalformedURLException
+     */
+    public UrlSet excludeJavaEndorsedDirs() throws MalformedURLException {
+        return excludePaths(System.getProperty("java.endorsed.dirs"));
+    }
+
+    public UrlSet excludeJavaHome() throws MalformedURLException {
+        String path = System.getProperty("java.home");
+        return exclude(new File(path));
+    }
+
+    public UrlSet excludePaths(String pathString) throws MalformedURLException {
+        String[] paths = pathString.split(File.pathSeparator);
+        UrlSet urlSet = this;
+        for (String path : paths) {
+            File file = new File(path);
+            urlSet = urlSet.exclude(file);
+        }
+        return urlSet;
+    }
+
+    public UrlSet matching(String pattern) {
+        Map<String, URL> urls = new HashMap<String, URL>();
+        for (Map.Entry<String, URL> entry : this.urls.entrySet()) {
+            String url = entry.getKey();
+            if (url.matches(pattern)){
+                urls.put(url, entry.getValue());
+            }
+        }
+        return new UrlSet(urls);
+    }
+
+    public UrlSet relative(File file) throws MalformedURLException {
+        String urlPath = file.toURL().toExternalForm();
+        Map<String, URL> urls = new HashMap<String, URL>();
+        for (Map.Entry<String, URL> entry : this.urls.entrySet()) {
+            String url = entry.getKey();
+            if (url.startsWith(urlPath) || url.startsWith("jar:"+urlPath)){
+                urls.put(url, entry.getValue());
+            }
+        }
+        return new UrlSet(urls);
+    }
+
+    public List<URL> getUrls() {
+        return new ArrayList<URL>(urls.values());
+    }
+
+    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;
+    }
+}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/UrlSetTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/UrlSetTest.java?view=auto&rev=482959
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/UrlSetTest.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/UrlSetTest.java
Wed Dec  6 00:12:40 2006
@@ -0,0 +1,107 @@
+/**
+ * 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 junit.framework.TestCase;
+
+import java.net.URL;
+import java.util.List;
+import java.io.File;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class UrlSetTest extends TestCase {
+    private UrlSet urlSet;
+    private URL[] originalUrls;
+
+    protected void setUp() throws Exception {
+        originalUrls = new URL[]{
+                new URL("file:/Users/dblevins/work/xbean/trunk/xbean-finder/target/classes/"),
+                new URL("file:/Users/dblevins/work/xbean/trunk/xbean-finder/target/test-classes/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/.compatibility/14compatibility.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/charsets.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/dt.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jce.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jconsole.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jsse.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/laf.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/ui.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/deploy.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/apple_provider.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/dnsns.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/localedata.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/sunjce_provider.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/sunpkcs11.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/plugin.jar!/"),
+                new URL("jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/sa-jdi.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/CoreAudio.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/MRJToolkit.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/QTJSupport.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/QTJava.zip!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/dns_sd.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/j3daudio.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/j3dcore.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/j3dutils.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/jai_codec.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/jai_core.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/mlibwrapper_jai.jar!/"),
+                new URL("jar:file:/System/Library/Java/Extensions/vecmath.jar!/"),
+                new URL("jar:file:/Users/dblevins/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar!/"),
+        };
+        urlSet = new UrlSet(originalUrls);
+    }
+
+    public void testAll() throws Exception {
+
+        assertEquals("Urls.size()", 32, urlSet.getUrls().size());
+
+        UrlSet homeSet = urlSet.matching(".*Home.*");
+        assertEquals("HomeSet.getUrls().size()", 8, homeSet.getUrls().size());
+
+        homeSet = urlSet.relative(new File("/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home"));
+        assertEquals("HomeSet.getUrls().size()", 8, homeSet.getUrls().size());
+
+        UrlSet urlSet2 = urlSet.exclude(homeSet);
+        assertEquals("Urls.size()", 24, urlSet2.getUrls().size());
+
+        UrlSet xbeanSet = urlSet.matching(".*xbean.*");
+        assertEquals("XbeanSet.getUrls().size()", 2, xbeanSet.getUrls().size());
+
+        UrlSet junitSet = urlSet.matching(".*junit.*");
+        assertEquals("JunitSet.getUrls().size()", 1, junitSet.getUrls().size());
+
+        UrlSet mergedSet = homeSet.include(xbeanSet);
+        assertEquals("MergedSet.getUrls().size()", 10, mergedSet.getUrls().size());
+
+        mergedSet.include(junitSet);
+        assertEquals("MergedSet.getUrls().size()", 10, mergedSet.getUrls().size());
+
+        UrlSet mergedSet2 = mergedSet.include(junitSet);
+        assertEquals("MergedSet2.getUrls().size()", 11, mergedSet2.getUrls().size());
+
+        UrlSet filteredSet = urlSet.exclude(".*System/Library.*");
+        assertEquals("FilteredSet.getUrls().size()", 3, filteredSet.getUrls().size());
+
+        filteredSet.exclude(junitSet);
+        assertEquals("FilteredSet.getUrls().size()", 3, filteredSet.getUrls().size());
+
+        UrlSet filteredSet2 = filteredSet.exclude(junitSet);
+        assertEquals("FilteredSet2.getUrls().size()", 2, filteredSet2.getUrls().size());
+    }
+}



Mime
View raw message