diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-03 00:14:53 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-03 00:14:53 +0000 |
commit | 56589672690a32a51b4fb1273133ffbe17f38739 (patch) | |
tree | 8e8d71840fa9c924874b766ae800454707e6fa42 /branches | |
parent | 960ec639e4c50b2f13f806b92d26bccd29862f0c (diff) |
Merge changes from trunk so that OSGi remote services can be run with Equinox and Felix over tribes's multicast. This introduces a dependency on tuscany maven-bundle-plugin 1.0.5-SNAPSHOT and we should release it with M4.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@832215 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
26 files changed, 698 insertions, 298 deletions
diff --git a/branches/sca-java-2.0-M4/distribution/all/pom.xml b/branches/sca-java-2.0-M4/distribution/all/pom.xml index b6f63538ca..e1df7a10f4 100644 --- a/branches/sca-java-2.0-M4/distribution/all/pom.xml +++ b/branches/sca-java-2.0-M4/distribution/all/pom.xml @@ -64,7 +64,7 @@ <plugin> <groupId>org.apache.tuscany.maven.plugins</groupId> <artifactId>maven-bundle-plugin</artifactId> - <version>1.0.4</version> + <version>1.0.5-SNAPSHOT</version> <executions> <execution> <id>distribution-modules</id> diff --git a/branches/sca-java-2.0-M4/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java b/branches/sca-java-2.0-M4/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java index c38061c1bf..59fa5990d6 100644 --- a/branches/sca-java-2.0-M4/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java +++ b/branches/sca-java-2.0-M4/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java @@ -39,6 +39,7 @@ import javax.xml.XMLConstants; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParserFactory; import javax.xml.stream.EventFilter; import javax.xml.stream.StreamFilter; import javax.xml.stream.XMLEventReader; @@ -49,6 +50,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.util.XMLEventAllocator; import javax.xml.transform.Source; +import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; @@ -56,9 +58,11 @@ import javax.xml.validation.SchemaFactory; import org.apache.tuscany.sca.assembly.xsd.Constants; import org.apache.tuscany.sca.common.xml.XMLDocumentHelper; import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.extensibility.ClassLoaderContext; import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.MonitorFactory; import org.w3c.dom.DOMImplementation; @@ -75,7 +79,7 @@ import org.xml.sax.SAXException; * @version $Rev$ $Date$ */ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory implements LSResourceResolver { - + private ExtensionPointRegistry registry; private XMLInputFactory inputFactory; private DocumentBuilderFactory documentBuilderFactory; private DOMImplementationLS ls; @@ -87,6 +91,7 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory private StAXHelper helper; public DefaultValidatingXMLInputFactory(ExtensionPointRegistry registry) { + this.registry = registry; FactoryExtensionPoint factoryExtensionPoint = registry.getExtensionPoint(FactoryExtensionPoint.class); this.inputFactory = factoryExtensionPoint.getFactory(XMLInputFactory.class); this.documentBuilderFactory = factoryExtensionPoint.getFactory(DocumentBuilderFactory.class); @@ -102,9 +107,11 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory * @param inputFactory * @param schemas */ + // FOR Test only public DefaultValidatingXMLInputFactory(XMLInputFactory inputFactory, ValidationSchemaExtensionPoint schemas) { this.inputFactory = inputFactory; this.schemas = schemas; + this.registry = new DefaultExtensionPointRegistry(); } @@ -188,8 +195,7 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory } final Collection<? extends Source> sources = aggregate(urls); - // Create an aggregated validation schemas from all the XSDs - final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + final SchemaFactory schemaFactory = newSchemaFactory(); DOMImplementation impl = null; try { impl = documentBuilderFactory.newDocumentBuilder().getDOMImplementation(); @@ -225,6 +231,30 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory } } + /** + * For OSGi: + * Create a SchemaFactory in the context of service provider classloaders + * @return + */ + private SchemaFactory newSchemaFactory() { + ClassLoader cl = + ClassLoaderContext.setContextClassLoader(getClass().getClassLoader(), + registry.getServiceDiscovery(), + SchemaFactory.class, + TransformerFactory.class, + SAXParserFactory.class, + DocumentBuilderFactory.class + ); + try { + // Create an aggregated validation schemas from all the XSDs + return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + } finally { + if (cl != null) { + Thread.currentThread().setContextClassLoader(cl); + } + } + } + @Override public XMLEventReader createFilteredReader(XMLEventReader arg0, EventFilter arg1) throws XMLStreamException { return inputFactory.createFilteredReader(arg0, arg1); diff --git a/branches/sca-java-2.0-M4/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java b/branches/sca-java-2.0-M4/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java index 91db32b806..132b7a8858 100644 --- a/branches/sca-java-2.0-M4/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java +++ b/branches/sca-java-2.0-M4/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java @@ -33,6 +33,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; +import org.apache.tuscany.sca.assembly.Extension; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; @@ -56,7 +57,6 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob private XMLInputFactory inputFactory; private XMLOutputFactory outputFactory; private StAXArtifactProcessorExtensionPoint processors; - /** * Constructs a new ExtensibleStAXArtifactProcessor. @@ -172,7 +172,6 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob @SuppressWarnings("unchecked") public void write(Object model, XMLStreamWriter outputSource, ProcessorContext context) throws ContributionWriteException, XMLStreamException { - Monitor monitor = context.getMonitor(); // Delegate to the processor associated with the model type if (model != null) { StAXArtifactProcessor processor = processors.getProcessor(model.getClass()); @@ -182,11 +181,8 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob if (logger.isLoggable(Level.WARNING)) { logger.warning("No StAX processor is configured to handle " + model.getClass()); } - if (!XMLStreamReader.class.isInstance(model)) { - warning(monitor, "NoStaxProcessor", processors, model.getClass()); - } StAXArtifactProcessor anyElementProcessor = processors.getProcessor(ANY_ELEMENT); - if (anyElementProcessor != null) { + if ((model instanceof Extension) && anyElementProcessor != null) { anyElementProcessor.write(model, outputSource, context); } } diff --git a/branches/sca-java-2.0-M4/modules/core/META-INF/MANIFEST.MF b/branches/sca-java-2.0-M4/modules/core/META-INF/MANIFEST.MF index 98800de77d..5205527437 100644 --- a/branches/sca-java-2.0-M4/modules/core/META-INF/MANIFEST.MF +++ b/branches/sca-java-2.0-M4/modules/core/META-INF/MANIFEST.MF @@ -1,5 +1,7 @@ Manifest-Version: 1.0
-Export-Package: org.apache.tuscany.sca.core.assembly;version="2.0.0";uses:="org.apache.tuscany.sca.assembly,org.apache.tuscany.sca.core",
+Export-Package: org.apache.tuscany.sca.core.assembly;version="2.0.0";
+ uses:="org.apache.tuscany.sca.assembly,org.apache.tuscany.sca.core",
+ org.apache.tuscany.sca.core.assembly.impl;version="2.0.0";scope=internal;mandatory:=scope,
org.apache.tuscany.sca.core.context;version="2.0.0";
uses:="javax.xml.stream,
org.apache.tuscany.sca.runtime,
diff --git a/branches/sca-java-2.0-M4/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java b/branches/sca-java-2.0-M4/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java index 1c4ded13fc..ecc1a4e599 100644 --- a/branches/sca-java-2.0-M4/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java +++ b/branches/sca-java-2.0-M4/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java @@ -497,7 +497,26 @@ public class CompositeActivatorImpl implements CompositeActivator { } compositeContext.bindComponent(runtimeComponent); + Implementation implementation = component.getImplementation(); + + if (implementation instanceof Composite) { + start(compositeContext, (Composite)implementation); + } else { + for (PolicyProvider policyProvider : runtimeComponent.getPolicyProviders()) { + policyProvider.start(); + } + ImplementationProvider implementationProvider = runtimeComponent.getImplementationProvider(); + if (implementationProvider != null) { + implementationProvider.start(); + } + } + if (component instanceof ScopedRuntimeComponent) { + ScopedRuntimeComponent scopedRuntimeComponent = (ScopedRuntimeComponent)component; + if (scopedRuntimeComponent.getScopeContainer() != null) { + scopedRuntimeComponent.getScopeContainer().start(); + } + } // Reference bindings aren't started until the wire is first used for (ComponentService service : component.getServices()) { @@ -526,26 +545,6 @@ public class CompositeActivatorImpl implements CompositeActivator { } } - Implementation implementation = component.getImplementation(); - if (implementation instanceof Composite) { - start(compositeContext, (Composite)implementation); - } else { - for (PolicyProvider policyProvider : runtimeComponent.getPolicyProviders()) { - policyProvider.start(); - } - ImplementationProvider implementationProvider = runtimeComponent.getImplementationProvider(); - if (implementationProvider != null) { - implementationProvider.start(); - } - } - - if (component instanceof ScopedRuntimeComponent) { - ScopedRuntimeComponent scopedRuntimeComponent = (ScopedRuntimeComponent)component; - if (scopedRuntimeComponent.getScopeContainer() != null) { - scopedRuntimeComponent.getScopeContainer().start(); - } - } - runtimeComponent.setStarted(true); } diff --git a/branches/sca-java-2.0-M4/modules/endpoint-tribes/META-INF/MANIFEST.MF b/branches/sca-java-2.0-M4/modules/endpoint-tribes/META-INF/MANIFEST.MF index 8c7feecb30..83df715831 100644 --- a/branches/sca-java-2.0-M4/modules/endpoint-tribes/META-INF/MANIFEST.MF +++ b/branches/sca-java-2.0-M4/modules/endpoint-tribes/META-INF/MANIFEST.MF @@ -21,6 +21,7 @@ Import-Package: org.apache.catalina.tribes, org.apache.catalina.tribes.util,
org.apache.juli.logging;resolution:=optional,
org.apache.tuscany.sca.assembly;version="2.0.0",
+ org.apache.tuscany.sca.core.assembly.impl;version="2.0.0";scope=internal;resolution:=optional,
org.apache.tuscany.sca.core;version="2.0.0",
org.apache.tuscany.sca.management;version="2.0.0",
org.apache.tuscany.sca.policy;version="2.0.0",
diff --git a/branches/sca-java-2.0-M4/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java b/branches/sca-java-2.0-M4/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java index 3a703bca62..ac2933564e 100644 --- a/branches/sca-java-2.0-M4/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java +++ b/branches/sca-java-2.0-M4/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java @@ -354,10 +354,12 @@ public class ReplicatedEndpointRegistry implements EndpointRegistry, LifeCycleLi public void entryAdded(Object key, Object value) { MapEntry entry = (MapEntry)value; + Endpoint newEp = (Endpoint)entry.getValue(); if (!isLocal(entry)) { logger.info(id + " Remote endpoint added: " + entry.getValue()); + newEp.setRemote(true); } - Endpoint newEp = (Endpoint)entry.getValue(); + newEp.setExtensionPointRegistry(registry); for (EndpointListener listener : listeners) { listener.endpointAdded(newEp); } diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java b/branches/sca-java-2.0-M4/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java index 7b5b5f2ea1..9111a7d03c 100644 --- a/branches/sca-java-2.0-M4/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java @@ -37,6 +37,7 @@ import org.apache.tuscany.sca.assembly.Extensible; import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; +import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory; import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; @@ -63,14 +64,18 @@ public class OSGiImplementationProvider implements ImplementationProvider { private RuntimeComponent component; private ProxyFactoryExtensionPoint proxyFactoryExtensionPoint; private Bundle osgiBundle; + private boolean startedByMe; private OSGiImplementation implementation; private List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>(); + private OSGiImplementationFactory implementationFactory; public OSGiImplementationProvider(RuntimeComponent component, OSGiImplementation impl, - ProxyFactoryExtensionPoint proxyFactoryExtensionPoint) throws BundleException { + ProxyFactoryExtensionPoint proxyFactoryExtensionPoint, + OSGiImplementationFactory implementationFactory) throws BundleException { this.component = component; this.proxyFactoryExtensionPoint = proxyFactoryExtensionPoint; + this.implementationFactory = implementationFactory; this.implementation = impl; this.osgiBundle = impl.getBundle(); } @@ -85,6 +90,7 @@ public class OSGiImplementationProvider implements ImplementationProvider { int state = osgiBundle.getState(); if ((state & Bundle.STARTING) == 0 && (state & Bundle.ACTIVE) == 0) { osgiBundle.start(); + startedByMe = true; } } catch (BundleException e) { throw new ServiceRuntimeException(e); @@ -93,8 +99,8 @@ public class OSGiImplementationProvider implements ImplementationProvider { for (ComponentReference ref : component.getReferences()) { RuntimeComponentReference reference = (RuntimeComponentReference)ref; InterfaceContract interfaceContract = reference.getInterfaceContract(); - JavaInterface javaInterface = (JavaInterface)interfaceContract.getInterface(); - final Class<?> interfaceClass = javaInterface.getJavaClass(); + final JavaInterface javaInterface = (JavaInterface)interfaceContract.getInterface(); + // final Class<?> interfaceClass = javaInterface.getJavaClass(); // final Hashtable<String, Object> props = new Hashtable<String, Object>(); // props.put(FILTER_MATCH_CRITERIA, ""); @@ -109,20 +115,39 @@ public class OSGiImplementationProvider implements ImplementationProvider { osgiProps.put(SERVICE_IMPORTED_CONFIGS, new String[] {REMOTE_CONFIG_SCA}); for (RuntimeWire wire : reference.getRuntimeWires()) { - final OSGiServiceFactory serviceFactory = new OSGiServiceFactory(interfaceClass.getName(), wire); + final OSGiServiceFactory serviceFactory = new OSGiServiceFactory(javaInterface.getName(), wire); ServiceRegistration registration = AccessController.doPrivileged(new PrivilegedAction<ServiceRegistration>() { public ServiceRegistration run() { // Register the proxy as OSGi service BundleContext context = osgiBundle.getBundleContext(); ServiceRegistration registration = - context.registerService(interfaceClass.getName(), serviceFactory, osgiProps); + context.registerService(javaInterface.getName(), serviceFactory, osgiProps); return registration; } }); registrations.add(registration); } } + + // Set the OSGi service reference properties into the SCA service + for (ComponentService service : component.getServices()) { + // The properties might have been set by the export service + boolean found = false; + for (Object ext : service.getExtensions()) { + if (ext instanceof OSGiProperty) { + found = true; + break; + } + } + if (found) { + continue; + } + ServiceReference serviceReference = getServiceReference(osgiBundle.getBundleContext(), service); + if (serviceReference != null) { + service.getExtensions().addAll(implementationFactory.createOSGiProperties(serviceReference)); + } + } } public void stop() { @@ -134,13 +159,18 @@ public class OSGiImplementationProvider implements ImplementationProvider { } } registrations.clear(); - try { - int state = osgiBundle.getState(); - if ((state & Bundle.STOPPING) == 0 && (state & Bundle.ACTIVE) != 0) { - osgiBundle.stop(); + // [REVIEW] Shoud it take care of stopping the bundle? + if (startedByMe) { + try { + int state = osgiBundle.getState(); + if ((state & Bundle.STOPPING) == 0 && (state & Bundle.ACTIVE) != 0) { + osgiBundle.stop(); + } + } catch (BundleException e) { + throw new ServiceRuntimeException(e); + } finally { + startedByMe = false; } - } catch (BundleException e) { - throw new ServiceRuntimeException(e); } } @@ -165,24 +195,29 @@ public class OSGiImplementationProvider implements ImplementationProvider { } protected Object getOSGiService(ComponentService service) { + BundleContext bundleContext = osgiBundle.getBundleContext(); + ServiceReference ref = getServiceReference(bundleContext, service); + if (ref != null) { + Object instance = bundleContext.getService(ref); + return instance; + } else { + return null; + } + } + + private ServiceReference getServiceReference(BundleContext bundleContext, ComponentService service) { JavaInterface javaInterface = (JavaInterface)service.getInterfaceContract().getInterface(); // String filter = getOSGiFilter(provider.getOSGiProperties(service)); // FIXME: What is the filter? String filter = "(!(" + SERVICE_IMPORTED + "=*))"; // "(sca.service=" + component.getURI() + "#service-name\\(" + service.getName() + "\\))"; - BundleContext bundleContext = osgiBundle.getBundleContext(); ServiceReference ref; try { ref = bundleContext.getServiceReferences(javaInterface.getName(), filter)[0]; } catch (InvalidSyntaxException e) { throw new ServiceRuntimeException(e); } - if (ref != null) { - Object instance = bundleContext.getService(ref); - return instance; - } else { - return null; - } + return ref; } RuntimeComponent getComponent() { diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java b/branches/sca-java-2.0-M4/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java index 0e4a3ef995..11b29de248 100644 --- a/branches/sca-java-2.0-M4/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java @@ -19,8 +19,10 @@ package org.apache.tuscany.sca.implementation.osgi.runtime; import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; +import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory; import org.apache.tuscany.sca.provider.ImplementationProvider; import org.apache.tuscany.sca.provider.ImplementationProviderFactory; import org.apache.tuscany.sca.runtime.RuntimeComponent; @@ -32,20 +34,20 @@ import org.osgi.framework.BundleException; * @version $Rev$ $Date$ */ public class OSGiImplementationProviderFactory implements ImplementationProviderFactory<OSGiImplementation> { - + private OSGiImplementationFactory implementationFactory; private ProxyFactoryExtensionPoint proxyFactoryExtensionPoint; - public OSGiImplementationProviderFactory(ExtensionPointRegistry extensionPoints) { - proxyFactoryExtensionPoint = extensionPoints.getExtensionPoint(ProxyFactoryExtensionPoint.class); + public OSGiImplementationProviderFactory(ExtensionPointRegistry registry) { + proxyFactoryExtensionPoint = registry.getExtensionPoint(ProxyFactoryExtensionPoint.class); + FactoryExtensionPoint factoryExtensionPoint = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.implementationFactory = factoryExtensionPoint.getFactory(OSGiImplementationFactory.class); } public ImplementationProvider createImplementationProvider(RuntimeComponent component, OSGiImplementation implementation) { - try { - - return new OSGiImplementationProvider(component, implementation, proxyFactoryExtensionPoint); - + return new OSGiImplementationProvider(component, implementation, proxyFactoryExtensionPoint, + implementationFactory); } catch (BundleException e) { throw new RuntimeException(e); } diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java index 0a83624e76..be39e4d3e0 100644 --- a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java @@ -19,6 +19,10 @@ package org.apache.tuscany.sca.implementation.osgi; +import java.util.Collection; + +import org.osgi.framework.ServiceReference; + /** * The factory interface to create OSGiImplementation instances */ @@ -34,4 +38,7 @@ public interface OSGiImplementationFactory { * @return */ OSGiProperty createOSGiProperty(); + OSGiProperty createOSGiProperty(String name, String stringValue, String type); + OSGiProperty createOSGiProperty(String name, Object value); + Collection<OSGiProperty> createOSGiProperties(ServiceReference reference); } diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiProperty.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiProperty.java index e0b7f43add..ebe4ef2dbc 100644 --- a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiProperty.java +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiProperty.java @@ -26,6 +26,8 @@ import javax.xml.namespace.QName; */ public interface OSGiProperty { String NAME = "name"; + String TYPE = "type"; + String VALUE = "value"; QName PROPERTY_QNAME = new QName(OSGiImplementation.SCA11_TUSCANY_NS, "osgi.property"); String REMOTE_CONFIG_SCA = "org.osgi.sca"; @@ -169,11 +171,16 @@ public interface OSGiProperty { public final String SERVICE_IMPORTED_CONFIGS = "service.imported.configs"; - String getValue(); - - void setValue(String value); + Object getValue(); + void setValue(Object value); String getName(); void setName(String name); + + String getType(); + void setType(String type); + + String getStringValue(); + void setStringValue(String value); } diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java index 9fab65272f..2978e8e823 100644 --- a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java @@ -19,10 +19,15 @@ package org.apache.tuscany.sca.implementation.osgi.impl; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory; import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; +import org.osgi.framework.ServiceReference; /** * @@ -40,4 +45,72 @@ public class OSGiImplementationFactoryImpl implements OSGiImplementationFactory return new OSGiPropertyImpl(); } + public OSGiProperty createOSGiProperty(String propName, String propValue, String propType) { + OSGiProperty prop = new OSGiPropertyImpl(); + if (propType == null) { + propType = "String"; + } + prop.setName(propName); + prop.setStringValue(propValue); + prop.setType(propType); + + Object value = propValue; + if ("Integer".equals(propType)) { + value = Integer.valueOf(propValue); + } else if ("Long".equals(propType)) { + value = Long.valueOf(propValue); + } else if ("Float".equals(propType)) { + value = Float.valueOf(propValue); + } else if ("Double".equals(propType)) { + value = Double.valueOf(propValue); + } else if ("Short".equals(propType)) { + value = Short.valueOf(propValue); + } else if ("Character".equals(propType)) { + value = propValue.charAt(0); + } else if ("Byte".equals(propType)) { + value = Byte.valueOf(propValue); + } else if ("Boolean".equals(propType)) { + value = Boolean.valueOf(propValue); + } else if ("String+".equals(propType)) { + value = propValue.split(" "); + } else { + // String + value = propValue; + } + prop.setValue(value); + return prop; + } + + public OSGiProperty createOSGiProperty(String propName, Object value) { + OSGiProperty prop = new OSGiPropertyImpl(); + prop.setName(propName); + prop.setValue(value); + + if (value instanceof String[]) { + StringBuffer sb = new StringBuffer(); + for (String s : (String[])value) { + sb.append(s).append(' '); + } + if (sb.length() > 0) { + sb.deleteCharAt(sb.length() - 1); + } + prop.setStringValue(sb.toString()); + prop.setType("String+"); + } else if (value != null) { + prop.setStringValue(String.valueOf(value)); + prop.setType(value.getClass().getSimpleName()); + } + return prop; + } + + public Collection<OSGiProperty> createOSGiProperties(ServiceReference reference) { + List<OSGiProperty> props = new ArrayList<OSGiProperty>(); + for(String key: reference.getPropertyKeys()) { + Object value = reference.getProperty(key); + OSGiProperty prop = createOSGiProperty(key, value); + props.add(prop); + } + return props; + } + } diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiPropertyImpl.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiPropertyImpl.java index ec8b98f53f..b3e934a970 100644 --- a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiPropertyImpl.java +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiPropertyImpl.java @@ -25,13 +25,15 @@ import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; * Implementation of OSGiProperty */ public class OSGiPropertyImpl implements OSGiProperty { + private String name; + private String type; + private Object value; + private String stringValue; + public OSGiPropertyImpl() { super(); } - private String name; - private String value; - public String getName() { return name; } @@ -40,12 +42,28 @@ public class OSGiPropertyImpl implements OSGiProperty { this.name = name; } - public String getValue() { + public Object getValue() { return value; } - public void setValue(String value) { + public void setValue(Object value) { this.value = value; } + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(String stringValue) { + this.stringValue = stringValue; + } + } diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java index 9bd469df06..84734b88db 100644 --- a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java @@ -23,6 +23,8 @@ import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.NAME; import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.PROPERTY_QNAME; +import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.TYPE; +import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.VALUE; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; @@ -57,14 +59,18 @@ public class OSGiPropertyProcessor implements StAXArtifactProcessor<OSGiProperty case START_ELEMENT: QName name = reader.getName(); if (PROPERTY_QNAME.equals(name)) { - prop = factory.createOSGiProperty(); - prop.setName(reader.getAttributeValue(null, NAME)); - // After the following call, the reader will be positioned at END_ELEMENT - String text = reader.getElementText(); - if (text != null) { - text = text.trim(); + String propName = reader.getAttributeValue(null, NAME); + String propValue = reader.getAttributeValue(null, VALUE); + String propType = reader.getAttributeValue(null, TYPE); + + if (propValue == null) { + propValue = reader.getElementText(); + } + if (propValue != null) { + propValue = propValue.trim(); } - prop.setValue(text); + + prop = factory.createOSGiProperty(propName, propValue, propType); return prop; } break; @@ -90,7 +96,8 @@ public class OSGiPropertyProcessor implements StAXArtifactProcessor<OSGiProperty public void write(OSGiProperty model, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { writer.writeStartElement(PROPERTY_QNAME.getNamespaceURI(), PROPERTY_QNAME.getLocalPart()); writer.writeAttribute(NAME, model.getName()); - writer.writeCharacters(model.getValue()); + writer.writeAttribute(TYPE, model.getType()); + writer.writeCharacters(model.getStringValue()); writer.writeEndElement(); } diff --git a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/AbstractOSGiServiceHandler.java b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/AbstractOSGiServiceHandler.java index a043e33c32..7c9cf8ef6e 100644 --- a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/AbstractOSGiServiceHandler.java +++ b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/AbstractOSGiServiceHandler.java @@ -37,6 +37,7 @@ public class AbstractOSGiServiceHandler implements LifeCycleListener { protected EndpointIntrospector introspector; protected ServiceTracker discoveryTracker; protected String domainRegistry; + protected String domainURI; /** * @param context @@ -45,6 +46,8 @@ public class AbstractOSGiServiceHandler implements LifeCycleListener { */ protected AbstractOSGiServiceHandler(BundleContext context) { this.context = context; + this.domainRegistry = context.getProperty("org.osgi.sca.domain.registry"); + this.domainURI = context.getProperty("org.osgi.sca.domain.uri"); } protected ExtensionPointRegistry getExtensionPointRegistry() { @@ -87,4 +90,8 @@ public class AbstractOSGiServiceHandler implements LifeCycleListener { this.domainRegistry = domainRegistry; } + public void setDomainURI(String domainURI) { + this.domainURI = domainURI; + } + } diff --git a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java index 8ac949ccc5..d50d1ef9f9 100644 --- a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java +++ b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java @@ -23,9 +23,9 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.UUID; import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; import org.apache.tuscany.sca.interfacedef.Interface; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; @@ -37,7 +37,6 @@ import org.osgi.framework.Constants; * Implementation of {@link EndpointDescription} */ public class EndpointHelper { - private final static String FRAMEWORK_UUID = "org.osgi.framework.uuid"; private EndpointHelper() { } @@ -54,11 +53,21 @@ public class EndpointHelper { private static Map<String, Object> getProperties(BundleContext bundleContext, Endpoint endpoint) { Map<String, Object> props = new HashMap<String, Object>(); - String uuid = getFrameworkUUID(bundleContext); + if (!endpoint.isRemote()) { + String uuid = OSGiHelper.getFrameworkUUID(bundleContext); + props.put(RemoteConstants.SERVICE_REMOTE_FRAMEWORK_UUID, uuid); + } + + for (Object ext : endpoint.getService().getExtensions()) { + if (ext instanceof OSGiProperty) { + OSGiProperty prop = (OSGiProperty)ext; + props.put(prop.getName(), prop.getStringValue()); + } + } - props.put(RemoteConstants.SERVICE_REMOTE_FRAMEWORK_UUID, uuid); + props.put(RemoteConstants.SERVICE_REMOTE_ID, props.get(Constants.SERVICE_ID)); props.put(RemoteConstants.SERVICE_REMOTE_URI, endpoint.getURI()); - props.put(RemoteConstants.SERVICE_REMOTE_ID, String.valueOf(System.currentTimeMillis())); + // FIXME: [rfeng] How to pass in the remote service id from the endpoint XML props.put(RemoteConstants.SERVICE_EXPORTED_CONFIGS, new String[] {"org.osgi.sca"}); props.put(Endpoint.class.getName(), endpoint); List<String> interfaces = getInterfaces(endpoint); @@ -66,15 +75,6 @@ public class EndpointHelper { return props; } - public synchronized static String getFrameworkUUID(BundleContext bundleContext) { - String uuid = System.getProperty(FRAMEWORK_UUID); - if (uuid == null) { - uuid = UUID.randomUUID().toString(); - } - System.setProperty(FRAMEWORK_UUID, uuid); - return uuid; - } - public static Endpoint getEndpoint(EndpointDescription endpointDescription) { return (Endpoint)endpointDescription.getProperties().get(Endpoint.class.getName()); } diff --git a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java index c88632407c..a649dac357 100644 --- a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java +++ b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java @@ -24,6 +24,7 @@ import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SCA_BINDIN import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SERVICE_EXPORTED_INTENTS; import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SERVICE_EXPORTED_INTENTS_EXTRA; import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SERVICE_EXPORTED_INTERFACES; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.OSGiHelper.getStringArray; import static org.osgi.framework.Constants.OBJECTCLASS; import static org.osgi.framework.Constants.SERVICE_ID; @@ -55,17 +56,17 @@ import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; -import org.apache.tuscany.sca.core.UtilityExtensionPoint; -import org.apache.tuscany.sca.deployment.Deployer; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory; import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; import org.apache.tuscany.sca.implementation.osgi.SCAConfig; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; @@ -86,16 +87,17 @@ import org.osgi.util.tracker.ServiceTracker; * implementation.osgi */ public class EndpointIntrospector { - private BundleContext context; + // private BundleContext context; private AssemblyFactory assemblyFactory; private ContributionFactory contributionFactory; private OSGiImplementationFactory implementationFactory; private PolicyFactory policyFactory; - private ExtensionPointRegistry registry; + // private ExtensionPointRegistry registry; private FactoryExtensionPoint factories; private ModelResolverExtensionPoint modelResolvers; + // private StAXArtifactProcessor<Composite> compositeProcessor; private JavaInterfaceFactory javaInterfaceFactory; - private Deployer deployer; + // private Deployer deployer; private ServiceTracker discoveryTracker; /** @@ -124,17 +126,19 @@ public class EndpointIntrospector { */ public EndpointIntrospector(BundleContext context, ExtensionPointRegistry registry, ServiceTracker discoveryTracker) { super(); - this.context = context; + // this.context = context; this.discoveryTracker = discoveryTracker; - this.registry = registry; + // this.registry = registry; this.factories = registry.getExtensionPoint(FactoryExtensionPoint.class); this.modelResolvers = registry.getExtensionPoint(ModelResolverExtensionPoint.class); +// this.compositeProcessor = +// registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class).getProcessor(Composite.class); this.assemblyFactory = factories.getFactory(AssemblyFactory.class); this.contributionFactory = factories.getFactory(ContributionFactory.class); this.policyFactory = factories.getFactory(PolicyFactory.class); this.implementationFactory = factories.getFactory(OSGiImplementationFactory.class); this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); - this.deployer = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(Deployer.class); + // this.deployer = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(Deployer.class); } private Intent getIntent(String intent) { @@ -232,54 +236,79 @@ public class EndpointIntrospector { * @throws Exception */ public Contribution introspect(ServiceReference reference, Map<String, Object> props) throws Exception { + Bundle bundle = reference.getBundle(); Map<String, Object> properties = getProperties(reference, props); + Long sid = (Long)reference.getProperty(SERVICE_ID); - OSGiProperty serviceID = implementationFactory.createOSGiProperty(); - serviceID.setName(SERVICE_ID); - // The service.id is Long - serviceID.setValue(String.valueOf(reference.getProperty(SERVICE_ID))); + String[] requiredIntents = getStringArray(properties.get(SERVICE_EXPORTED_INTENTS)); + List<Intent> intents = getIntents(requiredIntents); + String[] requiredIntentsExtra = getStringArray(properties.get(SERVICE_EXPORTED_INTENTS_EXTRA)); + List<Intent> extraIntents = getIntents(requiredIntentsExtra); + Set<Intent> allIntents = new HashSet<Intent>(intents); + allIntents.addAll(extraIntents); + + String[] bindingNames = getStringArray(properties.get(SCA_BINDINGS)); + Collection<Binding> bindings = loadBindings(bindingNames); + + String[] remoteInterfaces = getStringArray(reference.getProperty(SERVICE_EXPORTED_INTERFACES)); + if (remoteInterfaces == null || remoteInterfaces.length > 0 && "*".equals(remoteInterfaces[0])) { + remoteInterfaces = getStringArray(reference.getProperty(OBJECTCLASS)); + } else { + remoteInterfaces = parse(remoteInterfaces); + String[] objectClasses = getStringArray(reference.getProperty(OBJECTCLASS)); + Set<String> objectClassSet = new HashSet<String>(Arrays.asList(objectClasses)); + if (!objectClassSet.containsAll(Arrays.asList(remoteInterfaces))) { + throw new IllegalArgumentException( + "The exported interfaces are not a subset of the types" + " listed in the objectClass service property from the Service Reference"); + } + } + + Contribution contribution = generateContribution(bundle, sid, remoteInterfaces, bindings, allIntents); + return contribution; + } + /** + * Generate a contribution that contains the composite for the exported service + * @param bundle The OSGi bundle + * @param sid The service id + * @param remoteInterfaces + * @param bindings + * @param allIntents + * @return + * @throws ClassNotFoundException + * @throws InvalidInterfaceException + */ + private Contribution generateContribution(Bundle bundle, + Long sid, + String[] remoteInterfaces, + Collection<Binding> bindings, + Set<Intent> allIntents) throws ClassNotFoundException, + InvalidInterfaceException { String id = "osgi.service." + UUID.randomUUID(); Composite composite = assemblyFactory.createComposite(); composite.setName(new QName(SCA11_TUSCANY_NS, id)); Component component = assemblyFactory.createComponent(); component.setName(id); - component.setAutowire(Boolean.TRUE); composite.getComponents().add(component); - Bundle bundle = reference.getBundle(); OSGiImplementation implementation = implementationFactory.createOSGiImplementation(); implementation.setBundle(bundle); component.setImplementation(implementation); implementation.setUnresolved(false); - String[] remoteInterfaces = getStrings(reference.getProperty(SERVICE_EXPORTED_INTERFACES)); - if (remoteInterfaces == null || remoteInterfaces.length > 0 && "*".equals(remoteInterfaces[0])) { - remoteInterfaces = getStrings(reference.getProperty(OBJECTCLASS)); - } else { - remoteInterfaces = parse(remoteInterfaces); - String[] objectClasses = getStrings(reference.getProperty(OBJECTCLASS)); - Set<String> objectClassSet = new HashSet<String>(Arrays.asList(objectClasses)); - if (!objectClassSet.containsAll(Arrays.asList(remoteInterfaces))) { - throw new IllegalArgumentException( - "The exported interfaces are not a subset of the types" + " listed in the objectClass service property from the Service Reference"); - } - } + OSGiProperty serviceID = implementationFactory.createOSGiProperty(); + serviceID.setName(SERVICE_ID); + // The service.id is Long + serviceID.setValue(String.valueOf(sid)); + for (String intf : remoteInterfaces) { Service service = assemblyFactory.createService(); - JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); - Class<?> interfaceClass = bundle.loadClass(intf); - JavaInterface javaInterface = javaInterfaceFactory.createJavaInterface(interfaceClass); - interfaceContract.setInterface(javaInterface); - if (javaInterface.getCallbackClass() != null) { - interfaceContract.setCallbackInterface(javaInterfaceFactory.createJavaInterface(javaInterface - .getCallbackClass())); - } - - service.setName(interfaceClass.getSimpleName()); + JavaInterfaceContract interfaceContract = createJavaInterfaceContract(bundle, intf); + String name = intf.substring(intf.lastIndexOf('.') + 1); + service.setName(name); service.setInterfaceContract(interfaceContract); service.getExtensions().add(serviceID); @@ -292,39 +321,63 @@ public class EndpointIntrospector { componentService.setService(service); } - String[] requiredIntents = getStrings(properties.get(SERVICE_EXPORTED_INTENTS)); - List<Intent> intents = getIntents(requiredIntents); - String[] requiredIntentsExtra = getStrings(properties.get(SERVICE_EXPORTED_INTENTS_EXTRA)); - List<Intent> extraIntents = getIntents(requiredIntentsExtra); - - String[] bindingNames = getStrings(properties.get(SCA_BINDINGS)); - Collection<Binding> bindings = loadBindings(bindingNames); - for (ComponentService componentService : component.getServices()) { - componentService.getRequiredIntents().addAll(intents); - componentService.getRequiredIntents().addAll(extraIntents); + componentService.getRequiredIntents().addAll(allIntents); componentService.getBindings().addAll(bindings); } // FIXME: Should we scan the owning bundle to create the SCA contribution? + Contribution contribution = createContribution(bundle, id, composite); + return contribution; + } + + private Contribution createContribution(Bundle bundle, String id, Composite composite) { Contribution contribution = contributionFactory.createContribution(); + contribution.setClassLoader(OSGiHelper.createBundleClassLoader(bundle)); contribution.setURI("urn:" + id); contribution.setLocation(bundle.getEntry("/").toString()); contribution.getDeployables().add(composite); ModelResolver modelResolver = new ExtensibleModelResolver(contribution, modelResolvers, factories); contribution.setModelResolver(modelResolver); + // compositeProcessor.resolve(composite, modelResolver, new ProcessorContext(registry)); contribution.setUnresolved(true); return contribution; } + /** + * @param bundle + * @param endpoint + * @return + * @throws Exception + */ public Contribution introspect(Bundle bundle, EndpointDescription endpoint) throws Exception { + Collection<Binding> bindings = Collections.emptyList(); + Collection<String> interfaces = Collections.emptyList(); + Collection<Intent> intents = Collections.emptyList(); Endpoint ep = (Endpoint)endpoint.getProperties().get(Endpoint.class.getName()); if (ep != null) { - return introspect(bundle, ep); + bindings = Collections.singletonList(ep.getBinding()); + interfaces = Collections.singletonList(((JavaInterface)ep.getInterfaceContract().getInterface()).getName()); + intents = ep.getRequiredIntents(); + } else { + Map<String, Object> properties = endpoint.getProperties(); + interfaces = endpoint.getInterfaces(); + String[] requiredIntents = getStringArray(properties.get(SERVICE_EXPORTED_INTENTS)); + intents = getIntents(requiredIntents); + + String[] bindingNames = getStringArray(properties.get(SCA_BINDINGS)); + bindings = loadBindings(bindingNames); } - Map<String, Object> properties = endpoint.getProperties(); - List<String> remoteInterfaces = endpoint.getInterfaces(); + Contribution contribution = generateContribution(bundle, interfaces, bindings, intents); + return contribution; + } + + private Contribution generateContribution(Bundle bundle, + Collection<String> remoteInterfaces, + Collection<Binding> bindings, + Collection<Intent> intents) throws ClassNotFoundException, + InvalidInterfaceException, ContributionResolveException { String id = "osgi.reference." + UUID.randomUUID(); Composite composite = assemblyFactory.createComposite(); composite.setName(new QName(Base.SCA11_TUSCANY_NS, id)); @@ -344,14 +397,7 @@ public class EndpointIntrospector { int count = 0; for (String intf : remoteInterfaces) { Reference reference = assemblyFactory.createReference(); - JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); - Class<?> interfaceClass = bundle.loadClass(intf); - JavaInterface javaInterface = javaInterfaceFactory.createJavaInterface(interfaceClass); - interfaceContract.setInterface(javaInterface); - if (javaInterface.getCallbackClass() != null) { - interfaceContract.setCallbackInterface(javaInterfaceFactory.createJavaInterface(javaInterface - .getCallbackClass())); - } + JavaInterfaceContract interfaceContract = createJavaInterfaceContract(bundle, intf); reference.setName("ref" + (count++)); reference.setInterfaceContract(interfaceContract); @@ -365,77 +411,30 @@ public class EndpointIntrospector { componentReference.setWiredByImpl(true); } - String[] requiredIntents = getStrings(properties.get(SERVICE_EXPORTED_INTENTS)); - List<Intent> intents = getIntents(requiredIntents); - - String[] bindingNames = getStrings(properties.get(SCA_BINDINGS)); - Collection<Binding> bindings = loadBindings(bindingNames); - for (ComponentReference componentReference : component.getReferences()) { componentReference.getRequiredIntents().addAll(intents); componentReference.getBindings().addAll(bindings); } - // FIXME: Should we scan the owning bundle to create the SCA contribution? - Contribution contribution = contributionFactory.createContribution(); - contribution.setURI("urn:" + id); - contribution.setLocation(bundle.getEntry("/").toString()); - contribution.getDeployables().add(composite); - ModelResolver modelResolver = new ExtensibleModelResolver(contribution, modelResolvers, factories); - contribution.setModelResolver(modelResolver); - contribution.setUnresolved(true); + + Contribution contribution = createContribution(bundle, id, composite); return contribution; } - public Contribution introspect(Bundle bundle, Endpoint endpoint) throws Exception { - String id = "osgi.reference." + UUID.randomUUID(); - Composite composite = assemblyFactory.createComposite(); - composite.setName(new QName(Base.SCA11_TUSCANY_NS, id)); - - Component component = assemblyFactory.createComponent(); - component.setName(id); - // component.setAutowire(Boolean.TRUE); - - composite.getComponents().add(component); - - OSGiImplementation implementation = implementationFactory.createOSGiImplementation(); - - implementation.setBundle(bundle); - component.setImplementation(implementation); - implementation.setUnresolved(false); - - Reference reference = assemblyFactory.createReference(); - Service service = endpoint.getService().getService(); - reference.setInterfaceContract(service.getInterfaceContract()); - reference.setName("ref"); - - reference.getBindings().add(endpoint.getBinding()); - - /* - reference.getRequiredIntents().addAll(service.getRequiredIntents()); - reference.getPolicySets().addAll(service.getPolicySets()); - */ - - implementation.getReferences().add(reference); - - ComponentReference componentReference = assemblyFactory.createComponentReference(); - component.getReferences().add(componentReference); - componentReference.setReference(reference); - componentReference.setName(reference.getName()); - componentReference.setWiredByImpl(true); - - // FIXME: Should we scan the owning bundle to create the SCA contribution? - Contribution contribution = contributionFactory.createContribution(); - contribution.setURI("urn:" + id); - contribution.setLocation(bundle.getEntry("/").toString()); - contribution.getDeployables().add(composite); - ModelResolver modelResolver = new ExtensibleModelResolver(contribution, modelResolvers, factories); - contribution.setModelResolver(modelResolver); - contribution.setUnresolved(true); - return contribution; + private JavaInterfaceContract createJavaInterfaceContract(Bundle bundle, String intf) + throws ClassNotFoundException, InvalidInterfaceException { + JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + Class<?> interfaceClass = bundle.loadClass(intf); + JavaInterface javaInterface = javaInterfaceFactory.createJavaInterface(interfaceClass); + interfaceContract.setInterface(javaInterface); + if (javaInterface.getCallbackClass() != null) { + interfaceContract.setCallbackInterface(javaInterfaceFactory.createJavaInterface(javaInterface + .getCallbackClass())); + } + return interfaceContract; } - private Collection<Binding> loadBindings(String[] qnames) throws IOException, - ContributionReadException, XMLStreamException { + private Collection<Binding> loadBindings(String[] qnames) throws IOException, ContributionReadException, + XMLStreamException { if (qnames == null || qnames.length == 0) { return Collections.emptyList(); } @@ -472,23 +471,4 @@ public class EndpointIntrospector { return bindingMap.values(); } - /** - * In OSGi, the value of String+ can be a single String, String[] or Collection<String> - * @param value - * @return - */ - private String[] getStrings(Object value) { - if (value == null) { - return null; - } - if (value instanceof String) { - return new String[] {(String)value}; - } else if (value instanceof Collection) { - Collection<String> collection = (Collection)value; - return collection.toArray(new String[collection.size()]); - } - return (String[])value; - - } - } diff --git a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java index d5a2b7aa95..a4b51d9d0c 100644 --- a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java +++ b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java @@ -19,13 +19,20 @@ package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; +import java.io.IOException; import java.net.URL; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; +import java.util.List; +import java.util.UUID; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory; +import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Filter; @@ -36,6 +43,8 @@ import org.osgi.framework.ServiceReference; * */ public class OSGiHelper { + public final static String FRAMEWORK_UUID = "org.osgi.framework.uuid"; + private OSGiHelper() { } @@ -131,5 +140,65 @@ public class OSGiHelper { } return files; } + + public static Collection<OSGiProperty> getOSGiProperties(ExtensionPointRegistry registry, ServiceReference reference) { + FactoryExtensionPoint factoryExtensionPoint = registry.getExtensionPoint(FactoryExtensionPoint.class); + OSGiImplementationFactory implementationFactory= factoryExtensionPoint.getFactory(OSGiImplementationFactory.class); + return implementationFactory.createOSGiProperties(reference); + } + + public static OSGiProperty createOSGiProperty(ExtensionPointRegistry registry, String name, Object value) { + FactoryExtensionPoint factoryExtensionPoint = registry.getExtensionPoint(FactoryExtensionPoint.class); + OSGiImplementationFactory implementationFactory= factoryExtensionPoint.getFactory(OSGiImplementationFactory.class); + return implementationFactory.createOSGiProperty(name, value); + } + + + public synchronized static String getFrameworkUUID(BundleContext bundleContext) { + String uuid = null; + if (bundleContext != null) { + uuid = bundleContext.getProperty(FRAMEWORK_UUID); + } else { + uuid = System.getProperty(FRAMEWORK_UUID); + } + if (uuid == null) { + uuid = UUID.randomUUID().toString(); + } + System.setProperty(FRAMEWORK_UUID, uuid); + return uuid; + } + + public static ClassLoader createBundleClassLoader(Bundle bundle) { + return new BundleClassLoader(bundle); + } + + private static class BundleClassLoader extends ClassLoader { + private Bundle bundle; + public BundleClassLoader(Bundle bundle) { + super(null); + this.bundle = bundle; + } + + @Override + protected Class<?> findClass(String name) throws ClassNotFoundException { + return bundle.loadClass(name); + } + + @Override + protected URL findResource(String name) { + return bundle.getResource(name); + } + + @Override + protected Enumeration<URL> findResources(String name) throws IOException { + Enumeration<URL> urls = bundle.getResources(name); + if (urls == null) { + List<URL> list = Collections.emptyList(); + return Collections.enumeration(list); + } else { + return urls; + } + } + } } diff --git a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java index 02603417cc..479ae54e42 100644 --- a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java +++ b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java @@ -19,7 +19,13 @@ package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteConstants.SERVICE_REMOTE_FRAMEWORK_UUID; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteConstants.SERVICE_REMOTE_ID; import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.EndpointHelper.createEndpointDescription; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.OSGiHelper.createOSGiProperty; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.OSGiHelper.getFrameworkUUID; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.OSGiHelper.getOSGiProperties; +import static org.osgi.framework.Constants.SERVICE_ID; import java.util.ArrayList; import java.util.Collections; @@ -70,14 +76,25 @@ public class OSGiServiceExporter extends AbstractOSGiServiceHandler implements S if (domainRegistry != null) { configuration.setDomainRegistryURI(domainRegistry); } + if (domainURI != null) { + configuration.setDomainURI(domainURI); + } configuration.setURI(contribution.getURI()); configuration.getExtensions().add(reference.getBundle()); + Component component = contribution.getDeployables().get(0).getComponents().get(0); + ComponentService service = component.getServices().get(0); + service.getExtensions().addAll(getOSGiProperties(registry, reference)); + service.getExtensions().add(createOSGiProperty(registry, + SERVICE_REMOTE_FRAMEWORK_UUID, + getFrameworkUUID(reference.getBundle() + .getBundleContext()))); + service.getExtensions().add(createOSGiProperty(registry, SERVICE_REMOTE_ID, reference + .getProperty(SERVICE_ID))); + // FIXME: Configure the domain and node URI NodeImpl node = new NodeImpl(nodeFactory, configuration, Collections.singletonList(contribution)); node.start(); List<ExportRegistration> exportedServices = new ArrayList<ExportRegistration>(); - Component component = contribution.getDeployables().get(0).getComponents().get(0); - ComponentService service = component.getServices().get(0); for (Endpoint endpoint : service.getEndpoints()) { EndpointDescription endpointDescription = createEndpointDescription(context, endpoint); ExportRegistration exportRegistration = diff --git a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java index 59a21ade06..c869ab8c14 100644 --- a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java +++ b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java @@ -61,6 +61,9 @@ public class OSGiServiceImporter extends AbstractOSGiServiceHandler { if (domainRegistry != null) { configuration.setDomainRegistryURI(domainRegistry); } + if (domainURI != null) { + configuration.setDomainURI(domainURI); + } configuration.setURI(contribution.getURI()); configuration.getExtensions().add(bundle); // FIXME: Configure the domain and node URI diff --git a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java index bc2e3ce012..9da7b5f014 100644 --- a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java +++ b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java @@ -276,9 +276,14 @@ public class RemoteServiceAdminImpl implements RemoteServiceAdmin, ManagedServic return; } String domainRegistry = (String)props.get("org.osgi.sca.domain.registry"); + String domainURI = (String)props.get("org.osgi.sca.domain.uri"); if (domainRegistry != null) { exporter.setDomainRegistry(domainRegistry); importer.setDomainRegistry(domainRegistry); } + if (domainURI != null) { + exporter.setDomainURI(domainURI); + importer.setDomainURI(domainURI); + } } } diff --git a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java index 2577a68808..bf70c98634 100644 --- a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java +++ b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java @@ -30,6 +30,7 @@ import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; import org.apache.tuscany.sca.runtime.DomainRegistryFactory; import org.apache.tuscany.sca.runtime.EndpointListener; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; /** @@ -56,8 +57,13 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements return; } - OSGiImplementation osgiImpl = (OSGiImplementation)impl; - BundleContext bundleContext = osgiImpl.getBundle().getBundleContext(); + BundleContext bundleContext = null; + // Remote endpoint doesn't have a bundle + if (!endpoint.isRemote()) { + OSGiImplementation osgiImpl = (OSGiImplementation)impl; + Bundle bundle = osgiImpl.getBundle(); + bundleContext = bundle != null ? bundle.getBundleContext() : null; + } /* if (!endpoint.isRemote()) { @@ -114,4 +120,5 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements props.put(SUPPORTED_PROTOCOLS, new String[] {"org.osgi.sca"}); return props; } + } diff --git a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java index 594fe32e54..11dcd56272 100644 --- a/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java +++ b/branches/sca-java-2.0-M4/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java @@ -38,7 +38,7 @@ import org.apache.tuscany.sca.implementation.osgi.ServiceDescription; import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions; import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteConstants; -import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.EndpointHelper; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.OSGiHelper; import org.oasisopen.sca.ServiceRuntimeException; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -92,7 +92,7 @@ public class LocalDiscoveryService extends AbstractDiscoveryService implements B props.put(RemoteConstants.SERVICE_REMOTE_ID, String.valueOf(System.currentTimeMillis())); } if (!props.containsKey(RemoteConstants.SERVICE_REMOTE_FRAMEWORK_UUID)) { - props.put(RemoteConstants.SERVICE_REMOTE_FRAMEWORK_UUID, EndpointHelper.getFrameworkUUID(context)); + props.put(RemoteConstants.SERVICE_REMOTE_FRAMEWORK_UUID, OSGiHelper.getFrameworkUUID(context)); } if (!props.containsKey(RemoteConstants.SERVICE_REMOTE_URI)) { props.put(RemoteConstants.SERVICE_REMOTE_URI, UUID.randomUUID().toString()); diff --git a/branches/sca-java-2.0-M4/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java b/branches/sca-java-2.0-M4/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java index 345a06880e..fd4d0ee5bf 100644 --- a/branches/sca-java-2.0-M4/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java +++ b/branches/sca-java-2.0-M4/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java @@ -186,71 +186,7 @@ public class EquinoxHost { try { if (injectedBundleContext == null) { - String version = getSystemProperty("java.specification.version"); - - /** - * [rfeng] I have to remove javax.transaction.* packages from the system bundle - * See: http://www.mail-archive.com/dev@geronimo.apache.org/msg70761.html - */ - String profile = "J2SE-1.5.profile"; - if (version.startsWith("1.6")) { - profile = "JavaSE-1.6.profile"; - } - Properties props = new Properties(); - InputStream is = getClass().getResourceAsStream(profile); - if (is != null) { - props.load(is); - is.close(); - } - - props.putAll(getSystemProperties()); - - // Configure Eclipse properties - - // Use the boot classloader as the parent classloader - put(props, PROP_OSGI_CONTEXT_CLASS_LOADER_PARENT, "app"); - - // Set startup properties - put(props, PROP_OSGI_CLEAN, "true"); - - // Set location properties - // FIXME Use proper locations - String tmpDir = getSystemProperty("java.io.tmpdir"); - File root = new File(tmpDir); - // Add user name as the prefix. For multiple users on the same Lunix, - // there will be permission issue if one user creates the .tuscany folder - // first under /tmp with no write permission for others. - String userName = getSystemProperty(PROP_USER_NAME); - if (userName != null) { - root = new File(root, userName); - } - root = new File(root, ".tuscany/equinox/" + UUID.randomUUID().toString()); - if (logger.isLoggable(Level.FINE)) { - logger.fine("Equinox location: " + root); - } - - put(props, PROP_INSTANCE_AREA_DEFAULT, new File(root, "workspace").toURI().toString()); - put(props, PROP_INSTALL_AREA, new File(root, "install").toURI().toString()); - put(props, PROP_CONFIG_AREA_DEFAULT, new File(root, "config").toURI().toString()); - put(props, PROP_USER_AREA_DEFAULT, new File(root, "user").toURI().toString()); - - // Test if the configuration/config.ini or osgi.bundles has been set - // If yes, try to avoid discovery of bundles - if (bundleLocations == null) { - if (props.getProperty("osgi.bundles") != null) { - bundleLocations = Collections.emptySet(); - } else { - String config = props.getProperty(PROP_CONFIG_AREA); - File ini = new File(config, "config.ini"); - if (ini.isFile()) { - Properties iniProps = new Properties(); - iniProps.load(new FileInputStream(ini)); - if (iniProps.getProperty("osgi.bundles") != null) { - bundleLocations = Collections.emptySet(); - } - } - } - } + Properties props = configureProperties(); startFramework(props); } else { @@ -396,6 +332,75 @@ public class EquinoxHost { } } + protected Properties configureProperties() throws IOException, FileNotFoundException { + String version = getSystemProperty("java.specification.version"); + + /** + * [rfeng] I have to remove javax.transaction.* packages from the system bundle + * See: http://www.mail-archive.com/dev@geronimo.apache.org/msg70761.html + */ + String profile = "J2SE-1.5.profile"; + if (version.startsWith("1.6")) { + profile = "JavaSE-1.6.profile"; + } + Properties props = new Properties(); + InputStream is = getClass().getResourceAsStream(profile); + if (is != null) { + props.load(is); + is.close(); + } + + props.putAll(getSystemProperties()); + + // Configure Eclipse properties + + // Use the boot classloader as the parent classloader + put(props, PROP_OSGI_CONTEXT_CLASS_LOADER_PARENT, "app"); + + // Set startup properties + put(props, PROP_OSGI_CLEAN, "true"); + + // Set location properties + // FIXME Use proper locations + String tmpDir = getSystemProperty("java.io.tmpdir"); + File root = new File(tmpDir); + // Add user name as the prefix. For multiple users on the same Lunix, + // there will be permission issue if one user creates the .tuscany folder + // first under /tmp with no write permission for others. + String userName = getSystemProperty(PROP_USER_NAME); + if (userName != null) { + root = new File(root, userName); + } + root = new File(root, ".tuscany/equinox/" + UUID.randomUUID().toString()); + if (logger.isLoggable(Level.FINE)) { + logger.fine("Equinox location: " + root); + } + + put(props, PROP_INSTANCE_AREA_DEFAULT, new File(root, "workspace").toURI().toString()); + put(props, PROP_INSTALL_AREA, new File(root, "install").toURI().toString()); + put(props, PROP_CONFIG_AREA_DEFAULT, new File(root, "config").toURI().toString()); + put(props, PROP_USER_AREA_DEFAULT, new File(root, "user").toURI().toString()); + + // Test if the configuration/config.ini or osgi.bundles has been set + // If yes, try to avoid discovery of bundles + if (bundleLocations == null) { + if (props.getProperty("osgi.bundles") != null) { + bundleLocations = Collections.emptySet(); + } else { + String config = props.getProperty(PROP_CONFIG_AREA); + File ini = new File(config, "config.ini"); + if (ini.isFile()) { + Properties iniProps = new Properties(); + iniProps.load(new FileInputStream(ini)); + if (iniProps.getProperty("osgi.bundles") != null) { + bundleLocations = Collections.emptySet(); + } + } + } + } + return props; + } + private boolean isServiceProvider(Bundle bundle, Set<String> serviceProviders) { if (bundle != null) { String export = (String)bundle.getHeaders().get(Constants.EXPORT_PACKAGE); diff --git a/branches/sca-java-2.0-M4/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/FrameworkLauncher.java b/branches/sca-java-2.0-M4/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/FrameworkLauncher.java index 5714df2a5d..414f3aae63 100644 --- a/branches/sca-java-2.0-M4/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/FrameworkLauncher.java +++ b/branches/sca-java-2.0-M4/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/FrameworkLauncher.java @@ -23,8 +23,10 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.HashMap; import java.util.Hashtable; import java.util.Map; +import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -101,7 +103,28 @@ public class FrameworkLauncher implements BundleActivator { if (factory == null) { return null; } - return factory.newFramework(properties); + String propertyFile = null; + String factoryName = factory.getClass().getName(); + if (factoryName.startsWith("org.eclipse.osgi.")) { + propertyFile = "equinox.properties"; + } else if (factoryName.startsWith("org.apache.felix.")) { + propertyFile = "felix.properties"; + } + Map propMap = new HashMap(); + if (propertyFile != null) { + InputStream is = getClass().getResourceAsStream(propertyFile); + if (is != null) { + Properties props = new Properties(); + try { + props.load(is); + } catch (IOException e) { + logger.log(Level.WARNING, e.getMessage(), e); + } + propMap.putAll(props); + } + } + propMap.putAll(properties); + return factory.newFramework(propMap); } public boolean isEquinox() { @@ -123,4 +146,109 @@ public class FrameworkLauncher implements BundleActivator { public void stop(BundleContext context) throws Exception { EquinoxHost.injectedBundleContext = null; } + + private static final String DELIM_START = "${"; + private static final String DELIM_STOP = "}"; + + /** + * <p> + * This method performs property variable substitution on the + * specified value. If the specified value contains the syntax + * <tt>${<prop-name>}</tt>, where <tt><prop-name></tt> + * refers to either a configuration property or a system property, + * then the corresponding property value is substituted for the variable + * placeholder. Multiple variable placeholders may exist in the + * specified value as well as nested variable placeholders, which + * are substituted from inner most to outer most. Configuration + * properties override system properties. + * </p> + * @param val The string on which to perform property substitution. + * @param currentKey The key of the property being evaluated used to + * detect cycles. + * @param cycleMap Map of variable references used to detect nested cycles. + * @param configProps Set of configuration properties. + * @return The value of the specified string after system property substitution. + * @throws IllegalArgumentException If there was a syntax error in the + * property placeholder syntax or a recursive variable reference. + **/ + public static String substVars(String val, String currentKey, Map cycleMap, Properties configProps) + throws IllegalArgumentException { + // If there is currently no cycle map, then create + // one for detecting cycles for this invocation. + if (cycleMap == null) { + cycleMap = new HashMap(); + } + + // Put the current key in the cycle map. + cycleMap.put(currentKey, currentKey); + + // Assume we have a value that is something like: + // "leading ${foo.${bar}} middle ${baz} trailing" + + // Find the first ending '}' variable delimiter, which + // will correspond to the first deepest nested variable + // placeholder. + int stopDelim = val.indexOf(DELIM_STOP); + + // Find the matching starting "${" variable delimiter + // by looping until we find a start delimiter that is + // greater than the stop delimiter we have found. + int startDelim = val.indexOf(DELIM_START); + while (stopDelim >= 0) { + int idx = val.indexOf(DELIM_START, startDelim + DELIM_START.length()); + if ((idx < 0) || (idx > stopDelim)) { + break; + } else if (idx < stopDelim) { + startDelim = idx; + } + } + + // If we do not have a start or stop delimiter, then just + // return the existing value. + if ((startDelim < 0) && (stopDelim < 0)) { + return val; + } + // At this point, we found a stop delimiter without a start, + // so throw an exception. + else if (((startDelim < 0) || (startDelim > stopDelim)) && (stopDelim >= 0)) { + throw new IllegalArgumentException("stop delimiter with no start delimiter: " + val); + } + + // At this point, we have found a variable placeholder so + // we must perform a variable substitution on it. + // Using the start and stop delimiter indices, extract + // the first, deepest nested variable placeholder. + String variable = val.substring(startDelim + DELIM_START.length(), stopDelim); + + // Verify that this is not a recursive variable reference. + if (cycleMap.get(variable) != null) { + throw new IllegalArgumentException("recursive variable reference: " + variable); + } + + // Get the value of the deepest nested variable placeholder. + // Try to configuration properties first. + String substValue = (configProps != null) ? configProps.getProperty(variable, null) : null; + if (substValue == null) { + // Ignore unknown property values. + substValue = System.getProperty(variable, ""); + } + + // Remove the found variable from the cycle map, since + // it may appear more than once in the value and we don't + // want such situations to appear as a recursive reference. + cycleMap.remove(variable); + + // Append the leading characters, the substituted value of + // the variable, and the trailing characters to get the new + // value. + val = val.substring(0, startDelim) + substValue + val.substring(stopDelim + DELIM_STOP.length(), val.length()); + + // Now perform substitution again, since there could still + // be substitutions to make. + val = substVars(val, currentKey, cycleMap, configProps); + + // Return the value. + return val; + } + } diff --git a/branches/sca-java-2.0-M4/pom.xml b/branches/sca-java-2.0-M4/pom.xml index 9327ced1e4..fcc4164038 100644 --- a/branches/sca-java-2.0-M4/pom.xml +++ b/branches/sca-java-2.0-M4/pom.xml @@ -340,7 +340,7 @@ <plugin> <groupId>org.apache.tuscany.maven.plugins</groupId> <artifactId>maven-bundle-plugin</artifactId> - <version>1.0.4</version> + <version>1.0.5-SNAPSHOT</version> <extensions>true</extensions> </plugin> <plugin> |