Eliminated thread local and anonymous inner classe for returning
local URLs and instead just convert the bundle URL to a local URL
at the point of return, if necessary. (FELIX-2645)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1022674 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java b/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
index 4a54aaf..36d673a 100644
--- a/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
+++ b/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
@@ -610,7 +610,6 @@
         return null;
     }
 
-    // TODO: REMOVE - Remove when class path scanning is implemented.
     public URL getEntryAsURL(String name)
     {
         return null;
@@ -764,7 +763,6 @@
             return getClass().getClassLoader().getResourceAsStream(urlPath);
         }
 
-        // TODO: REMOVE - Remove when class path scanning is implemented.
         public URL getLocalURL(int index, String urlPath)
         {
             return getClass().getClassLoader().getResource(urlPath);
diff --git a/framework/src/main/java/org/apache/felix/framework/ModuleImpl.java b/framework/src/main/java/org/apache/felix/framework/ModuleImpl.java
index b45b0c9..c80c3b6 100644
--- a/framework/src/main/java/org/apache/felix/framework/ModuleImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/ModuleImpl.java
@@ -145,6 +145,8 @@
 
     // Boolean flag to enable/disable implicit boot delegation.
     private final boolean m_implicitBootDelegation;
+    // Boolean flag to enable/disable local URLs.
+    private final boolean m_useLocalURLs;
 
     // Re-usable security manager for accessing class context.
     private static SecurityManagerEx m_sm = new SecurityManagerEx();
@@ -158,13 +160,6 @@
     // Flag indicating whether we are on an old JVM or not.
     private volatile static boolean m_isPreJava5 = false;
 
-    static ThreadLocal<Boolean> m_jarUrl = new ThreadLocal<Boolean>() {
-        @Override
-        protected Boolean initialValue() {
-            return false;
-        }
-    };
-
     /**
      * This constructor is used by the extension manager, since it needs
      * a constructor that does not throw an exception.
@@ -201,6 +196,9 @@
         m_activationExcludes = null;
         m_activationIncludes = null;
         m_implicitBootDelegation = false;
+        m_useLocalURLs =
+            (m_configMap.get(FelixConstants.USE_LOCALURLS_PROP) == null)
+                ? false : true;
         m_bootClassLoader = m_defBootClassLoader;
     }
 
@@ -228,6 +226,10 @@
                 (String) m_configMap.get(
                     FelixConstants.IMPLICIT_BOOT_DELEGATION_PROP)).booleanValue();
 
+        m_useLocalURLs =
+            (m_configMap.get(FelixConstants.USE_LOCALURLS_PROP) == null)
+                ? false : true;
+
         ClassLoader bootLoader = m_defBootClassLoader;
         Object map = m_configMap.get(FelixConstants.BOOT_CLASSLOADERS_PROP);
         if (map instanceof Map)
@@ -1113,39 +1115,28 @@
 
     private URL createURL(int port, String path)
     {
-         if (useJarUrl())
-         {
-             return getLocalURL(port, path);
-         }
-         // Add a slash if there is one already, otherwise
-         // the is no slash separating the host from the file
-         // in the resulting URL.
-         if (!path.startsWith("/"))
-         {
-             path = "/" + path;
-         }
+        // Add a slash if there is one already, otherwise
+        // the is no slash separating the host from the file
+        // in the resulting URL.
+        if (!path.startsWith("/"))
+        {
+            path = "/" + path;
+        }
 
-         try
-         {
-             return m_secureAction.createURL(null,
-                 FelixConstants.BUNDLE_URL_PROTOCOL + "://" +
-                 m_id + ":" + port + path, m_streamHandler);
-         }
-         catch (MalformedURLException ex)
-         {
-             m_logger.log(m_bundle,
-                 Logger.LOG_ERROR,
-                 "Unable to create resource URL.",
-                 ex);
-         }
-         return null;
-    }
-
-    private boolean useJarUrl()
-    {
-        String val = (String) ((BundleImpl) getBundle()).getFramework()
-                        .getConfig().get("org.apache.felix.jarurls");
-        return m_jarUrl.get() && Boolean.parseBoolean(val);
+        try
+        {
+            return m_secureAction.createURL(null,
+                FelixConstants.BUNDLE_URL_PROTOCOL + "://" +
+                m_id + ":" + port + path, m_streamHandler);
+        }
+        catch (MalformedURLException ex)
+        {
+            m_logger.log(m_bundle,
+                Logger.LOG_ERROR,
+                "Unable to create resource URL.",
+                ex);
+        }
+        return null;
     }
 
     //
@@ -1718,16 +1709,30 @@
 
         public Enumeration getResources(String name)
         {
-            boolean jarUrl = m_jarUrl.get();
-            try
+            Enumeration urls = ModuleImpl.this.getResourcesByDelegation(name);
+            if (m_useLocalURLs)
             {
-                m_jarUrl.set(true);
-                return ModuleImpl.this.getResourcesByDelegation(name);
+                List<URL> localURLs = new ArrayList();
+                while (urls.hasMoreElements())
+                {
+                    URL url = (URL) urls.nextElement();
+                    if (url.getProtocol().equals("bundle"))
+                    {
+                        try
+                        {
+                            url = ((URLHandlersBundleURLConnection)
+                                url.openConnection()).getLocalURL();
+                        }
+                        catch (IOException ex)
+                        {
+                            // Ignore and add original url.
+                        }
+                    }
+                    localURLs.add(url);
+                }
+                urls = new IteratorToEnumeration(localURLs.iterator());
             }
-            finally
-            {
-                m_jarUrl.set(jarUrl);
-            }
+            return urls;
         }
 
         protected Enumeration findResources(String name)
@@ -2028,16 +2033,23 @@
 
         public URL getResource(String name)
         {
-            boolean jarUrl = m_jarUrl.get();
-            try
+            URL url = ModuleImpl.this.getResourceByDelegation(name);
+            if (m_useLocalURLs)
             {
-                m_jarUrl.set(true);
-                return ModuleImpl.this.getResourceByDelegation(name);
+                if (url.getProtocol().equals("bundle"))
+                {
+                    try
+                    {
+                        url = ((URLHandlersBundleURLConnection)
+                            url.openConnection()).getLocalURL();
+                    }
+                    catch (IOException ex)
+                    {
+                        // Ignore and return original url.
+                    }
+                }
             }
-            finally
-            {
-                m_jarUrl.set(jarUrl);
-            }
+            return url;
         }
 
         protected URL findResource(String name)
@@ -2052,16 +2064,30 @@
         // can't. As a workaround, we make findResources() delegate instead.
         protected Enumeration findResources(String name)
         {
-            boolean jarUrl = m_jarUrl.get();
-            try
+            Enumeration urls = ModuleImpl.this.getResourcesByDelegation(name);
+            if (m_useLocalURLs)
             {
-                m_jarUrl.set(true);
-                return getResourcesByDelegation(name);
+                List<URL> localURLs = new ArrayList();
+                while (urls.hasMoreElements())
+                {
+                    URL url = (URL) urls.nextElement();
+                    if (url.getProtocol().equals("bundle"))
+                    {
+                        try
+                        {
+                            url = ((URLHandlersBundleURLConnection)
+                                url.openConnection()).getLocalURL();
+                        }
+                        catch (IOException ex)
+                        {
+                            // Ignore and add original url.
+                        }
+                    }
+                    localURLs.add(url);
+                }
+                urls = Collections.enumeration(localURLs);
             }
-            finally
-            {
-                m_jarUrl.set(jarUrl);
-            }
+            return urls;
         }
 
         protected String findLibrary(String name)
diff --git a/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleURLConnection.java b/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleURLConnection.java
index 5968924..9f555e3 100644
--- a/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleURLConnection.java
+++ b/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleURLConnection.java
@@ -201,7 +201,6 @@
      *
      * @return the local URL
      */
-    // TODO: REMOVE - Remove when class path scanning is implemented.
     URL getLocalURL()
     {
         if ((m_targetModule == null) || (m_classPathIdx < 0))
@@ -210,4 +209,4 @@
         }
         return m_targetModule.getLocalURL(m_classPathIdx, url.getPath());
     }
