summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
Diffstat (limited to 'java')
-rw-r--r--java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java44
-rw-r--r--java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java109
-rw-r--r--java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java2
-rw-r--r--java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java2
-rw-r--r--java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/sca/bundle.componentType (renamed from java/sca/modules/implementation-osgi/src/test/resources/bundle.componentType)0
5 files changed, 153 insertions, 4 deletions
diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java
index 1bad4102e7..ecf685c679 100644
--- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java
+++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java
@@ -19,25 +19,65 @@
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 OSGiImplementationActivator implements BundleActivator {
+public class OSGiImplementationActivator implements BundleActivator, SynchronousBundleListener {
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 {
- bundleContext = context;
+ 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())) {
+ // The bundle is an SCA implementation, init implementation.osgi
+ bundleContext.removeBundleListener(this);
+ init();
+ }
+ }
+
+ }
+
}
diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java
new file mode 100644
index 0000000000..a6dc7c22cc
--- /dev/null
+++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java
@@ -0,0 +1,109 @@
+/*
+ * 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.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<Bundle, OSGiImplementation> implementations = new ConcurrentHashMap<Bundle, OSGiImplementation>();
+
+ 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.getEntryPaths("OSGI-INF/sca/");
+ 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.getEntryPaths("OSGI-INF/blueprint/");
+ if (entries != null && entries.hasMoreElements()) {
+ return true;
+ }
+ return false;
+ }
+
+ private void bundleStarted(Bundle bundle) {
+ if (!isSCABundle(bundle)) {
+ return;
+ }
+ }
+
+ 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/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java
index 3d0831f1bb..cf401ba630 100644
--- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java
+++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java
@@ -200,7 +200,7 @@ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiIm
}
ComponentType componentType = assemblyFactory.createComponentType();
- componentType.setURI("META-INF/bundle.componentType");
+ componentType.setURI("OSGI-INF/sca/bundle.componentType");
componentType.setUnresolved(true);
componentType = resolver.resolveModel(ComponentType.class, componentType);
if (componentType.isUnresolved()) {
diff --git a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java
index 8a6e04807f..4573bdb1f0 100644
--- a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java
+++ b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java
@@ -96,7 +96,7 @@ public class OSGiReadImplTestCase {
Composite composite = (Composite)staxProcessor.read(reader);
assertNotNull(composite);
- is = getClass().getClassLoader().getResourceAsStream("bundle.componentType");
+ is = getClass().getClassLoader().getResourceAsStream("OSGI-INF/sca/bundle.componentType");
reader = inputFactory.createXMLStreamReader(is);
ComponentType componentType = (ComponentType)staxProcessor.read(reader);
diff --git a/java/sca/modules/implementation-osgi/src/test/resources/bundle.componentType b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/sca/bundle.componentType
index 73c7ca7c13..73c7ca7c13 100644
--- a/java/sca/modules/implementation-osgi/src/test/resources/bundle.componentType
+++ b/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/sca/bundle.componentType