From 9084bd19216f4865f63d7d68f3d4c3ac6f9d37f4 Mon Sep 17 00:00:00 2001 From: rfeng Date: Mon, 16 Mar 2009 22:04:54 +0000 Subject: Start to hook up the osgi-based node git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@755018 13f79535-47bb-0310-9956-ffa450edef68 --- .../osgi/runtime/OSGiImplementationManager.java | 112 --------------------- .../OSGiImplementationRuntimeActivator.java | 44 +------- .../dosgi/test/CalculatorOSGiNodeTestCase.java | 3 +- .../modules/node-impl-osgi/META-INF/MANIFEST.MF | 1 + java/sca/modules/node-impl-osgi/pom.xml | 7 ++ .../tuscany/sca/node/osgi/impl/NodeActivator.java | 84 ++++++++++++++++ .../tuscany/sca/node/osgi/impl/NodeManager.java | 112 +++++++++++++++++++++ 7 files changed, 207 insertions(+), 156 deletions(-) delete mode 100644 java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java create mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java create mode 100644 java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeManager.java (limited to 'java/sca/modules') diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java deleted file mode 100644 index fdc2e1ee2d..0000000000 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java +++ /dev/null @@ -1,112 +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.implementation.osgi.runtime; - -import java.net.URL; -import java.util.Dictionary; -import java.util.Enumeration; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleEvent; -import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceListener; -import org.osgi.framework.SynchronousBundleListener; - -/** - * Managing the mapping between OSGi bundles and SCA implementation.osgi - */ -public class OSGiImplementationManager implements SynchronousBundleListener, ServiceListener { - private BundleContext bundleContext; - private Map implementations = new ConcurrentHashMap(); - - public OSGiImplementationManager(BundleContext bundleContext) { - super(); - this.bundleContext = bundleContext; - } - - public void start() { - for (Bundle b : bundleContext.getBundles()) { - if ((b.getState() & Bundle.ACTIVE) != 0) { - // Process the active bundles - bundleStarted(b); - } - } - } - - public static boolean isSCABundle(Bundle bundle) { - Dictionary headers = bundle.getHeaders(); - // OSGi RFC 119 SCA - if (headers.get("SCA-Composite") != null) { - return true; - } - Enumeration entries = bundle.findEntries("OSGI-INF/sca", "*", false); - if (entries != null && entries.hasMoreElements()) { - return true; - } - - // OSGi Declarative Services - if (headers.get("Service-Component") != null) { - return true; - } - - // OSGI RFC 124: BluePrint Service - if (headers.get("Bundle-Blueprint") != null) { - return true; - } - - entries = bundle.findEntries("OSGI-INF/blueprint", "*.xml", false); - if (entries != null && entries.hasMoreElements()) { - return true; - } - return false; - } - - private void bundleStarted(Bundle bundle) { - if (!isSCABundle(bundle)) { - return; - } - URL compositeFile = bundle.getEntry("OSGI-INF/sca/bundle.composite"); - URL componentTypeFile = bundle.getEntry("OSGI-INF/sca/bundle.componentType"); - } - - private void bundleStopping(Bundle bundle) { - OSGiImplementation impl = implementations.remove(bundle); - if (impl == null) { - return; - } - } - - public void serviceChanged(ServiceEvent event) { - } - - public void bundleChanged(BundleEvent event) { - int type = event.getType(); - if (type == BundleEvent.STOPPING) { - bundleStopping(event.getBundle()); - } else if (type == BundleEvent.STARTED) { - bundleStarted(event.getBundle()); - } - } - -} diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationRuntimeActivator.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationRuntimeActivator.java index 4567f8a708..f3d59cc513 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationRuntimeActivator.java +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationRuntimeActivator.java @@ -19,65 +19,25 @@ package org.apache.tuscany.sca.implementation.osgi.runtime; -import static org.apache.tuscany.sca.implementation.osgi.runtime.OSGiImplementationManager.isSCABundle; - -import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleEvent; -import org.osgi.framework.SynchronousBundleListener; /** * Bundle activator to receive the BundleContext */ -public class OSGiImplementationRuntimeActivator implements BundleActivator, SynchronousBundleListener { +public class OSGiImplementationRuntimeActivator implements BundleActivator { private static BundleContext bundleContext; - private boolean inited; - - private void init() { - synchronized (this) { - if (inited) { - return; - } - inited = true; - } - } public void start(BundleContext context) throws Exception { bundleContext = context; - boolean found = false; - for (Bundle b : context.getBundles()) { - if (isSCABundle(b)) { - found = true; - break; - } - } - - if (found) { - init(); - } else { - context.addBundleListener(this); - } } public void stop(BundleContext context) throws Exception { - context.removeBundleListener(this); bundleContext = null; } - public static BundleContext getBundleContext() { + static BundleContext getBundleContext() { return bundleContext; } - public void bundleChanged(BundleEvent event) { - if (event.getType() == BundleEvent.STARTING) { - if (isSCABundle(event.getBundle())) { - // The bundle is an SCA implementation, init implementation.osgi - bundleContext.removeBundleListener(this); - init(); - } - } - - } - } diff --git a/java/sca/modules/implementation-osgi-runtime/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java b/java/sca/modules/implementation-osgi-runtime/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java index abd2985005..1dee0b1ae7 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java +++ b/java/sca/modules/implementation-osgi-runtime/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java @@ -85,6 +85,7 @@ public class CalculatorOSGiNodeTestCase { bundles.add(getCodeLocation(OSGiImplementation.class)); bundles.add(getCodeLocation(OSGiBundleContributionScanner.class)); + bundles.add(getCodeLocation(NodeImpl.class)); bundles.add(OSGiTestBundles.createBundle("target/test-classes/calculator-bundle.jar", "calculator/dosgi/META-INF/MANIFEST.MF", @@ -131,8 +132,6 @@ public class CalculatorOSGiNodeTestCase { for (Bundle b : context.getBundles()) { if (b.getSymbolicName().equals("calculator.dosgi")) { b.start(); - NodeImpl node = new NodeImpl(b); - node.start(); System.out.println(string(b, false)); } } diff --git a/java/sca/modules/node-impl-osgi/META-INF/MANIFEST.MF b/java/sca/modules/node-impl-osgi/META-INF/MANIFEST.MF index c02de5f8f8..886e434868 100644 --- a/java/sca/modules/node-impl-osgi/META-INF/MANIFEST.MF +++ b/java/sca/modules/node-impl-osgi/META-INF/MANIFEST.MF @@ -26,6 +26,7 @@ Bundle-Vendor: The Apache Software Foundation Bundle-Version: 2.0.0 Bnd-LastModified: 1225397240796 Bundle-ManifestVersion: 2 +Bundle-Activator: org.apache.tuscany.sca.node.osgi.impl.NodeActivator Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt Bundle-Description: Apache Tuscany SCA Node Implementation Import-Package: javax.xml.namespace, diff --git a/java/sca/modules/node-impl-osgi/pom.xml b/java/sca/modules/node-impl-osgi/pom.xml index 3ba75702b9..f3e6d79c4e 100644 --- a/java/sca/modules/node-impl-osgi/pom.xml +++ b/java/sca/modules/node-impl-osgi/pom.xml @@ -155,6 +155,13 @@ 2.0-SNAPSHOT test + + + org.apache.tuscany.sca + tuscany-node-launcher-equinox + 2.0-SNAPSHOT + test + 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 new file mode 100644 index 0000000000..42ae9d89f3 --- /dev/null +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java @@ -0,0 +1,84 @@ +/* + * 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 static org.apache.tuscany.sca.node.osgi.impl.NodeManager.isSCABundle; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; +import org.osgi.framework.SynchronousBundleListener; + +/** + * Bundle activator to receive the BundleContext + */ +public class NodeActivator implements BundleActivator, SynchronousBundleListener { + private static BundleContext bundleContext; + private boolean inited; + + private void init() { + synchronized (this) { + if (inited) { + return; + } + NodeManager manager = new NodeManager(bundleContext); + manager.start(); + bundleContext.addBundleListener(manager); + inited = true; + } + } + + public void start(BundleContext context) throws Exception { + bundleContext = context; + boolean found = false; + for (Bundle b : context.getBundles()) { + if (isSCABundle(b)) { + found = true; + break; + } + } + + if (found) { + init(); + } else { + context.addBundleListener(this); + } + } + + public void stop(BundleContext context) throws Exception { + context.removeBundleListener(this); + bundleContext = null; + } + + public static BundleContext getBundleContext() { + return bundleContext; + } + + public void bundleChanged(BundleEvent event) { + if (event.getType() == BundleEvent.STARTING) { + if (isSCABundle(event.getBundle())) { + init(); + } + } + + } + +} diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeManager.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeManager.java new file mode 100644 index 0000000000..68575b6c74 --- /dev/null +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeManager.java @@ -0,0 +1,112 @@ +/* + * 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.Dictionary; +import java.util.Enumeration; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; +import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceListener; +import org.osgi.framework.SynchronousBundleListener; + +/** + * Managing the mapping between OSGi bundles and SCA implementation.osgi + */ +public class NodeManager implements SynchronousBundleListener, ServiceListener { + private BundleContext bundleContext; + private Map nodes = new ConcurrentHashMap(); + + public NodeManager(BundleContext bundleContext) { + super(); + this.bundleContext = bundleContext; + } + + public void start() { + for (Bundle b : bundleContext.getBundles()) { + if ((b.getState() & Bundle.ACTIVE) != 0) { + // Process the active bundles + bundleStarted(b); + } + } + } + + public static boolean isSCABundle(Bundle bundle) { + Dictionary headers = bundle.getHeaders(); + // OSGi RFC 119 SCA + if (headers.get("SCA-Composite") != null) { + return true; + } + Enumeration entries = bundle.findEntries("OSGI-INF/sca", "*", false); + if (entries != null && entries.hasMoreElements()) { + return true; + } + + // OSGi Declarative Services + if (headers.get("Service-Component") != null) { + return true; + } + + // OSGI RFC 124: BluePrint Service + if (headers.get("Bundle-Blueprint") != null) { + return true; + } + + entries = bundle.findEntries("OSGI-INF/blueprint", "*.xml", false); + if (entries != null && entries.hasMoreElements()) { + return true; + } + return false; + } + + private void bundleStarted(Bundle bundle) { + if (!isSCABundle(bundle)) { + return; + } + NodeImpl node = new NodeImpl(bundle); + nodes.put(bundle, node); + node.start(); + } + + private void bundleStopping(Bundle bundle) { + NodeImpl node = nodes.remove(bundle); + if (node == null) { + return; + } + node.stop(); + } + + public void serviceChanged(ServiceEvent event) { + } + + public void bundleChanged(BundleEvent event) { + int type = event.getType(); + if (type == BundleEvent.STOPPING) { + bundleStopping(event.getBundle()); + } else if (type == BundleEvent.STARTED) { + bundleStarted(event.getBundle()); + } + } + +} -- cgit v1.2.3