Applied patch (FELIX-153) to implement Bundle.getResources().


git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@450113 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/BundleImpl.java b/framework/src/main/java/org/apache/felix/framework/BundleImpl.java
index 07a6d0c..faaf929 100644
--- a/framework/src/main/java/org/apache/felix/framework/BundleImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/BundleImpl.java
@@ -160,6 +160,26 @@
         return m_felix.getBundleResource(this, name);
     }
 
+    public Enumeration getResources(String name) throws IOException
+    {
+        Object sm = System.getSecurityManager();
+
+        if (sm != null)
+        {
+            try
+            {
+                ((SecurityManager) sm).checkPermission(new AdminPermission(this,
+                    AdminPermission.RESOURCE));
+            }
+            catch (Exception e)
+            {
+                return null; // No permission
+            }
+        }
+
+        return m_felix.getBundleResources(this, name);
+    }
+
     /**
      * Returns an array of service references corresponding to
      * the bundle's registered services.
@@ -393,27 +413,6 @@
         return null;
     }
 
-    public Enumeration getResources(String name) throws IOException
-    {
-        // TODO: Implement Bundle.getResources()
-        Object sm = System.getSecurityManager();
-
-        if (sm != null)
-        {
-            try
-            {
-                ((SecurityManager) sm).checkPermission(new AdminPermission(this,
-                    AdminPermission.RESOURCE));
-            }
-            catch (Exception e)
-            {
-                return null; // No permission
-            }
-        }
-
-        return null;
-    }
-
     public boolean equals(Object obj)
     {
         if (obj instanceof BundleImpl)
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 0a30700..ecedc7f 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -995,6 +995,18 @@
     }
 
     /**
+     * Implementation for Bundle.getResources().
+    **/
+    protected Enumeration getBundleResources(BundleImpl bundle, String name)
+    {
+        if (bundle.getInfo().getState() == Bundle.UNINSTALLED)
+        {
+            throw new IllegalStateException("The bundle is uninstalled.");
+        }
+        return bundle.getInfo().getCurrentModule().getResources(name);
+    }
+
+    /**
      * Implementation for Bundle.getEntry().
     **/
     protected URL getBundleEntry(BundleImpl bundle, String name)
diff --git a/framework/src/main/java/org/apache/felix/framework/SystemBundleContentLoader.java b/framework/src/main/java/org/apache/felix/framework/SystemBundleContentLoader.java
index 2809b2d..6fc10df 100644
--- a/framework/src/main/java/org/apache/felix/framework/SystemBundleContentLoader.java
+++ b/framework/src/main/java/org/apache/felix/framework/SystemBundleContentLoader.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * 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
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Enumeration;
 
 import org.apache.felix.moduleloader.*;
 
@@ -91,6 +92,18 @@
         return getClass().getClassLoader().getResource(name);
     }
 
+    public Enumeration getResources(String name)
+    {
+       try
+       {
+           return getClass().getClassLoader().getResources(name);
+       }
+       catch (IOException ex)
+       {
+           return null;
+       }
+    }
+
     public URL getResourceFromContent(String name)
     {
         // There is no content for the system bundle, so return null.
diff --git a/framework/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java b/framework/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java
index de81ed0..53e9a87 100644
--- a/framework/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java
@@ -150,7 +150,7 @@
         return url;
     }
 
-    protected Enumeration getResources(String name)
+    public Enumeration getResources(String name)
     {
         Vector v = new Vector();
 
diff --git a/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicy.java b/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicy.java
index 8dffe0e..d928dfd 100644
--- a/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicy.java
+++ b/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicy.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * 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
@@ -19,6 +19,7 @@
 package org.apache.felix.framework.searchpolicy;
 
 import java.net.URL;
+import java.util.Enumeration;
 
 import org.apache.felix.moduleloader.*;
 
@@ -50,6 +51,12 @@
         return m_policyCore.findResource(m_module, name);
     }
 
+    public Enumeration findResources(String name)
+        throws ResourceNotFoundException
+    {
+        return m_policyCore.findResources(m_module, name);
+    }
+
     public String findLibrary(String name)
     {
         return m_policyCore.findLibrary(m_module, name);
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 47cd12c..d3f496a 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
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.framework.searchpolicy;
 
+import java.io.IOException;
 import java.net.URL;
 import java.security.ProtectionDomain;
 import java.util.*;
@@ -88,7 +89,8 @@
         }
         else
         {
-            throw new IllegalStateException("Module manager is already initialized");
+            throw new IllegalStateException(
+                "Module manager is already initialized");
         }
     }
 
@@ -162,11 +164,118 @@
         {
             throw ex;
         }
-
+    
         // We should never reach this point.
         return null;
     }
 
