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;
+ }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java
index e14188d..2e51e4d 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java
@@ -18,6 +18,8 @@
*/
package org.apache.felix.dm.test.bundle.annotation.resource;
+import java.net.URL;
+
import junit.framework.Assert;
import org.apache.felix.dm.annotation.api.ResourceDependency;
@@ -25,7 +27,6 @@
import org.apache.felix.dm.annotation.api.ServiceDependency;
import org.apache.felix.dm.annotation.api.Start;
import org.apache.felix.dm.annotation.api.Stop;
-import org.apache.felix.dm.resources.Resource;
import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
/**
@@ -45,30 +46,28 @@
System.out.println("ResourceConsumer.start: sequencer=" + m_sequencer);
}
- @ResourceDependency(required = false, filter = "(&(path=/test)(name=*.txt)(repository=TestRepository))")
- public void add(Resource resource)
+ @ResourceDependency(required = false, filter = "(&(path=/path/to/*.txt)(host=localhost))")
+ public void add(URL resource)
{
- System.out.println("ResourceConsumer.add: resource=" + resource.getName() + ", m_sequencer=" + m_sequencer);
- if (match(resource, "test1.txt", "/test", "TestRepository"))
+ System.out.println("ResourceConsumer.add: resource=" + resource + ", m_sequencer=" + m_sequencer);
+ if (match(resource, "file://localhost/path/to/test1.txt"))
{
m_resourcesSeen ++;
return;
}
- if (match(resource, "test2.txt", "/test", "TestRepository"))
+ if (match(resource, "file://localhost/path/to/test2.txt"))
{
m_resourcesSeen ++;
return;
}
- Assert.fail("Got unexpected resource: " + resource.getName() + "/" + resource.getPath()
- + "/" + resource.getRepository());
+ Assert.fail("Got unexpected resource: " + resource);
}
- private boolean match(Resource resource, String name, String path, String repo)
+ private boolean match(URL resource, String url)
{
- return name.equals(resource.getName()) && path.equals(resource.getPath())
- && repo.equals(resource.getRepository());
+ return url.equals(resource.toString());
}
@Stop
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java
index 87b9492..5d3c117 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java
@@ -18,8 +18,7 @@
*/
package org.apache.felix.dm.test.bundle.annotation.resource;
-import java.io.IOException;
-import java.io.InputStream;
+import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -30,6 +29,7 @@
import org.apache.felix.dm.annotation.api.Service;
import org.apache.felix.dm.annotation.api.ServiceDependency;
import org.apache.felix.dm.resources.ResourceHandler;
+import org.apache.felix.dm.resources.ResourceUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
@@ -42,28 +42,16 @@
{
private volatile BundleContext m_context;
private final Map m_handlers = new HashMap();
- private StaticResource[] m_resources = {
- new StaticResource("test1.txt", "/test", "TestRepository")
- {
- public InputStream openStream() throws IOException
- {
- return null;
- };
- }, new StaticResource("test2.txt", "/test", "TestRepository")
- {
- public InputStream openStream() throws IOException
- {
- return null;
- };
- }, new StaticResource("README.doc", "/", "TestRepository")
- {
- public InputStream openStream() throws IOException
- {
- Assert.fail("resource should not have matched the filter");
- return null;
- };
- } };
-
+ private URL[] m_resources;
+
+ public ResourceProvider() throws Exception {
+ m_resources = new URL[] {
+ new URL("file://localhost/path/to/test1.txt"),
+ new URL("file://localhost/path/to/test2.txt"),
+ new URL("file://localhost/path/to/README.doc")
+ };
+ }
+
/**
* Handles a new Resource consumer
* @param serviceProperties
@@ -91,7 +79,7 @@
}
for (int i = 0; i < m_resources.length; i++)
{
- if (filter == null || filter.match(m_resources[i].getProperties()))
+ if (filter == null || filter.match(ResourceUtil.createProperties(m_resources[i])))
{
handler.added(m_resources[i]);
}
@@ -116,7 +104,7 @@
{
for (int i = 0; i < m_resources.length; i++)
{
- if (filter == null || filter.match(m_resources[i].getProperties()))
+ if (filter == null || filter.match(ResourceUtil.createProperties(m_resources[i])))
{
handler.removed(m_resources[i]);
}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java
index ba1c68f..a844981 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java
@@ -18,25 +18,26 @@
*/
package org.apache.felix.dm.test.bundle.annotation.resource;
+import java.net.URL;
+
import junit.framework.Assert;
import org.apache.felix.dm.annotation.api.Property;
import org.apache.felix.dm.annotation.api.ResourceAdapterService;
import org.apache.felix.dm.annotation.api.ServiceDependency;
-import org.apache.felix.dm.resources.Resource;
import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
/**
* Our ServiceInterface provider, which service is activated by a ResourceAdapter.
*/
@ResourceAdapterService(
- filter = "(&(path=/test)(name=test1.txt)(repository=TestRepository))",
+ filter = "(&(path=/path/to/test1.txt)(host=localhost))",
properties = {@Property(name="foo", value="bar")},
propagate = true)
public class ServiceProvider implements ServiceInterface
{
// Injected by reflection
- Resource m_resource;
+ URL m_resource;
@ServiceDependency(filter="(test=adapter)")
Sequencer m_sequencer;
@@ -44,9 +45,7 @@
public void run()
{
Assert.assertNotNull("Resource has not been injected in the adapter", m_resource);
- Assert.assertEquals("ServiceProvider did not get expected resource", "test1.txt", m_resource.getName());
- Assert.assertEquals("ServiceProvider did not get expected resource", "/test", m_resource.getPath());
- Assert.assertEquals("ServiceProvider did not get expected resource", "TestRepository", m_resource.getRepository());
+ Assert.assertEquals("ServiceProvider did not get expected resource", "file://localhost/path/to/test1.txt", m_resource.toString());
m_sequencer.step(2);
}
}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java
deleted file mode 100644
index 5fd9627..0000000
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java
+++ /dev/null
@@ -1,70 +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.test.bundle.annotation.resource;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Dictionary;
-import java.util.Properties;
-
-import org.apache.felix.dm.resources.Resource;
-
-public class StaticResource implements Resource
-{
- private String m_id;
- private String m_name;
- private String m_path;
- private String m_repository;
-
- public StaticResource(String name, String path, String repository) {
- m_id = repository + ":" + path + "/" + name;
- m_name = name;
- m_path = path;
- m_repository = repository;
- }
-
- public String getID() {
- return m_id;
- }
-
- public String getName() {
- return m_name;
- }
-
- public String getPath() {
- return m_path;
- }
-
- public String getRepository() {
- return m_repository;
- }
-
- public Dictionary getProperties() {
- return new Properties() {{
- put(Resource.ID, getID());
- put(Resource.NAME, getName());
- put(Resource.PATH, getPath());
- put(Resource.REPOSITORY, getRepository());
- }};
- }
-
- public InputStream openStream() throws IOException {
- return null;
- }
-}
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2348_ResourceAdapterTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2348_ResourceAdapterTest.java
index a58e4e0..7024dbc 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2348_ResourceAdapterTest.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2348_ResourceAdapterTest.java
@@ -22,21 +22,20 @@
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.provision;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.util.Dictionary;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.HashMap;
import java.util.Map;
-import java.util.Properties;
import java.util.Map.Entry;
import junit.framework.Assert;
import org.apache.felix.dm.DependencyManager;
-import org.apache.felix.dm.resources.Resource;
import org.apache.felix.dm.resources.ResourceHandler;
+import org.apache.felix.dm.resources.ResourceUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
@@ -60,18 +59,18 @@
}
@Test
- public void testBasicResourceAdapter(BundleContext context) {
+ public void testBasicResourceAdapter(BundleContext context) throws Exception {
DependencyManager m = new DependencyManager(context);
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
- m.add(m.createResourceAdapterService("(&(path=/test)(name=*.txt)(repository=TestRepository))", false, null, "changed")
+ m.add(m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, null, "changed")
.setImplementation(new ResourceAdapter(e)));
m.add(m.createService().setImplementation(new ResourceProvider(e)).add(m.createServiceDependency().setService(ResourceHandler.class).setCallbacks("add", "remove")));
e.waitForStep(3, 5000);
}
static class ResourceAdapter {
- protected Resource m_resource; // injected by reflection.
+ protected URL m_resource; // injected by reflection.
private Ensure m_ensure;
ResourceAdapter(Ensure e) {
@@ -83,19 +82,14 @@
Assert.assertNotNull("resource not injected", m_resource);
m_ensure.step(2);
try {
- InputStream in= m_resource.openStream();
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- int c;
- while ((c = in.read()) != -1) {
- out.write(c);
- }
- String msg = new String(out.toByteArray(), "UTF8");
- Assert.assertEquals("resource", msg);
- } catch (Throwable t) {
- t.printStackTrace();
- Assert.fail();
+ InputStream in = m_resource.openStream();
+ }
+ catch (FileNotFoundException e) {
+ m_ensure.step(3);
}
- m_ensure.step(3);
+ catch (IOException e) {
+ Assert.fail("We should not have gotten this exception.");
+ }
}
}
@@ -103,16 +97,13 @@
private volatile BundleContext m_context;
private final Ensure m_ensure;
private final Map m_handlers = new HashMap();
- private StaticResource[] m_resources = {
- new StaticResource("test1.txt", "/test", "TestRepository") {
- public InputStream openStream() throws IOException {
- return new ByteArrayInputStream("resource".getBytes("UTF8"));
- };
- }
- };
+ private URL[] m_resources;
- public ResourceProvider(Ensure ensure) {
+ public ResourceProvider(Ensure ensure) throws MalformedURLException {
m_ensure = ensure;
+ m_resources = new URL[] {
+ new URL("file://localhost/path/to/file1.txt")
+ };
}
public void add(ServiceReference ref, ResourceHandler handler) {
@@ -131,7 +122,7 @@
m_handlers.put(handler, filter);
}
for (int i = 0; i < m_resources.length; i++) {
- if (filter == null || filter.match(m_resources[i].getProperties())) {
+ if (filter == null || filter.match(ResourceUtil.createProperties(m_resources[i]))) {
handler.added(m_resources[i]);
}
}
@@ -147,7 +138,7 @@
private void removeResources(ResourceHandler handler, Filter filter) {
for (int i = 0; i < m_resources.length; i++) {
- if (filter == null || filter.match(m_resources[i].getProperties())) {
+ if (filter == null || filter.match(ResourceUtil.createProperties(m_resources[i]))) {
handler.removed(m_resources[i]);
}
}
@@ -165,47 +156,4 @@
System.out.println("DESTROY..." + m_handlers.size());
}
}
-
- static class StaticResource implements Resource {
- private String m_id;
- private String m_name;
- private String m_path;
- private String m_repository;
-
- public StaticResource(String name, String path, String repository) {
- m_id = repository + ":" + path + "/" + name;
- m_name = name;
- m_path = path;
- m_repository = repository;
- }
-
- public String getID() {
- return m_id;
- }
-
- public String getName() {
- return m_name;
- }
-
- public String getPath() {
- return m_path;
- }
-
- public String getRepository() {
- return m_repository;
- }
-
- public Dictionary getProperties() {
- return new Properties() {{
- put(Resource.ID, getID());
- put(Resource.NAME, getName());
- put(Resource.PATH, getPath());
- put(Resource.REPOSITORY, getRepository());
- }};
- }
-
- public InputStream openStream() throws IOException {
- return null;
- }
- }
}
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceDependencyTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceDependencyTest.java
index 2af35b9..866866e 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceDependencyTest.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceDependencyTest.java
@@ -22,22 +22,20 @@
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.provision;
-import java.io.IOException;
-import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
-import java.util.Dictionary;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.HashMap;
import java.util.Map;
-import java.util.Properties;
import java.util.Map.Entry;
import junit.framework.Assert;
import org.apache.felix.dm.DependencyManager;
-import org.apache.felix.dm.resources.Resource;
import org.apache.felix.dm.resources.ResourceHandler;
+import org.apache.felix.dm.resources.ResourceUtil;
import org.apache.felix.dm.service.Service;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -67,16 +65,16 @@
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
- ResourceConsumer c = new ResourceConsumer();
+ ResourceConsumer c = new ResourceConsumer(e);
Service consumer = m.createService()
.setImplementation(c)
.add(m.createResourceDependency()
- .setFilter("(&(path=/test)(name=*.txt)(repository=TestRepository))")
+ .setFilter("(&(path=/path/to/*.txt)(host=localhost))")
.setCallbacks("add", "remove"));
Service dynamicProxyConsumer = m.createService()
.setFactory(new ResourceConsumerFactory(e), "create")
.add(m.createResourceDependency()
- .setFilter("(name=*.doc)")
+ .setFilter("(path=*.doc)")
.setCallbacks("add", null));
Service resourceProvider = m.createService()
.setImplementation(new ResourceProvider(e))
@@ -110,16 +108,17 @@
static class ResourceConsumer {
private volatile int m_counter;
- public void add(Resource resource) {
- m_counter++;
- try {
- resource.openStream();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
+ private Ensure m_ensure;
+
+ public ResourceConsumer(Ensure ensure) {
+ m_ensure = ensure;
}
- public void remove(Resource resource) {
+
+ public void add(URL resource) {
+ m_counter++;
+ m_ensure.step();
+ }
+ public void remove(URL resource) {
m_counter--;
}
public void ensure() {
@@ -131,29 +130,20 @@
private volatile BundleContext m_context;
private final Ensure m_ensure;
private final Map m_handlers = new HashMap();
- private StaticResource[] m_resources = {
- new StaticResource("test1.txt", "/test", "TestRepository") {
- public InputStream openStream() throws IOException {
- m_ensure.step(1);
- return null;
- };
- },
- new StaticResource("test2.txt", "/test", "TestRepository") {
- public InputStream openStream() throws IOException {
- m_ensure.step(2);
- return null;
- };
- },
- new StaticResource("README.doc", "/", "TestRepository") {
- public InputStream openStream() throws IOException {
- Assert.fail("resource should not have matched the filter");
- return null;
- };
- }
- };
-
+ private URL[] m_resources;
+
public ResourceProvider(Ensure ensure) {
m_ensure = ensure;
+ try {
+ m_resources = new URL[] {
+ new URL("file://localhost/path/to/file1.txt"),
+ new URL("file://localhost/path/to/file2.txt"),
+ new URL("file://localhost/path/to/file3.doc")
+ };
+ }
+ catch (MalformedURLException e) {
+ throw new IllegalStateException(e);
+ }
}
public void add(ServiceReference ref, ResourceHandler handler) {
@@ -170,7 +160,7 @@
m_handlers.put(handler, filter);
}
for (int i = 0; i < m_resources.length; i++) {
- if (filter.match(m_resources[i].getProperties())) {
+ if (filter.match(ResourceUtil.createProperties(m_resources[i]))) {
handler.added(m_resources[i]);
}
}
@@ -186,7 +176,7 @@
private void removeResources(ResourceHandler handler, Filter filter) {
for (int i = 0; i < m_resources.length; i++) {
- if (filter.match(m_resources[i].getProperties())) {
+ if (filter.match(ResourceUtil.createProperties(m_resources[i]))) {
handler.removed(m_resources[i]);
}
}
@@ -205,49 +195,6 @@
}
}
- static class StaticResource implements Resource {
- private String m_id;
- private String m_name;
- private String m_path;
- private String m_repository;
-
- public StaticResource(String name, String path, String repository) {
- m_id = repository + ":" + path + "/" + name;
- m_name = name;
- m_path = path;
- m_repository = repository;
- }
-
- public String getID() {
- return m_id;
- }
-
- public String getName() {
- return m_name;
- }
-
- public String getPath() {
- return m_path;
- }
-
- public String getRepository() {
- return m_repository;
- }
-
- public Dictionary getProperties() {
- return new Properties() {{
- put(Resource.ID, getID());
- put(Resource.NAME, getName());
- put(Resource.PATH, getPath());
- put(Resource.REPOSITORY, getRepository());
- }};
- }
-
- public InputStream openStream() throws IOException {
- return null;
- }
- }
-
static class ResourceConsumerFactory {
private final Ensure m_ensure;
public ResourceConsumerFactory(Ensure ensure) {
@@ -255,7 +202,7 @@
}
public Object create() {
System.out.println("create");
- ResourceConsumer resourceConsumer = new ResourceConsumer();
+ ResourceConsumer resourceConsumer = new ResourceConsumer(m_ensure);
// create a dynamic proxy for the ResourceProvider
return Proxy.newProxyInstance(resourceConsumer.getClass().getClassLoader(), resourceConsumer.getClass().getInterfaces(), new DynamicProxyHandler(resourceConsumer, m_ensure));
}
@@ -270,7 +217,7 @@
m_ensure = ensure;
}
- public void add(Resource resource) {
+ public void add(URL resource) {
m_ensure.step(4);
System.out.println("Add resource: " + resource);
}
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/SharingDependenciesWithMultipleServicesTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/SharingDependenciesWithMultipleServicesTest.java
index 4066d1b..4214b85 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/SharingDependenciesWithMultipleServicesTest.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/SharingDependenciesWithMultipleServicesTest.java
@@ -22,8 +22,7 @@
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.provision;
-import java.io.IOException;
-import java.io.InputStream;
+import java.net.URL;
import java.util.Dictionary;
import java.util.Properties;
@@ -32,8 +31,8 @@
import org.apache.felix.dm.dependencies.ConfigurationDependency;
import org.apache.felix.dm.dependencies.ResourceDependency;
import org.apache.felix.dm.dependencies.ServiceDependency;
-import org.apache.felix.dm.resources.Resource;
import org.apache.felix.dm.resources.ResourceHandler;
+import org.apache.felix.dm.resources.ResourceUtil;
import org.apache.felix.dm.service.Service;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -132,12 +131,12 @@
}
@Test
- public void testShareResourceDependencyWithMultipleServices(BundleContext context) {
+ public void testShareResourceDependencyWithMultipleServices(BundleContext context) throws Exception {
DependencyManager m = new DependencyManager(context);
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
- ResourceDependency dependency = m.createResourceDependency().setFilter("(" + Resource.REPOSITORY + "=TestRepository)").setRequired(true);
+ ResourceDependency dependency = m.createResourceDependency().setFilter("(" + ResourceHandler.HOST + "=localhost)").setRequired(true);
Service consumer1 = m.createService().setImplementation(new ResourceConsumer(e, 1)).add(dependency);
Service consumer2 = m.createService().setImplementation(new ResourceConsumer(e, 2)).add(dependency);
Service resourceProvider = m.createService().setImplementation(new ResourceProvider()).add(m.createServiceDependency().setService(ResourceHandler.class).setCallbacks("add", "remove"));;
@@ -250,17 +249,22 @@
static class ResourceProvider {
private volatile BundleContext m_context;
- private StaticResource[] m_resources = {
- new StaticResource("test1.txt", "/test", "TestRepository"),
- new StaticResource("test2.txt", "/test", "TestRepository")
- };
+ private URL[] m_resources;
+
+ public ResourceProvider() throws Exception {
+ m_resources = new URL[] {
+ new URL("file://localhost/path/to/test1.txt"),
+ new URL("file://localhost/path/to/test2.txt")
+ };
+
+ }
public void add(ServiceReference ref, ResourceHandler handler) {
String filterString = (String) ref.getProperty("filter");
try {
Filter filter = m_context.createFilter(filterString);
for (int i = 0; i < m_resources.length; i++) {
- if (filter.match(m_resources[i].getProperties())) {
+ if (filter.match(ResourceUtil.createProperties(m_resources[i]))) {
handler.added(m_resources[i]);
}
}
@@ -275,7 +279,7 @@
try {
Filter filter = m_context.createFilter(filterString);
for (int i = 0; i < m_resources.length; i++) {
- if (filter.match(m_resources[i].getProperties())) {
+ if (filter.match(ResourceUtil.createProperties(m_resources[i]))) {
handler.removed(m_resources[i]);
}
}
@@ -285,46 +289,4 @@
}
}
}
-
- static class StaticResource implements Resource {
- private String m_id;
- private String m_name;
- private String m_path;
- private String m_repository;
-
- public StaticResource(String name, String path, String repository) {
- m_id = repository + ":" + path + "/" + name;
- m_name = name;
- m_path = path;
- m_repository = repository;
- }
-
- public String getID() {
- return m_id;
- }
-
- public String getName() {
- return m_name;
- }
-
- public String getPath() {
- return m_path;
- }
-
- public String getRepository() {
- return m_repository;
- }
-
- public Dictionary getProperties() {
- return new Properties() {{
- put(Resource.NAME, getName());
- put(Resource.PATH, getPath());
- put(Resource.REPOSITORY, getRepository());
- }};
- }
-
- public InputStream openStream() throws IOException {
- return null;
- }
- }
}