Modify URL Handlers impl to use same hook tracking mechanism as
the other hooks, which was introduced for byte-code weaving hooks.
(FELIX-2959)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1127547 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java b/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
index fe0a079..d9b4cd9 100644
--- a/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
+++ b/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.framework;
 
+import java.net.ContentHandler;
 import java.util.*;
 import org.apache.felix.framework.capabilityset.CapabilitySet;
 import org.apache.felix.framework.capabilityset.SimpleFilter;
@@ -26,8 +27,8 @@
 import org.osgi.framework.*;
 import org.osgi.framework.hooks.service.*;
 import org.osgi.framework.hooks.weaving.WeavingHook;
-import org.osgi.framework.launch.Framework;
 import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.service.url.URLStreamHandlerService;
 
 public class ServiceRegistry
 {
@@ -54,7 +55,9 @@
         EventHook.class,
         FindHook.class,
         ListenerHook.class,
-        WeavingHook.class
+        WeavingHook.class,
+        URLStreamHandlerService.class,
+        ContentHandler.class
     };
     private final Map<Class<?>, Set<ServiceReference<?>>> m_allHooks =
         new HashMap<Class<?>, Set<ServiceReference<?>>>();
diff --git a/framework/src/main/java/org/apache/felix/framework/URLHandlersActivator.java b/framework/src/main/java/org/apache/felix/framework/URLHandlersActivator.java
index fc874d2..4eca450 100644
--- a/framework/src/main/java/org/apache/felix/framework/URLHandlersActivator.java
+++ b/framework/src/main/java/org/apache/felix/framework/URLHandlersActivator.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
@@ -18,14 +18,17 @@
  */
 package org.apache.felix.framework;
 
+import java.net.ContentHandler;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.felix.framework.util.FelixConstants;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
+import org.osgi.service.url.URLStreamHandlerService;
 import org.osgi.util.tracker.ServiceTracker;
 
 /**
@@ -63,10 +66,6 @@
 
         if (enable)
         {
-            m_streamTracker = new ServiceTracker(context, "org.osgi.service.url.URLStreamHandlerService", null);
-            m_contentTracker= new ServiceTracker(context, "java.net.ContentHandler", null);
-            m_streamTracker.open();
-            m_contentTracker.open();
             m_framework.setURLHandlersActivator(this);
         }
         URLHandlers.registerFrameworkInstance(m_framework, enable);
@@ -76,66 +75,49 @@
     {
         URLHandlers.unregisterFrameworkInstance(m_framework);
         m_framework.setURLHandlersActivator(null);
-        if (m_streamTracker != null)
-        {
-            m_streamTracker.close();
-        }
-        if (m_contentTracker != null)
-        {
-            m_contentTracker.close();
-        }
-        m_streamTracker = null;
-        m_contentTracker = null;
     }
 
-    private volatile ServiceTracker m_streamTracker;
-    private volatile ServiceTracker m_contentTracker;
-    
     protected Object getStreamHandlerService(String protocol)
     {
-        return get(m_streamTracker, "url.handler.protocol", protocol);
+        return get(
+            m_framework.getHooks(URLStreamHandlerService.class),
+            "url.handler.protocol", protocol);
     }
 
     protected Object getContentHandlerService(String mimeType)
     {
-        return get(m_contentTracker, "url.content.mimetype", mimeType);
+        return get(
+            m_framework.getHooks(ContentHandler.class),
+            "url.content.mimetype", mimeType);
     }
 
-    private Object get(ServiceTracker tracker, String key, String value)
+    private <S> S get(Set<ServiceReference<S>> hooks, String key, String value)
     {
     	Object service = null;
-        if (tracker != null)
+        if (!hooks.isEmpty())
         {
-            ServiceReference[] refs = tracker.getServiceReferences();
-
-            if (refs != null)
+            for (ServiceReference<S> ref : hooks)
             {
-                if (refs.length > 1)
+                Object values = ref.getProperty(key);
+                if (values instanceof String[])
                 {
-                    Arrays.sort(refs, Collections.reverseOrder());
-                }
-
-                for (int i = 0;(i < refs.length) && (service == null);i++)
-                {
-                    Object values = refs[i].getProperty(key);
-                    if (values instanceof String[])
+                    for (int valueIdx = 0;
+                        (valueIdx < ((String[]) values).length) && (service == null);
+                        valueIdx++)
                     {
-                        for (int j = 0;(j < ((String[]) values).length) && (service == null);j++)
+                        if (value.equals(((String[]) values)[valueIdx]))
                         {
-                            if (value.equals(((String[]) values)[j]))
-                            {
-                                service = tracker.getService(refs[i]);
-                            }
+                            return m_framework.getService(m_framework, ref);
                         }
                     }
-                    else if (value.equals(values))
-                    {
-                        service = tracker.getService(refs[i]);
-                    }
+                }
+                else if (value.equals(values))
+                {
+                    return m_framework.getService(m_framework, ref);
                 }
             }
         }
 
-        return service;
+        return null;
     }
-}
+}
\ No newline at end of file