+    public Enumeration findResources(IModule module, String name)
+        throws ResourceNotFoundException
+    {
+        Enumeration urls;
+        // First, try to resolve the originating module.
+        // TODO: Consider opimizing this call to resolve, since it is called
+        // for each class load.
+        try
+        {
+            resolve(module);
+        }
+        catch (ResolveException ex)
+        {
+            // The spec states that if the bundle cannot be resolved, then
+            // only the local bundle's resources should be searched. So we
+            // will ask the module's own class path.
+            urls = module.getContentLoader().getResources(name);
+            if (urls != null)
+            {
+                return urls;
+            }
+            // We need to throw a resource not found exception.
+            throw new ResourceNotFoundException(name
+                + ": cannot resolve package " + ex.getPackage());
+        }
+
+        // Get the package of the target class/resource.
+        String pkgName = Util.getResourcePackage(name);
+
+        // Delegate any packages listed in the boot delegation
+        // property to the parent class loader.
+        // NOTE for the default package:
+        // Only consider delegation if we have a package name, since
+        // we don't want to promote the default package. The spec does
+        // not take a stand on this issue.
+        if (pkgName.length() > 0)
+        {
+            for (int i = 0; i < m_bootPkgs.length; i++)
+            {
+                // A wildcarded boot delegation package will be in the form of
+                // "foo.", so if the package is wildcarded do a startsWith() or a
+                // regionMatches() to ignore the trailing "." to determine if the
+                // request should be delegated to the parent class loader. If the
+                // package is not wildcarded, then simply do an equals() test to
+                // see if the request should be delegated to the parent class loader.
+                if ((m_bootPkgWildcards[i] &&
+                    (pkgName.startsWith(m_bootPkgs[i]) ||
+                    m_bootPkgs[i].regionMatches(0, pkgName, 0, pkgName.length())))
+                    || (!m_bootPkgWildcards[i] && m_bootPkgs[i].equals(pkgName)))
+                {
+                    try
+                    {
+                        urls = getClass().getClassLoader().getResources(name);
+                        return urls;
+                    }
+                    catch (IOException ex)
+                    {
+                        return null;
+                    }
+                }
+            }
+        }
+
+        // Look in the module's imports.
+        // We delegate to the module's wires to the resources.
+        // If any resources are found, this means that the package of these
+        // resources is imported, we must not keep looking since we do not
+        // support split-packages.
+
+        // Note that the search may be aborted if this method throws an
+        // exception, otherwise it continues if a null is returned.
+        IWire[] wires = module.getWires();
+        for (int i = 0; (wires != null) && (i < wires.length); i++)
+        {
+            // If we find the class or resource, then return it.
+            urls = wires[i].getResources(name);
+            if (urls != null)
+            {
+                return urls;
+            }
+        }
+
+        // If not found, try the module's own class path.
+        urls = module.getContentLoader().getResources(name);
+        if (urls != null)
+        {
+            return urls;
+        }
+
+        // If still not found, then try the module's dynamic imports.
+        // At this point, the module's imports were searched and so was the
+        // the module's content. Now we make an attempt to load the
+        // class/resource via a dynamic import, if possible.
+        IWire wire = attemptDynamicImport(module, pkgName);
+        if (wire != null)
+        {
+            urls = wire.getResources(name);
+        }
+
+        if (urls == null)
+        {
+            throw new ResourceNotFoundException(name);
+        }
+
+        return urls;
+    }
+
     private Object findClassOrResource(IModule module, String name, boolean isClass)
         throws ClassNotFoundException, ResourceNotFoundException
     {
@@ -531,7 +640,7 @@
                 PackagePermission perm = new PackagePermission(pkg.getName(),
                     PackagePermission.EXPORT);
 
-                for (int i = 0;i < exporters.length;i++)
+                for (int i = 0; i < exporters.length; i++)
                 {
                     if (exporters[i] != null)
                     {
@@ -1784,4 +1893,4 @@
 
         return sb.toString();
     }
-}
+}
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4Wire.java b/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4Wire.java
index 56bfd5c..7415de5 100755
--- a/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4Wire.java
+++ b/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4Wire.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * 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
@@ -19,6 +19,7 @@
 package org.apache.felix.framework.searchpolicy;
 
 import java.net.URL;
+import java.util.Enumeration;
 
 import org.apache.felix.framework.util.Util;
 import org.apache.felix.moduleloader.*;
@@ -125,6 +126,31 @@
         return url;
     }
 
