reworked ResourceAdapter API in order to allow to provide adapter parameters by reusing the Service interface methods (setInterface, setImplementation, etc ...)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@947496 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyActivatorBase.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyActivatorBase.java
index 10f396e..1548034 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyActivatorBase.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyActivatorBase.java
@@ -165,11 +165,8 @@
return m_manager.createAdapterService(serviceInterface, serviceFilter);
}
- public Service createResourceAdapter(String resourceFilter, Object adapterHandler, String adapterInterface, Dictionary adapterProperties, Object adapterImplementation, boolean propagate) {
- return m_manager.createResourceAdapterService(resourceFilter, adapterHandler, adapterInterface, adapterProperties, adapterImplementation, propagate);
- }
- public Service createResourceAdapter(String resourceFilter, Object adapterHandler, String[] adapterInterface, Dictionary adapterProperties, Object adapterImplementation, boolean propagate) {
- return m_manager.createResourceAdapterService(resourceFilter, adapterHandler, adapterInterface, adapterProperties, adapterImplementation, propagate);
+ public Service createResourceAdapter(String resourceFilter, boolean propagate) {
+ return m_manager.createResourceAdapterService(resourceFilter, propagate);
}
public Service createBundleAdapterService(int bundleStateMask, String bundleFilter, Object adapterImplementation, String adapterInterface, Dictionary adapterProperties, boolean propagate) {
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 c911461..d9b8b4c 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
@@ -36,7 +36,7 @@
import org.apache.felix.dm.impl.FactoryConfigurationAdapterImpl;
import org.apache.felix.dm.impl.FactoryConfigurationAdapterMetaTypeImpl;
import org.apache.felix.dm.impl.Logger;
-import org.apache.felix.dm.impl.ResourceAdapterImpl;
+import org.apache.felix.dm.impl.ResourceAdapterServiceImpl;
import org.apache.felix.dm.impl.ServiceImpl;
import org.apache.felix.dm.impl.dependencies.BundleDependencyImpl;
import org.apache.felix.dm.impl.dependencies.ConfigurationDependencyImpl;
@@ -213,7 +213,7 @@
*
* <blockquote>
* manager.createAdapterService(AdapteeService.class, "(foo=bar)")
- * // The the interface to use when registering adapters
+ * // The interface to use when registering adapter
* .setInterface(AdapterService.class, new Hashtable() {{ put("additional", "properties"); }})
* // the implementation of the adapter
* .setImplementation(AdapterImpl.class);
@@ -237,35 +237,29 @@
* It will also inherit all dependencies, and if you declare the original
* service as a member it will be injected.
*
+ * <h3>Usage Example</h3>
+ *
+ * <blockquote>
+ * manager.createResourceAdapterService("(&(path=/test)(repository=TestRepository))", true)
+ * // The interface to use when registering adapter
+ * .setInterface(AdapterService.class, new Hashtable() {{ put("foo", "bar"); }})
+ * // the implementation of the adapter
+ * .setImplementation(AdapterServiceImpl.class)
+ * // The callback invoked on adapter lifecycle events
+ * .setCallbacks(new Handler(), "init", "start", "stop", "destroy");
+ * <pre>
+ * </pre>
+ * </blockquote>
+ *
* @param resourceFilter the filter condition to use with the resource
- * @param adapterHandler a handler to invoke life cycle methods on for the adapter instance, or <code>null</code>
- * if you want to invoke these methods on the instance
- * @param adapterInterface the interface to use when registering adapters
- * @param adapterProperties additional properties to use with the adapter service registration
- * @param adapterImplementation the implementation of the adapter
* @param propagate <code>true</code> if properties from the resource should be propagated to the service
* @return a service that acts as a factory for generating resource adapters
* @see Resource
*/
- public Service createResourceAdapterService(String resourceFilter, Object adapterHandler, String adapterInterface, Dictionary adapterProperties, Object adapterImplementation, boolean propagate) {
- return createService()
- .setImplementation(new ResourceAdapterImpl(resourceFilter, adapterHandler, adapterImplementation, adapterInterface, adapterProperties, propagate))
- .add(createResourceDependency()
- .setFilter(resourceFilter)
- .setAutoConfig(false)
- .setCallbacks("added", "removed")
- );
+ public Service createResourceAdapterService(String resourceFilter, boolean propagate) {
+ return new ResourceAdapterServiceImpl(this, resourceFilter, propagate);
}
- public Service createResourceAdapterService(String resourceFilter, Object adapterHandler, String[] adapterInterface, Dictionary adapterProperties, Object adapterImplementation, boolean propagate) {
- return createService()
- .setImplementation(new ResourceAdapterImpl(resourceFilter, adapterHandler, adapterImplementation, adapterInterface, adapterProperties, propagate))
- .add(createResourceDependency()
- .setFilter(resourceFilter)
- .setAutoConfig(false)
- .setCallbacks("added", "removed")
- );
- }
-
+
/**
* Creates a new bundle adapter. The adapter will be applied to any bundle that
* matches the specified bundle state mask and filter condition. For each matching
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ResourceAdapterImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ResourceAdapterImpl.java
deleted file mode 100644
index 6d52f89..0000000
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ResourceAdapterImpl.java
+++ /dev/null
@@ -1,99 +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.impl;
-
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Properties;
-
-import org.apache.felix.dm.resources.Resource;
-import org.apache.felix.dm.service.Service;
-
-public class ResourceAdapterImpl extends AbstractDecorator {
- private volatile Service m_service;
- private final String m_resourceFilter;
- private final Object m_adapterImplementation;
- private final Object m_adapterInterface;
- private final Dictionary m_adapterProperties;
- private final boolean m_propagate;
- private final Object m_adapterHandler;
-
- public ResourceAdapterImpl(String resourceFilter, Object adapterHandler, Object adapterImplementation, String adapterInterface, Dictionary adapterProperties, boolean propagate) {
- m_resourceFilter = resourceFilter;
- m_adapterHandler = adapterHandler;
- m_adapterImplementation = adapterImplementation;
- m_adapterInterface = adapterInterface;
- m_adapterProperties = adapterProperties;
- m_propagate = propagate;
- }
-
- public ResourceAdapterImpl(String resourceFilter, Object adapterHandler, Object adapterImplementation, String[] adapterInterfaces, Dictionary adapterProperties, boolean propagate) {
- m_resourceFilter = resourceFilter;
- m_adapterHandler = adapterHandler;
- m_adapterImplementation = adapterImplementation;
- m_adapterInterface = adapterInterfaces;
- m_adapterProperties = adapterProperties;
- m_propagate = propagate;
- }
-
- public Service createService(Object[] properties) {
- Resource resource = (Resource) properties[0];
- Properties props = new Properties();
- if (m_adapterProperties != null) {
- Enumeration e = m_adapterProperties.keys();
- while (e.hasMoreElements()) {
- Object key = e.nextElement();
- props.put(key, m_adapterProperties.get(key));
- }
- }
- List dependencies = m_service.getDependencies();
- // the first dependency is always the dependency on the resource, which
- // will be replaced with a more specific dependency below
- dependencies.remove(0);
- if (m_adapterInterface instanceof String) {
- return m_manager.createService()
- .setInterface((String) m_adapterInterface, props)
- .setImplementation(m_adapterImplementation)
- .setCallbacks(m_adapterHandler, "init", "start", "stop", "destroy")
- .add(dependencies)
- .add(m_manager.createResourceDependency()
- .setResource(resource)
- .setPropagate(m_propagate)
- .setCallbacks(null, "changed", null)
- .setAutoConfig(true)
- .setRequired(true)
- );
- }
- else {
- return m_manager.createService()
- .setInterface((String[]) m_adapterInterface, props)
- .setImplementation(m_adapterImplementation)
- .setCallbacks(m_adapterHandler, "init", "start", "stop", "destroy")
- .add(dependencies)
- .add(m_manager.createResourceDependency()
- .setResource(resource)
- .setPropagate(m_propagate)
- .setCallbacks(null, "changed", null)
- .setAutoConfig(true)
- .setRequired(true)
- );
- }
- }
-}
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
new file mode 100644
index 0000000..9f7f1cc
--- /dev/null
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ResourceAdapterServiceImpl.java
@@ -0,0 +1,86 @@
+/*
+ * 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.impl;
+
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.resources.Resource;
+import org.apache.felix.dm.service.Service;
+
+/**
+ * Resource adapter service implementation. This class extends the FilterService in order to catch
+ * some Service methods for configuring actual resource adapter service implementation.
+ */
+public class ResourceAdapterServiceImpl extends FilterService
+{
+ /**
+ * Creates a new Resource Adapter Service implementation.
+ * @param dm the dependency manager used to create our internal adapter service
+ */
+ public ResourceAdapterServiceImpl(DependencyManager dm, String resourceFilter, boolean propagate) {
+ super(dm.createService()); // This service will be filtered by our super class, allowing us to take control.
+ m_service.setImplementation(new ResourceAdapterImpl(resourceFilter, propagate))
+ .add(dm.createResourceDependency()
+ .setFilter(resourceFilter)
+ .setAutoConfig(false)
+ .setCallbacks("added", "removed"));
+ }
+
+ public class ResourceAdapterImpl extends AbstractDecorator {
+ private final String m_resourceFilter;
+ private final boolean m_propagate;
+
+ public ResourceAdapterImpl(String resourceFilter, boolean propagate) {
+ m_resourceFilter = resourceFilter;
+ m_propagate = propagate;
+ }
+
+ public Service createService(Object[] properties) {
+ Resource resource = (Resource) properties[0];
+ Properties props = new Properties();
+ if (m_serviceProperties != null) {
+ Enumeration e = m_serviceProperties.keys();
+ while (e.hasMoreElements()) {
+ Object key = e.nextElement();
+ props.put(key, m_serviceProperties.get(key));
+ }
+ }
+ List dependencies = m_service.getDependencies();
+ // the first dependency is always the dependency on the resource, which
+ // will be replaced with a more specific dependency below
+ dependencies.remove(0);
+ return m_manager.createService()
+ .setInterface(m_serviceInterfaces, props)
+ .setImplementation(m_serviceImpl)
+ .setFactory(m_factory, m_factoryCreateMethod) // if not set, no effect
+ .setComposition(m_compositionInstance, m_compositionMethod) // if not set, no effect
+ .setCallbacks(m_callbackObject, m_init, m_start, m_stop, m_destroy) // if not set, no effect
+ .add(dependencies)
+ .add(m_manager.createResourceDependency()
+ .setResource(resource)
+ .setPropagate(m_propagate)
+ .setCallbacks(null, "changed", null)
+ .setAutoConfig(true)
+ .setRequired(true));
+ }
+ }
+}
diff --git a/dependencymanager/runtime/src/main/java/org/apache/felix/dm/runtime/ResourceAdapterServiceBuilder.java b/dependencymanager/runtime/src/main/java/org/apache/felix/dm/runtime/ResourceAdapterServiceBuilder.java
index e172f30..f281b1e 100644
--- a/dependencymanager/runtime/src/main/java/org/apache/felix/dm/runtime/ResourceAdapterServiceBuilder.java
+++ b/dependencymanager/runtime/src/main/java/org/apache/felix/dm/runtime/ResourceAdapterServiceBuilder.java
@@ -46,7 +46,9 @@
String[] service = serviceMetaData.getStrings(Params.service, null);
Dictionary<String, Object> properties = serviceMetaData.getDictionary(Params.properties, null);
boolean propagate = "true".equals(serviceMetaData.getString(Params.propagate, "false"));
- Service srv = dm.createResourceAdapterService(filter, null, service, properties, impl, propagate);
+ Service srv = dm.createResourceAdapterService(filter, propagate)
+ .setInterface(service, properties)
+ .setImplementation(impl);
setCommonServiceParams(srv, serviceMetaData);
for (MetaData dependencyMetaData: serviceDependencies)
{
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 9deeb81..a836bf0 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
@@ -64,11 +64,8 @@
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))",
- null,
- (String) null, null,
- new ResourceAdapter(e),
- false));
+ m.add(m.createResourceAdapterService("(&(path=/test)(name=*.txt)(repository=TestRepository))", false)
+ .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);
}