From a3643fc530fbbb2369a17f888d8227ade55066d5 Mon Sep 17 00:00:00 2001 From: slaws Date: Wed, 22 Jul 2009 08:48:23 +0000 Subject: TUSCANY-3138 fix up callbacks now that the system relies solely on the ServiceReference interface. I've reinstated the CallbackReference class as CallbackServiceReference to provide the extra features required callbacks. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@796631 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/core/context/ServiceReferenceExt.java | 15 +- .../core/context/impl/CallableReferenceImpl.java | 11 +- .../context/impl/CallbackServiceReferenceImpl.java | 152 +++++++++++++++++++++ .../sca/core/context/impl/RequestContextImpl.java | 2 +- .../core/context/impl/ServiceReferenceImpl.java | 1 - .../invocation/CallbackReferenceObjectFactory.java | 3 +- .../impl/JDKCallbackInvocationHandler.java | 15 +- .../core/invocation/impl/JDKInvocationHandler.java | 6 +- .../sca/core/invocation/impl/JDKProxyFactory.java | 3 +- 9 files changed, 187 insertions(+), 21 deletions(-) create mode 100644 java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallbackServiceReferenceImpl.java (limited to 'java/sca') diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ServiceReferenceExt.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ServiceReferenceExt.java index 2a6940a8df..d590e0118c 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ServiceReferenceExt.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/ServiceReferenceExt.java @@ -25,6 +25,7 @@ import java.io.IOException; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; import org.apache.tuscany.sca.runtime.RuntimeWire; @@ -35,16 +36,28 @@ import org.oasisopen.sca.ServiceReference; */ public interface ServiceReferenceExt extends ServiceReference, Externalizable { /** - * @return + * Return the wire that sits behind this service reference + * @return wire */ RuntimeWire getRuntimeWire(); + + /** + * Return the EndpointReference that sits behind this service reference + * @return endpoint reference + */ + EndpointReference getEndpointReference(); + // TODO - EPR - the the following still required? /** + * TBD + * * @param callbackID */ void attachCallbackID(Object callbackID); /** + * TBD + * * @return */ XMLStreamReader getXMLReader(); diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallableReferenceImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallableReferenceImpl.java index d331ea729d..705934e557 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallableReferenceImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallableReferenceImpl.java @@ -169,11 +169,10 @@ public class CallableReferenceImpl implements ServiceReferenceExt { public CallableReferenceImpl(Class businessInterface, RuntimeWire wire, ProxyFactory proxyFactory) { this.proxyFactory = proxyFactory; this.businessInterface = businessInterface; - ExtensionPointRegistry registry = ((RuntimeWireImpl)wire).getExtensionPoints(); - this.modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); - this.assemblyFactory = (RuntimeAssemblyFactory)modelFactories.getFactory(AssemblyFactory.class); + //ExtensionPointRegistry registry = ((RuntimeWireImpl)wire).getExtensionPoints(); + //this.modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + //this.assemblyFactory = (RuntimeAssemblyFactory)modelFactories.getFactory(AssemblyFactory.class); bind(wire); - } public RuntimeWire getRuntimeWire() { @@ -188,6 +187,10 @@ public class CallableReferenceImpl implements ServiceReferenceExt { throw new ServiceRuntimeException(e); } } + + public EndpointReference getEndpointReference() { + return endpointReference; + } protected void bind(RuntimeWire wire) { if (wire != null) { diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallbackServiceReferenceImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallbackServiceReferenceImpl.java new file mode 100644 index 0000000000..47dc99aac9 --- /dev/null +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/CallbackServiceReferenceImpl.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.core.context.impl; + + +import java.util.List; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.core.assembly.impl.RuntimeWireImpl; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.invocation.ThreadMessageContext; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +public class CallbackServiceReferenceImpl extends ServiceReferenceImpl { + private RuntimeWire wire; + private List wires; + private Endpoint resolvedEndpoint; + + /* + * Public constructor for Externalizable serialization/deserialization + */ + public CallbackServiceReferenceImpl() { + super(); + } + + public CallbackServiceReferenceImpl(Class interfaze, List wires, ProxyFactory proxyFactory) { + super(interfaze, null, proxyFactory); + this.wires = wires; + init(); + } + + public void init() { + Message msgContext = ThreadMessageContext.getMessageContext(); + wire = selectCallbackWire(msgContext); + if (wire == null) { + //FIXME: need better exception + throw new RuntimeException("No callback binding found for " + msgContext.getTo().toString()); + } + resolvedEndpoint = msgContext.getFrom().getCallbackEndpoint(); + } + + @Override + protected Object createProxy() throws Exception { + return proxyFactory.createCallbackProxy(this); + } + + public RuntimeWire getCallbackWire() { + if (resolvedEndpoint == null) { + return null; + } else { + return cloneAndBind(wire); + } + } + + public Endpoint getResolvedEndpoint() { + return resolvedEndpoint; + } + + private RuntimeWire selectCallbackWire(Message msgContext) { + // look for callback binding with same name as service binding + Endpoint to = msgContext.getTo(); + if (to == null) { + //FIXME: need better exception + throw new RuntimeException("Destination for forward call is not available"); + } + for (RuntimeWire wire : wires) { + if (wire.getEndpointReference().getBinding().getName().equals(to.getBinding().getName())) { + return wire; + } + } + + // if no match, look for callback binding with same type as service binding + for (RuntimeWire wire : wires) { + if (wire.getEndpointReference().getBinding().getClass() == to.getBinding().getClass()) { + return wire; + } + } + + // no suitable callback wire was found + return null; + } + + private RuntimeWire cloneAndBind(RuntimeWire wire) { + RuntimeWire boundWire = null; + if (resolvedEndpoint != null) { + boundWire = ((RuntimeWireImpl)wire).lookupCache(resolvedEndpoint); + if (boundWire != null) { + return boundWire; + } + try { + // TODO - EPR - is this correct? + + // Fluff up a new response wire based on the callback endpoint + RuntimeComponentReference ref = + bind((RuntimeComponentReference)wire.getEndpointReference().getReference(), + resolvedEndpoint); + + boundWire = ref.getRuntimeWires().get(0); + + Binding binding = wire.getEndpointReference().getBinding(); + + ((RuntimeWireImpl)wire).addToCache(resolvedEndpoint, boundWire); + } catch (CloneNotSupportedException e) { + // will not happen + } + } + return boundWire; + } + + private RuntimeComponentReference bind(RuntimeComponentReference reference, + Endpoint callbackEndpoint) throws CloneNotSupportedException { + + // clone the callback reference ready to configure it for this callback endpoint + RuntimeComponentReference ref = (RuntimeComponentReference)reference.clone(); + ref.getTargets().clear(); + ref.getBindings().clear(); + ref.getEndpointReferences().clear(); + + // no access to the assembly factory so clone an existing epr + EndpointReference callbackEndpointReference = (EndpointReference)reference.getEndpointReferences().get(0).clone(); + + callbackEndpointReference.setReference(ref); + callbackEndpointReference.setTargetEndpoint(callbackEndpoint); + callbackEndpointReference.setUnresolved(true); + + // The callback endpoint will be resolved with the registry + // when the wire chains are created + ref.getEndpointReferences().add(callbackEndpointReference); + + return ref; + } +} diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java index ece3e79492..a4ea709fa1 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java @@ -103,7 +103,7 @@ public class RequestContextImpl implements RequestContext { Class javaClass = (Class)javaInterface.getJavaClass(); List wires = callbackReference.getRuntimeWires(); ProxyFactory proxyFactory = new ExtensibleProxyFactory(proxyFactoryExtensionPoint); - ServiceReferenceImpl ref = new ServiceReferenceImpl(javaClass, wires.get(0), proxyFactory); + ServiceReferenceImpl ref = new CallbackServiceReferenceImpl(javaClass, wires, proxyFactory); if (ref != null) { //ref.resolveTarget(); // TODO - EPR - not required for OASIS diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ServiceReferenceImpl.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ServiceReferenceImpl.java index fed984cef6..edd11aacc5 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ServiceReferenceImpl.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ServiceReferenceImpl.java @@ -22,7 +22,6 @@ import javax.xml.stream.XMLStreamReader; import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.core.assembly.CompositeActivator; -import org.apache.tuscany.sca.core.context.ServiceReferenceExt; import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentReference; diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceObjectFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceObjectFactory.java index 866e62abde..021d588cb4 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceObjectFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackReferenceObjectFactory.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.core.invocation; import java.util.List; +import org.apache.tuscany.sca.core.context.impl.CallbackServiceReferenceImpl; import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; import org.apache.tuscany.sca.core.factory.ObjectCreationException; import org.apache.tuscany.sca.core.factory.ObjectFactory; @@ -43,7 +44,7 @@ public class CallbackReferenceObjectFactory implements ObjectFactory getInstance() throws ObjectCreationException { - return new ServiceReferenceImpl(businessInterface, wires.get(0), proxyFactory); + return new CallbackServiceReferenceImpl(businessInterface, wires, proxyFactory); } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKCallbackInvocationHandler.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKCallbackInvocationHandler.java index 00bb51163a..e64b9f3068 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKCallbackInvocationHandler.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKCallbackInvocationHandler.java @@ -21,7 +21,9 @@ package org.apache.tuscany.sca.core.invocation.impl; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; + import org.apache.tuscany.sca.core.assembly.impl.RuntimeWireImpl; +import org.apache.tuscany.sca.core.context.impl.CallbackServiceReferenceImpl; import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; import org.apache.tuscany.sca.invocation.InvocationChain; import org.apache.tuscany.sca.invocation.MessageFactory; @@ -45,24 +47,19 @@ public class JDKCallbackInvocationHandler extends JDKInvocationHandler { @Override @SuppressWarnings( {"unchecked"}) public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if (Object.class == method.getDeclaringClass()) { return invokeObjectMethod(method, args); } - + // obtain a dedicated wire to be used for this callback invocation - RuntimeWire wire = ((ServiceReferenceImpl)callableReference).getRuntimeWire(); + RuntimeWire wire = ((CallbackServiceReferenceImpl)callableReference).getCallbackWire(); if (wire == null) { //FIXME: need better exception throw new ServiceRuntimeException("No callback wire found"); } - // set the conversational state based on the interface that - // is specified for the reference that this wire belongs to - // TODO - EPR - not required for OASIS - //initConversational(wire); - -// TODO - EPR - Fix up callbacks now callable reference has gone -// setEndpoint(((ServiceReferenceImpl)callableReference).getEndpointReference().getTargetEndpoint()); + setEndpoint(((CallbackServiceReferenceImpl)callableReference).getResolvedEndpoint()); InvocationChain chain = getInvocationChain(method, wire); if (chain == null) { diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java index b0788bd4b2..5f36803b91 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java @@ -55,7 +55,7 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { protected EndpointReference source; protected Endpoint target; protected RuntimeWire wire; - protected ServiceReference callableReference; + protected ServiceReferenceExt callableReference; protected Class businessInterface; protected boolean fixedWire = true; @@ -71,7 +71,7 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { public JDKInvocationHandler(MessageFactory messageFactory, ServiceReference callableReference) { this.messageFactory = messageFactory; - this.callableReference = callableReference; + this.callableReference = (ServiceReferenceExt)callableReference; if (callableReference != null) { this.businessInterface = callableReference.getBusinessInterface(); this.wire = ((ServiceReferenceExt)callableReference).getRuntimeWire(); @@ -494,7 +494,7 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { * @param callableReference the callableReference to set */ public void setCallableReference(ServiceReference callableReference) { - this.callableReference = callableReference; + this.callableReference = (ServiceReferenceExt)callableReference; } /** diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java index d7adec292d..ed994ce372 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java @@ -24,6 +24,7 @@ import java.security.PrivilegedAction; import java.util.List; import org.apache.tuscany.sca.core.context.impl.CallableReferenceImpl; +import org.apache.tuscany.sca.core.context.impl.CallbackServiceReferenceImpl; import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; import org.apache.tuscany.sca.core.invocation.CachedProxy; import org.apache.tuscany.sca.core.invocation.ProxyCreationException; @@ -72,7 +73,7 @@ public class JDKProxyFactory implements ProxyFactory { } public T createCallbackProxy(Class interfaze, List wires) throws ProxyCreationException { - ServiceReferenceImpl callbackReference = new ServiceReferenceImpl(interfaze, wires.get(0), null); + ServiceReferenceImpl callbackReference = new CallbackServiceReferenceImpl(interfaze, wires, this); return callbackReference != null ? createCallbackProxy(callbackReference) : null; } -- cgit v1.2.3