diff options
12 files changed, 139 insertions, 39 deletions
diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java index a84268cc69..8c8ace95ad 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java @@ -21,10 +21,12 @@ package org.apache.tuscany.sca.extensibility.equinox; import java.util.Dictionary;
import java.util.Hashtable;
+import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
+import org.apache.tuscany.sca.core.ModuleActivator;
import org.apache.tuscany.sca.extensibility.ServiceDeclaration;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -99,4 +101,25 @@ public class OSGiExtensionPointRegistry extends DefaultExtensionPointRegistry { services.remove(i);
}
+ @Override
+ public void destroy() {
+ // Get a unique map as an extension point may exist in the map by different keys
+ Map<ModuleActivator, ModuleActivator> map = new IdentityHashMap<ModuleActivator, ModuleActivator>();
+ for (ServiceRegistration reg : services.values()) {
+ ServiceReference ref = reg.getReference();
+ if (ref != null) {
+ Object service = bundleContext.getService(ref);
+ if (service instanceof ModuleActivator) {
+ ModuleActivator activator = (ModuleActivator)service;
+ map.put(activator, activator);
+ }
+ reg.unregister();
+ }
+ }
+ for (ModuleActivator activator : map.values()) {
+ activator.stop(this);
+ }
+ services.clear();
+ }
+
}
diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultExtensionPointRegistry.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultExtensionPointRegistry.java index 3ed134dcec..3ea587c5f2 100644 --- a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultExtensionPointRegistry.java +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultExtensionPointRegistry.java @@ -25,6 +25,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.Map; import java.util.Set; @@ -64,7 +65,9 @@ public class DefaultExtensionPointRegistry implements ExtensionPointRegistry { if (extensionPoint == null) { throw new IllegalArgumentException("Cannot register null as an ExtensionPoint"); } - + if (extensionPoint instanceof ModuleActivator) { + ((ModuleActivator)extensionPoint).start(this); + } Set<Class<?>> interfaces = getAllInterfaces(extensionPoint.getClass()); for (Class<?> i : interfaces) { registerExtensionPoint(i, extensionPoint, declaration); @@ -112,13 +115,15 @@ public class DefaultExtensionPointRegistry implements ExtensionPointRegistry { // Dynamically load an extension point class declared under META-INF/services try { - ServiceDeclaration extensionPointDeclaration = ServiceDiscovery.getInstance().getServiceDeclaration(extensionPointType.getName()); + ServiceDeclaration extensionPointDeclaration = + ServiceDiscovery.getInstance().getServiceDeclaration(extensionPointType.getName()); if (extensionPointDeclaration != null) { Class<?> extensionPointClass = extensionPointDeclaration.loadClass(); // Construct the extension point Constructor<?>[] constructors = extensionPointClass.getConstructors(); - Constructor<?> constructor = getConstructor(constructors, new Class<?>[] {ExtensionPointRegistry.class}); + Constructor<?> constructor = + getConstructor(constructors, new Class<?>[] {ExtensionPointRegistry.class}); if (constructor != null) { extensionPoint = constructor.newInstance(this); } else { @@ -165,6 +170,10 @@ public class DefaultExtensionPointRegistry implements ExtensionPointRegistry { throw new IllegalArgumentException("Cannot remove null as an ExtensionPoint"); } + if (extensionPoint instanceof ModuleActivator) { + ((ModuleActivator)extensionPoint).stop(this); + } + Set<Class<?>> interfaces = getAllInterfaces(extensionPoint.getClass()); for (Class<?> i : interfaces) { unregisterExtensionPoint(i); @@ -199,4 +208,19 @@ public class DefaultExtensionPointRegistry implements ExtensionPointRegistry { } } + public void destroy() { + // Get a unique map as an extension point may exist in the map by different keys + Map<ModuleActivator, ModuleActivator> map = new IdentityHashMap<ModuleActivator, ModuleActivator>(); + for (Object extp : extensionPoints.values()) { + if (extp instanceof ModuleActivator) { + ModuleActivator activator = (ModuleActivator)extp; + map.put(activator, activator); + } + } + for (ModuleActivator activator : map.values()) { + activator.stop(this); + } + extensionPoints.clear(); + } + } diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/ExtensionPointRegistry.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/ExtensionPointRegistry.java index f2f67b2d52..d5647d6601 100644 --- a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/ExtensionPointRegistry.java +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/ExtensionPointRegistry.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.core; @@ -53,4 +53,9 @@ public interface ExtensionPointRegistry { * @throws IllegalArgumentException if extensionPoint is null */ void removeExtensionPoint(Object extensionPoint); + + /** + * Destroy the extension point registry + */ + void destroy(); } diff --git a/java/sca/modules/host-rmi/META-INF/MANIFEST.MF b/java/sca/modules/host-rmi/META-INF/MANIFEST.MF index 78d9be3563..045f10a935 100644 --- a/java/sca/modules/host-rmi/META-INF/MANIFEST.MF +++ b/java/sca/modules/host-rmi/META-INF/MANIFEST.MF @@ -9,7 +9,8 @@ Bnd-LastModified: 1225397321468 Bundle-ManifestVersion: 2
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-Description: Apache Tuscany SCA RMI Host Extension Point
-Import-Package: org.apache.tuscany.sca.host.rmi;version="2.0.0"
+Import-Package: org.apache.tuscany.sca.core;version="2.0.0",
+ org.apache.tuscany.sca.host.rmi;version="2.0.0"
Bundle-SymbolicName: org.apache.tuscany.sca.host.rmi
Bundle-DocURL: http://www.apache.org/
Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 diff --git a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java index c5c1f16869..f98ce018e5 100644 --- a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java +++ b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java @@ -21,11 +21,13 @@ package org.apache.tuscany.sca.host.rmi; import java.net.URI; import java.rmi.AlreadyBoundException; +import java.rmi.NoSuchObjectException; import java.rmi.NotBoundException; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; @@ -153,4 +155,15 @@ public class DefaultRMIHost implements RMIHost { } } + public void destroy() { + for (Registry registry : rmiRegistries.values()) { + try { + UnicastRemoteObject.unexportObject(registry, false); + } catch (NoSuchObjectException e) { + e.printStackTrace(); + } + } + rmiRegistries.clear(); + } + } diff --git a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHostExtensionPoint.java b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHostExtensionPoint.java index 3ac086a2ee..edfda79dd0 100644 --- a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHostExtensionPoint.java +++ b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHostExtensionPoint.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.host.rmi; @@ -22,15 +22,18 @@ package org.apache.tuscany.sca.host.rmi; import java.util.ArrayList; import java.util.List; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; + /** * Default implementation of an RMI host extension point. - * + * * @version $Rev$ $Date$ */ -public class DefaultRMIHostExtensionPoint implements RMIHostExtensionPoint { +public class DefaultRMIHostExtensionPoint implements RMIHostExtensionPoint, ModuleActivator { private List<RMIHost> rmiHosts = new ArrayList<RMIHost>(); - + public DefaultRMIHostExtensionPoint() { addRMIHost(new DefaultRMIHost()); } @@ -46,4 +49,13 @@ public class DefaultRMIHostExtensionPoint implements RMIHostExtensionPoint { public List<RMIHost> getRMIHosts() { return rmiHosts; } + + public void start(ExtensionPointRegistry registry) { + } + + public void stop(ExtensionPointRegistry registry) { + for (RMIHost host : rmiHosts) { + host.destroy(); + } + } } diff --git a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/ExtensibleRMIHost.java b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/ExtensibleRMIHost.java index 29b4e195c7..e2fad5d9b5 100644 --- a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/ExtensibleRMIHost.java +++ b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/ExtensibleRMIHost.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.host.rmi; @@ -24,36 +24,44 @@ import java.rmi.Remote; /** * Default implementation of an extensible RMI host. - * + * * @version $Rev$ $Date$ */ public class ExtensibleRMIHost implements RMIHost { - + private RMIHostExtensionPoint rmiHosts; - + public ExtensibleRMIHost(RMIHostExtensionPoint rmiHosts) { this.rmiHosts = rmiHosts; } - + public void registerService(String uri, Remote serviceObject) throws RMIHostException, RMIHostRuntimeException { if (rmiHosts.getRMIHosts().isEmpty()) { throw new RMIHostException("No RMI host available"); } - rmiHosts.getRMIHosts().get(0).registerService(uri, serviceObject); + getDefaultHost().registerService(uri, serviceObject); } - + public void unregisterService(String uri) throws RMIHostException, RMIHostRuntimeException { if (rmiHosts.getRMIHosts().isEmpty()) { throw new RMIHostException("No RMI host available"); } - rmiHosts.getRMIHosts().get(0).unregisterService(uri); + getDefaultHost().unregisterService(uri); } - + public Remote findService(String uri) throws RMIHostException, RMIHostRuntimeException { if (rmiHosts.getRMIHosts().isEmpty()) { throw new RMIHostException("No RMI host available"); } - return rmiHosts.getRMIHosts().get(0).findService(uri); + return getDefaultHost().findService(uri); } - + + protected RMIHost getDefaultHost() { + return rmiHosts.getRMIHosts().get(0); + } + + public void destroy() { + getDefaultHost().destroy(); + } + } diff --git a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMIHost.java b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMIHost.java index b95b91f003..b85cc2fc2a 100644 --- a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMIHost.java +++ b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMIHost.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.host.rmi; @@ -31,17 +31,17 @@ public interface RMIHost { /** * Register an RMI service with the given name and port - * + * * @param uri the URI against which the server is to be registered * @param serviceObject the server object to be registered * @throws RMIHostException * @throws RMIHostRuntimeException - */ + */ void registerService(String uri, Remote serviceObject) throws RMIHostException, RMIHostRuntimeException; /** * Unregister a service registered under the given service name and port number - * + * * @param uri the URI of the server * @throws RMIHostException * @throws RMIHostRuntimeException @@ -50,12 +50,16 @@ public interface RMIHost { /** * find a remote service hosted on the given host, port and service name - * + * * @param uri the URI of the service - * @return the RMI server object + * @return the RMI server object * @throws RMIHostException * @throws RMIHostRuntimeException */ Remote findService(String uri) throws RMIHostException, RMIHostRuntimeException; + /** + * Destroy the host. It can be used to unbind the RMI registry + */ + void destroy(); } diff --git a/java/sca/modules/implementation-osgi/src/main/resources/impl-osgi-validation-messages.properties b/java/sca/modules/implementation-osgi/src/main/resources/impl-osgi-validation-messages.properties index 2503be0b13..a406ea21c3 100644 --- a/java/sca/modules/implementation-osgi/src/main/resources/impl-osgi-validation-messages.properties +++ b/java/sca/modules/implementation-osgi/src/main/resources/impl-osgi-validation-messages.properties @@ -22,4 +22,5 @@ ContributionReadException = ContributionReadException occured due to: ContributionResolveException = ContributionResolveException occured due to: PropertyShouldSpecifySR = Properties in implementation.osgi should specify service or reference CouldNotLocateOSGiBundle = Could not locate OSGi bundle: {0} -MissingComponentTypeFile = Missing .componentType side file: {0}
\ No newline at end of file +MissingComponentTypeFile = Missing .componentType side file: {0} +OSGiFrameworkNotStarted = OSGi Framework is not started
\ No newline at end of file diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiNodeFactoryImpl.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiNodeFactoryImpl.java index 4dfa7ace90..a92f7c7d77 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiNodeFactoryImpl.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiNodeFactoryImpl.java @@ -47,6 +47,7 @@ public class OSGiNodeFactoryImpl extends NodeFactoryImpl { */ public OSGiNodeFactoryImpl(BundleContext bundleContext) { this.bundleContext = bundleContext; + autoDestroy = false; setNodeFactory(this); } diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java index 6bca9fc9e7..dfdbc54f13 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java @@ -129,13 +129,23 @@ public class NodeFactoryImpl extends NodeFactory { private WorkScheduler workScheduler; private StAXArtifactProcessorExtensionPoint xmlProcessors; + /** + * Automatically destroy the factory when last node is stopped. Subclasses + * can set this flag. + */ + protected boolean autoDestroy = true; + @Override public Node createNode(NodeConfiguration configuration) { return new NodeImpl(this, configuration); } protected Node removeNode(NodeConfiguration configuration) { - return nodes.remove(getNodeKey(configuration)); + Node node = nodes.remove(getNodeKey(configuration)); + if (autoDestroy && nodes.isEmpty()) { + destroy(); + } + return node; } protected void addNode(NodeConfiguration configuration, Node node) { @@ -195,6 +205,7 @@ public class NodeFactoryImpl extends NodeFactory { // Stop and destroy the work manager workScheduler.destroy(); + extensionPoints.destroy(); inited = false; } } diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java index ac53f805df..febf4033e8 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java @@ -114,9 +114,6 @@ public class NodeImpl implements Node, Client { manager.removeNode(configuration); this.compositeActivator = null; this.proxyFactory = null; - if (manager.getNodes().isEmpty()) { - manager.destroy(); - } } catch (ActivationException e) { throw new IllegalStateException(e); |