From 85c0991a9f09866b5452efe2dd2ded4440de7f52 Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 24 Jul 2009 22:38:36 +0000 Subject: Move the OSGi service importer/exporter Change the node URI Delete empty folders git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@797679 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/node/osgi/impl/OSGiServiceExporter.java | 160 --------------------- .../sca/node/osgi/impl/OSGiServiceImporter.java | 121 ---------------- .../remoteadmin/impl/OSGiServiceExporter.java | 134 +++++++++++++++++ .../remoteadmin/impl/OSGiServiceImporter.java | 119 +++++++++++++++ .../service/remoteadmin/impl/RemoteAdminImpl.java | 2 - 5 files changed, 253 insertions(+), 283 deletions(-) delete mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceExporter.java delete mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceImporter.java create mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiServiceExporter.java create mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiServiceImporter.java diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceExporter.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceExporter.java deleted file mode 100644 index 016c88ba43..0000000000 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceExporter.java +++ /dev/null @@ -1,160 +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.node.osgi.impl; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.apache.tuscany.sca.assembly.Component; -import org.apache.tuscany.sca.assembly.ComponentService; -import org.apache.tuscany.sca.assembly.Endpoint; -import org.apache.tuscany.sca.contribution.Contribution; -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.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.service.remoteadmin.impl.EndpointDescriptionImpl; -import org.apache.tuscany.sca.osgi.service.remoteadmin.impl.EndpointIntrospector; -import org.apache.tuscany.sca.osgi.service.remoteadmin.impl.ExportRegistrationImpl; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceReference; -import org.osgi.util.tracker.ServiceTracker; -import org.osgi.util.tracker.ServiceTrackerCustomizer; - -/** - * Watching and exporting OSGi services - */ -public class OSGiServiceExporter implements ServiceTrackerCustomizer, LifeCycleListener { - private ExtensionPointRegistry registry; - private BundleContext context; - private ServiceTracker serviceTracker; - private NodeFactoryImpl nodeFactory; - private EndpointIntrospector introspector; - - /** - * @param context - * @param clazz - * @param customizer - */ - public OSGiServiceExporter(BundleContext context) { - this.context = context; - } - - private synchronized void init() { - if (nodeFactory == null) { - this.nodeFactory = (NodeFactoryImpl)NodeFactory.newInstance(); - this.nodeFactory.init(); - this.introspector = new EndpointIntrospector(context, getExtensionPointRegistry()); - } - } - - public void start() { - init(); - /* - String filterStr = - "(& (" + SERVICE_EXPORTED_CONFIGS - + "=sca) (" - + SERVICE_EXPORTED_INTERFACES - + "=*) (!(" - + SERVICE_IMPORTED - + "=*)) )"; - try { - Filter filter = context.createFilter(filterStr); - serviceTracker = new ServiceTracker(context, filter, this); - serviceTracker.open(true); - } catch (InvalidSyntaxException e) { - // Ignore - } - */ - } - - public void stop() { - /* - if (serviceTracker != null) { - serviceTracker.close(); - serviceTracker = null; - } - */ - } - - public Object addingService(ServiceReference reference) { - return exportService(reference); - } - - public List exportService(ServiceReference reference) { - try { - Contribution contribution = introspector.introspect(reference); - if (contribution != null) { - - NodeConfiguration configuration = nodeFactory.createNodeConfiguration(); - configuration.setURI("osgi.service." + String.valueOf(reference.getProperty(Constants.SERVICE_ID))); - configuration.getExtensions().add(reference.getBundle()); - // FIXME: Configure the domain and node URI - NodeImpl node = new NodeImpl(nodeFactory, configuration, Collections.singletonList(contribution)); - node.start(); - List exportedServices = new ArrayList(); - Component component = contribution.getDeployables().get(0).getComponents().get(0); - ComponentService service = component.getServices().get(0); - for (Endpoint endpoint : service.getEndpoints()) { - EndpointDescription endpointDescription = new EndpointDescriptionImpl(endpoint); - ExportRegistration exportRegistration = - new ExportRegistrationImpl(node, reference, endpointDescription); - exportedServices.add(exportRegistration); - } - return exportedServices; - } else { - return null; - } - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - public void modifiedService(ServiceReference reference, Object service) { - removedService(reference, service); - exportService(reference); - } - - public void removedService(ServiceReference reference, Object service) { - List exportedServices = (List)service; - for(ExportRegistration exportRegistration: exportedServices) { - exportRegistration.close(); - } - } - - protected ExtensionPointRegistry getExtensionPointRegistry() { - if (registry == null) { - ServiceTracker tracker = new ServiceTracker(context, ExtensionPointRegistry.class.getName(), null); - tracker.open(); - // tracker.waitForService(1000); - registry = (ExtensionPointRegistry)tracker.getService(); - tracker.close(); - } - return registry; - } - -} diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceImporter.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceImporter.java deleted file mode 100644 index 7a0f3bbbc0..0000000000 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceImporter.java +++ /dev/null @@ -1,121 +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.node.osgi.impl; - -import java.util.Collections; - -import org.apache.tuscany.sca.assembly.Component; -import org.apache.tuscany.sca.assembly.ComponentReference; -import org.apache.tuscany.sca.contribution.Contribution; -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.LifeCycleListener; -import org.apache.tuscany.sca.node.Node; -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.service.remoteadmin.impl.EndpointIntrospector; -import org.apache.tuscany.sca.osgi.service.remoteadmin.impl.ImportRegistrationImpl; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.util.tracker.ServiceTracker; - -/** - * Watching and exporting OSGi services - */ -public class OSGiServiceImporter implements LifeCycleListener { - private ExtensionPointRegistry registry; - private BundleContext context; - private NodeFactoryImpl nodeFactory; - private EndpointIntrospector introspector; - - /** - * @param context - * @param clazz - * @param customizer - */ - public OSGiServiceImporter(BundleContext context) { - this.context = context; - } - - private synchronized void init() { - if (nodeFactory == null) { - this.nodeFactory = (NodeFactoryImpl)NodeFactory.newInstance(); - this.nodeFactory.init(); - this.introspector = new EndpointIntrospector(context, getExtensionPointRegistry()); - } - } - - public void start() { - } - - public void stop() { - } - - public ImportRegistration importService(Bundle bundle, EndpointDescription endpointDescription) { - init(); - try { - Contribution contribution = introspector.introspect(bundle, endpointDescription); - if (contribution != null) { - - NodeConfiguration configuration = nodeFactory.createNodeConfiguration(); - configuration.setURI("osgi.reference." + endpointDescription.getURI()); - configuration.getExtensions().add(bundle); - // FIXME: Configure the domain and node URI - NodeImpl node = new NodeImpl(nodeFactory, configuration, Collections.singletonList(contribution)); - node.start(); - - Component component = contribution.getDeployables().get(0).getComponents().get(0); - ComponentReference componentReference = component.getReferences().get(0); - ServiceReference serviceReference = - context.getServiceReference("(sca.reference=" + component.getURI() - + "#reference(" - + componentReference.getName() - + ")"); - return new ImportRegistrationImpl(node, serviceReference, endpointDescription); - } else { - return null; - } - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - public void unimportService(ImportRegistration importRegistration) { - Node node = (Node)importRegistration.getImportedService().getProperty("sca.node"); - node.stop(); - } - - protected ExtensionPointRegistry getExtensionPointRegistry() { - if (registry == null) { - ServiceTracker tracker = new ServiceTracker(context, ExtensionPointRegistry.class.getName(), null); - tracker.open(); - // tracker.waitForService(1000); - registry = (ExtensionPointRegistry)tracker.getService(); - tracker.close(); - } - return registry; - } - -} 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/service/remoteadmin/impl/OSGiServiceExporter.java new file mode 100644 index 0000000000..5a381f0b67 --- /dev/null +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiServiceExporter.java @@ -0,0 +1,134 @@ +/* + * 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.Collections; +import java.util.List; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.contribution.Contribution; +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.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.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; + +/** + * Watching and exporting OSGi services + */ +public class OSGiServiceExporter implements ServiceTrackerCustomizer, LifeCycleListener { + private ExtensionPointRegistry registry; + private BundleContext context; + private NodeFactoryImpl nodeFactory; + private EndpointIntrospector introspector; + + /** + * @param context + * @param clazz + * @param customizer + */ + public OSGiServiceExporter(BundleContext context) { + this.context = context; + } + + private synchronized void init() { + if (nodeFactory == null) { + this.nodeFactory = (NodeFactoryImpl)NodeFactory.newInstance(); + this.nodeFactory.init(); + this.introspector = new EndpointIntrospector(context, getExtensionPointRegistry()); + } + } + + public void start() { + init(); + } + + public void stop() { + this.introspector = null; + } + + public Object addingService(ServiceReference reference) { + return exportService(reference); + } + + public List exportService(ServiceReference reference) { + try { + Contribution contribution = introspector.introspect(reference); + if (contribution != null) { + + NodeConfiguration configuration = nodeFactory.createNodeConfiguration(); + configuration.setURI(contribution.getURI()); + configuration.getExtensions().add(reference.getBundle()); + // FIXME: Configure the domain and node URI + NodeImpl node = new NodeImpl(nodeFactory, configuration, Collections.singletonList(contribution)); + node.start(); + List exportedServices = new ArrayList(); + Component component = contribution.getDeployables().get(0).getComponents().get(0); + ComponentService service = component.getServices().get(0); + for (Endpoint endpoint : service.getEndpoints()) { + EndpointDescription endpointDescription = new EndpointDescriptionImpl(endpoint); + ExportRegistration exportRegistration = + new ExportRegistrationImpl(node, reference, endpointDescription); + exportedServices.add(exportRegistration); + } + return exportedServices; + } else { + return null; + } + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public void modifiedService(ServiceReference reference, Object service) { + removedService(reference, service); + exportService(reference); + } + + public void removedService(ServiceReference reference, Object service) { + List exportedServices = (List)service; + for(ExportRegistration exportRegistration: exportedServices) { + exportRegistration.close(); + } + } + + protected ExtensionPointRegistry getExtensionPointRegistry() { + if (registry == null) { + ServiceTracker tracker = new ServiceTracker(context, ExtensionPointRegistry.class.getName(), null); + tracker.open(); + // tracker.waitForService(1000); + registry = (ExtensionPointRegistry)tracker.getService(); + tracker.close(); + } + return registry; + } + +} 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/service/remoteadmin/impl/OSGiServiceImporter.java new file mode 100644 index 0000000000..e42baab02d --- /dev/null +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/remoteadmin/impl/OSGiServiceImporter.java @@ -0,0 +1,119 @@ +/* + * 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.Collections; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.LifeCycleListener; +import org.apache.tuscany.sca.node.Node; +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.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; + +/** + * Watching and exporting OSGi services + */ +public class OSGiServiceImporter implements LifeCycleListener { + private ExtensionPointRegistry registry; + private BundleContext context; + private NodeFactoryImpl nodeFactory; + private EndpointIntrospector introspector; + + /** + * @param context + * @param clazz + * @param customizer + */ + public OSGiServiceImporter(BundleContext context) { + this.context = context; + } + + private synchronized void init() { + if (nodeFactory == null) { + this.nodeFactory = (NodeFactoryImpl)NodeFactory.newInstance(); + this.nodeFactory.init(); + this.introspector = new EndpointIntrospector(context, getExtensionPointRegistry()); + } + } + + public void start() { + } + + public void stop() { + } + + public ImportRegistration importService(Bundle bundle, EndpointDescription endpointDescription) { + init(); + try { + Contribution contribution = introspector.introspect(bundle, endpointDescription); + if (contribution != null) { + + NodeConfiguration configuration = nodeFactory.createNodeConfiguration(); + configuration.setURI(contribution.getURI()); + configuration.getExtensions().add(bundle); + // FIXME: Configure the domain and node URI + NodeImpl node = new NodeImpl(nodeFactory, configuration, Collections.singletonList(contribution)); + node.start(); + + Component component = contribution.getDeployables().get(0).getComponents().get(0); + ComponentReference componentReference = component.getReferences().get(0); + ServiceReference serviceReference = + context.getServiceReference("(sca.reference=" + component.getURI() + + "#reference(" + + componentReference.getName() + + ")"); + return new ImportRegistrationImpl(node, serviceReference, endpointDescription); + } else { + return null; + } + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public void unimportService(ImportRegistration importRegistration) { + Node node = (Node)importRegistration.getImportedService().getProperty("sca.node"); + node.stop(); + } + + protected ExtensionPointRegistry getExtensionPointRegistry() { + if (registry == null) { + ServiceTracker tracker = new ServiceTracker(context, ExtensionPointRegistry.class.getName(), null); + tracker.open(); + // tracker.waitForService(1000); + registry = (ExtensionPointRegistry)tracker.getService(); + tracker.close(); + } + return registry; + } + +} 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 index 05e59844e9..f05c17b4dc 100644 --- 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 @@ -24,8 +24,6 @@ import java.util.Collection; import java.util.List; import java.util.Map; -import org.apache.tuscany.sca.node.osgi.impl.OSGiServiceExporter; -import org.apache.tuscany.sca.node.osgi.impl.OSGiServiceImporter; 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; -- cgit v1.2.3