summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/node-impl-osgi/src
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-03-16 22:04:54 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-03-16 22:04:54 +0000
commit9084bd19216f4865f63d7d68f3d4c3ac6f9d37f4 (patch)
tree92c8a4294ca795b146a0ab58d9527d1cab6f7383 /java/sca/modules/node-impl-osgi/src
parentcefd69376ea57f21a5f5e7cc23883ef05bb5be96 (diff)
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
Diffstat (limited to 'java/sca/modules/node-impl-osgi/src')
-rw-r--r--java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java84
-rw-r--r--java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeManager.java112
2 files changed, 196 insertions, 0 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
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<Bundle, NodeImpl> nodes = new ConcurrentHashMap<Bundle, NodeImpl>();
+
+ 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());
+ }
+ }
+
+}