From 18940688871a0d2f0d98b629b0b388c4dde1cf74 Mon Sep 17 00:00:00 2001 From: rfeng Date: Thu, 13 Aug 2009 00:15:42 +0000 Subject: A few renames of the classes git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@803747 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/node/osgi/impl/NodeActivator.java | 6 +- .../discovery/impl/AbstractDiscoveryService.java | 6 +- .../sca/osgi/service/remoteadmin/RemoteAdmin.java | 101 --------------- .../service/remoteadmin/RemoteServiceAdmin.java | 101 +++++++++++++++ .../osgi/service/remoteadmin/impl/OSGiHelper.java | 86 +++++++++++++ .../remoteadmin/impl/RemoteAdminHelper.java | 86 ------------- .../service/remoteadmin/impl/RemoteAdminImpl.java | 142 --------------------- .../remoteadmin/impl/RemoteControllerImpl.java | 8 +- .../remoteadmin/impl/RemoteServiceAdminImpl.java | 142 +++++++++++++++++++++ 9 files changed, 339 insertions(+), 339 deletions(-) delete mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteAdmin.java create mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteServiceAdmin.java create mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiHelper.java delete mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteAdminHelper.java delete mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteAdminImpl.java create mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteServiceAdminImpl.java (limited to 'java/sca') 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 2684bcc2ae..126ffd941a 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 @@ -22,7 +22,7 @@ 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.service.discovery.impl.DiscoveryActivator; -import org.apache.tuscany.sca.osgi.service.remoteadmin.impl.RemoteAdminImpl; +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; @@ -39,7 +39,7 @@ public class NodeActivator implements BundleActivator, SynchronousBundleListener private NodeManager manager; private DiscoveryActivator discoveryActivator = new DiscoveryActivator(); - private RemoteAdminImpl remoteAdmin; + private RemoteServiceAdminImpl remoteAdmin; private RemoteControllerImpl controller; private void init() { @@ -60,7 +60,7 @@ public class NodeActivator implements BundleActivator, SynchronousBundleListener // FIXME: We should try to avoid aggressive initialization init(); - remoteAdmin = new RemoteAdminImpl(context); + remoteAdmin = new RemoteServiceAdminImpl(context); remoteAdmin.start(); controller = new RemoteControllerImpl(context); 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 d56b25ef40..d6ae4c5bdb 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 @@ -40,7 +40,7 @@ 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.RemoteAdminHelper; +import org.apache.tuscany.sca.osgi.service.remoteadmin.impl.OSGiHelper; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; @@ -207,7 +207,7 @@ public abstract class AbstractDiscoveryService implements Discovery, LifeCycleLi } private boolean filterMatches(String filterValue, EndpointDescription sd) { - Filter filter = RemoteAdminHelper.createFilter(context, filterValue); + Filter filter = OSGiHelper.createFilter(context, filterValue); Hashtable props = new Hashtable(sd.getProperties()); // Add two faked properties to make the filter match props.put(Constants.OBJECTCLASS, sd.getInterfaces()); @@ -242,7 +242,7 @@ public abstract class AbstractDiscoveryService implements Discovery, LifeCycleLi String property, Map> forwardMap, Map> reverseMap) { - Collection collection = RemoteAdminHelper.getStringCollection(reference, property); + Collection collection = OSGiHelper.getStringCollection(reference, property); if (logger.isLoggable(Level.FINE)) { logger.fine("adding listener: " + listener + " collection: " diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteAdmin.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteAdmin.java deleted file mode 100644 index c1b2f3d826..0000000000 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/RemoteAdmin.java +++ /dev/null @@ -1,101 +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; - -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import org.osgi.framework.ServiceReference; - -/** - * A Remote Service Admin manages the import and export of services. A - * Distribution Provider can expose a control interface. This interface allows - * the a remote controller to control the export and import of services. The API - * allows a remote controller to export a service, to import a service, and find - * out about the current imports and exports. - * - * @ThreadSafe - */ - -public interface RemoteAdmin { - /** - * Export a service to an endpoint. The Remote Service Admin must create an - * endpoint that can be used by other Distrbution Providers to connect to - * this Remote Service Admin and use the exported service. This method can - * return null if the service could not be exported. - * - * @param ref The Service Reference to export - * @return Export Registration that combines the Endpoint Description and - * the Service Reference or null exportService(ServiceReference ref); - - /** - * Export a service to a given endpoint. The Remote Service Admin must - * create an endpoint from the given description that can be used by other - * Distrbution Providers to connect to this Remote Service Admin and use the - * exported service. This method can return null if the service could not be - * exported because the endpoint could not be implemented by this Remote - * Service Admin. - * - * @param ref The Service Reference to export - * @param properties The properties to create a local endpoint that can be - * implemented by this Remote Service Admin. If this is null, the - * endpoint will be determined by the properties on the service, - * @see exportService(ServiceReference). The properties are the same as - * given for an exported service. They are overlaid over any properties - * the service defines - * @return Export Registration that combines the Endpoint Description and - * the Service Reference or null exportService(ServiceReference ref, Map properties); - - /** - * Answer the currently active Export Registrations. - * - * @return Returns A collection of Export Registrations that are currently - * active. - */ - public Collection getExportedServices(); - - /** - * Answer the currently active Import Registrations. - * - * @return Returns A collection of Import Registrations that are currently - * active. - */ - public Collection getImportedEndpoints(); - - /** - * Import a service from an endpoint. The Remote Service Admin must use the - * given endpoint to create a proxy. This method can return null if the - * service could not be imported. - * - * @param endpoint The Endpoint Description to be used for import - * @return An Import Registration that combines the Endpoint Description and - * the Service Reference or nullnull exportService(ServiceReference ref); + + /** + * Export a service to a given endpoint. The Remote Service Admin must + * create an endpoint from the given description that can be used by other + * Distrbution Providers to connect to this Remote Service Admin and use the + * exported service. This method can return null if the service could not be + * exported because the endpoint could not be implemented by this Remote + * Service Admin. + * + * @param ref The Service Reference to export + * @param properties The properties to create a local endpoint that can be + * implemented by this Remote Service Admin. If this is null, the + * endpoint will be determined by the properties on the service, + * @see exportService(ServiceReference). The properties are the same as + * given for an exported service. They are overlaid over any properties + * the service defines + * @return Export Registration that combines the Endpoint Description and + * the Service Reference or null exportService(ServiceReference ref, Map properties); + + /** + * Answer the currently active Export Registrations. + * + * @return Returns A collection of Export Registrations that are currently + * active. + */ + public Collection getExportedServices(); + + /** + * Answer the currently active Import Registrations. + * + * @return Returns A collection of Import Registrations that are currently + * active. + */ + public Collection getImportedEndpoints(); + + /** + * Import a service from an endpoint. The Remote Service Admin must use the + * given endpoint to create a proxy. This method can return null if the + * service could not be imported. + * + * @param endpoint The Endpoint Description to be used for import + * @return An Import Registration that combines the Endpoint Description and + * the Service Reference or null + * @param value + * @return + */ + public static String[] getStringArray(Object value) { + if (value == null) { + return null; + } + if (value instanceof String) { + return new String[] {(String)value}; + } else if (value instanceof Collection) { + Collection collection = (Collection)value; + return collection.toArray(new String[collection.size()]); + } + return (String[])value; + + } + + public static Collection getStringCollection(Object value) { + String[] values = getStringArray(value); + if (values == null) { + return null; + } else { + return Arrays.asList(values); + } + } + + public static String[] getStringArray(ServiceReference serviceReference, String propertyName) { + Object propertyValue = serviceReference.getProperty(propertyName); + return getStringArray(propertyValue); + } + + public static Collection getStringCollection(ServiceReference serviceReference, String propertyName) { + Object propertyValue = serviceReference.getProperty(propertyName); + return getStringCollection(propertyValue); + } + + public static Filter createFilter(BundleContext context, String filterValue) { + if (filterValue == null) { + return null; + } + try { + return context.createFilter(filterValue); + } catch (InvalidSyntaxException ex) { + throw new IllegalArgumentException("Invalid Filter: " + filterValue, ex); + } + } + +} diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteAdminHelper.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteAdminHelper.java deleted file mode 100644 index 7f467854a0..0000000000 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteAdminHelper.java +++ /dev/null @@ -1,86 +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.Arrays; -import java.util.Collection; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.Filter; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; - -/** - * - */ -public class RemoteAdminHelper { - private RemoteAdminHelper() { - } - - /** - * In OSGi, the value of String+ can be a single String, String[] or Collection - * @param value - * @return - */ - public static String[] getStringArray(Object value) { - if (value == null) { - return null; - } - if (value instanceof String) { - return new String[] {(String)value}; - } else if (value instanceof Collection) { - Collection collection = (Collection)value; - return collection.toArray(new String[collection.size()]); - } - return (String[])value; - - } - - public static Collection getStringCollection(Object value) { - String[] values = getStringArray(value); - if (values == null) { - return null; - } else { - return Arrays.asList(values); - } - } - - public static String[] getStringArray(ServiceReference serviceReference, String propertyName) { - Object propertyValue = serviceReference.getProperty(propertyName); - return getStringArray(propertyValue); - } - - public static Collection getStringCollection(ServiceReference serviceReference, String propertyName) { - Object propertyValue = serviceReference.getProperty(propertyName); - return getStringCollection(propertyValue); - } - - public static Filter createFilter(BundleContext context, String filterValue) { - if (filterValue == null) { - return null; - } - try { - return context.createFilter(filterValue); - } catch (InvalidSyntaxException ex) { - throw new IllegalArgumentException("Invalid Filter: " + filterValue, ex); - } - } - -} diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteAdminImpl.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteAdminImpl.java deleted file mode 100644 index f05c17b4dc..0000000000 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteAdminImpl.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.RemoteAdmin; -import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteAdminListener; -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 RemoteAdmin} - */ -public class RemoteAdminImpl implements RemoteAdmin { - private BundleContext context; - private ServiceRegistration registration; - private ServiceTracker listeners; - - private OSGiServiceExporter exporter; - private OSGiServiceImporter importer; - - private Collection importedEndpoints = new ArrayList(); - private Collection exportedServices = new ArrayList(); - - public RemoteAdminImpl(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(RemoteAdmin.class.getName(), this, null); - listeners = new ServiceTracker(this.context, RemoteAdminListener.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.RemoteAdmin#exportService(org.osgi.framework.ServiceReference) - */ - public List exportService(ServiceReference ref) { - List exportRegistrations = exporter.exportService(ref); - if (exportRegistrations != null) { - exportedServices.addAll(exportRegistrations); - } - return exportRegistrations; - } - - /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteAdmin#exportService(org.osgi.framework.ServiceReference, - * java.util.Map) - */ - public List exportService(ServiceReference ref, Map properties) { - List exportRegistrations = exporter.exportService(ref); - if (exportRegistrations != null) { - exportedServices.addAll(exportRegistrations); - } - return exportRegistrations; - } - - /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteAdmin#getExportedServices() - */ - public Collection getExportedServices() { - return exportedServices; - } - - /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteAdmin#getImportedEndpoints() - */ - public Collection getImportedEndpoints() { - return importedEndpoints; - } - - /** - * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteAdmin#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; - } - -} 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/service/remoteadmin/impl/RemoteControllerImpl.java index b7fe173c85..53c9d6fb4a 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/service/remoteadmin/impl/RemoteControllerImpl.java @@ -49,7 +49,7 @@ 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.RemoteAdmin; +import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin; import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteAdminEvent; import org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteAdminListener; import org.osgi.framework.Bundle; @@ -107,7 +107,7 @@ public class RemoteControllerImpl implements ListenerHook, RemoteAdminListener, } endpointListener = context.registerService(EndpointListener.class.getName(), this, null); - remoteAdmins = new ServiceTracker(this.context, RemoteAdmin.class.getName(), null); + remoteAdmins = new ServiceTracker(this.context, RemoteServiceAdmin.class.getName(), null); remoteAdmins.open(); // DO NOT register EventHook.class.getName() as it cannot report existing services @@ -183,7 +183,7 @@ public class RemoteControllerImpl implements ListenerHook, RemoteAdminListener, logger.warning("No RemoteAdmin services are available."); } else { for (Object ra : admins) { - RemoteAdmin remoteAdmin = (RemoteAdmin)ra; + RemoteServiceAdmin remoteAdmin = (RemoteServiceAdmin)ra; List exportRegistrations = remoteAdmin.exportService(reference); if (exportRegistrations != null && !exportRegistrations.isEmpty()) { exportedServices.putValue(reference, exportRegistrations); @@ -331,7 +331,7 @@ public class RemoteControllerImpl implements ListenerHook, RemoteAdminListener, if (admins != null) { for (Object ra : admins) { - RemoteAdmin remoteAdmin = (RemoteAdmin)ra; + RemoteServiceAdmin remoteAdmin = (RemoteServiceAdmin)ra; ImportRegistration importRegistration = remoteAdmin.importService(description); if (importRegistration != null) { importedServices.putValue(endpoint, importRegistration); 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 new file mode 100644 index 0000000000..325911d916 --- /dev/null +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/RemoteServiceAdminImpl.java @@ -0,0 +1,142 @@ +/* + * 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.RemoteAdminListener; +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 importedEndpoints = new ArrayList(); + private Collection exportedServices = new ArrayList(); + + 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, RemoteAdminListener.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 exportService(ServiceReference ref) { + List 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 exportService(ServiceReference ref, Map properties) { + List exportRegistrations = exporter.exportService(ref); + if (exportRegistrations != null) { + exportedServices.addAll(exportRegistrations); + } + return exportRegistrations; + } + + /** + * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin#getExportedServices() + */ + public Collection getExportedServices() { + return exportedServices; + } + + /** + * @see org.apache.tuscany.sca.osgi.service.remoteadmin.RemoteServiceAdmin#getImportedEndpoints() + */ + public Collection 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; + } + +} -- cgit v1.2.3