diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-20 06:20:53 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-20 06:20:53 +0000 |
commit | 60f5bce98ed1ce21b111c0d006d2dfd02106371b (patch) | |
tree | 1964272ea9422fd03f2e694ebbdca94b0132bd7d /sca-java-2.x/trunk/modules/implementation-osgi | |
parent | 734dac1eaf4ff319d8355d08335aeb019abf727e (diff) |
Update to the latest version of OSGi Remote Service Admin
Fix the issues to pass OSGi SCA configuration type TCK
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@901079 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
5 files changed, 104 insertions, 19 deletions
diff --git a/sca-java-2.x/trunk/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java b/sca-java-2.x/trunk/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java index 78b1ea476f..1d5d99cd34 100644 --- a/sca-java-2.x/trunk/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java +++ b/sca-java-2.x/trunk/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationProvider.java @@ -25,6 +25,10 @@ import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SERVICE_IM import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SERVICE_IMPORTED_CONFIGS; import static org.osgi.framework.Constants.SERVICE_RANKING; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; @@ -54,6 +58,7 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceException; import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; @@ -130,7 +135,7 @@ public class OSGiImplementationProvider implements ImplementationProvider { 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 @@ -229,6 +234,66 @@ public class OSGiImplementationProvider implements ImplementationProvider { return implementation; } + /** + * A proxy invocation handler that wrap exceptions into OSGi ServiceException + */ + private static class InvocationHandlerDelegate implements InvocationHandler { + private final Object instance; + + public InvocationHandlerDelegate(Object instance) { + super(); + this.instance = instance; + } + + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if (!Proxy.isProxyClass(instance.getClass())) { + Method m = instance.getClass().getMethod(method.getName(), method.getParameterTypes()); + try { + return m.invoke(instance, args); + } catch (InvocationTargetException e) { + wrapException(method, e.getCause()); + return null; + } + } else { + InvocationHandler handler = Proxy.getInvocationHandler(instance); + try { + return handler.invoke(instance, method, args); + } catch (Throwable e) { + wrapException(method, e); + return null; + } + } + } + + private void wrapException(Method method, Throwable e) throws Throwable { + for (Class<?> exType : method.getExceptionTypes()) { + if (exType.isInstance(e)) { + throw e; + } + } + throw new ServiceException(e.getMessage(), ServiceException.REMOTE, e); + } + + /** + * A utility to cast the object to the given interface. If the class for the object + * is loaded by a different classloader, a proxy will be created. + * + * @param <T> + * @param obj + * @param cls + * @return + */ + static <T> T cast(Object obj, Class<T> cls) { + if (obj == null) { + return null; + } else { + return cls.cast(Proxy.newProxyInstance(cls.getClassLoader(), + new Class<?>[] {cls}, + new InvocationHandlerDelegate(obj))); + } + } + } + public class OSGiServiceFactory implements ServiceFactory { private RuntimeEndpointReference epr; private String interfaceName; @@ -240,22 +305,26 @@ public class OSGiImplementationProvider implements ImplementationProvider { public OSGiServiceFactory(String interfaceName, EndpointReference epr) { super(); this.interfaceName = interfaceName; - this.epr = (RuntimeEndpointReference) epr; + this.epr = (RuntimeEndpointReference)epr; } public Object getService(Bundle bundle, ServiceRegistration registration) { - Class<?> interfaceClass = null; try { - interfaceClass = bundle.loadClass(interfaceName); - } catch (ClassNotFoundException e) { - return null; - } - ProxyFactory proxyService = proxyFactoryExtensionPoint.getInterfaceProxyFactory(); - if (!interfaceClass.isInterface()) { - proxyService = proxyFactoryExtensionPoint.getClassProxyFactory(); + Class<?> interfaceClass = null; + try { + interfaceClass = bundle.loadClass(interfaceName); + } catch (ClassNotFoundException e) { + return null; + } + ProxyFactory proxyService = proxyFactoryExtensionPoint.getInterfaceProxyFactory(); + if (!interfaceClass.isInterface()) { + proxyService = proxyFactoryExtensionPoint.getClassProxyFactory(); + } + Object proxy = proxyService.createProxy(interfaceClass, epr); + return InvocationHandlerDelegate.cast(proxy, interfaceClass); + } catch (Throwable e) { + throw new ServiceException(e.getMessage(), ServiceException.FACTORY_EXCEPTION, e); } - Object proxy = proxyService.createProxy(interfaceClass, epr); - return proxy; } public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) { diff --git a/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java b/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java index be39e4d3e0..23952dfee4 100644 --- a/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java +++ b/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/OSGiImplementationFactory.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.implementation.osgi; import java.util.Collection; +import java.util.Map; import org.osgi.framework.ServiceReference; @@ -41,4 +42,6 @@ public interface OSGiImplementationFactory { OSGiProperty createOSGiProperty(String name, String stringValue, String type); OSGiProperty createOSGiProperty(String name, Object value); Collection<OSGiProperty> createOSGiProperties(ServiceReference reference); + Collection<OSGiProperty> createOSGiProperties(Map<String, Object> properties); + } diff --git a/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java b/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java index 2978e8e823..17ebf12903 100644 --- a/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java +++ b/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationFactoryImpl.java @@ -22,6 +22,7 @@ package org.apache.tuscany.sca.implementation.osgi.impl; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; @@ -112,5 +113,13 @@ public class OSGiImplementationFactoryImpl implements OSGiImplementationFactory } return props; } - + + public Collection<OSGiProperty> createOSGiProperties(Map<String, Object> properties) { + List<OSGiProperty> props = new ArrayList<OSGiProperty>(); + for (Map.Entry<String, Object> e : properties.entrySet()) { + OSGiProperty prop = createOSGiProperty(e.getKey(), e.getValue()); + props.add(prop); + } + return props; + } } diff --git a/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/ServiceDescriptionImpl.java b/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/ServiceDescriptionImpl.java index 1ffe16bf71..5bd1d17253 100644 --- a/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/ServiceDescriptionImpl.java +++ b/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/ServiceDescriptionImpl.java @@ -46,6 +46,6 @@ public class ServiceDescriptionImpl implements ServiceDescription { } public String toString() { - return "service-description: " + properties; + return "endpoint-description: " + properties; } } diff --git a/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java b/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java index 84734b88db..a0c9baf6f1 100644 --- a/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java +++ b/sca-java-2.x/trunk/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java @@ -94,11 +94,15 @@ 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.writeAttribute(TYPE, model.getType()); - writer.writeCharacters(model.getStringValue()); - writer.writeEndElement(); + if (model.getStringValue() != null) { + writer.writeStartElement(PROPERTY_QNAME.getNamespaceURI(), PROPERTY_QNAME.getLocalPart()); + writer.writeAttribute(NAME, model.getName()); + if (model.getType() != null) { + writer.writeAttribute(TYPE, model.getType()); + } + writer.writeAttribute(VALUE, model.getStringValue()); + writer.writeEndElement(); + } } public Class<OSGiProperty> getModelType() { |