Author: rfeng
Date: Tue May  5 23:53:06 2009
New Revision: 772014

URL: http://svn.apache.org/viewvc?rev=772014&view=rev
Log:
Allow the SCA composite for bundles can be packaged in an external bundle and 
leave the original bundles as is

Modified:
    
tuscany/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java
    
tuscany/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java
    
tuscany/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java

Modified: 
tuscany/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java?rev=772014&r1=772013&r2=772014&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java
 (original)
+++ 
tuscany/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java
 Tue May  5 23:53:06 2009
@@ -6,15 +6,15 @@
  * 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.    
+ * under the License.
  */
 
 package org.apache.tuscany.sca.implementation.osgi.runtime;
@@ -38,13 +38,14 @@
 import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
 import org.apache.tuscany.sca.runtime.RuntimeComponentService;
 import org.apache.tuscany.sca.runtime.RuntimeWire;
+import org.oasisopen.sca.ServiceRuntimeException;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 
 /**
- * 
+ *
  */
 public class OSGiImplementationProvider implements ImplementationProvider {
     private RuntimeComponent component;
@@ -66,6 +67,15 @@
     }
 
     public void start() {
+        // First try to start the osgi bundle
+        try {
+            int state = osgiBundle.getState();
+            if ((state & Bundle.STARTING) == 0 && (state & Bundle.ACTIVE) == 
0) {
+                osgiBundle.start();
+            }
+        } catch (BundleException e) {
+            throw new ServiceRuntimeException(e);
+        }
         for (ComponentReference ref : component.getReferences()) {
             RuntimeComponentReference reference = 
(RuntimeComponentReference)ref;
             InterfaceContract interfaceContract = 
reference.getInterfaceContract();
@@ -88,9 +98,8 @@
                 final Object proxy = proxyService.createProxy(interfaceClass, 
wire);
                 AccessController.doPrivileged(new 
PrivilegedAction<ServiceRegistration>() {
                     public ServiceRegistration run() {
-                        return 
osgiBundle.getBundleContext().registerService(interfaceClass.getName(),
-                                                                             
proxy,
-                                                                             
osgiProps);
+                        return osgiBundle.getBundleContext()
+                            .registerService(interfaceClass.getName(), proxy, 
osgiProps);
                     }
                 });
             }
@@ -100,6 +109,14 @@
 
     public void stop() {
         // Do we have to unregister the services?
+        try {
+            int state = osgiBundle.getState();
+            if ((state & Bundle.STARTING) == 0) {
+                osgiBundle.stop();
+            }
+        } catch (BundleException e) {
+            throw new ServiceRuntimeException(e);
+        }
     }
 
     public boolean supportsOneWayInvocation() {

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=772014&r1=772013&r2=772014&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
 Tue May  5 23:53:06 2009
@@ -24,6 +24,8 @@
 import static 
org.apache.tuscany.sca.implementation.osgi.OSGiImplementation.BUNDLE_VERSION;
 import static 
org.apache.tuscany.sca.implementation.osgi.OSGiImplementation.IMPLEMENTATION_OSGI;
 
+import java.net.URI;
+import java.net.URL;
 import java.util.List;
 
 import javax.xml.namespace.QName;
@@ -40,8 +42,11 @@
 import 
org.apache.tuscany.sca.contribution.processor.ContributionResolveException;
 import 
org.apache.tuscany.sca.contribution.processor.ContributionWriteException;
 import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor;
+import 
org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint;
 import org.apache.tuscany.sca.contribution.resolver.ClassReference;
 import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.core.FactoryExtensionPoint;
 import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation;
 import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory;
@@ -71,13 +76,18 @@
  * @version $Rev$ $Date$
  */
 public class OSGiImplementationProcessor implements 
StAXArtifactProcessor<OSGiImplementation> {
+    private static final String BUNDLE_COMPONENT_TYPE = 
"OSGI-INF/sca/bundle.componentType";
+    private static final String COMPONENT_TYPE_HEADER = "SCA-ComponentType";
+
     private AssemblyFactory assemblyFactory;
     private ServiceDescriptionsFactory serviceDescriptionsFactory;
     private OSGiImplementationFactory osgiImplementationFactory;
     private JavaInterfaceFactory javaInterfaceFactory;
     private Monitor monitor;
+    private ExtensionPointRegistry registry;
+    private StAXArtifactProcessor artifactProcessor;
 
-    public OSGiImplementationProcessor(FactoryExtensionPoint modelFactories, 
Monitor monitor) {
+    protected OSGiImplementationProcessor(FactoryExtensionPoint 
modelFactories, Monitor monitor) {
         this.monitor = monitor;
         this.serviceDescriptionsFactory = 
modelFactories.getFactory(ServiceDescriptionsFactory.class);
         this.assemblyFactory = 
modelFactories.getFactory(AssemblyFactory.class);
@@ -85,6 +95,12 @@
         this.javaInterfaceFactory = 
modelFactories.getFactory(JavaInterfaceFactory.class);
     }
 
+    public OSGiImplementationProcessor(ExtensionPointRegistry registry, 
StAXArtifactProcessor processor, Monitor monitor) {
+        this(registry.getExtensionPoint(FactoryExtensionPoint.class), monitor);
+        this.artifactProcessor = processor;
+        this.registry = registry;
+    }
+
     /**
      * Report a error.
      *
@@ -172,6 +188,14 @@
             return;
         }
 
+        try {
+            if (introspect(impl, resolver, bundle)) {
+                return;
+            }
+        } catch (ContributionReadException e) {
+            throw new ContributionResolveException(e);
+        }
+
         // The bundle may be different from the current contribution
         ComponentType componentType = assemblyFactory.createComponentType();
         // Try to find a bundle.componentType for the target bundle
@@ -183,7 +207,7 @@
             // See 
org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver.resolveModel(Class<T>,
 T)
             componentType = assemblyFactory.createComponentType();
             // Try a generic one
-            componentType.setURI("OSGI-INF/sca/bundle.componentType");
+            componentType.setURI(BUNDLE_COMPONENT_TYPE);
             componentType = resolver.resolveModel(ComponentType.class, 
componentType);
         }
         if (componentType.isUnresolved()) {
@@ -293,4 +317,23 @@
         writer.writeEndElement();
     }
 
+    private boolean introspect(OSGiImplementation implementation, 
ModelResolver resolver, Bundle bundle)
+        throws ContributionReadException, ContributionResolveException {
+        String componentTypeFile = 
(String)bundle.getHeaders().get(COMPONENT_TYPE_HEADER);
+        if (componentTypeFile == null) {
+            componentTypeFile = BUNDLE_COMPONENT_TYPE;
+        }
+        URL url = bundle.getEntry(componentTypeFile);
+        if (url != null) {
+            URLArtifactProcessorExtensionPoint processors =
+                
registry.getExtensionPoint(URLArtifactProcessorExtensionPoint.class);
+            URLArtifactProcessor<ComponentType> processor = 
processors.getProcessor(ComponentType.class);
+            ComponentType componentType = processor.read(null, 
URI.create(BUNDLE_COMPONENT_TYPE), url);
+            artifactProcessor.resolve(componentType, resolver);
+            mergeFromComponentType(implementation, componentType, resolver);
+            return true;
+        }
+        return false;
+    }
+
 }

Modified: 
tuscany/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java?rev=772014&r1=772013&r2=772014&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java
 (original)
+++ 
tuscany/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java
 Tue May  5 23:53:06 2009
@@ -81,7 +81,7 @@
                 System.out.println("Generating calculator.dosgi.operations 
bundle...");
                 operationsBundle = context.installBundle("reference:" + 
generateOperationsBundle().toString());
             }
-            
+
             scaBundle = context.installBundle("reference:" + 
generateCalculatorSCABundle().toString());
 
             for (Bundle b : context.getBundles()) {
@@ -98,7 +98,7 @@
                     System.out.println(bundleStatus(b, false));
                 }
             }
-            
+
             if (scaBundle != null) {
                 scaBundle.start();
             }
@@ -141,6 +141,9 @@
     @AfterClass
     public static void tearDownAfterClass() throws Exception {
         if (host != null) {
+            if (scaBundle != null) {
+                scaBundle.stop();
+            }
             if (client != null && !client.booleanValue()) {
                 System.out.println("Press Enter to stop the node...");
                 System.in.read();


Reply via email to