You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2009/03/12 21:29:07 UTC

svn commit: r753000 - in /tuscany/java/sca/modules/implementation-osgi/src: main/java/org/apache/tuscany/sca/implementation/osgi/runtime/ main/java/org/apache/tuscany/sca/implementation/osgi/xml/ test/java/org/apache/tuscany/sca/implementation/osgi/xml...

Author: rfeng
Date: Thu Mar 12 20:29:06 2009
New Revision: 753000

URL: http://svn.apache.org/viewvc?rev=753000&view=rev
Log:
Start to add code to listen on the bundles and create the SCA domain composite

Added:
    tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java   (with props)
    tuscany/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/sca/
    tuscany/java/sca/modules/implementation-osgi/src/test/resources/OSGI-INF/sca/bundle.componentType
      - copied unchanged from r752928, tuscany/java/sca/modules/implementation-osgi/src/test/resources/bundle.componentType
Removed:
    tuscany/java/sca/modules/implementation-osgi/src/test/resources/bundle.componentType
Modified:
    tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java
    tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java
    tuscany/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java

Modified: tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java?rev=753000&r1=752999&r2=753000&view=diff
==============================================================================
--- tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java (original)
+++ tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java Thu Mar 12 20:29:06 2009
@@ -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();
+            }
+        }
+
+    }
+
 }

Added: tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java?rev=753000&view=auto
==============================================================================
--- tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java (added)
+++ tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java Thu Mar 12 20:29:06 2009
@@ -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());
+        }
+    }
+
+}

Propchange: tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationManager.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java?rev=753000&r1=752999&r2=753000&view=diff
==============================================================================
--- tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java (original)
+++ tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java Thu Mar 12 20:29:06 2009
@@ -200,7 +200,7 @@
         }
 
         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()) {

Modified: tuscany/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java?rev=753000&r1=752999&r2=753000&view=diff
==============================================================================
--- tuscany/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java (original)
+++ tuscany/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java Thu Mar 12 20:29:06 2009
@@ -96,7 +96,7 @@
         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);