You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2006/07/03 09:57:30 UTC

svn commit: r418703 - in /incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework: Felix.java PackageAdminImpl.java URLHandlers.java

Author: rickhall
Date: Mon Jul  3 00:57:28 2006
New Revision: 418703

URL: http://svn.apache.org/viewvc?rev=418703&view=rev
Log:
Implemented getBundle() and getBundles() for R4 PackageAdmin (FELIX-35).

Modified:
    incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java
    incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/PackageAdminImpl.java
    incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/URLHandlers.java

Modified: incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java?rev=418703&r1=418702&r2=418703&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java (original)
+++ incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java Mon Jul  3 00:57:28 2006
@@ -957,35 +957,6 @@
         return (((BundleImpl) bundle).getInfo().getPersistentState() == Bundle.ACTIVE);
     }
 
-    /**
-     * <p>
-     * This method is used to determine if the specified class is from
-     * a bundle installed in the framework instance. This method is used
-     * by the URL Handlers service when determining the framework instance
-     * associated with call stack.
-     * </p>
-     * @param clazz The class to test for whether it comes from a bundle.
-     * @return <tt>true</tt> if the class comes from a bundle installed in
-     *         the framework, <tt>false</tt> otherwise.
-    **/
-    protected boolean isBundleClass(Class clazz)
-    {
-        if (clazz.getClassLoader() instanceof ContentClassLoader)
-        {
-            IContentLoader contentLoader =
-                ((ContentClassLoader) clazz.getClassLoader()).getContentLoader();
-            IModule[] modules = m_factory.getModules();
-            for (int i = 0; i < modules.length; i++)
-            {
-                if (modules[i].getContentLoader() == contentLoader)
-                {
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-
     //
     // Implementation of Bundle interface methods.
     //
@@ -2552,6 +2523,36 @@
     //
     // PackageAdmin related methods.
     //
+
+    /**
+     * This method returns the bundle associated with the specified class if
+     * the class was loaded from a bundle from this framework instance. If the
+     * class was not loaded from a bundle or was loaded by a bundle in another
+     * framework instance, then <tt>null</tt> is returned.
+     * 
+     * @param clazz the class for which to find its associated bundle.
+     * @return the bundle associated with the specified class or <tt>null</tt>
+     *         if the class was not loaded by a bundle or its associated
+     *         bundle belongs to a different framework instance.
+    **/
+    protected Bundle getBundle(Class clazz)
+    {
+        if (clazz.getClassLoader() instanceof ContentClassLoader)
+        {
+            IContentLoader contentLoader =
+                ((ContentClassLoader) clazz.getClassLoader()).getContentLoader();
+            IModule[] modules = m_factory.getModules();
+            for (int i = 0; i < modules.length; i++)
+            {
+                if (modules[i].getContentLoader() == contentLoader)
+                {
+                    long id = Util.getBundleIdFromModuleId(modules[i].getId());
+                    return getBundle(id);
+                }
+            }
+        }
+        return null;
+    }
 
     /**
      * Returns the exported package associated with the specified

Modified: incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/PackageAdminImpl.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/PackageAdminImpl.java?rev=418703&r1=418702&r2=418703&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/PackageAdminImpl.java (original)
+++ incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/PackageAdminImpl.java Mon Jul  3 00:57:28 2006
@@ -16,7 +16,10 @@
  */
 package org.apache.felix.framework;
 
-import org.osgi.framework.Bundle;
+import java.util.*;
+
+import org.apache.felix.framework.searchpolicy.VersionRange;
+import org.osgi.framework.*;
 import org.osgi.service.packageadmin.*;
 
 class PackageAdminImpl implements PackageAdmin, Runnable
@@ -35,6 +38,71 @@
     }
 
     /**
+     * Returns the bundle associated with this class if the class was
+     * loaded from a bundle, otherwise returns null.
+     * 
+     * @param clazz the class for which to determine its associated bundle.
+     * @return the bundle associated with the specified class, otherwise null.
+    **/
+    public Bundle getBundle(Class clazz)
+    {
+        return m_felix.getBundle(clazz);
+    }
+
+    /**
+     * Returns all bundles that have a specified symbolic name and whose
+     * version is in the specified version range. If no version range is
+     * specified, then all bundles with the specified symbolic name are
+     * returned. The array is sorted in descending version order.
+     * 
+     * @param symbolicName the target symbolic name.
+     * @param versionRange the target version range.
+     * @return an array of matching bundles sorted in descending version order.
+    **/
+    public Bundle[] getBundles(String symbolicName, String versionRange)
+    {
+// TODO: PACKAGEADMIN - This could be made more efficient by reducing object creation.
+        VersionRange vr = (versionRange == null)
+            ? null : VersionRange.parse(versionRange);
+        Bundle[] bundles = m_felix.getBundles();
+        List list = new ArrayList();
+        for (int i = 0; (bundles != null) && (i < bundles.length); i++)
+        {
+            String sym = (String) ((BundleImpl) bundles[i])
+                .getInfo().getCurrentHeader().get(Constants.BUNDLE_SYMBOLICNAME);
+            if ((sym != null) && sym.equals(symbolicName))
+            {
+                String s = (String) ((BundleImpl) bundles[i])
+                    .getInfo().getCurrentHeader().get(Constants.BUNDLE_VERSION);
+                Version v = (s == null) ? new Version("0.0.0") : new Version(s);
+                if ((vr == null) || vr.isInRange(v))
+                {
+                    list.add(bundles[i]);
+                }
+            }
+        }
+        if (list.size() == 0)
+        {
+            return null;
+        }
+        bundles = (Bundle[]) list.toArray(new Bundle[list.size()]);
+        Arrays.sort(bundles, new Comparator() {
+            public int compare(Object o1, Object o2)
+            {
+                String s1 = (String) ((BundleImpl) o1)
+                    .getInfo().getCurrentHeader().get(Constants.BUNDLE_VERSION);
+                String s2 = (String) ((BundleImpl) o2)
+                    .getInfo().getCurrentHeader().get(Constants.BUNDLE_VERSION);
+                Version v1 = (s1 == null) ? new Version("0.0.0") : new Version(s1);
+                Version v2 = (s2 == null) ? new Version("0.0.0") : new Version(s2);
+                // Compare in reverse order to get descending sort.
+                return v2.compareTo(v1);
+            }
+        });
+        return bundles;
+    }
+
+    /**
      * Returns the exported package associated with the specified
      * package name.
      *
@@ -155,12 +223,6 @@
         return null;
     }
 
-    public Bundle[] getBundles(String symbolicName, String versionRange)
-    {
-        // TODO: Implement PackageAdmin.getBundles()
-        return null;
-    }
-
     public Bundle[] getFragments(Bundle bundle)
     {
         // TODO: Implement PackageAdmin.getFragments()
@@ -170,12 +232,6 @@
     public Bundle[] getHosts(Bundle bundle)
     {
         // TODO: Implement PackageAdmin.getHosts()
-        return null;
-    }
-
-    public Bundle getBundle(Class clazz)
-    {
-        // TODO: Implement PackageAdmin.getBundle()
         return null;
     }
 

Modified: incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/URLHandlers.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/URLHandlers.java?rev=418703&r1=418702&r2=418703&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/URLHandlers.java (original)
+++ incubator/felix/trunk/org.apache.felix.framework/src/main/java/org/apache/felix/framework/URLHandlers.java Mon Jul  3 00:57:28 2006
@@ -359,7 +359,7 @@
                     // Check the registry of framework instances
                     for (int i = 0; i < m_frameworkList.size(); i++)
                     {
-                        if (((Felix) m_frameworkList.get(i)).isBundleClass(targetClass))
+                        if (((Felix) m_frameworkList.get(i)).getBundle(targetClass) != null)
                         {
                             return (Felix) m_frameworkList.get(i);
                         }