Refactored resource dependencies to use URLs instead of the Resource interface we used previously. Adapted all tests to reflect that.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@960066 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyManager.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyManager.java
index 4d5f507..0a59f1b 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyManager.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyManager.java
@@ -41,7 +41,6 @@
 import org.apache.felix.dm.impl.dependencies.ServiceDependencyImpl;
 import org.apache.felix.dm.impl.dependencies.TemporalServiceDependencyImpl;
 import org.apache.felix.dm.impl.metatype.PropertyMetaDataImpl;
-import org.apache.felix.dm.resources.Resource;
 import org.apache.felix.dm.service.Service;
 import org.osgi.framework.BundleContext;
 
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/dependencies/ResourceDependency.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/dependencies/ResourceDependency.java
index 366e3ae..fb9a992 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/dependencies/ResourceDependency.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/dependencies/ResourceDependency.java
@@ -18,7 +18,8 @@
  */
 package org.apache.felix.dm.dependencies;
 
-import org.apache.felix.dm.resources.Resource;
+import java.net.URL;
+
 import org.apache.felix.dm.resources.ResourceHandler;
 
 public interface ResourceDependency extends Dependency, ResourceHandler {
@@ -94,7 +95,7 @@
      */
      public ResourceDependency setAutoConfig(String instanceName);
     
-     public ResourceDependency setResource(Resource resource);
+     public ResourceDependency setResource(URL resource);
 	
      public ResourceDependency setRequired(boolean required);
 
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AbstractDecorator.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AbstractDecorator.java
index cb9803e..f2afb90 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AbstractDecorator.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AbstractDecorator.java
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.dm.impl;
 
+import java.net.URL;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -26,7 +27,6 @@
 
 import org.apache.felix.dm.DependencyManager;
 import org.apache.felix.dm.dependencies.Dependency;
-import org.apache.felix.dm.resources.Resource;
 import org.apache.felix.dm.service.Service;
 import org.apache.felix.dm.service.ServiceStateListener;
 import org.osgi.framework.Bundle;
@@ -173,13 +173,13 @@
     }
 
     // callbacks for resources
