diff options
Diffstat (limited to 'java/sca/modules/node-impl-osgi/src')
23 files changed, 374 insertions, 222 deletions
diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java index 126ffd941a..f03f1e266a 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java @@ -21,9 +21,9 @@ package org.apache.tuscany.sca.node.osgi.impl; import static org.apache.tuscany.sca.node.osgi.impl.NodeManager.isSCABundle; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.TopologyManagerImpl; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.RemoteServiceAdminImpl; import org.apache.tuscany.sca.osgi.service.discovery.impl.DiscoveryActivator; -import org.apache.tuscany.sca.osgi.service.remoteadmin.impl.RemoteServiceAdminImpl; -import org.apache.tuscany.sca.osgi.service.remoteadmin.impl.RemoteControllerImpl; import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -40,7 +40,7 @@ public class NodeActivator implements BundleActivator, SynchronousBundleListener private DiscoveryActivator discoveryActivator = new DiscoveryActivator(); private RemoteServiceAdminImpl remoteAdmin; - private RemoteControllerImpl controller; + private TopologyManagerImpl controller; private void init() { synchronized (this) { @@ -63,7 +63,7 @@ public class NodeActivator implements BundleActivator, SynchronousBundleListener remoteAdmin = new RemoteServiceAdminImpl(context); remoteAdmin.start(); - controller = new RemoteControllerImpl(context); + controller = new TopologyManagerImpl(context); controller.start(); // exporter = new OSGiServiceExporter(context); diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/EndpointDescription.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/EndpointDescription.java index 0486f14517..23a91b89ef 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/EndpointDescription.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/EndpointDescription.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.tuscany.sca.osgi.service.remoteadmin; +package org.apache.tuscany.sca.osgi.remoteserviceadmin; import java.util.Arrays; import java.util.Collections; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/EndpointListener.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/EndpointListener.java index 33f9b143d8..94c08b7632 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/EndpointListener.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/EndpointListener.java @@ -1,4 +1,4 @@ -package org.apache.tuscany.sca.osgi.service.remoteadmin; +package org.apache.tuscany.sca.osgi.remoteserviceadmin; /** * A white board service that represents a listener for endpoints. diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/EndpointPermission.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/EndpointPermission.java index 48a817b005..4aca1ffb9c 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/EndpointPermission.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/EndpointPermission.java @@ -1,4 +1,4 @@ -package org.apache.tuscany.sca.osgi.service.remoteadmin; +package org.apache.tuscany.sca.osgi.remoteserviceadmin; // TODO Hacked from ServiePermission diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/ExportRegistration.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/ExportRegistration.java index 0f1805131c..a2d6f58a3b 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/ExportRegistration.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/ExportRegistration.java @@ -1,4 +1,4 @@ -package org.apache.tuscany.sca.osgi.service.remoteadmin; +package org.apache.tuscany.sca.osgi.remoteserviceadmin; import org.osgi.framework.ServiceReference; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/ImportRegistration.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/ImportRegistration.java index c943380068..ae9726e798 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/ImportRegistration.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/ImportRegistration.java @@ -1,4 +1,4 @@ -package org.apache.tuscany.sca.osgi.service.remoteadmin; +package org.apache.tuscany.sca.osgi.remoteserviceadmin; import org.osgi.framework.ServiceReference; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteConstants.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/RemoteConstants.java index e45a600347..c3f16aecb7 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteConstants.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/RemoteConstants.java @@ -1,4 +1,4 @@ -package org.apache.tuscany.sca.osgi.service.remoteadmin; +package org.apache.tuscany.sca.osgi.remoteserviceadmin; /** * Provide the definition of the constants used in the Remote Services API. diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteServiceAdmin.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/RemoteServiceAdmin.java index 627780b46f..09ad0635f3 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteServiceAdmin.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/RemoteServiceAdmin.java @@ -1,4 +1,4 @@ -package org.apache.tuscany.sca.osgi.service.remoteadmin; +package org.apache.tuscany.sca.osgi.remoteserviceadmin; import java.util.Collection; import java.util.List; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteServiceAdminEvent.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/RemoteServiceAdminEvent.java index ad2e0c4057..bdb083b940 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteServiceAdminEvent.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/RemoteServiceAdminEvent.java @@ -1,4 +1,4 @@ -package org.apache.tuscany.sca.osgi.service.remoteadmin; +package org.apache.tuscany.sca.osgi.remoteserviceadmin; import org.osgi.framework.Bundle; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteServiceAdminListener.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/RemoteServiceAdminListener.java index f653c323c0..28a5958704 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteServiceAdminListener.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/RemoteServiceAdminListener.java @@ -1,4 +1,4 @@ -package org.apache.tuscany.sca.osgi.service.remoteadmin; +package org.apache.tuscany.sca.osgi.remoteserviceadmin; /** * A Remote Service Admin Listener is notified asynchronously of any export or diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/EndpointHelper.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java index a372b62a6c..8ac949ccc5 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/EndpointHelper.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java @@ -17,9 +17,8 @@ * under the License. */ -package org.apache.tuscany.sca.osgi.service.remoteadmin.impl; +package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; -import java.net.URL; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -29,8 +28,8 @@ import java.util.UUID; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.interfacedef.Interface; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; -import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteConstants; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteConstants; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; @@ -38,8 +37,7 @@ import org.osgi.framework.Constants; * Implementation of {@link EndpointDescription} */ public class EndpointHelper { - private static final String FRAMEWORK_UUID = UUID.randomUUID().toString(); - + private final static String FRAMEWORK_UUID = "org.osgi.framework.uuid"; private EndpointHelper() { } @@ -68,14 +66,12 @@ public class EndpointHelper { return props; } - public static String getFrameworkUUID(BundleContext bundleContext) { - String uuid = null; - if (bundleContext != null) { - URL url = bundleContext.getBundle(0).getEntry("/"); // bundleentry://0.fwk24942249/ - uuid = url.getHost(); - } else { - uuid = FRAMEWORK_UUID; + public synchronized static String getFrameworkUUID(BundleContext bundleContext) { + String uuid = System.getProperty(FRAMEWORK_UUID); + if (uuid == null) { + uuid = UUID.randomUUID().toString(); } + System.setProperty(FRAMEWORK_UUID, uuid); return uuid; } diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/EndpointIntrospector.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java index 6ce012ba2a..6adb1701c8 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/EndpointIntrospector.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.tuscany.sca.osgi.service.remoteadmin.impl; +package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; import static org.apache.tuscany.sca.assembly.Base.SCA11_TUSCANY_NS; import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SCA_BINDINGS; @@ -33,11 +33,14 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import javax.xml.namespace.QName; @@ -78,11 +81,12 @@ import org.apache.tuscany.sca.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; import org.apache.tuscany.sca.monitor.MonitorFactory; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; import org.apache.tuscany.sca.policy.Intent; import org.apache.tuscany.sca.policy.PolicyFactory; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; /** @@ -168,17 +172,50 @@ public class EndpointIntrospector { return intentList; } - private Map<String, Object> getProperties(ServiceReference reference) { + /** + * Any property in the map overrides the service reference properties, regardless of + * case. That is, if the map contains a key then it will override any case variant + * of this key in the Service Reference.<p> + * If the map contains the objectClass or service. id property key in any case + * variant, then these properties must not override the Service References value. This + * implies that the map can provide the service.exported. interfaces, property allowing + * the Topology Manager to export any registered service, also services not specifically + * marked to be exported. + * @param reference + * @param props + * @return + */ + private Map<String, Object> getProperties(ServiceReference reference, Map<String, Object> props) { String[] names = reference.getPropertyKeys(); + Map<String, Object> properties = new HashMap<String, Object>(); if (names != null) { - Map<String, Object> properties = new HashMap<String, Object>(); for (String name : names) { properties.put(name, reference.getProperty(name)); } - return properties; - } else { - return Collections.emptyMap(); } + if (props != null) { + // Create a map of names (key = lowcase name, value = name) + Map<String, String> nameMap = new HashMap<String, String>(); + if (names != null) { + for (String name : names) { + nameMap.put(name.toLowerCase(), name); + } + } + for (Map.Entry<String, Object> p : props.entrySet()) { + if (Constants.OBJECTCLASS.equalsIgnoreCase(p.getKey())) { + throw new IllegalArgumentException(Constants.OBJECTCLASS + " property cannot be overridden."); + } else if (Constants.SERVICE_ID.equalsIgnoreCase(p.getKey())) { + throw new IllegalArgumentException(Constants.SERVICE_ID + " property cannot be overridden."); + } + String key = nameMap.get(p.getKey().toLowerCase()); + if (key != null) { + properties.put(key, p.getValue()); + } else { + properties.put(p.getKey(), p.getValue()); + } + } + } + return properties; } /** @@ -204,11 +241,12 @@ public class EndpointIntrospector { * Introspect a local OSGi Service represented by the ServiceReference to create * an SCA service with the required intents and bindings * @param reference The service reference for a local OSGi service + * @param props Addiontal properties * @return An SCA contribution with a deployable composite for the SCA service * @throws Exception */ - public Contribution introspect(ServiceReference reference) throws Exception { - Map<String, Object> properties = getProperties(reference); + public Contribution introspect(ServiceReference reference, Map<String, Object> props) throws Exception { + Map<String, Object> properties = getProperties(reference, props); OSGiProperty serviceID = implementationFactory.createOSGiProperty(); serviceID.setName(SERVICE_ID); @@ -237,6 +275,12 @@ public class EndpointIntrospector { remoteInterfaces = getStrings(reference.getProperty(OBJECTCLASS)); } else { remoteInterfaces = parse(remoteInterfaces); + String[] objectClasses = getStrings(reference.getProperty(OBJECTCLASS)); + Set<String> objectClassSet = new HashSet<String>(Arrays.asList(objectClasses)); + if (!objectClassSet.containsAll(Arrays.asList(remoteInterfaces))) { + throw new IllegalArgumentException("The exported interfaces are not a subset of the types" + + " listed in the objectClass service property from the Service Reference"); + } } for (String intf : remoteInterfaces) { Service service = assemblyFactory.createService(); diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/ExportRegistrationImpl.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportRegistrationImpl.java index d7881681f0..58d72f389f 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/ExportRegistrationImpl.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportRegistrationImpl.java @@ -17,11 +17,11 @@ * under the License. */ -package org.apache.tuscany.sca.osgi.service.remoteadmin.impl; +package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; import org.apache.tuscany.sca.node.Node; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; -import org.apache.tuscany.sca.osgi.service.remoteadmin.ExportRegistration; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.ExportRegistration; import org.osgi.framework.ServiceReference; /** @@ -58,7 +58,7 @@ public class ExportRegistrationImpl implements ExportRegistration { } /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.ExportRegistration#close() + * @see org.apache.tuscany.sca.osgi.remoteserviceadmin.ExportRegistration#close() */ public void close() { if (node != null) { diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/ImportRegistrationImpl.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportRegistrationImpl.java index fb145a95d0..d085bd282d 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/ImportRegistrationImpl.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportRegistrationImpl.java @@ -17,11 +17,11 @@ * under the License. */ -package org.apache.tuscany.sca.osgi.service.remoteadmin.impl; +package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; import org.apache.tuscany.sca.node.Node; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; -import org.apache.tuscany.sca.osgi.service.remoteadmin.ImportRegistration; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.ImportRegistration; import org.osgi.framework.ServiceReference; /** @@ -61,7 +61,7 @@ public class ImportRegistrationImpl implements ImportRegistration { } /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.ImportRegistration#close() + * @see org.apache.tuscany.sca.osgi.remoteserviceadmin.ImportRegistration#close() */ public void close() { if (node != null) { diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiHelper.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java index dc69a5c232..856cab3412 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiHelper.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.tuscany.sca.osgi.service.remoteadmin.impl; +package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; import java.util.Arrays; import java.util.Collection; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiServiceExporter.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java index bfe024e746..ab7aff34c0 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiServiceExporter.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java @@ -17,13 +17,14 @@ * under the License. */ -package org.apache.tuscany.sca.osgi.service.remoteadmin.impl; +package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; -import static org.apache.tuscany.sca.osgi.service.remoteadmin.impl.EndpointHelper.createEndpointDescription; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.EndpointHelper.createEndpointDescription; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import org.apache.tuscany.sca.assembly.Component; import org.apache.tuscany.sca.assembly.ComponentService; @@ -35,8 +36,8 @@ import org.apache.tuscany.sca.node.NodeFactory; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; import org.apache.tuscany.sca.node.impl.NodeFactoryImpl; import org.apache.tuscany.sca.node.impl.NodeImpl; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; -import org.apache.tuscany.sca.osgi.service.remoteadmin.ExportRegistration; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.ExportRegistration; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.util.tracker.ServiceTracker; @@ -77,12 +78,13 @@ public class OSGiServiceExporter implements ServiceTrackerCustomizer, LifeCycleL } public Object addingService(ServiceReference reference) { - return exportService(reference); + return exportService(reference, null); } - public List<ExportRegistration> exportService(ServiceReference reference) { + public List<ExportRegistration> exportService(ServiceReference reference, Map<String, Object> properties) { + // FIXME: [rfeng] We need to check if a corresponding endpoint has been exported try { - Contribution contribution = introspector.introspect(reference); + Contribution contribution = introspector.introspect(reference, properties); if (contribution != null) { NodeConfiguration configuration = nodeFactory.createNodeConfiguration(); @@ -112,7 +114,7 @@ public class OSGiServiceExporter implements ServiceTrackerCustomizer, LifeCycleL public void modifiedService(ServiceReference reference, Object service) { removedService(reference, service); - exportService(reference); + exportService(reference, null); } public void removedService(ServiceReference reference, Object service) { diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiServiceImporter.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java index e42baab02d..aeaaf005c2 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiServiceImporter.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.tuscany.sca.osgi.service.remoteadmin.impl; +package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; import java.util.Collections; @@ -31,8 +31,8 @@ import org.apache.tuscany.sca.node.NodeFactory; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; import org.apache.tuscany.sca.node.impl.NodeFactoryImpl; import org.apache.tuscany.sca.node.impl.NodeImpl; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; -import org.apache.tuscany.sca.osgi.service.remoteadmin.ImportRegistration; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.ImportRegistration; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java new file mode 100644 index 0000000000..698fbb2942 --- /dev/null +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java @@ -0,0 +1,252 @@ +/* + * 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.tuscany.sca.osgi.remoteserviceadmin.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.ExportRegistration; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.ImportRegistration; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdmin; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdminEvent; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdminListener; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.event.Event; +import org.osgi.service.event.EventAdmin; +import org.osgi.util.tracker.ServiceTracker; + +/** + * SCA Implementation of {@link RemoteServiceAdmin} + */ +public class RemoteServiceAdminImpl implements RemoteServiceAdmin { + private BundleContext context; + private ServiceRegistration registration; + private ServiceTracker listeners; + + private OSGiServiceExporter exporter; + private OSGiServiceImporter importer; + + private Collection<ImportRegistration> importedEndpoints = new ArrayList<ImportRegistration>(); + private Collection<ExportRegistration> exportedServices = new ArrayList<ExportRegistration>(); + + public RemoteServiceAdminImpl(BundleContext context) { + this.context = context; + } + + public void start() { + this.exporter = new OSGiServiceExporter(context); + this.importer = new OSGiServiceImporter(context); + exporter.start(); + importer.start(); + registration = context.registerService(RemoteServiceAdmin.class.getName(), this, null); + listeners = new ServiceTracker(this.context, RemoteServiceAdminListener.class.getName(), null); + listeners.open(); + } + + public void stop() { + if (registration != null) { + registration.unregister(); + registration = null; + } + if (listeners != null) { + listeners.close(); + listeners = null; + } + for (ExportRegistration exportRegistration : exportedServices) { + exportRegistration.close(); + } + exportedServices.clear(); + for (ImportRegistration importRegistration : importedEndpoints) { + importRegistration.close(); + } + importedEndpoints.clear(); + if (importer != null) { + importer.stop(); + importer = null; + } + if (exporter != null) { + exporter.stop(); + exporter = null; + } + } + + /** + * @see org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdmin#exportService(org.osgi.framework.ServiceReference, + * java.util.Map) + */ + public List<ExportRegistration> exportService(ServiceReference ref, Map properties) { + List<ExportRegistration> exportRegistrations = exporter.exportService(ref, properties); + if (exportRegistrations != null) { + exportedServices.addAll(exportRegistrations); + fireExportEvents(ref.getBundle(), exportRegistrations); + } + return exportRegistrations; + } + + private void fireExportEvents(Bundle source, List<ExportRegistration> exportRegistrations) { + for (ExportRegistration registration : exportRegistrations) { + RemoteServiceAdminEvent rsaEvent = + new RemoteServiceAdminEvent(RemoteServiceAdminEvent.EXPORT_REGISTRATION, source, registration, + registration.getException()); + EventAdmin eventAdmin = getEventAdmin(); + if (eventAdmin != null) { + eventAdmin.postEvent(wrap(rsaEvent)); + } + for (Object listener : listeners.getServices()) { + RemoteServiceAdminListener rsaListener = (RemoteServiceAdminListener)listener; + rsaListener.remoteAdminEvent(rsaEvent); + } + } + } + + private EventAdmin getEventAdmin() { + ServiceReference reference = context.getServiceReference(EventAdmin.class.getName()); + if (reference == null) { + return null; + } else { + return (EventAdmin)context.getService(reference); + } + } + + private Event wrap(RemoteServiceAdminEvent rsaEvent) { + int type = rsaEvent.getType(); + String eventType = null; + switch (type) { + case RemoteServiceAdminEvent.EXPORT_ERROR: + eventType = "EXPORT_ERROR"; + break; + case RemoteServiceAdminEvent.EXPORT_REGISTRATION: + eventType = "EXPORT_REGISTRATION"; + break; + case RemoteServiceAdminEvent.EXPORT_UNREGISTRATION: + eventType = "EXPORT_UNREGISTRATION"; + break; + case RemoteServiceAdminEvent.EXPORT_WARNING: + eventType = "EXPORT_WARNING"; + break; + case RemoteServiceAdminEvent.IMPORT_ERROR: + eventType = "IMPORT_ERROR"; + break; + case RemoteServiceAdminEvent.IMPORT_REGISTRATION: + eventType = "IMPORT_REGISTRATION"; + break; + case RemoteServiceAdminEvent.IMPORT_UNREGISTRATION: + eventType = "EXPORT_ERROR"; + break; + case RemoteServiceAdminEvent.IMPORT_WARNING: + eventType = "IMPORT_UNREGISTRATION"; + break; + } + String topic = "org/osgi/service/remoteserviceadmin/" + eventType; + Map<String, Object> props = new HashMap<String, Object>(); + /* + * <ul> + <li>bundle (Bundle) The Remote Service Admin bundle + <li>bundle-id (Long) The id of the Blueprint bundle. + <li>bundle-symbolicname (String) The Bundle Symbolic Name of the + Remote Service Admin bundle. + <li>bundle-version - (Version) The version of the Blueprint bundle. + <li>cause The exception, if present. + <li>import.registration An imported endpoint, if present + <li>export.registration An exported endpoint, if present + <li>service.remote.id Remote service UUID, if present + <li>service.remote.uuid Remote service UUID, if present + <li>service.remote.uri (String) The URI of the endpoint, if present + <li>objectClass (String[]) The interface names, if present + <li>service.imported.configs (String+) The configuration types of the + imported services, if present + <li>timestamp (Long) The time when the event occurred + <li>event (RemoteServiceAdminEvent) The RemoteServiceAdminEvent + object that caused this event. + </ul> + */ + Bundle rsaBundle = context.getBundle(); + props.put("bundle", rsaBundle); + props.put("bundle-id", rsaBundle.getBundleId()); + props.put("bundle-symbolicname", rsaBundle.getSymbolicName()); + props.put("bundle-version", rsaBundle.getHeaders().get(Constants.BUNDLE_VERSION)); + props.put("cause", rsaEvent.getException()); + props.put("import.registration", rsaEvent.getImportRegistration()); + props.put("export.registration", rsaEvent.getExportRegistration()); + EndpointDescription ep = null; + if (rsaEvent.getImportRegistration() != null) { + ep = rsaEvent.getImportRegistration().getImportedEndpointDescription(); + } else { + ep = rsaEvent.getExportRegistration().getEndpointDescription(); + } + props.put("service.remote.id", ep.getRemoteServiceID()); + props.put("service.remote.uuid", ep.getRemoteFrameworkUUID()); + props.put("service.remote.uri", ep.getRemoteURI()); + props.put("objectClass", ep.getInterfaces()); + props.put("service.imported.configs", ep.getConfigurationTypes()); + props.put("timestamp", new Long(System.currentTimeMillis())); + props.put("event", rsaEvent); + return new Event(topic, props); + } + + private void fireImportEvents(Bundle source, ImportRegistration registration) { + RemoteServiceAdminEvent rsaEvent = + new RemoteServiceAdminEvent(RemoteServiceAdminEvent.IMPORT_REGISTRATION, source, registration, registration + .getException()); + EventAdmin eventAdmin = getEventAdmin(); + if (eventAdmin != null) { + eventAdmin.postEvent(wrap(rsaEvent)); + } + for (Object listener : listeners.getServices()) { + RemoteServiceAdminListener rsaListener = (RemoteServiceAdminListener)listener; + rsaListener.remoteAdminEvent(rsaEvent); + } + } + + /** + * @see org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdmin#getExportedServices() + */ + public Collection<ExportRegistration> getExportedServices() { + return exportedServices; + } + + /** + * @see org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdmin#getImportedEndpoints() + */ + public Collection<ImportRegistration> getImportedEndpoints() { + return importedEndpoints; + } + + /** + * @see org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdmin#importService(org.apache.tuscany.sca.dosgi.discovery.EndpointDescription) + */ + public ImportRegistration importService(EndpointDescription endpoint) { + Bundle bundle = (Bundle)endpoint.getProperties().get(Bundle.class.getName()); + ImportRegistration importReg = importer.importService(bundle, endpoint); + if (importReg != null) { + fireImportEvents(bundle, importReg); + importedEndpoints.add(importReg); + } + return importReg; + } +} diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteControllerImpl.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/TopologyManagerImpl.java index bc1cf86055..7c7a873d82 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteControllerImpl.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/TopologyManagerImpl.java @@ -17,11 +17,11 @@ * under the License. */ -package org.apache.tuscany.sca.osgi.service.remoteadmin.impl; +package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SERVICE_EXPORTED_INTERFACES; -import static org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteConstants.SERVICE_EXPORTED_CONFIGS; -import static org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteConstants.SERVICE_IMPORTED; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_CONFIGS; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteConstants.SERVICE_IMPORTED; import java.util.Collection; import java.util.Dictionary; @@ -37,13 +37,13 @@ import java.util.logging.Logger; import org.apache.tuscany.sca.common.java.collection.CollectionMap; import org.apache.tuscany.sca.core.LifeCycleListener; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointListener; -import org.apache.tuscany.sca.osgi.service.remoteadmin.ExportRegistration; -import org.apache.tuscany.sca.osgi.service.remoteadmin.ImportRegistration; -import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin; -import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdminEvent; -import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdminListener; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointListener; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.ExportRegistration; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.ImportRegistration; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdmin; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdminEvent; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteServiceAdminListener; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; @@ -58,9 +58,9 @@ import org.osgi.util.tracker.ServiceTrackerCustomizer; /** * Implementation of Remote Controller */ -public class RemoteControllerImpl implements ListenerHook, RemoteServiceAdminListener, EndpointListener, +public class TopologyManagerImpl implements ListenerHook, RemoteServiceAdminListener, EndpointListener, ServiceTrackerCustomizer, LifeCycleListener /*, EventHook */{ - private final static Logger logger = Logger.getLogger(RemoteControllerImpl.class.getName()); + private final static Logger logger = Logger.getLogger(TopologyManagerImpl.class.getName()); public final static String ENDPOINT_LOCAL = "service.local"; private BundleContext context; @@ -81,7 +81,7 @@ public class RemoteControllerImpl implements ListenerHook, RemoteServiceAdminLis private Filter remotableServiceFilter; - public RemoteControllerImpl(BundleContext context) { + public TopologyManagerImpl(BundleContext context) { this.context = context; } @@ -312,7 +312,7 @@ public class RemoteControllerImpl implements ListenerHook, RemoteServiceAdminLis } /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointListener#addEndpoint(org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription, + * @see org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointListener#addEndpoint(org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription, * java.lang.String) */ public void endpointAdded(EndpointDescription endpoint, String matchedFilter) { @@ -320,7 +320,7 @@ public class RemoteControllerImpl implements ListenerHook, RemoteServiceAdminLis } /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointListener#removeEndpoint(org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription) + * @see org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointListener#removeEndpoint(org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription) */ public void endpointRemoved(EndpointDescription endpoint, String matchedFilter) { unimportService(endpoint); diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java index a78bfb2c3b..e798ea7a38 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java @@ -19,7 +19,7 @@ package org.apache.tuscany.sca.osgi.service.discovery.impl; -import static org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointListener.ENDPOINT_LISTENER_SCOPE; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointListener.ENDPOINT_LISTENER_SCOPE; import java.util.ArrayList; import java.util.Collection; @@ -37,10 +37,10 @@ import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.LifeCycleListener; import org.apache.tuscany.sca.node.NodeFactory; import org.apache.tuscany.sca.node.impl.NodeFactoryImpl; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointListener; -import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteConstants; -import org.apache.tuscany.sca.osgi.service.remoteadmin.impl.OSGiHelper; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointListener; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteConstants; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.OSGiHelper; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java index ae2d704015..3709c737ee 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java @@ -19,13 +19,13 @@ package org.apache.tuscany.sca.osgi.service.discovery.impl; -import static org.apache.tuscany.sca.osgi.service.remoteadmin.impl.EndpointHelper.createEndpointDescription; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.EndpointHelper.createEndpointDescription; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.Implementation; import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; import org.apache.tuscany.sca.runtime.DomainRegistryFactory; import org.apache.tuscany.sca.runtime.EndpointListener; import org.osgi.framework.BundleContext; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java index 1c13cc90fc..07e0ff07a2 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java @@ -50,9 +50,9 @@ import org.apache.tuscany.sca.implementation.osgi.ServiceDescription; import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions; import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.MonitorFactory; -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; -import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteConstants; -import org.apache.tuscany.sca.osgi.service.remoteadmin.impl.EndpointHelper; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteConstants; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.EndpointHelper; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteServiceAdminImpl.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteServiceAdminImpl.java deleted file mode 100644 index 4c92bab569..0000000000 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteServiceAdminImpl.java +++ /dev/null @@ -1,142 +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.tuscany.sca.osgi.service.remoteadmin.impl; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import org.apache.tuscany.sca.osgi.service.remoteadmin.EndpointDescription; -import org.apache.tuscany.sca.osgi.service.remoteadmin.ExportRegistration; -import org.apache.tuscany.sca.osgi.service.remoteadmin.ImportRegistration; -import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin; -import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdminListener; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; -import org.osgi.util.tracker.ServiceTracker; - -/** - * SCA Implementation of {@link RemoteServiceAdmin} - */ -public class RemoteServiceAdminImpl implements RemoteServiceAdmin { - private BundleContext context; - private ServiceRegistration registration; - private ServiceTracker listeners; - - private OSGiServiceExporter exporter; - private OSGiServiceImporter importer; - - private Collection<ImportRegistration> importedEndpoints = new ArrayList<ImportRegistration>(); - private Collection<ExportRegistration> exportedServices = new ArrayList<ExportRegistration>(); - - public RemoteServiceAdminImpl(BundleContext context) { - this.context = context; - } - - public void start() { - this.exporter = new OSGiServiceExporter(context); - this.importer = new OSGiServiceImporter(context); - exporter.start(); - importer.start(); - registration = context.registerService(RemoteServiceAdmin.class.getName(), this, null); - listeners = new ServiceTracker(this.context, RemoteServiceAdminListener.class.getName(), null); - listeners.open(); - } - - public void stop() { - if (registration != null) { - registration.unregister(); - registration = null; - } - if (listeners != null) { - listeners.close(); - listeners = null; - } - for (ExportRegistration exportRegistration : exportedServices) { - exportRegistration.close(); - } - exportedServices.clear(); - for (ImportRegistration importRegistration : importedEndpoints) { - importRegistration.close(); - } - importedEndpoints.clear(); - if (importer != null) { - importer.stop(); - importer = null; - } - if (exporter != null) { - exporter.stop(); - exporter = null; - } - } - - /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin#exportService(org.osgi.framework.ServiceReference) - */ - public List<ExportRegistration> exportService(ServiceReference ref) { - List<ExportRegistration> exportRegistrations = exporter.exportService(ref); - if (exportRegistrations != null) { - exportedServices.addAll(exportRegistrations); - } - return exportRegistrations; - } - - /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin#exportService(org.osgi.framework.ServiceReference, - * java.util.Map) - */ - public List<ExportRegistration> exportService(ServiceReference ref, Map properties) { - List<ExportRegistration> exportRegistrations = exporter.exportService(ref); - if (exportRegistrations != null) { - exportedServices.addAll(exportRegistrations); - } - return exportRegistrations; - } - - /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin#getExportedServices() - */ - public Collection<ExportRegistration> getExportedServices() { - return exportedServices; - } - - /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin#getImportedEndpoints() - */ - public Collection<ImportRegistration> getImportedEndpoints() { - return importedEndpoints; - } - - /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin#importService(org.apache.tuscany.sca.dosgi.discovery.EndpointDescription) - */ - public ImportRegistration importService(EndpointDescription endpoint) { - Bundle bundle = (Bundle) endpoint.getProperties().get(Bundle.class.getName()); - ImportRegistration importReg = importer.importService(bundle, endpoint); - if (importReg != null) { - importedEndpoints.add(importReg); - } - return importReg; - } - -} |