-}
+}
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/ContentDirectoryContent.java b/framework/src/main/java/org/apache/felix/framework/cache/ContentDirectoryContent.java
index 1fa4f0c..aed76c6 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/ContentDirectoryContent.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/ContentDirectoryContent.java
@@ -82,7 +82,6 @@
         return m_content.getEntryAsStream(m_rootPath + name);
     }
 
-    // TODO: REMOVE - Remove when class path scanning is implemented.
     public URL getEntryAsURL(String name)
     {
         return m_content.getEntryAsURL(m_rootPath + name);
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java
index f9276ab..b9ecc1c 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java
@@ -133,7 +133,6 @@
         return new FileInputStream(new File(m_dir, name));
     }
 
-    // TODO: REMOVE - Remove when class path scanning is implemented.
     public URL getEntryAsURL(String name)
     {
         if ((name.length() > 0) && (name.charAt(0) == '/'))
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/JarContent.java b/framework/src/main/java/org/apache/felix/framework/cache/JarContent.java
index 57c21e4..5085fd6 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/JarContent.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/JarContent.java
@@ -192,7 +192,6 @@
         return is;
     }
 
-    // TODO: REMOVE - Remove when class path scanning is implemented.
     public URL getEntryAsURL(String name)
     {
         try
diff --git a/framework/src/main/java/org/apache/felix/framework/resolver/Content.java b/framework/src/main/java/org/apache/felix/framework/resolver/Content.java
index 79aa8d7..f820ec8 100644
--- a/framework/src/main/java/org/apache/felix/framework/resolver/Content.java
+++ b/framework/src/main/java/org/apache/felix/framework/resolver/Content.java
@@ -113,13 +113,12 @@
 
     /**
      * <p>
-     *  This method allows retrieving an entry as a plain standard URL.
+     *  This method allows retrieving an entry as a local URL.
      * </p>
      *
      * @param name The name of the entry to retrieve as a URL
-     * @return A URL using a standard protocol such as file, jar
+     * @return A URL using a local protocol such as file, jar
      *           or null if not possible.
      */
-    // TODO: REMOVE - Remove when class path scanning is implemented.
     URL getEntryAsURL(String name);
 }
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/resolver/Module.java b/framework/src/main/java/org/apache/felix/framework/resolver/Module.java
index db7d7bf..695bfd3 100644
--- a/framework/src/main/java/org/apache/felix/framework/resolver/Module.java
+++ b/framework/src/main/java/org/apache/felix/framework/resolver/Module.java
@@ -72,6 +72,5 @@
         throws IOException;
     InputStream getInputStream(int index, String urlPath)
         throws IOException;
-    // TODO: REMOVE - Remove when class path scanning is implemented.
     URL getLocalURL(int index, String urlPath);
 }
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/util/FelixConstants.java b/framework/src/main/java/org/apache/felix/framework/util/FelixConstants.java
index 0c08a2f..b4e8ad0 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/FelixConstants.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/FelixConstants.java
@@ -53,6 +53,7 @@
     public static final String SERVICE_URLHANDLERS_PROP = "felix.service.urlhandlers";
     public static final String IMPLICIT_BOOT_DELEGATION_PROP = "felix.bootdelegation.implicit";
     public static final String BOOT_CLASSLOADERS_PROP = "felix.bootdelegation.classloaders";
+    public static final String USE_LOCALURLS_PROP = "felix.jarurls";
 
     // Start level-related constants.
     public static final int FRAMEWORK_INACTIVE_STARTLEVEL = 0;