geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r996697 - in /geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util: BundleClassLoader.java BundleResourceClassLoader.java
Date Mon, 13 Sep 2010 21:40:40 GMT
Author: gawor
Date: Mon Sep 13 21:40:40 2010
New Revision: 996697

URL: http://svn.apache.org/viewvc?rev=996697&view=rev
Log:
A BundleClassLoader implementation that returns resources embedded in jar files in a bundle
as 'jar' urls

Added:
    geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleResourceClassLoader.java
  (with props)
Modified:
    geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleClassLoader.java

Modified: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleClassLoader.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleClassLoader.java?rev=996697&r1=996696&r2=996697&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleClassLoader.java
(original)
+++ geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleClassLoader.java
Mon Sep 13 21:40:40 2010
@@ -47,7 +47,7 @@ public class BundleClassLoader extends C
     private final static String META_INF_1 = "META-INF/";
     private final static String META_INF_2 = "/META-INF/";
     
-    private final Bundle bundle;
+    protected final Bundle bundle;
     private boolean searchWiredBundles;
 
     public BundleClassLoader(Bundle bundle) {
@@ -77,12 +77,12 @@ public class BundleClassLoader extends C
     public String toString() {
         return "[BundleClassLoader] " + bundle;
     }
-
+    
     @Override
     public URL getResource(String name) {
         URL resource = bundle.getResource(name);
         if (resource == null && isMetaInfResource(name)) {
-            LinkedHashSet<Bundle> wiredBundles = BundleUtils.getWiredBundles(bundle);
+            LinkedHashSet<Bundle> wiredBundles = getWiredBundles();
             Iterator<Bundle> iterator = wiredBundles.iterator();
             while (iterator.hasNext() && resource == null) {                
                 resource = iterator.next().getResource(name);
@@ -98,7 +98,7 @@ public class BundleClassLoader extends C
         if (isMetaInfResource(name)) {
             ArrayList<URL> allResources = new ArrayList<URL>();
             addToList(allResources, e);
-            LinkedHashSet<Bundle> wiredBundles = BundleUtils.getWiredBundles(bundle);
+            LinkedHashSet<Bundle> wiredBundles = getWiredBundles();
             for (Bundle wiredBundle : wiredBundles) {
                 Enumeration<URL> resources = wiredBundle.getResources(name);
                 addToList(allResources, resources);
@@ -121,7 +121,11 @@ public class BundleClassLoader extends C
         return searchWiredBundles;
     }
     
-    private boolean isMetaInfResource(String name) {
+    protected LinkedHashSet<Bundle> getWiredBundles() {
+        return BundleUtils.getWiredBundles(bundle);
+    }
+    
+    protected boolean isMetaInfResource(String name) {
         return searchWiredBundles && name != null && (name.startsWith(META_INF_1)
|| name.startsWith(META_INF_2));
     }
       

Added: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleResourceClassLoader.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleResourceClassLoader.java?rev=996697&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleResourceClassLoader.java
(added)
+++ geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleResourceClassLoader.java
Mon Sep 13 21:40:40 2010
@@ -0,0 +1,121 @@
+/*
+ * 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.osgi.bundle.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.zip.ZipEntry;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+/**
+ * ClassLoader for a {@link Bundle}. 
+ * <br/>
+ * This ClassLoader implementation extends the {@link BundleClassLoader} and returns resources
embedded
+ * in jar files in a bundle with <tt>jar</tt> URLs. 
+ * 
+ * @version $Rev$ $Date$
+ */
+public class BundleResourceClassLoader extends BundleClassLoader {
+
+    public BundleResourceClassLoader(Bundle bundle) {
+        super(bundle);
+    }
+
+    @Override
+    public String toString() {
+        return "[BundleResourceClassLoader] " + bundle;
+    }
+  
+    @Override
+    public URL getResource(String name) {
+        ServiceReference reference = bundle.getBundleContext().getServiceReference(PackageAdmin.class.getName());
+        PackageAdmin packageAdmin = (PackageAdmin) bundle.getBundleContext().getService(reference);
+        try {
+            List<URL> resources = findResources(packageAdmin, bundle, name, false);
+            if (resources.isEmpty() && isMetaInfResource(name)) {
+                LinkedHashSet<Bundle> wiredBundles = getWiredBundles();
+                Iterator<Bundle> iterator = wiredBundles.iterator();
+                while (iterator.hasNext() && resources.isEmpty()) {    
+                    Bundle wiredBundle = iterator.next();
+                    resources = findResources(packageAdmin, wiredBundle, name, false);
+                }
+            }
+            return (resources.isEmpty()) ? null : resources.get(0);
+        } catch (Exception e) {
+            return null;
+        } finally {
+            bundle.getBundleContext().ungetService(reference);
+        }
+    }
+
+    @Override
+    public Enumeration<URL> getResources(String name) throws IOException {
+        ServiceReference reference = bundle.getBundleContext().getServiceReference(PackageAdmin.class.getName());
+        PackageAdmin packageAdmin = (PackageAdmin) bundle.getBundleContext().getService(reference);
+        try {
+            List<URL> resources = findResources(packageAdmin, bundle, name, true);
+            if (isMetaInfResource(name)) {
+                LinkedHashSet<Bundle> wiredBundles = getWiredBundles();
+                for (Bundle wiredBundle : wiredBundles) {
+                    resources.addAll(findResources(packageAdmin, wiredBundle, name, true));
+                }
+            }
+            return Collections.enumeration(resources);
+        } catch (Exception e) {
+            throw new IOException("Error discovering resources", e);
+        } finally {
+            bundle.getBundleContext().ungetService(reference);
+        }
+    }
+    
+    private static List<URL> findResources(PackageAdmin packageAdmin, 
+                                           Bundle bundle, 
+                                           String name, 
+                                           final boolean continueScanning) throws Exception
{
+        BundleResourceFinder finder = new BundleResourceFinder(packageAdmin, bundle, "",
name);
+        final List<URL> resources = new ArrayList<URL>();
+        finder.find(new BundleResourceFinder.ResourceFinderCallback() {
+
+            public boolean foundInDirectory(Bundle bundle, String baseDir, URL url) throws
Exception {
+                resources.add(url);
+                return continueScanning;
+            }
+
+            public boolean foundInJar(Bundle bundle, String jarName, ZipEntry entry, InputStream
inputStream) throws Exception {
+                URL jarURL = bundle.getEntry(jarName);
+                URL url = new URL("jar:" + jarURL.toString() + "!/" + entry.getName());
+                resources.add(url);
+                return continueScanning;
+            }
+        });                   
+        return resources;           
+    }
+   
+}

Propchange: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleResourceClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleResourceClassLoader.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/BundleResourceClassLoader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message