+    public Enumeration getResources(String name) throws ResourceNotFoundException
+    {
+        Enumeration urls = null;
+
+        // Get the package of the target class.
+        String pkgName = Util.getResourcePackage(name);
+
+        // Only check when the package of the target resource is
+        // the same as the package for the wire.
+        if (m_export.getName().equals(pkgName))
+        {
+            urls = m_exporter.getContentLoader().getResources(name);
+
+            // If no resource was found, then we must throw an exception
+            // since the exporter for this package did not contain the
+            // requested class.
+            if (urls == null)
+            {
+                throw new ResourceNotFoundException(name);
+            }
+        }
+
+        return urls;
+    }
+
     public String toString()
     {
         return m_importer + " -> " + m_export.getName() + " -> " + m_exporter;
diff --git a/framework/src/main/java/org/apache/felix/moduleloader/IContentLoader.java b/framework/src/main/java/org/apache/felix/moduleloader/IContentLoader.java
index 746388e..20f8553 100644
--- a/framework/src/main/java/org/apache/felix/moduleloader/IContentLoader.java
+++ b/framework/src/main/java/org/apache/felix/moduleloader/IContentLoader.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * 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
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Enumeration;
 
 public interface IContentLoader
 {
@@ -37,6 +38,7 @@
 
     public Class getClass(String name);
     public URL getResource(String name);
+    public Enumeration getResources(String name);
     public URL getResourceFromContent(String name);
 
     public boolean hasInputStream(String urlPath)
diff --git a/framework/src/main/java/org/apache/felix/moduleloader/IModule.java b/framework/src/main/java/org/apache/felix/moduleloader/IModule.java
index 6716ffb..58cd74a 100644
--- a/framework/src/main/java/org/apache/felix/moduleloader/IModule.java
+++ b/framework/src/main/java/org/apache/felix/moduleloader/IModule.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * 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
@@ -19,6 +19,7 @@
 package org.apache.felix.moduleloader;
 
 import java.net.URL;
+import java.util.Enumeration;
 
 public interface IModule
 {
@@ -31,6 +32,7 @@
 
     public Class getClass(String name);
     public URL getResource(String name);
+    public Enumeration getResources(String name);
 
     public Object getSecurityContext();
 }
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/moduleloader/ISearchPolicy.java b/framework/src/main/java/org/apache/felix/moduleloader/ISearchPolicy.java
index bbd0a5f..de269e1 100644
--- a/framework/src/main/java/org/apache/felix/moduleloader/ISearchPolicy.java
+++ b/framework/src/main/java/org/apache/felix/moduleloader/ISearchPolicy.java
@@ -19,13 +19,13 @@
 package org.apache.felix.moduleloader;
 
 import java.net.URL;
-
-import org.apache.felix.moduleloader.ResourceNotFoundException;
+import java.util.Enumeration;
 
 public interface ISearchPolicy
 {
     public Object[] definePackage(String name);
     public Class findClass(String name) throws ClassNotFoundException;
     public URL findResource(String name) throws ResourceNotFoundException;
+    public Enumeration findResources(String name) throws ResourceNotFoundException;
     public String findLibrary(String name);
 }
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/moduleloader/IWire.java b/framework/src/main/java/org/apache/felix/moduleloader/IWire.java
index c4ca07f..6807363 100644
--- a/framework/src/main/java/org/apache/felix/moduleloader/IWire.java
+++ b/framework/src/main/java/org/apache/felix/moduleloader/IWire.java
@@ -19,6 +19,7 @@
 package org.apache.felix.moduleloader;
 
 import java.net.URL;
+import java.util.Enumeration;
 
 import org.apache.felix.framework.searchpolicy.R4Export;
 
@@ -29,4 +30,5 @@
     public R4Export getExport();
     public Class getClass(String name) throws ClassNotFoundException;
     public URL getResource(String name) throws ResourceNotFoundException;
+    public Enumeration getResources(String name) throws ResourceNotFoundException;
 }
\ No newline at end of file
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 0175252..fd598f8 100644
--- a/framework/src/main/java/org/apache/felix/moduleloader/ModuleImpl.java
+++ b/framework/src/main/java/org/apache/felix/moduleloader/ModuleImpl.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * 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
@@ -19,6 +19,7 @@
 package org.apache.felix.moduleloader;
 
 import java.net.URL;
+import java.util.Enumeration;
 
 import org.apache.felix.framework.Logger;
 
@@ -116,6 +117,22 @@
         return null;
     }
 
+    public Enumeration getResources(String name)
+    {
+        try
+        {
+           return m_contentLoader.getSearchPolicy().findResources(name);
+        }
+        catch (ResourceNotFoundException ex)
+        {
+            m_logger.log(
+                Logger.LOG_WARNING,
+                ex.getMessage(),
+                ex);
+        }
+        return null;
+    }
+
     public String toString()
     {
         return m_id;