From 53b98fd7f18aae43f32a3ed11131b5876e90f887 Mon Sep 17 00:00:00 2001 From: rfeng Date: Mon, 17 May 2010 17:27:54 +0000 Subject: Make sure callback endpoints are created When a component is invoked from a non bidirectional interface, inject null to the fields/setters with @Callback git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@945259 13f79535-47bb-0310-9956-ffa450edef68 --- .../trunk/modules/builder/META-INF/MANIFEST.MF | 1 + .../builder/impl/EndpointReferenceBuilderImpl.java | 12 ++++++++ .../core/context/impl/ComponentContextImpl.java | 2 +- .../sca/core/invocation/CglibProxyFactory.java | 16 ++++++++-- .../invocation/impl/AsyncJDKInvocationHandler.java | 8 +++++ .../core/invocation/impl/JDKInvocationHandler.java | 35 ++++++++++++++++------ .../sca/core/invocation/impl/JDKProxyFactory.java | 30 +++++++++++++++++-- .../runtime/impl/EndpointReferenceBinderImpl.java | 7 ++--- 8 files changed, 92 insertions(+), 19 deletions(-) diff --git a/sca-java-2.x/trunk/modules/builder/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/modules/builder/META-INF/MANIFEST.MF index 0f7918bea8..7786e2bd40 100644 --- a/sca-java-2.x/trunk/modules/builder/META-INF/MANIFEST.MF +++ b/sca-java-2.x/trunk/modules/builder/META-INF/MANIFEST.MF @@ -31,6 +31,7 @@ Import-Package: javax.xml.namespace, org.apache.tuscany.sca.policy;version="2.0.0", org.apache.tuscany.sca.runtime;version="2.0.0", org.apache.tuscany.sca.xsd;version="2.0.0", + org.apache.ws.commons.schema, org.oasisopen.sca;version="2.0.0", org.w3c.dom Bundle-SymbolicName: org.apache.tuscany.sca.builder diff --git a/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointReferenceBuilderImpl.java b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointReferenceBuilderImpl.java index 2dd21fe3e6..d7c345b9f1 100644 --- a/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointReferenceBuilderImpl.java +++ b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointReferenceBuilderImpl.java @@ -314,6 +314,13 @@ public class EndpointReferenceBuilderImpl { endpointRef.setTargetEndpoint(createEndpoint(false)); endpointRef.setStatus(EndpointReference.Status.RESOLVED_BINDING); } + + if (reference.getCallbackService() != null) { + Endpoint callbackEndpoint = + createEndpoint(component, reference.getCallbackService(), false); + endpointRef.setCallbackEndpoint(callbackEndpoint); + } + reference.getEndpointReferences().add(endpointRef); continue; } // end if @@ -345,6 +352,11 @@ public class EndpointReferenceBuilderImpl { } endpointRef.setTargetEndpoint(endpoint); + if (reference.getCallbackService() != null) { + Endpoint callbackEndpoint = + createEndpoint(component, reference.getCallbackService(), false); + endpointRef.setCallbackEndpoint(callbackEndpoint); + } reference.getEndpointReferences().add(endpointRef); } } diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java index 470ed168cc..999854a822 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/ComponentContextImpl.java @@ -251,8 +251,8 @@ public class ComponentContextImpl implements RuntimeComponentContext { String bindingName = null; int index = serviceName.indexOf('/'); if (index != -1) { - serviceName = serviceName.substring(0, index); bindingName = serviceName.substring(index + 1); + serviceName = serviceName.substring(0, index); } for (ComponentService service : component.getServices()) { if (serviceName.equals(service.getName())) { diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CglibProxyFactory.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CglibProxyFactory.java index ed781b7ab1..cb2c01f7d6 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CglibProxyFactory.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CglibProxyFactory.java @@ -35,6 +35,7 @@ import org.apache.tuscany.sca.core.invocation.impl.JDKInvocationHandler; import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; import org.apache.tuscany.sca.invocation.MessageFactory; import org.apache.tuscany.sca.runtime.Invocable; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; import org.oasisopen.sca.ServiceReference; /** @@ -51,8 +52,15 @@ public class CglibProxyFactory implements ProxyFactory { } - public T createProxy(Class interfaze, Invocable wire) throws ProxyCreationException { - ServiceReference serviceReference = new ServiceReferenceImpl(interfaze, wire, null); + public T createProxy(final Class interfaze, Invocable invocable) throws ProxyCreationException { + if (invocable instanceof RuntimeEndpoint) { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(interfaze); + enhancer.setCallback(new CglibMethodInterceptor(interfaze, invocable)); + Object proxy = enhancer.create(); + return interfaze.cast(proxy); + } + ServiceReference serviceReference = new ServiceReferenceImpl(interfaze, invocable, null); return createProxy(serviceReference); } @@ -123,6 +131,10 @@ public class CglibProxyFactory implements ProxyFactory { invocationHandler = new JDKInvocationHandler(messageFactory, callableReference); } + public CglibMethodInterceptor(Class interfaze, Invocable invocable) { + invocationHandler = new JDKInvocationHandler(messageFactory, interfaze, invocable); + } + public CglibMethodInterceptor(ServiceReferenceImpl callbackReference) { invocationHandler = new JDKCallbackInvocationHandler(messageFactory, callbackReference); } diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/AsyncJDKInvocationHandler.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/AsyncJDKInvocationHandler.java index aeccfa8b05..67b1b923eb 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/AsyncJDKInvocationHandler.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/AsyncJDKInvocationHandler.java @@ -27,15 +27,23 @@ import javax.xml.ws.AsyncHandler; import javax.xml.ws.Response; import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.Invocable; import org.oasisopen.sca.ServiceReference; public class AsyncJDKInvocationHandler extends JDKInvocationHandler { + private static final long serialVersionUID = 1L; public AsyncJDKInvocationHandler(MessageFactory messageFactory, ServiceReference callableReference) { super(messageFactory, callableReference); } + public AsyncJDKInvocationHandler(MessageFactory messageFactory, + Class businessInterface, + Invocable source) { + super(messageFactory, businessInterface, source); + } + @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (isAsyncCallback(method)) { diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java index 77b72ed1d1..d34d9c8638 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKInvocationHandler.java @@ -38,6 +38,7 @@ import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.invocation.MessageFactory; import org.apache.tuscany.sca.runtime.Invocable; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; import org.oasisopen.sca.ServiceReference; import org.oasisopen.sca.ServiceRuntimeException; @@ -48,10 +49,9 @@ import org.oasisopen.sca.ServiceRuntimeException; public class JDKInvocationHandler implements InvocationHandler, Serializable { private static final long serialVersionUID = -3366410500152201371L; - protected boolean conversational; protected MessageFactory messageFactory; protected Endpoint target; - protected RuntimeEndpointReference source; + protected Invocable source; protected ServiceReferenceExt callableReference; protected Class businessInterface; @@ -59,7 +59,7 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { protected transient Map chains = new IdentityHashMap(); - public JDKInvocationHandler(MessageFactory messageFactory, Class businessInterface, RuntimeEndpointReference source) { + public JDKInvocationHandler(MessageFactory messageFactory, Class businessInterface, Invocable source) { this.messageFactory = messageFactory; this.source = source; this.businessInterface = businessInterface; @@ -91,9 +91,12 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { throw new ServiceRuntimeException("No runtime source is available"); } - if (source.isOutOfDate()) { - source.rebuild(); - chains.clear(); + if (source instanceof RuntimeEndpointReference) { + RuntimeEndpointReference epr = (RuntimeEndpointReference)source; + if (epr.isOutOfDate()) { + epr.rebuild(); + chains.clear(); + } } InvocationChain chain = getInvocationChain(method, source); @@ -188,6 +191,16 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { } protected synchronized InvocationChain getInvocationChain(Method method, Invocable source) { + if (source instanceof RuntimeEndpoint) { + InvocationChain invocationChain = source.getBindingInvocationChain(); + for (InvocationChain chain : source.getInvocationChains()) { + Operation operation = chain.getTargetOperation(); + if (method.getName().equals(operation.getName())) { + invocationChain.setTargetOperation(operation); + } + } + return source.getBindingInvocationChain(); + } if (fixedWire && chains.containsKey(method)) { return chains.get(method); } @@ -213,14 +226,18 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { this.target = endpoint; } - protected Object invoke(InvocationChain chain, Object[] args, RuntimeEndpointReference source) + protected Object invoke(InvocationChain chain, Object[] args, Invocable source) throws Throwable { Message msg = messageFactory.createMessage(); - msg.setFrom(source); + if (source instanceof RuntimeEndpointReference) { + msg.setFrom((RuntimeEndpointReference)source); + } if (target != null) { msg.setTo(target); } else { - msg.setTo(source.getTargetEndpoint()); + if (source instanceof RuntimeEndpointReference) { + msg.setTo(((RuntimeEndpointReference)source).getTargetEndpoint()); + } } Invoker headInvoker = chain.getHeadInvoker(); Operation operation = chain.getTargetOperation(); diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java index 13d4040b8e..a162110835 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java @@ -40,7 +40,9 @@ import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; import org.apache.tuscany.sca.invocation.MessageFactory; import org.apache.tuscany.sca.runtime.Invocable; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.ServiceRuntimeException; /** @@ -61,8 +63,24 @@ public class JDKProxyFactory implements ProxyFactory, LifeCycleListener { * The original createProxy method assumes that the proxy doesn't want to * share conversation state so sets the conversation object to null */ - public T createProxy(Class interfaze, Invocable wire) throws ProxyCreationException { - ServiceReference serviceReference = new ServiceReferenceImpl(interfaze, wire, null); + public T createProxy(final Class interfaze, Invocable invocable) throws ProxyCreationException { + if (invocable instanceof RuntimeEndpoint) { + InvocationHandler handler; + if (isAsync(interfaze)) { + handler = new AsyncJDKInvocationHandler(messageFactory, interfaze, invocable); + } else { + handler = new JDKInvocationHandler(messageFactory, interfaze, invocable); + } + // Allow privileged access to class loader. Requires RuntimePermission in security policy. + ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction() { + public ClassLoader run() { + return interfaze.getClassLoader(); + } + }); + T proxy = interfaze.cast(newProxyInstance(cl, new Class[] {interfaze}, handler)); + return proxy; + } + ServiceReference serviceReference = new ServiceReferenceImpl(interfaze, invocable, null); return createProxy(serviceReference); } @@ -105,7 +123,13 @@ public class JDKProxyFactory implements ProxyFactory, LifeCycleListener { } public T createCallbackProxy(Class interfaze, List wires) throws ProxyCreationException { - ServiceReferenceImpl callbackReference = new CallbackServiceReferenceImpl(interfaze, wires); + ServiceReferenceImpl callbackReference = null; + try { + callbackReference = new CallbackServiceReferenceImpl(interfaze, wires); + } catch (ServiceRuntimeException e) { + // [rfeng] In case that the call is not from a bidirectional interface, the field should be injected with null + callbackReference = null; + } return callbackReference != null ? createCallbackProxy(callbackReference) : null; } diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java index 1d2a4f9436..7b0f50ebd0 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java @@ -40,7 +40,6 @@ import org.apache.tuscany.sca.assembly.builder.PolicyBuilder; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.core.UtilityExtensionPoint; -import org.apache.tuscany.sca.core.assembly.impl.RuntimeEndpointImpl; import org.apache.tuscany.sca.definitions.Definitions; import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; import org.apache.tuscany.sca.monitor.Monitor; @@ -202,9 +201,9 @@ public class EndpointReferenceBinderImpl implements EndpointReferenceBinder { // a remote binding // still need to check that the callback endpoint is set correctly - if (hasCallback(endpointReference) && - endpointReference.getCallbackEndpoint() != null && - endpointReference.getCallbackEndpoint().isUnresolved() == true ){ + if (hasCallback(endpointReference) && + (endpointReference.getCallbackEndpoint() == null + || endpointReference.getCallbackEndpoint().isUnresolved())) { selectCallbackEndpoint(endpointReference, endpointReference.getReference().getCallbackService().getEndpoints(), matchAudit); -- cgit v1.2.3