geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r906227 - in /geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo: gbean/runtime/GBeanInstance.java kernel/osgi/BundleClassLoader.java kernel/osgi/BundleUtils.java
Date Wed, 03 Feb 2010 20:30:04 GMT
Author: gawor
Date: Wed Feb  3 20:30:04 2010
New Revision: 906227

URL: http://svn.apache.org/viewvc?rev=906227&view=rev
Log:
extact some code into a separate utility function

Modified:
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/runtime/GBeanInstance.java
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleUtils.java

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/runtime/GBeanInstance.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/runtime/GBeanInstance.java?rev=906227&r1=906226&r2=906227&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/runtime/GBeanInstance.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/gbean/runtime/GBeanInstance.java
Wed Feb  3 20:30:04 2010
@@ -914,7 +914,7 @@
         try {
             try {
                 //TODO OSGI NO!
-                instance = objectRecipe.create(new BundleClassLoader(bundleContext.getBundle()));
+                instance = objectRecipe.create(classLoader);
             } catch (ConstructionException e) {
                 Throwable targetException = e.getCause();
                 if (targetException instanceof Exception) {

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java?rev=906227&r1=906226&r2=906227&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java
Wed Feb  3 20:30:04 2010
@@ -30,9 +30,6 @@
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleReference;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.packageadmin.ExportedPackage;
-import org.osgi.service.packageadmin.PackageAdmin;
 
 /**
  * ClassLoader for a {@link Bundle}. 
@@ -85,7 +82,7 @@
     public URL getResource(String name) {
         URL resource = bundle.getResource(name);
         if (resource == null && isMetaInfResource(name)) {
-            LinkedHashSet<Bundle> wiredBundles = getWiredBundles();
+            LinkedHashSet<Bundle> wiredBundles = BundleUtils.getWiredBundles(bundle);
             Iterator<Bundle> iterator = wiredBundles.iterator();
             while (iterator.hasNext() && resource == null) {                
                 resource = iterator.next().getResource(name);
@@ -101,7 +98,7 @@
         if (isMetaInfResource(name)) {
             ArrayList<URL> allResources = new ArrayList<URL>();
             addToList(allResources, e);
-            LinkedHashSet<Bundle> wiredBundles = getWiredBundles();
+            LinkedHashSet<Bundle> wiredBundles = BundleUtils.getWiredBundles(bundle);
             for (Bundle wiredBundle : wiredBundles) {
                 Enumeration<URL> resources = wiredBundle.getResources(name);
                 addToList(allResources, resources);
@@ -127,58 +124,7 @@
     private boolean isMetaInfResource(String name) {
         return searchWiredBundles && name != null && (name.startsWith(META_INF_1)
|| name.startsWith(META_INF_2));
     }
-    
-    private LinkedHashSet<Bundle> getWiredBundles() {
-        ServiceReference reference = bundle.getBundleContext().getServiceReference(PackageAdmin.class.getName());
-        PackageAdmin packageAdmin = (PackageAdmin) bundle.getBundleContext().getService(reference);
-        
-        BundleDescription description = new BundleDescription(bundle.getHeaders());
-        
-        // handle static wire via Import-Package
-        List<BundleDescription.ImportPackage> imports = description.getExternalImports();
-        LinkedHashSet<Bundle> wiredBundles = new LinkedHashSet<Bundle>();
-        for (BundleDescription.ImportPackage packageImport : imports) {
-            ExportedPackage[] exports = packageAdmin.getExportedPackages(packageImport.getName());
-            Bundle wiredBundle = getWiredBundle(exports);
-            if (wiredBundle != null) {
-                wiredBundles.add(wiredBundle);
-            }
-        }
-                
-        // handle dynamic wire via DynamicImport-Package
-        if (!description.getDynamicImportPackage().isEmpty()) {
-            for (Bundle b : bundle.getBundleContext().getBundles()) {
-                if (!wiredBundles.contains(b)) {
-                    ExportedPackage[] exports = packageAdmin.getExportedPackages(b);
-                    Bundle wiredBundle = getWiredBundle(exports); 
-                    if (wiredBundle != null) {
-                        wiredBundles.add(wiredBundle);
-                    }
-                }
-            }
-        }
-        
-        bundle.getBundleContext().ungetService(reference);
-        
-        return wiredBundles;
-    }
-    
-    private Bundle getWiredBundle(ExportedPackage[] exports) {
-        if (exports != null) {
-            for (ExportedPackage exportedPackage : exports) {
-                Bundle[] importingBundles = exportedPackage.getImportingBundles();
-                if (importingBundles != null) {
-                    for (Bundle importingBundle : importingBundles) {
-                        if (importingBundle == bundle) {
-                            return exportedPackage.getExportingBundle();
-                        }
-                    }
-                }
-            }
-        }
-        return null;
-    }
-    
+      
     private void addToList(List<URL> list, Enumeration<URL> enumeration) {
         if (enumeration != null) {
             while (enumeration.hasMoreElements()) {

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleUtils.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleUtils.java?rev=906227&r1=906226&r2=906227&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleUtils.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleUtils.java
Wed Feb  3 20:30:04 2010
@@ -20,9 +20,15 @@
 package org.apache.geronimo.kernel.osgi;
 
 import java.util.Dictionary;
+import java.util.LinkedHashSet;
+import java.util.List;
 
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleReference;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
 
 /**
  * @version $Rev$ $Date$
@@ -37,4 +43,72 @@
         Dictionary headers = bundle.getHeaders();
         return (headers != null && headers.get(Constants.FRAGMENT_HOST) != null);
     }
+    
+    /**
+     * Returns Bundle (if any) associated with current thread's context ClassLoader.
+     */
+    public static Bundle getContextBundle() {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        if (classLoader instanceof BundleReference) {
+            return ((BundleReference) classLoader).getBundle();
+        } else {
+            return null;
+        }
+    }
+    
+    public static LinkedHashSet<Bundle> getWiredBundles(Bundle bundle) {
+        ServiceReference reference = bundle.getBundleContext().getServiceReference(PackageAdmin.class.getName());
+        PackageAdmin packageAdmin = (PackageAdmin) bundle.getBundleContext().getService(reference);
       
+        try {
+            return getWiredBundles(packageAdmin, bundle);
+        } finally {
+            bundle.getBundleContext().ungetService(reference);
+        }
+    }
+    
+    public static LinkedHashSet<Bundle> getWiredBundles(PackageAdmin packageAdmin,
Bundle bundle) {
+        BundleDescription description = new BundleDescription(bundle.getHeaders());
+        
+        // handle static wire via Import-Package
+        List<BundleDescription.ImportPackage> imports = description.getExternalImports();
+        LinkedHashSet<Bundle> wiredBundles = new LinkedHashSet<Bundle>();
+        for (BundleDescription.ImportPackage packageImport : imports) {
+            ExportedPackage[] exports = packageAdmin.getExportedPackages(packageImport.getName());
+            Bundle wiredBundle = getWiredBundle(bundle, exports);
+            if (wiredBundle != null) {
+                wiredBundles.add(wiredBundle);
+            }
+        }
+                
+        // handle dynamic wire via DynamicImport-Package
+        if (!description.getDynamicImportPackage().isEmpty()) {
+            for (Bundle b : bundle.getBundleContext().getBundles()) {
+                if (!wiredBundles.contains(b)) {
+                    ExportedPackage[] exports = packageAdmin.getExportedPackages(b);
+                    Bundle wiredBundle = getWiredBundle(bundle, exports); 
+                    if (wiredBundle != null) {
+                        wiredBundles.add(wiredBundle);
+                    }
+                }
+            }
+        }
+        
+        return wiredBundles;
+    }
+    
+    private static Bundle getWiredBundle(Bundle bundle, ExportedPackage[] exports) {
+        if (exports != null) {
+            for (ExportedPackage exportedPackage : exports) {
+                Bundle[] importingBundles = exportedPackage.getImportingBundles();
+                if (importingBundles != null) {
+                    for (Bundle importingBundle : importingBundles) {
+                        if (importingBundle == bundle) {
+                            return exportedPackage.getExportingBundle();
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
 }



Mime
View raw message