summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca
diff options
context:
space:
mode:
Diffstat (limited to 'tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca')
-rw-r--r--tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java86
-rw-r--r--tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeManager.java143
-rw-r--r--tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java87
-rw-r--r--tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiNodeFactoryImpl.java120
-rw-r--r--tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceTracker.java40
5 files changed, 476 insertions, 0 deletions
diff --git a/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java
new file mode 100644
index 0000000000..43892d44d8
--- /dev/null
+++ b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java
@@ -0,0 +1,86 @@
+/*
+ * 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 NodeManager manager;
+
+ private void init() {
+ synchronized (this) {
+ if (inited) {
+ return;
+ }
+ 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())) {
+ bundleContext.removeBundleListener(this);
+ init();
+ }
+ }
+
+ }
+
+}
diff --git a/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeManager.java b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeManager.java
new file mode 100644
index 0000000000..3f71117c1a
--- /dev/null
+++ b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeManager.java
@@ -0,0 +1,143 @@
+/*
+ * 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.net.URL;
+import java.util.Dictionary;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.configuration.NodeConfiguration;
+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 static final Logger logger = Logger.getLogger(NodeManager.class.getName());
+ private BundleContext bundleContext;
+ private OSGiNodeFactoryImpl factory;
+
+ public NodeManager(BundleContext bundleContext) {
+ super();
+ this.bundleContext = bundleContext;
+ this.factory = new OSGiNodeFactoryImpl(this.bundleContext);
+ }
+
+ public void start() {
+ for (Bundle b : bundleContext.getBundles()) {
+ if ((b.getState() & Bundle.ACTIVE) != 0) {
+ // Process the active bundles
+ bundleStarted(b);
+ }
+ }
+ }
+
+ public void stop() {
+ if (factory != null) {
+ factory.destroy();
+ }
+ }
+
+ public static boolean isSCABundle(Bundle bundle) {
+ Dictionary<?, ?> headers = bundle.getHeaders();
+ // OSGi RFC 119 SCA
+ if (headers.get("SCA-Composite") != null) {
+ return true;
+ }
+
+ URL bundleComposite = bundle.getResource("OSGI-INF/sca/bundle.composite");
+ if (bundleComposite != null) {
+ return true;
+ }
+
+ /* FIXME: What if there is a META-INF/sca-contribution.xml? There are two cases:
+ * 1. The file contains deployable elements
+ * 2. The file doesn't contain deployable elements
+ */
+
+ /*
+ * FIXME: Do we want to use all of the .composite files under OSGI-INF/sca?
+ */
+
+ /*
+ Enumeration<?> entries = bundle.findEntries("OSGI-INF/sca", "bundle.composite", 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;
+ }
+ try {
+ NodeConfiguration configuration = factory.getConfiguration(bundle, null);
+ Node node = factory.createNode(configuration);
+ node.start();
+ } catch (Throwable e) {
+ logger.log(Level.SEVERE, e.getMessage(), e);
+ }
+ }
+
+ private void bundleStopping(Bundle bundle) {
+ Node node = factory.getNodes().get(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());
+ }
+ }
+
+}
diff --git a/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java
new file mode 100644
index 0000000000..fccd30d8ff
--- /dev/null
+++ b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java
@@ -0,0 +1,87 @@
+/*
+ * 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.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.tuscany.sca.contribution.Contribution;
+import org.apache.tuscany.sca.contribution.ContributionFactory;
+
+/**
+ * NodeUtil
+ *
+ * @version $Rev: $ $Date: $
+ */
+public class NodeUtil {
+ private NodeUtil() {
+ }
+
+ static Contribution contribution(ContributionFactory contributionFactory, org.apache.tuscany.sca.node.Contribution c) {
+ Contribution contribution = contributionFactory.createContribution();
+ contribution.setURI(c.getURI());
+ contribution.setLocation(c.getLocation());
+ contribution.setUnresolved(true);
+ return contribution;
+ }
+
+ /**
+ * Open a URL connection without cache
+ * @param url
+ * @return
+ * @throws IOException
+ */
+ static InputStream openStream(URL url) throws IOException {
+ InputStream is = null;
+ URLConnection connection = url.openConnection();
+ connection.setUseCaches(false);
+ is = connection.getInputStream();
+ return is;
+ }
+
+ /**
+ * Escape the space in URL string
+ * @param uri
+ * @return
+ */
+ static URI createURI(String uri) {
+ if (uri.indexOf('%') != -1) {
+ // Avoid double-escaping
+ return URI.create(uri);
+ }
+ int index = uri.indexOf(':');
+ String scheme = null;
+ String ssp = uri;
+ if (index != -1) {
+ scheme = uri.substring(0, index);
+ ssp = uri.substring(index + 1);
+ }
+ try {
+ return new URI(scheme, ssp, null);
+ } catch (URISyntaxException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+}
diff --git a/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiNodeFactoryImpl.java b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiNodeFactoryImpl.java
new file mode 100644
index 0000000000..3a698eeb4b
--- /dev/null
+++ b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiNodeFactoryImpl.java
@@ -0,0 +1,120 @@
+/*
+* 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.io.StringReader;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.logging.Level;
+
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.extensibility.equinox.OSGiExtensionPointRegistry;
+import org.apache.tuscany.sca.node.configuration.NodeConfiguration;
+import org.apache.tuscany.sca.node.configuration.NodeConfigurationFactory;
+import org.apache.tuscany.sca.node.impl.NodeFactoryImpl;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * The OSGi based NodeFactory
+ *
+ * @version $Rev$ $Date$
+ */
+public class OSGiNodeFactoryImpl extends NodeFactoryImpl {
+ private ServiceRegistration registration;
+ private BundleContext bundleContext;
+
+ /**
+ * Constructs a new Node controller
+ */
+ public OSGiNodeFactoryImpl(BundleContext bundleContext) {
+ this.bundleContext = bundleContext;
+ autoDestroy = false;
+ setNodeFactory(this);
+ }
+
+ protected NodeConfiguration getConfiguration(Bundle bundle, String compositeContent) {
+ init();
+
+ // Create a node configuration
+ NodeConfigurationFactory configurationFactory = modelFactories.getFactory(NodeConfigurationFactory.class);
+ NodeConfiguration configuration = configurationFactory.createNodeConfiguration();
+
+ URL location = bundle.getEntry("/");
+ String uri = bundle.getSymbolicName();
+ configuration.setURI(uri).addContribution(uri, location);
+
+ if (compositeContent != null) {
+ configuration.addDeploymentComposite(uri, new StringReader(compositeContent));
+ } else {
+ String compositeURI = (String)bundle.getHeaders().get("SCA-Composite");
+ if (compositeURI == null) {
+ compositeURI = "OSGI-INF/sca/bundle.composite";
+ }
+ if (compositeURI != null) {
+ configuration.addDeploymentComposite(uri, compositeURI);
+ }
+ }
+ // Set the bundle
+ configuration.getExtensions().add(bundle);
+ return configuration;
+ }
+
+ public synchronized void init() {
+ if (!inited) {
+ super.init();
+
+ // Register the ExtensionPointRegistry as an OSGi service
+ Dictionary<Object, Object> props = new Hashtable<Object, Object>();
+ registration =
+ bundleContext.registerService(ExtensionPointRegistry.class.getName(), extensionPoints, props);
+ }
+ }
+
+ public synchronized void destroy() {
+ if (inited) {
+ if (registration != null) {
+ registration.unregister();
+ registration = null;
+ }
+ super.destroy();
+ }
+
+ }
+
+ @Override
+ protected Object getNodeKey(NodeConfiguration configuration) {
+ // Use the bundle as the key
+ return configuration.getExtensions().get(0);
+ }
+
+ @Override
+ protected ExtensionPointRegistry createExtensionPointRegistry() {
+ return new OSGiExtensionPointRegistry(bundleContext);
+ }
+
+ @Override
+ protected boolean isSchemaValidationEnabled() {
+ return logger.isLoggable(Level.FINE);
+ }
+
+}
diff --git a/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceTracker.java b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceTracker.java
new file mode 100644
index 0000000000..59ee133061
--- /dev/null
+++ b/tags/java/sca/2.0-M3-RC4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/OSGiServiceTracker.java
@@ -0,0 +1,40 @@
+/*
+ * 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 org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
+
+/**
+ * A ServiceTracker
+ */
+public class OSGiServiceTracker implements ServiceTrackerCustomizer {
+
+ public Object addingService(ServiceReference serviceReference) {
+ return null;
+ }
+
+ public void modifiedService(ServiceReference serviceReference, Object service) {
+ }
+
+ public void removedService(ServiceReference serviceReference, Object service) {
+ }
+
+}