diff options
Diffstat (limited to 'sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding')
9 files changed, 552 insertions, 0 deletions
diff --git a/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/NoRemoteMethodException.java b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/NoRemoteMethodException.java new file mode 100755 index 0000000000..0f59da4709 --- /dev/null +++ b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/NoRemoteMethodException.java @@ -0,0 +1,34 @@ +/* + * + * Copyright 2006 The Apache Software Foundation or its licensors as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.osgi.binding; + +public class NoRemoteMethodException extends OSGiBindingException { + + private static final long serialVersionUID = 5037327699117168707L; + + public NoRemoteMethodException(String message) { + super(message); + } + + public NoRemoteMethodException(String message, Throwable cause) { + super(message, cause); + } + + public NoRemoteMethodException(Throwable cause) { + super("", cause); + } +} diff --git a/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/NoRemoteServiceException.java b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/NoRemoteServiceException.java new file mode 100755 index 0000000000..a370cb28c6 --- /dev/null +++ b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/NoRemoteServiceException.java @@ -0,0 +1,39 @@ +/* + * + * Copyright 2006 The Apache Software Foundation or its licensors as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.osgi.binding; + +import org.osgi.framework.BundleException; + +/** + * FIXME class not used + * + * @version $Rev$ $Date$ + */ +public class NoRemoteServiceException extends BundleException { + + public NoRemoteServiceException(String message) { + super(message); + } + + public NoRemoteServiceException(String message, Throwable cause) { + super(message, cause); + } + + public NoRemoteServiceException(Throwable cause) { + super("", cause); + } +} diff --git a/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingBuilder.java b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingBuilder.java new file mode 100755 index 0000000000..0967abf210 --- /dev/null +++ b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingBuilder.java @@ -0,0 +1,69 @@ +/* + * + * Copyright 2006 The Apache Software Foundation or its licensors as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.osgi.binding; + +import org.osoa.sca.annotations.Constructor; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ServiceBinding; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.BindingBuilderExtension; +import org.apache.tuscany.spi.model.BoundReferenceDefinition; +import org.apache.tuscany.spi.model.BoundServiceDefinition; + +import org.apache.tuscany.osgi.OSGiHost; + +/** + * Builds a Service or Reference for an OSGi binding. + * + * @version $Rev$ $Date$ + */ +public class OSGiBindingBuilder extends BindingBuilderExtension<OSGiBindingDefinition> { + + OSGiHost host; + + @Constructor + public OSGiBindingBuilder(@Autowire OSGiHost host) { + this.host = host; + } + + protected Class<OSGiBindingDefinition> getBindingType() { + return OSGiBindingDefinition.class; + } + + public ServiceBinding build(CompositeComponent parent, + BoundServiceDefinition boundServiceDefinition, + OSGiBindingDefinition bindingDefinition, + DeploymentContext deploymentContext) { + String name = boundServiceDefinition.getName(); + String osgiServiceName = bindingDefinition.getService(); + return new OSGiServiceBinding(name, parent, wireService, osgiServiceName, host); + } + + public OSGiReferenceBinding build(CompositeComponent parent, + BoundReferenceDefinition boundReferenceDefinition, + OSGiBindingDefinition bindingDefinition, DeploymentContext deploymentContext) { + String name = boundReferenceDefinition.getName(); + return new OSGiReferenceBinding(name, parent); + } + + protected Class<? extends Object> getServiceInterface(BoundServiceDefinition boundServiceDefinition) { + return boundServiceDefinition.getServiceContract().getInterfaceClass(); + } + +} diff --git a/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingDefinition.java b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingDefinition.java new file mode 100755 index 0000000000..15cacff25d --- /dev/null +++ b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingDefinition.java @@ -0,0 +1,45 @@ +/* + * + * Copyright 2006 The Apache Software Foundation or its licensors as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.osgi.binding; + +import org.apache.tuscany.spi.model.BindingDefinition; + +/** + * Represents a binding to an OSGi service. + * + * @version $Rev$ $Date$ + */ +public class OSGiBindingDefinition extends BindingDefinition { + private String uri; + private String service; + + public String getURI() { + return uri; + } + + public void setURI(String uri) { + this.uri = uri; + } + + public void setService(String service) { + this.service = service; + } + + public String getService() { + return service; + } +} diff --git a/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingException.java b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingException.java new file mode 100755 index 0000000000..288d4940f0 --- /dev/null +++ b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingException.java @@ -0,0 +1,39 @@ +/* + * + * Copyright 2006 The Apache Software Foundation or its licensors as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.osgi.binding; + +import org.apache.tuscany.api.TuscanyRuntimeException; + +/** + * @version $Rev$ $Date$ + */ +public abstract class OSGiBindingException extends TuscanyRuntimeException { + protected OSGiBindingException() { + } + + protected OSGiBindingException(String message) { + super(message); + } + + protected OSGiBindingException(String message, Throwable cause) { + super(message, cause); + } + + protected OSGiBindingException(Throwable cause) { + super(cause); + } +} diff --git a/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingLoader.java b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingLoader.java new file mode 100755 index 0000000000..80465feabb --- /dev/null +++ b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiBindingLoader.java @@ -0,0 +1,66 @@ +/** + * + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.osgi.binding; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.annotations.Constructor; +import org.osoa.sca.annotations.Scope; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.loader.LoaderUtil; +import org.apache.tuscany.spi.model.ModelObject; + +/** + * Loader for handling <code>binding.osgi</code> elements. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +public class OSGiBindingLoader extends LoaderExtension<OSGiBindingDefinition> { + public static final QName BINDING_OSGI = new QName("http://tuscany.apache.org/xmlns/osgi/1.0", "binding.osgi"); + + @Constructor + public OSGiBindingLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return BINDING_OSGI; + } + + public OSGiBindingDefinition load(CompositeComponent parent, ModelObject object, XMLStreamReader reader, + DeploymentContext deploymentContext) + throws XMLStreamException, LoaderException { + String uri = reader.getAttributeValue(null, "uri"); + String service = reader.getAttributeValue(null, "service"); + LoaderUtil.skipToEndElement(reader); + + OSGiBindingDefinition binding = new OSGiBindingDefinition(); + binding.setURI(uri); + binding.setService(service); + return binding; + } + +} diff --git a/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiInvoker.java b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiInvoker.java new file mode 100755 index 0000000000..bb48a8b1e1 --- /dev/null +++ b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiInvoker.java @@ -0,0 +1,79 @@ +/* + * + * Copyright 2006 The Apache Software Foundation or its licensors as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.osgi.binding; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * Invokes the target service of an OSGi reference. + * + * @version $Rev$ $Date$ + */ +public class OSGiInvoker implements TargetInvoker { + private final Method remoteMethod; + private final Object proxy; + + OSGiInvoker(Object proxy, Method remoteMethod) { + assert remoteMethod.isAccessible(); + this.remoteMethod = remoteMethod; + this.proxy = proxy; + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + Object resp = invokeTarget(msg.getBody(), TargetInvoker.NONE); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBody(e.getCause()); + } + return msg; + } + + public Object invokeTarget(Object payload, final short sequence) throws InvocationTargetException { + try { + return remoteMethod.invoke(proxy, (Object[]) payload); + } catch (IllegalAccessException e) { + // the method we are passed must be accessible + throw new AssertionError(e); + } + } + + public Object clone() throws CloneNotSupportedException { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(e); + } + } + + public boolean isOptimizable() { + return false; + } + + public boolean isCacheable() { + return false; + } + + // FIXME I think we can always have this cacheable as OSGi serviceBindings are thread-safe + public void setCacheable(boolean cacheable) { + } +} diff --git a/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiReferenceBinding.java b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiReferenceBinding.java new file mode 100755 index 0000000000..4523d7e001 --- /dev/null +++ b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiReferenceBinding.java @@ -0,0 +1,65 @@ +/* + * + * Copyright 2006 The Apache Software Foundation or its licensors as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.osgi.binding; + +import java.lang.reflect.Method; +import java.rmi.Remote; +import javax.xml.namespace.QName; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ReferenceBindingExtension; +import org.apache.tuscany.spi.idl.java.JavaIDLUtils; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * @version $Rev$ $Date$ + */ +public class OSGiReferenceBinding extends ReferenceBindingExtension { + private static final QName BINDING_OSGI = new QName("http://tuscany.apache.org/xmlns/osgi/1.0", "binding.osgi"); + //private final String uri; + + public OSGiReferenceBinding(String name, CompositeComponent parent) { + super(name, parent); + //this.uri = uri; + } + + public QName getBindingType() { + return BINDING_OSGI; + } + + public TargetInvoker createTargetInvoker(ServiceContract contract, Operation operation) { + try { + Object proxy = getProxy(); + String name = operation.getName(); + Method method = JavaIDLUtils.findMethod(operation, contract.getInterfaceClass().getMethods()); + Class<?>[] parameterTypes = method.getParameterTypes(); + Method remoteMethod = proxy.getClass().getMethod(name, parameterTypes); + return new OSGiInvoker(proxy, remoteMethod); + } catch (NoSuchMethodException e) { + // FIXME we should probably have this as a checked exception, which will entail adding to the SPI signature + throw new NoRemoteMethodException(operation.toString(), e); + } + } + + protected Remote getProxy() { + //FIXME use service reference + return null; + } + +} diff --git a/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiServiceBinding.java b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiServiceBinding.java new file mode 100755 index 0000000000..57cae13779 --- /dev/null +++ b/sandbox/old/contrib/binding-osgi/src/main/java/org/apache/tuscany/osgi/binding/OSGiServiceBinding.java @@ -0,0 +1,116 @@ +/* + * + * Copyright 2006 The Apache Software Foundation or its licensors as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.osgi.binding; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.Properties; +import javax.xml.namespace.QName; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.SCAObjectStartException; +import org.apache.tuscany.spi.extension.ServiceBindingExtension; +import org.apache.tuscany.spi.wire.WireService; + +import org.apache.tuscany.osgi.OSGiHost; +import org.osgi.framework.ServiceFactory; + +/** + * @version $Rev$ $Date$ + */ +public class OSGiServiceBinding extends ServiceBindingExtension { + private static final QName BINDING_OSGI = new QName("http://tuscany.apache.org/xmlns/osgi/1.0", "binding.osgi"); + private final String osgiServiceName; + //private final Class<? extends Object> service; + private OSGiHost host; + private WireService wireService; + + /** + * Creates a new service instance + * + * @param name the name of the SCA service + * @param parent the containing composite + * @param wireService the wire service to use for creating proxies + * @param osgiServiceName the name to publish the service as to the OSGi hose + * @param host the OSGi host + */ + public OSGiServiceBinding(String name, + CompositeComponent parent, + WireService wireService, + String osgiServiceName, + OSGiHost host) { + super(name, parent); + this.osgiServiceName = osgiServiceName; + //this.service = service; + this.wireService = wireService; + this.host = host; + } + + public void start() throws SCAObjectStartException { + super.start(); + Properties properties = new Properties(); + Object instance; + Class<?> clazz = inboundWire.getServiceContract().getInterfaceClass(); + instance = wireService.createProxy(clazz, inboundWire); + if (instance instanceof ServiceFactory) { + host.registerService(osgiServiceName, instance, properties); + } else { + ServiceFactoryProxyHandler factoryProxy = new ServiceFactoryProxyHandler(instance); + Class<?> interfaces[] = instance.getClass().getInterfaces(); + Class<?> proxyInterfaces[] = new Class<?>[interfaces.length + 1]; + int i; + for (i = 0; i < interfaces.length; i++) { + proxyInterfaces[i] = interfaces[i]; + } + proxyInterfaces[i] = ServiceFactory.class; + ClassLoader classLoader = instance.getClass().getClassLoader(); + // TODO It seems we need a mixin capability for the service. We could add that to WireService + Object targetProxy = Proxy.newProxyInstance(classLoader, proxyInterfaces, factoryProxy); + host.registerService(osgiServiceName, targetProxy, properties); + } + //Register the service with OSGi + } + + public void stop() { + super.stop(); + //Unregister the service with OSGi + } + + public QName getBindingType() { + return BINDING_OSGI; + } + + private class ServiceFactoryProxyHandler implements InvocationHandler { + + private Object instance; + + public ServiceFactoryProxyHandler(Object instance) { + this.instance = instance; + } + + public Object invoke(Object object, Method method, Object[] parms) throws Throwable { + if (method.getName().equals("getService")) { + return instance; + } else if (method.getName().equals("ungetService")) { + return null; + } + return method.invoke(instance, parms); + } + + } +} |