reworked the Adapter API in order to allow to provide adapter parameters by reusing the Service methods (setInterface, setImplementation, etc ...)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@947476 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 57ee254..10f396e 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
@@ -161,11 +161,8 @@
return m_manager.createAspectService(serviceInterface, serviceFilter, ranking, attributeName);
}
- public Service createAdapterService(Class serviceInterface, String serviceFilter, String adapterInterface, Object adapterImplementation, Dictionary adapterProperties) {
- return m_manager.createAdapterService(serviceInterface, serviceFilter, adapterInterface, adapterImplementation, adapterProperties);
- }
- public Service createAdapterService(Class serviceInterface, String serviceFilter, String[] adapterInterface, Object adapterImplementation, Dictionary adapterProperties) {
- return m_manager.createAdapterService(serviceInterface, serviceFilter, adapterInterface, adapterImplementation, adapterProperties);
+ public Service createAdapterService(Class serviceInterface, String serviceFilter) {
+ return m_manager.createAdapterService(serviceInterface, serviceFilter);
}
public Service createResourceAdapter(String resourceFilter, Object adapterHandler, String adapterInterface, Dictionary adapterProperties, Object adapterImplementation, 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 aeb5e3e..c911461 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
@@ -30,7 +30,7 @@
import org.apache.felix.dm.dependencies.ResourceDependency;
import org.apache.felix.dm.dependencies.ServiceDependency;
import org.apache.felix.dm.dependencies.TemporalServiceDependency;
-import org.apache.felix.dm.impl.AdapterImpl;
+import org.apache.felix.dm.impl.AdapterServiceImpl;
import org.apache.felix.dm.impl.AspectServiceImpl;
import org.apache.felix.dm.impl.BundleAdapterImpl;
import org.apache.felix.dm.impl.FactoryConfigurationAdapterImpl;
@@ -209,33 +209,25 @@
* 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.createAdapterService(AdapteeService.class, "(foo=bar)")
+ * // The the interface to use when registering adapters
+ * .setInterface(AdapterService.class, new Hashtable() {{ put("additional", "properties"); }})
+ * // the implementation of the adapter
+ * .setImplementation(AdapterImpl.class);
+ * <pre>
+ * </pre>
+ * </blockquote>
* @param serviceInterface the service interface to apply the adapter to
* @param serviceFilter the filter condition to use with the service interface
- * @param adapterInterface the interface to use when registering adapters
- * @param adapterImplementation the implementation of the adapter
- * @param adapterProperties additional properties to use with the adapter service registration
* @return a service that acts as a factory for generating adapters
*/
- public Service createAdapterService(Class serviceInterface, String serviceFilter, String adapterInterface, Object adapterImplementation, Dictionary adapterProperties) {
- return createService()
- .setImplementation(new AdapterImpl(serviceInterface, serviceFilter, adapterImplementation, adapterInterface, adapterProperties))
- .add(createServiceDependency()
- .setService(serviceInterface)
- .setAutoConfig(false)
- .setCallbacks("added", "removed")
- );
+ public Service createAdapterService(Class serviceInterface, String serviceFilter) {
+ return new AdapterServiceImpl(this, serviceInterface, serviceFilter);
}
-
- public Service createAdapterService(Class serviceInterface, String serviceFilter, String[] adapterInterface, Object adapterImplementation, Dictionary adapterProperties) {
- return createService()
- .setImplementation(new AdapterImpl(serviceInterface, serviceFilter, adapterImplementation, adapterInterface, adapterProperties))
- .add(createServiceDependency()
- .setService(serviceInterface)
- .setAutoConfig(false)
- .setCallbacks("added", "removed")
- );
- }
-
+
/**
* Creates a new resource adapter. The adapter will be applied to any resource that
* matches the specified filter condition. For each matching resource
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AdapterImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AdapterImpl.java
deleted file mode 100644
index d79023b..0000000
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AdapterImpl.java
+++ /dev/null
@@ -1,91 +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.service.Service;
-import org.osgi.framework.ServiceReference;
-
-public class AdapterImpl extends AbstractDecorator {
- private volatile Service m_service;
- private final Class m_serviceInterface;
- private final String m_serviceFilter;
- private final Object m_adapterImplementation;
- private final Object m_adapterInterface;
- private final Dictionary m_adapterProperties;
-
- public AdapterImpl(Class serviceInterface, String serviceFilter, Object adapterImplementation, String adapterInterface, Dictionary adapterProperties) {
- m_serviceInterface = serviceInterface;
- m_serviceFilter = serviceFilter;
- m_adapterImplementation = adapterImplementation;
- m_adapterInterface = adapterInterface;
- m_adapterProperties = adapterProperties;
- }
-
- public AdapterImpl(Class serviceInterface, String serviceFilter, Object adapterImplementation, String[] adapterInterfaces, Dictionary adapterProperties) {
- m_serviceInterface = serviceInterface;
- m_serviceFilter = serviceFilter;
- m_adapterImplementation = adapterImplementation;
- m_adapterInterface = adapterInterfaces;
- m_adapterProperties = adapterProperties;
- }
-
- public Service createService(Object[] properties) {
- ServiceReference ref = (ServiceReference) properties[0];
- Object service = properties[1];
- Properties props = new Properties();
- String[] keys = ref.getPropertyKeys();
- for (int i = 0; i < keys.length; i++) {
- props.put(keys[i], ref.getProperty(keys[i]));
- }
- 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();
- dependencies.remove(0);
- if (m_adapterInterface instanceof String) {
- return m_manager.createService()
- .setInterface((String) m_adapterInterface, props)
- .setImplementation(m_adapterImplementation)
- .add(dependencies)
- .add(m_manager.createServiceDependency()
- .setService(m_serviceInterface, ref)
- .setRequired(true)
- );
- }
- else {
- return m_manager.createService()
- .setInterface((String[]) m_adapterInterface, props)
- .setImplementation(m_adapterImplementation)
- .add(dependencies)
- .add(m_manager.createServiceDependency()
- .setService(m_serviceInterface, ref)
- .setRequired(true)
- );
- }
- }
-}
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AdapterServiceImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AdapterServiceImpl.java
new file mode 100644
index 0000000..adaa22f
--- /dev/null
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/AdapterServiceImpl.java
@@ -0,0 +1,88 @@
+/*
+ * 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.service.Service;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Adapter Service implementation. This class extends the FilterService in order to catch
+ * some Service methods for configuring actual adapter service implementation.
+ */
+public class AdapterServiceImpl extends FilterService
+{
+ /**
+ * Creates a new Adapter Service implementation.
+ * @param dm the dependency manager used to create our internal adapter service
+ * @param adapteeInterface the service interface to apply the adapter to
+ * @param adapteeFilter the filter condition to use with the service interface
+ */
+ public AdapterServiceImpl(DependencyManager dm, Class adapteeInterface, String adapteeFilter)
+ {
+ super(dm.createService()); // This service will be filtered by our super class, allowing us to take control.
+ m_service.setImplementation(new AdapterImpl(adapteeInterface, adapteeFilter))
+ .add(dm.createServiceDependency()
+ .setService(adapteeInterface)
+ .setAutoConfig(false)
+ .setCallbacks("added", "removed"));
+ }
+
+ public class AdapterImpl extends AbstractDecorator {
+ private final Class m_adapteeInterface;
+ private final String m_adapteeFilter;
+
+ public AdapterImpl(Class adapteeInterface, String adapteeFilter) {
+ m_adapteeInterface = adapteeInterface;
+ m_adapteeFilter = adapteeFilter;
+ }
+
+ public Service createService(Object[] properties) {
+ ServiceReference ref = (ServiceReference) properties[0];
+ Object service = properties[1];
+ Properties props = new Properties();
+ String[] keys = ref.getPropertyKeys();
+ for (int i = 0; i < keys.length; i++) {
+ props.put(keys[i], ref.getProperty(keys[i]));
+ }
+ 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();
+ 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.createServiceDependency()
+ .setService(m_adapteeInterface, ref)
+ .setRequired(true));
+ }
+ }
+}