diff options
3 files changed, 74 insertions, 11 deletions
diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java index ddcd49a235..ab139519c8 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java @@ -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.RuntimeComponent; 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 class OSGiImplementationProvider implements ImplementationProvider { } 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 @@ public class OSGiImplementationProvider implements ImplementationProvider { 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 class OSGiImplementationProvider implements ImplementationProvider { 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() { 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 db99bf510e..547de99f50 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 @@ -24,6 +24,8 @@ import static org.apache.tuscany.sca.implementation.osgi.OSGiImplementation.BUND 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.ContributionReadException; 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 @@ import org.osgi.framework.Version; * @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 @@ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiIm 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 @@ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiIm 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 @@ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiIm // 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 @@ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiIm 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; + } + } diff --git a/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java b/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java index a141cbeb21..268f16bc9f 100644 --- a/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java +++ b/java/sca/modules/node-impl-osgi/src/test/java/calculator/dosgi/test/CalculatorOSGiNodeTestCase.java @@ -81,7 +81,7 @@ public class CalculatorOSGiNodeTestCase { 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 @@ public class CalculatorOSGiNodeTestCase { System.out.println(bundleStatus(b, false)); } } - + if (scaBundle != null) { scaBundle.start(); } @@ -141,6 +141,9 @@ public class CalculatorOSGiNodeTestCase { @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(); |