diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-30 22:10:11 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-30 22:10:11 +0000 |
commit | ef25a099e9ea2064b9523ec33a15f114ee52ce45 (patch) | |
tree | 8bba02f2ca100c9331fb24d018293ae10cc6395c /java/sca/modules | |
parent | d058702bea1602cdb0f4a7887419c0db1104a0a9 (diff) |
Propagate osgi properties for OSGi remote services
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@831468 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules')
20 files changed, 446 insertions, 126 deletions
diff --git a/java/sca/modules/core/META-INF/MANIFEST.MF b/java/sca/modules/core/META-INF/MANIFEST.MF index 98800de77d..5205527437 100644 --- a/java/sca/modules/core/META-INF/MANIFEST.MF +++ b/java/sca/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/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java index 1c4ded13fc..ecc1a4e599 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/CompositeActivatorImpl.java +++ b/java/sca/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/java/sca/modules/endpoint-tribes/META-INF/MANIFEST.MF b/java/sca/modules/endpoint-tribes/META-INF/MANIFEST.MF index 8c7feecb30..83df715831 100644 --- a/java/sca/modules/endpoint-tribes/META-INF/MANIFEST.MF +++ b/java/sca/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/java/sca/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java b/java/sca/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java index 3a703bca62..1d0d0a1d99 100644 --- a/java/sca/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java +++ b/java/sca/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java @@ -358,6 +358,7 @@ public class ReplicatedEndpointRegistry implements EndpointRegistry, LifeCycleLi logger.info(id + " Remote endpoint added: " + entry.getValue()); } Endpoint newEp = (Endpoint)entry.getValue(); + newEp.setExtensionPointRegistry(registry); for (EndpointListener listener : listeners) { listener.endpointAdded(newEp); } 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 7b5b5f2ea1..1cf17c6356 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 @@ -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; @@ -65,12 +66,15 @@ public class OSGiImplementationProvider implements ImplementationProvider { private Bundle osgiBundle; 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(); } @@ -123,6 +127,14 @@ public class OSGiImplementationProvider implements ImplementationProvider { registrations.add(registration); } } + + // Set the OSGi service reference properties into the SCA service + for (ComponentService service : component.getServices()) { + ServiceReference serviceReference = getServiceReference(osgiBundle.getBundleContext(), service); + if (serviceReference != null) { + service.getExtensions().addAll(implementationFactory.createOSGiProperties(serviceReference)); + } + } } public void stop() { @@ -165,24 +177,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/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java index 0e4a3ef995..11b29de248 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProviderFactory.java +++ b/java/sca/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/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java index 0a83624e76..be39e4d3e0 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java +++ b/java/sca/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/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiProperty.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiProperty.java index e0b7f43add..ebe4ef2dbc 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiProperty.java +++ b/java/sca/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/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java index 9fab65272f..2978e8e823 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java +++ b/java/sca/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/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiPropertyImpl.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiPropertyImpl.java index ec8b98f53f..b3e934a970 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiPropertyImpl.java +++ b/java/sca/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/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java index 9bd469df06..595547f9e7 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java +++ b/java/sca/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.getName()); + writer.writeCharacters(model.getStringValue()); writer.writeEndElement(); } diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/AbstractOSGiServiceHandler.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/AbstractOSGiServiceHandler.java index a043e33c32..7c9cf8ef6e 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/AbstractOSGiServiceHandler.java +++ b/java/sca/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/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java index 8ac949ccc5..748f835859 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointHelper.java @@ -26,6 +26,7 @@ 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; @@ -54,11 +55,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 = 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); @@ -67,7 +78,12 @@ public class EndpointHelper { } public synchronized static String getFrameworkUUID(BundleContext bundleContext) { - String uuid = System.getProperty(FRAMEWORK_UUID); + String uuid = null; + if (bundleContext != null) { + uuid = bundleContext.getProperty(FRAMEWORK_UUID); + } else { + uuid = System.getProperty(FRAMEWORK_UUID); + } if (uuid == null) { uuid = UUID.randomUUID().toString(); } diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java index d5a2b7aa95..52abef059c 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java @@ -26,6 +26,10 @@ import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; +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; @@ -131,5 +135,11 @@ 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); + } } diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java index 02603417cc..21950d9d59 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java @@ -70,14 +70,19 @@ 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(OSGiHelper.getOSGiProperties(registry, reference)); + // 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/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java index 59a21ade06..c869ab8c14 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java +++ b/java/sca/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/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java index bc2e3ce012..9da7b5f014 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java +++ b/java/sca/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/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java index 2577a68808..bf70c98634 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java +++ b/java/sca/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/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java index 345a06880e..fd4d0ee5bf 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java +++ b/java/sca/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/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/FrameworkLauncher.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/FrameworkLauncher.java index 5714df2a5d..414f3aae63 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/FrameworkLauncher.java +++ b/java/sca/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; + } + } |