-    public void added(Resource resource) {
+    public void added(URL resource) {
         Service newService = createService(new Object[] { resource });
         m_services.put(resource, newService);
         m_manager.add(newService);
     }
 
-    public void removed(Resource resource) {
+    public void removed(URL resource) {
         Service newService = (Service) m_services.remove(resource);
         if (newService == null) {
             System.out.println("Service should not be null here, dumping stack.");
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ResourceAdapterServiceImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ResourceAdapterServiceImpl.java
index ea6f3e9..7bd7ca7 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ResourceAdapterServiceImpl.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ResourceAdapterServiceImpl.java
@@ -18,13 +18,13 @@
  */
 package org.apache.felix.dm.impl;
 
+import java.net.URL;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Properties;
 
 import org.apache.felix.dm.DependencyManager;
 import org.apache.felix.dm.dependencies.Dependency;
-import org.apache.felix.dm.resources.Resource;
 import org.apache.felix.dm.service.Service;
 import org.apache.felix.dm.service.ServiceStateListener;
 
@@ -61,7 +61,7 @@
         }
 
         public Service createService(Object[] properties) {
-            Resource resource = (Resource) properties[0]; 
+            URL resource = (URL) properties[0]; 
             Properties props = new Properties();
             if (m_serviceProperties != null) {
                 Enumeration e = m_serviceProperties.keys();
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/DependencyActivation.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/DependencyActivation.java
index 7bb7546..189e0a0 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/DependencyActivation.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/DependencyActivation.java
@@ -1,7 +1,6 @@
 package org.apache.felix.dm.impl.dependencies;
 
-public interface DependencyActivation
-{
+public interface DependencyActivation {
   public void start(DependencyService service);
   public void stop(DependencyService service);
 }
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ResourceDependencyImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ResourceDependencyImpl.java
index ddefa1e..b7faaab 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ResourceDependencyImpl.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ResourceDependencyImpl.java
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.dm.impl.dependencies;
 
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.List;
@@ -27,7 +28,6 @@
 import org.apache.felix.dm.dependencies.ResourceDependency;
 import org.apache.felix.dm.impl.Logger;
 import org.apache.felix.dm.management.ServiceComponentDependency;
-import org.apache.felix.dm.resources.Resource;
 import org.apache.felix.dm.resources.ResourceHandler;
 import org.apache.felix.dm.service.Service;
 import org.osgi.framework.BundleContext;
@@ -46,10 +46,10 @@
     private String m_autoConfigInstance;
     protected List m_services = new ArrayList();
 	private String m_resourceFilter;
-	private Resource m_trackedResource;
+	private URL m_trackedResource;
     private boolean m_isStarted;
     private List m_resources = new ArrayList();
-    private Resource m_resourceInstance;
+    private URL m_resourceInstance;
     private boolean m_propagate;
 	
     public ResourceDependencyImpl(BundleContext context, Logger logger) {
@@ -93,7 +93,7 @@
 	        Properties props = null;
 	        if (m_resourceFilter != null) {
 	            props = new Properties();
-	            props.setProperty(Resource.FILTER, m_resourceFilter);
+	            props.setProperty(ResourceHandler.FILTER, m_resourceFilter);
 	        }
 	        m_registration = m_context.registerService(ResourceHandler.class.getName(), this, props);
 	    }
@@ -114,7 +114,7 @@
 	    }
 	}
 
-	public void added(Resource resource) {
+	public void added(URL resource) {
 	    if (m_trackedResource == null || m_trackedResource.equals(resource)) {
     		long counter;
     		Object[] services;
@@ -139,7 +139,7 @@
 	    }
 	}
 
-	public void changed(Resource resource) {
+	public void changed(URL resource) {
         if (m_trackedResource == null || m_trackedResource.equals(resource)) {
             Object[] services;
             synchronized (this) {
@@ -152,7 +152,7 @@
         }
 	}
 
-	public void removed(Resource resource) {
+	public void removed(URL resource) {
         if (m_trackedResource == null || m_trackedResource.equals(resource)) {
     		long counter;
     		Object[] services;
@@ -177,22 +177,22 @@
         }
 	}
 	
-    public void invokeAdded(DependencyService ds, Resource serviceInstance) {
+    public void invokeAdded(DependencyService ds, URL serviceInstance) {
         invoke(ds, serviceInstance, m_callbackAdded);
     }
 
-    public void invokeChanged(DependencyService ds, Resource serviceInstance) {
+    public void invokeChanged(DependencyService ds, URL serviceInstance) {
         invoke(ds, serviceInstance, m_callbackChanged);
     }
 
-    public void invokeRemoved(DependencyService ds, Resource serviceInstance) {
+    public void invokeRemoved(DependencyService ds, URL serviceInstance) {
         invoke(ds, serviceInstance, m_callbackRemoved);
     }
     
-    private void invoke(DependencyService ds, Resource serviceInstance, String name) {
+    private void invoke(DependencyService ds, URL serviceInstance, String name) {
         if (name != null) {
             ds.invokeCallbackMethod(getCallbackInstances(ds), name,
-                new Class[][] {{ Service.class, Resource.class }, { Service.class, Object.class }, { Service.class },  { Resource.class }, { Object.class }, {}},
+                new Class[][] {{ Service.class, URL.class }, { Service.class, Object.class }, { Service.class },  { URL.class }, { Object.class }, {}},
                 new Object[][] {{ ds.getServiceInterface(), serviceInstance }, { ds.getServiceInterface(), serviceInstance }, { ds.getServiceInterface() }, { serviceInstance }, { serviceInstance }, {}}
             );
         }
@@ -368,7 +368,7 @@
         }
     }
 
-	public ResourceDependency setResource(Resource resource) {
+	public ResourceDependency setResource(URL resource) {
 		m_trackedResource = resource;
 		return this;
 	}
@@ -388,13 +388,13 @@
         return m_autoConfig;
     }
     
-    public Resource getResource() {
+    public URL getResource() {
     	return lookupResource();
     }
 
-    private Resource lookupResource() {
+    private URL lookupResource() {
         try {
-            return (Resource) m_resources.get(0);
+            return (URL) m_resources.get(0);
         }
         catch (IndexOutOfBoundsException e) {
             return null;
@@ -410,7 +410,7 @@
     }
 
     public Class getAutoConfigType() {
-        return Resource.class;
+        return URL.class;
     }
 
     public void invokeAdded(DependencyService service) {
@@ -431,12 +431,13 @@
     }
     
     public Dictionary getProperties() {
-        Resource resource = lookupResource();
+        URL resource = lookupResource();
         if (resource != null) {
             Properties props = new Properties();
-            props.put(Resource.NAME, resource.getName());
-            props.put(Resource.PATH, resource.getPath());
-            props.put(Resource.REPOSITORY, resource.getRepository());
+            props.setProperty(ResourceHandler.HOST, resource.getHost());
+            props.setProperty(ResourceHandler.PATH, resource.getPath());
+            props.setProperty(ResourceHandler.PROTOCOL, resource.getProtocol());
+            props.setProperty(ResourceHandler.PORT, Integer.toString(resource.getPort()));
             return props;
         }
         else {
@@ -459,7 +460,7 @@
             sb.append(m_resourceFilter);
         }
         if (m_trackedResource != null) {
-            sb.append(m_trackedResource.getID());
+            sb.append(m_trackedResource.toString());
         }
         return sb.toString();
     }
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/BundleResourceRepository.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/BundleResourceRepository.java
deleted file mode 100644
index 53b5d67..0000000
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/BundleResourceRepository.java
+++ /dev/null
@@ -1,197 +0,0 @@
-/*
- * 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.dm.resources;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.Dictionary;
-import java.util.Enumeration;
-
-import org.osgi.framework.Bundle;
-import org.osgi.framework.Filter;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-
-public class BundleResourceRepository {
-	private final Bundle m_bundle;
-	
-	public BundleResourceRepository(Bundle bundle) {
-		m_bundle = bundle;
-	}
-
-	public synchronized void addHandler(ServiceReference ref, ResourceHandler handler) {
-		String filter = (String) ref.getProperty("filter"); // "(&(repository=a)(path=b)(name=*.xml))"
-		Filter filterObject = null;
-		if (filter != null) {
-    		try {
-    			filterObject = FrameworkUtil.createFilter(filter);
-    		} 
-    		catch (InvalidSyntaxException e) {
-    			e.printStackTrace();
-    			return;
-    		}
-		}
-		Enumeration entries = m_bundle.findEntries("/", null, true);
-		while (entries.hasMoreElements()) {
-			EntryResource resource = new EntryResource(m_bundle, (URL) entries.nextElement());
-			if (filterObject == null || filterObject.match(resource)) {
-                handler.added(resource);
-			}
-		}
-	}
-
-	public synchronized void removeHandler(ServiceReference ref, ResourceHandler handler) {
-		String filter = (String) ref.getProperty("filter"); // "(&(repository=a)(path=b)(name=*.xml))"
-		Filter filterObject = null;
-		if (filter != null) {
-    		try {
-    			filterObject = FrameworkUtil.createFilter(filter);
-    		}
-    		catch (InvalidSyntaxException e) {
-    			e.printStackTrace();
-    			return;
-    		}
-		}
-		Enumeration entries = m_bundle.findEntries("/", null, true);
-        while (entries.hasMoreElements()) {
-            EntryResource resource = new EntryResource(m_bundle, (URL) entries.nextElement());
-            if (filterObject == null || filterObject.match(resource)) {
-                handler.removed(resource);
-            }
-        }
-	}
-
-	static class EntryResource extends Dictionary implements Resource {
-		private final URL m_entry;
-        private final String m_id;
-        private final String m_repository;
-        private final String m_path;
-        private final String m_name;
-        private static Object[] m_keys;
-        private Object[] m_values;
-
-		public EntryResource(Bundle bundle, URL entry) {
-			m_entry = entry;
-			// TODO is this unique? can we have the same url in more than one repository?
-			m_id = m_entry.toString();
-			m_repository = bundle.getSymbolicName() + "_" + bundle.getHeaders().get("Bundle-Version");
-			String path = entry.getPath();
-			int i = path.lastIndexOf('/');
-			if (i == -1) {
-			    m_path = "/";
-			    m_name = path;
-			}
-			else {
-			    if (path.length() > (i + 1)) {
-    			    m_path = path.substring(0, i);
-    			    m_name = path.substring(i + 1);
-			    }
-			    else {
-			        m_path = path;
-			        m_name = "";
-			    }
-			}
-		}
-
-		public final String getID() {
-		    return m_id;
-		}
-		
-		public final String getName() {
-			return m_name;
-		}
-
-		public final String getPath() {
-			return m_path;
-		}
-		
-		public final String getRepository() {
-			return m_repository;
-		}
-
-		public final InputStream openStream() throws IOException {
-			return m_entry.openStream();
-		}
-
-        public Enumeration elements() {
-            if (m_values == null) {
-                m_values = new Object[] { m_id, m_repository, m_path, m_name };
-            }
-            return new ArrayEnumeration(m_values);
-        }
-
-        public Object get(Object key) {
-            if (Resource.ID.equals(key)) {
-                return m_id;
-            }
-            else if (Resource.REPOSITORY.equals(key)) {
-                return m_repository;
-            }
-            else if (Resource.PATH.equals(key)) {
-                return m_path;
-            }
-            else if (Resource.NAME.equals(key)) {
-                return m_name;
-            }
-            return null;
-        }
-
-        public boolean isEmpty() {
-            return false;
-        }
-
-        public Enumeration keys() {
-            if (m_keys == null) {
-                m_keys = new Object[] { Resource.ID, Resource.REPOSITORY, Resource.PATH, Resource.NAME };
-            }
-            return new ArrayEnumeration(m_keys);
-        }
-
-        public Object put(Object key, Object value) {
-            return null;
-        }
-
-        public Object remove(Object key) {
-            return null;
-        }
-
-        public int size() {
-            return 4;
-        }
-	}
-	
-	static class ArrayEnumeration implements Enumeration {
-	    private int m_counter = 0;
-	    private Object[] m_elements;
-	    
-	    public ArrayEnumeration(Object[] array) {
-	        m_elements = array;
-	    }
-	    
-	    public boolean hasMoreElements() {
-	        return (m_counter < m_elements.length);
-	    }
-	    
-	    public Object nextElement() {
-	        return m_elements[m_counter++];
-	    }
-	}
-}
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/Resource.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/Resource.java
deleted file mode 100644
index 37cd638..0000000
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/Resource.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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.dm.resources;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-/** 
- * Interface that defines a resource. Each resource has a unique ID, which should be
- * used for equality tests. Resources also have a repository name, path and name, which
- * can all be used to filter on. To access a resource, use the <code>openStream()</code>
- * method.
- */
-public interface Resource {
-    public static final String ID = "id";
-    public static final String FILTER = "filter";
-	public static final String PATH = "path";
-	public static final String NAME = "name";
-	public static final String REPOSITORY = "repository";
-	
-	/** Returns the unique identification of this resource. */
-	public String getID();
-	/** Returns the name of this resource. */
-	public String getName();
-	/** Returns the path of this resource. */
-	public String getPath();
-	/** Returns the repository of this resource. */
-	public String getRepository();
-	/**	Returns an input stream containing all data behind this resource. */
-	public InputStream openStream() throws IOException;
-}
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/ResourceHandler.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/ResourceHandler.java
index 27b4fd8..898aacd 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/ResourceHandler.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/ResourceHandler.java
@@ -18,14 +18,28 @@
  */
 package org.apache.felix.dm.resources;
 
+import java.net.URL;
+
 /** 
  * Service interface for anybody wanting to be notified of changes to resources. 
  */
 public interface ResourceHandler {
+    /** Name of the property that's used to describe the filter condition for a resource. */
+    public static final String FILTER = "filter";
+    
+    /** The host part of the URL. */
+    public static final String HOST = "host";
+    /** The path part of the URL. */
+    public static final String PATH = "path";
+    /** The protocol part of the URL. */
+    public static final String PROTOCOL = "protocol";
+    /** The port part of the URL. */
+    public static final String PORT = "port";
+
     /** Invoked whenever a new resource is added. */
-	public void added(Resource resource);
+	public void added(URL resource);
 	/** Invoked whenever an existing resource changes. */
-	public void changed(Resource resource);
+	public void changed(URL resource);
 	/** Invoked whenever an existing resource is removed. */
-	public void removed(Resource resource);
+	public void removed(URL resource);
 }
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/ResourceUtil.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/ResourceUtil.java
new file mode 100644
index 0000000..b004b43
--- /dev/null
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/resources/ResourceUtil.java
@@ -0,0 +1,16 @@
+package org.apache.felix.dm.resources;
+
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Properties;
+
+public class ResourceUtil {
+    public static Dictionary createProperties(URL url) {
+        Properties props = new Properties();
+        props.setProperty(ResourceHandler.PROTOCOL, url.getProtocol());
+        props.setProperty(ResourceHandler.HOST, url.getHost());
+        props.setProperty(ResourceHandler.PORT, Integer.toString(url.getPort()));
+        props.setProperty(ResourceHandler.PATH, url.getPath());
+        return props;
+    }
+}