Implemented remaining PackageAdmin methods from R4. (FELIX-35)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@705035 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java
index 2c34bf5..5a76afe 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -2102,10 +2102,10 @@
         }
 
         // Extension Bundles are not removed until the framework is shutdown
-        if (bundle.getInfo().isExtension())
+        if (info.isExtension())
         {
-            bundle.getInfo().setPersistentStateUninstalled();
-            bundle.getInfo().setState(Bundle.INSTALLED);
+            info.setPersistentStateUninstalled();
+            info.setState(Bundle.INSTALLED);
             return;
         }
 
@@ -2882,14 +2882,7 @@
                 }
             }
         }
-        try
-        {
-            return (getInfo().getCurrentModule().getClass(clazz.getName()) == clazz) ? this : null;
-        }
-        catch(ClassNotFoundException ex)
-        {
-            return null;
-        }
+        return null;
     }
 
     /**
@@ -3056,7 +3049,7 @@
         }
     }
 
-    protected Bundle[] getDependentBundles(FelixBundle exporter)
+    Bundle[] getDependentBundles(FelixBundle exporter)
     {
         // Get exporting bundle.
         BundleInfo exporterInfo = exporter.getInfo();
diff --git a/framework/src/main/java/org/apache/felix/framework/PackageAdminImpl.java b/framework/src/main/java/org/apache/felix/framework/PackageAdminImpl.java
index c21f511..7d09d3f 100644
--- a/framework/src/main/java/org/apache/felix/framework/PackageAdminImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/PackageAdminImpl.java
@@ -20,7 +20,10 @@
 
 import java.util.*;
 
+import org.apache.felix.framework.util.Util;
 import org.apache.felix.framework.util.VersionRange;
+import org.apache.felix.moduleloader.IModule;
+import org.apache.felix.moduleloader.ModuleImpl;
 import org.osgi.framework.*;
 import org.osgi.service.packageadmin.*;
 
@@ -140,6 +143,11 @@
 
     public int getBundleType(Bundle bundle)
     {
+        Map headerMap = ((FelixBundle) bundle).getInfo().getCurrentHeader();
+        if (headerMap.containsKey(Constants.FRAGMENT_HOST))
+        {
+            return PackageAdmin.BUNDLE_TYPE_FRAGMENT;
+        }
         return 0;
     }
 
@@ -184,6 +192,61 @@
         return m_felix.getExportedPackages(bundle);
     }
 
+    public Bundle[] getFragments(Bundle bundle)
+    {
+        Bundle[] fragments = null;
+        // If the bundle is not a fragment, then return its fragments.
+        if ((getBundleType(bundle) & BUNDLE_TYPE_FRAGMENT) == 0)
+        {
+            // Get attached fragments.
+            IModule[] modules =
+                ((ModuleImpl)
+                    ((FelixBundle) bundle).getInfo().getCurrentModule()).getFragments();
+            // Convert fragment modules to bundles.
+            List list = new ArrayList();
+            for (int i = 0; (modules != null) && (i < modules.length); i++)
+            {
+                long id = Util.getBundleIdFromModuleId(modules[i].getId());
+                Bundle b = m_felix.getBundle(id);
+                if (b != null)
+                {
+                    list.add(b);
+                }
+            }
+            // Convert list to an array.
+            fragments = (list.size() == 0)
+                ? null
+                : (Bundle[]) list.toArray(new Bundle[list.size()]);
+        }
+        return fragments;
+    }
+
+    public Bundle[] getHosts(Bundle bundle)
+    {
+        if (getBundleType(bundle) == BUNDLE_TYPE_FRAGMENT)
+        {
+            return m_felix.getDependentBundles((FelixBundle) bundle);
+        }
+        return null;
+    }
+
+    public RequiredBundle[] getRequiredBundles(String symbolicName)
+    {
+        List list = new ArrayList();
+        Bundle[] bundles = m_felix.getBundles();
+        for (int i = 0; i < bundles.length; i++)
+        {
+            FelixBundle fb = (FelixBundle) bundles[i];
+            if ((symbolicName == null) || (symbolicName.equals(fb.getInfo().getSymbolicName())))
+            {
+                list.add(new RequiredBundleImpl(m_felix, fb));
+            }
+        }
+        return (list.size() == 0)
+            ? null
+            : (RequiredBundle[]) list.toArray(new RequiredBundle[list.size()]);
+    }
+
     /**
      * The OSGi specification states that refreshing packages is
      * asynchronous; this method simply notifies the package admin
@@ -218,6 +281,19 @@
         notifyAll();
     }
 
+    public boolean resolveBundles(Bundle[] bundles)
+    {
+        Object sm = System.getSecurityManager();
+        
+        if (sm != null)
+        {
+            ((SecurityManager) sm).checkPermission(
+                new AdminPermission(m_systemBundle, AdminPermission.RESOLVE));
+        }
+        
+        return m_felix.resolveBundles(bundles);
+    }
+
     /**
      * The OSGi specification states that package refreshes happen
      * asynchronously; this is the run() method for the package
@@ -274,35 +350,4 @@
             }
         }
     }
-
-    public boolean resolveBundles(Bundle[] bundles)
-    {
-        Object sm = System.getSecurityManager();
-        
-        if (sm != null)
-        {
-            ((SecurityManager) sm).checkPermission(
-                new AdminPermission(m_systemBundle, AdminPermission.RESOLVE));
-        }
-        
-        return m_felix.resolveBundles(bundles);
-    }
-
-    public RequiredBundle[] getRequiredBundles(String symbolicName)
-    {
-        // TODO: Implement PackageAdmin.getRequiredBundles()
-        return null;
-    }
-
-    public Bundle[] getFragments(Bundle bundle)
-    {
-        // TODO: Implement PackageAdmin.getFragments()
-        return null;
-    }
-
-    public Bundle[] getHosts(Bundle bundle)
-    {
-        // TODO: Implement PackageAdmin.getHosts()
-        return null;
-    }
 }
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/RequiredBundleImpl.java b/framework/src/main/java/org/apache/felix/framework/RequiredBundleImpl.java
new file mode 100644
index 0000000..f42282b
--- /dev/null
+++ b/framework/src/main/java/org/apache/felix/framework/RequiredBundleImpl.java
@@ -0,0 +1,109 @@
+/*
+ * 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.felix.framework;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.apache.felix.framework.util.Util;
+import org.apache.felix.moduleloader.ICapability;
+import org.apache.felix.moduleloader.IModule;
+import org.apache.felix.moduleloader.ModuleImpl;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
+import org.osgi.service.packageadmin.RequiredBundle;
+
+class RequiredBundleImpl implements RequiredBundle
+{
+    private final Felix m_felix;
+    private final FelixBundle m_bundle;
+
+    public RequiredBundleImpl(Felix felix, FelixBundle bundle)
+    {
+        m_felix = felix;
+        m_bundle = bundle;
+    }
+
+    public String getSymbolicName()
+    {
+        return m_bundle.getSymbolicName();
+    }
+
+    public Bundle getBundle()
+    {
+        return m_bundle;
+    }
+
+    public Bundle[] getRequiringBundles()
+    {
+        // Spec says to return null for stale bundles.
+        if (m_bundle.getInfo().isStale())
+        {
+            return null;
+        }
+
+        // We need to find all modules that require any of the modules
+        // associated with this bundle.
+        List moduleList = new ArrayList();
+        // Loop through all of this bundle's modules.
+        IModule[] modules = m_bundle.getInfo().getModules();
+        for (int modIdx = 0; (modules != null) && (modIdx < modules.length); modIdx++)
+        {
+            // For each of this bundle's modules, loop through all of the
+            // modules that require it and add them to the module list.
+            IModule[] dependents = ((ModuleImpl) modules[modIdx]).getDependentRequirers();
+            for (int depIdx = 0; (dependents != null) && (depIdx < dependents.length); depIdx++)
+            {
+                moduleList.add(dependents[modIdx]);
+            }
+        }
+        // Convert the list of dependent modules into a non-duplicated
+        // array of bundles.
+        Set bundleSet = new HashSet();
+        for (int modIdx = 0; modIdx < moduleList.size(); modIdx++)
+        {
+            long id = Util.getBundleIdFromModuleId(((IModule) moduleList.get(modIdx)).getId());
+            Bundle bundle = m_felix.getBundle(id);
+            if (bundle != null)
+            {
+                bundleSet.add(bundle);
+            }
+        }
+        return (Bundle[]) bundleSet.toArray(new Bundle[bundleSet.size()]);
+    }
+
+    public Version getVersion()
+    {
+        ICapability[] caps = 
+            Util.getCapabilityByNamespace(
+                m_bundle.getInfo().getCurrentModule(), ICapability.MODULE_NAMESPACE);
+        if ((caps != null) && (caps.length > 0))
+        {
+            return (Version) caps[0].getProperties().get(Constants.BUNDLE_VERSION_ATTRIBUTE);
+        }
+        return null;
+    }
+
+    public boolean isRemovalPending()
+    {
+        return m_bundle.getInfo().isRemovalPending();
+    }
+}
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java b/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java
index a894872..8fd75a4 100755
--- a/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java
+++ b/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java
@@ -36,7 +36,6 @@
 import org.apache.felix.framework.util.SecurityManagerEx;
 import org.apache.felix.framework.util.Util;
 import org.apache.felix.framework.util.manifestparser.Capability;
-import org.apache.felix.framework.util.manifestparser.ManifestParser;
 import org.apache.felix.framework.util.manifestparser.R4Attribute;
 import org.apache.felix.framework.util.manifestparser.R4Directive;
 import org.apache.felix.framework.util.manifestparser.R4Library;
@@ -1148,13 +1147,12 @@
         }
     }
 
-// TODO: FRAGMENT - Not very efficient.
     private boolean isFragment(IModule module)
     {
-        IRequirement[] reqs = module.getDefinition().getRequirements();
-        for (int reqIdx = 0; (reqs != null) && (reqIdx < reqs.length); reqIdx++)
+        if (module.getDefinition() instanceof ModuleDefinition)
         {
-            if (reqs[reqIdx].getNamespace().equals(ICapability.HOST_NAMESPACE))
+            Map headerMap = ((ModuleDefinition) module.getDefinition()).getHeaders();
+            if (headerMap.containsKey(Constants.FRAGMENT_HOST))
             {
                 return true;
             }
diff --git a/framework/src/main/java/org/apache/felix/moduleloader/ModuleImpl.java b/framework/src/main/java/org/apache/felix/moduleloader/ModuleImpl.java
index e6f438f..8963cf8 100644
--- a/framework/src/main/java/org/apache/felix/moduleloader/ModuleImpl.java
+++ b/framework/src/main/java/org/apache/felix/moduleloader/ModuleImpl.java
@@ -65,6 +65,11 @@
         m_contentLoader = contentLoader;
     }
 
+    public synchronized IModule[] getFragments()
+    {
+        return m_fragments;
+    }
+
     public synchronized void attachFragments(IModule[] fragments) throws Exception
     {
         // Remove module from old fragment dependencies.