You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jb...@apache.org on 2007/03/18 22:51:09 UTC

svn commit: r519706 - in /incubator/tuscany/java/sca/kernel/core/src: main/java/org/apache/tuscany/core/implementation/java/ test/java/org/apache/tuscany/core/implementation/java/

Author: jboynes
Date: Sun Mar 18 14:51:08 2007
New Revision: 519706

URL: http://svn.apache.org/viewvc?view=rev&rev=519706
Log:
remove WorkContext from JavaInvokerInterceptor
unify test cases

Removed:
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptorSequenceTestCase.java
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptorStatelessDestroyTestCase.java
Modified:
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptor.java
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaPhysicalComponentBuilder.java
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptorBasicTestCase.java

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptor.java?view=diff&rev=519706&r1=519705&r2=519706
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptor.java Sun Mar 18 14:51:08 2007
@@ -20,8 +20,6 @@
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
-import java.net.URI;
-import java.util.LinkedList;
 
 import org.apache.tuscany.spi.component.AtomicComponent;
 import org.apache.tuscany.spi.component.ComponentException;
@@ -37,8 +35,10 @@
  * Responsible for dispatching an invocation to a Java component implementation instance.
  *
  * @version $Rev$ $Date$
+ * @param <T> the implementation class for the component being invoked
+ * @param <CONTEXT> the type of context id used by the ScopeContainer
  */
-public class JavaInvokerInterceptor implements Interceptor {
+public class JavaInvokerInterceptor<T, CONTEXT> implements Interceptor {
     /* indicates that no conversational sequence is associated with the message */
     public final static short NONE = 0;
     /* indicates that the message initiates a conversation */
@@ -49,25 +49,22 @@
     public final static short END = 3;
 
     private Method operation;
-    private AtomicComponent component;
-    private WorkContext workContext;
-    private ScopeContainer scopeContainer;
+    private AtomicComponent<T> component;
+    private ScopeContainer<CONTEXT> scopeContainer;
 
     /**
      * Creates a new interceptor instance.
      *
      * @param operation      the method to invoke on the target instance
      * @param component      the target component
-     * @param workContext    the work context
      * @param scopeContainer the ScopeContainer that manages implementation instances for the target component
      */
     public JavaInvokerInterceptor(Method operation,
-                                  AtomicComponent component,
-                                  ScopeContainer scopeContainer,
-                                  WorkContext workContext) {
+                                  AtomicComponent<T> component,
+                                  ScopeContainer<CONTEXT> scopeContainer
+    ) {
         this.operation = operation;
         this.component = component;
-        this.workContext = workContext;
         this.scopeContainer = scopeContainer;
     }
 
@@ -85,15 +82,10 @@
 
     public Message invoke(Message msg) {
         try {
-            Object messageId = msg.getMessageId();
-            if (messageId != null) {
-                workContext.setCorrelationId(messageId);
-            }
-            LinkedList<URI> callbackRoutingChain = msg.getCallbackUris();
-            if (callbackRoutingChain != null) {
-                workContext.setCallbackUris(callbackRoutingChain);
-            }
-            Object resp = invokeTarget(msg.getBody(), msg.getConversationSequence());
+            Object body = msg.getBody();
+            short sequence = msg.getConversationSequence();
+            WorkContext workContext = msg.getWorkContext();
+            Object resp = invokeTarget(body, sequence, workContext);
             msg.setBody(resp);
         } catch (InvocationTargetException e) {
             msg.setBodyWithFault(e.getCause());
@@ -101,23 +93,22 @@
         return msg;
     }
 
-    @SuppressWarnings({"unchecked"})
-    private Object invokeTarget(final Object payload, final short sequence) throws InvocationTargetException {
+    private Object invokeTarget(final Object payload, final short sequence, final WorkContext workContext)
+        throws InvocationTargetException {
+        @SuppressWarnings("unchecked")
+        CONTEXT contextId = (CONTEXT) workContext.getIdentifier(scopeContainer.getScope());
         try {
-            InstanceWrapper<?> wrapper = getInstance(sequence);
+            InstanceWrapper<T> wrapper = getInstance(sequence, contextId);
             Object instance = wrapper.getInstance();
-            Object ret;
-            if (payload != null && !payload.getClass().isArray()) {
-                ret = operation.invoke(instance, payload);
-            } else {
-                ret = operation.invoke(instance, (Object[]) payload);
-            }
-            scopeContainer.returnWrapper(component, wrapper);
-            if (sequence == END) {
-                // if end conversation, remove resource
-                scopeContainer.remove(component);
+            try {
+                return operation.invoke(instance, (Object[]) payload);
+            } finally {
+                scopeContainer.returnWrapper(component, wrapper, contextId);
+                if (sequence == END) {
+                    // if end conversation, remove resource
+                    scopeContainer.remove(component);
+                }
             }
-            return ret;
         } catch (IllegalAccessException e) {
             throw new InvocationTargetException(e);
         } catch (ComponentException e) {
@@ -129,18 +120,18 @@
      * Resolves the target service instance or returns a cached one
      *
      * @param sequence the conversational sequence
+     * @param contextId the scope contextId
      * @return the InstanceWrapper
      * @throws TargetException if an exception getting the wrapper is encountered
      */
-    private InstanceWrapper<?> getInstance(short sequence) throws TargetException {
+    private InstanceWrapper<T> getInstance(short sequence, CONTEXT contextId) throws TargetException {
         switch (sequence) {
             case NONE:
-                return scopeContainer.getWrapper(component);
             case START:
-                return scopeContainer.getWrapper(component);
+                return scopeContainer.getWrapper(component, contextId);
             case CONTINUE:
             case END:
-                return scopeContainer.getAssociatedWrapper(component);
+                return scopeContainer.getAssociatedWrapper(component, contextId);
             default:
                 throw new InvalidConversationSequenceException("Unknown sequence type", String.valueOf(sequence));
         }

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaPhysicalComponentBuilder.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaPhysicalComponentBuilder.java?view=diff&rev=519706&r1=519705&r2=519706
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaPhysicalComponentBuilder.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaPhysicalComponentBuilder.java Sun Mar 18 14:51:08 2007
@@ -67,8 +67,6 @@
     // Classloader registry
     private ClassLoaderRegistry classLoaderRegistry;
 
-    private WorkContext workContext;
-
     private ProxyService proxyService;
 
     public JavaPhysicalComponentBuilder(
@@ -78,11 +76,6 @@
     }
 
     @Reference
-    public void setWorkContext(WorkContext workContext) {
-        this.workContext = workContext;
-    }
-
-    @Reference
     public void setProxyService(ProxyService proxyService) {
         this.proxyService = proxyService;
     }
@@ -204,7 +197,7 @@
                 URI targetUri = wire.getTargetUri();
                 throw new WireAttachException("No matching method found", sourceUri, targetUri, e);
             }
-            chain.addInterceptor(new JavaInvokerInterceptor(method, component, scopeContainer, workContext));
+            chain.addInterceptor(new JavaInvokerInterceptor(method, component, scopeContainer));
         }
     }
 

Modified: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptorBasicTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptorBasicTestCase.java?view=diff&rev=519706&r1=519705&r2=519706
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptorBasicTestCase.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/implementation/java/JavaInvokerInterceptorBasicTestCase.java Sun Mar 18 14:51:08 2007
@@ -20,115 +20,191 @@
 
 import java.lang.reflect.Method;
 
+import junit.framework.TestCase;
+import org.easymock.IMocksControl;
+import org.easymock.classextension.EasyMock;
+
+import org.apache.tuscany.spi.component.AtomicComponent;
 import org.apache.tuscany.spi.component.InstanceWrapper;
 import org.apache.tuscany.spi.component.ScopeContainer;
 import org.apache.tuscany.spi.component.WorkContext;
 import org.apache.tuscany.spi.model.Scope;
 import org.apache.tuscany.spi.wire.Message;
-import org.apache.tuscany.spi.wire.MessageImpl;
-
-import junit.framework.TestCase;
-import org.easymock.classextension.EasyMock;
 
-public class JavaInvokerInterceptorBasicTestCase extends TestCase {
+public class JavaInvokerInterceptorBasicTestCase<CONTEXT> extends TestCase {
+    private TestBean bean;
+    private CONTEXT contextId;
     private Method echoMethod;
     private Method arrayMethod;
     private Method nullParamMethod;
     private Method primitiveMethod;
     private Method checkedMethod;
     private Method runtimeMethod;
-    private WorkContext context;
-    private ScopeContainer scopeContainer;
-    private InstanceWrapper wrapper;
-    private TestBean bean;
-    private JavaAtomicComponent component;
 
-    public JavaInvokerInterceptorBasicTestCase(String arg0) {
-        super(arg0);
-    }
+    private IMocksControl control;
+    private WorkContext workContext;
+    private ScopeContainer<CONTEXT> scopeContainer;
+    private InstanceWrapper<TestBean> wrapper;
+    private AtomicComponent<TestBean> component;
+    private Message message;
 
     public void testObjectInvoke() throws Throwable {
-        JavaInvokerInterceptor invoker = new JavaInvokerInterceptor(echoMethod, component, scopeContainer, context);
-        Message message = new MessageImpl();
-        message.setBody("foo");
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(echoMethod, component, scopeContainer);
+        String value = "foo";
+        mockCall(new Object[]{value});
+        message.setBody(value);
+        control.replay();
+        Message ret = invoker.invoke(message);
+        assertSame(ret, message);
+        control.verify();
+    }
+
+    public void testPrimitiveInvoke() throws Throwable {
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(primitiveMethod, component, scopeContainer);
+        Integer value = 1;
+        mockCall(new Object[]{value});
+        message.setBody(value);
+        control.replay();
         Message ret = invoker.invoke(message);
-        assertEquals("foo", ret.getBody());
+        assertSame(ret, message);
+        control.verify();
     }
 
     public void testArrayInvoke() throws Throwable {
-        JavaInvokerInterceptor invoker = new JavaInvokerInterceptor(arrayMethod, component, scopeContainer, context);
-        String[] args = new String[]{"foo", "bar"};
-        Message message = new MessageImpl();
-        message.setBody(new Object[]{args});
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(arrayMethod, component, scopeContainer);
+        String[] value = new String[]{"foo", "bar"};
+        mockCall(new Object[]{value});
+        message.setBody(value);
+        control.replay();
+        Message ret = invoker.invoke(message);
+        assertSame(ret, message);
+        control.verify();
+    }
 
+    public void testEmptyInvoke() throws Throwable {
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(nullParamMethod, component, scopeContainer);
+        mockCall(new Object[]{});
+        message.setBody("foo");
+        control.replay();
         Message ret = invoker.invoke(message);
-        String[] retA = (String[]) ret.getBody();
-        assertNotNull(retA);
-        assertEquals(2, retA.length);
-        assertEquals("foo", retA[0]);
-        assertEquals("bar", retA[1]);
+        assertSame(ret, message);
+        control.verify();
     }
 
     public void testNullInvoke() throws Throwable {
-        JavaInvokerInterceptor invoker =
-            new JavaInvokerInterceptor(nullParamMethod, component, scopeContainer, context);
-        Message message = new MessageImpl();
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(nullParamMethod, component, scopeContainer);
+        mockCall(null);
+        message.setBody("foo");
+        control.replay();
         Message ret = invoker.invoke(message);
-        String retS = (String) ret.getBody();
-        assertEquals("foo", retS);
+        assertSame(ret, message);
+        control.verify();
     }
 
-    public void testPrimitiveInvoke() throws Throwable {
-        JavaInvokerInterceptor invoker =
-            new JavaInvokerInterceptor(primitiveMethod, component, scopeContainer, context);
-        Message message = new MessageImpl();
-        message.setBody(new Integer[]{1});
+    public void testInvokeCheckedException() throws Throwable {
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(checkedMethod, component, scopeContainer);
+        mockCall(null);
+        message.setBodyWithFault(EasyMock.isA(TestException.class));
+        control.replay();
         Message ret = invoker.invoke(message);
-        Integer retI = (Integer) ret.getBody();
-        assertEquals(1, retI.intValue());
+        assertSame(ret, message);
+        control.verify();
     }
 
-    public void testInvokeCheckedException() throws Throwable {
-        JavaInvokerInterceptor invoker = new JavaInvokerInterceptor(checkedMethod, component, scopeContainer, context);
-        Message message = new MessageImpl();
+    public void testInvokeRuntimeException() throws Throwable {
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(runtimeMethod, component, scopeContainer);
+        mockCall(null);
+        message.setBodyWithFault(EasyMock.isA(TestRuntimeException.class));
+        control.replay();
+        Message ret = invoker.invoke(message);
+        assertSame(ret, message);
+        control.verify();
+    }
+
+    public void testSequenceStart() throws Throwable {
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(nullParamMethod, component, scopeContainer);
+        mockCall(null, JavaInvokerInterceptor.START);
+        message.setBody("foo");
+        control.replay();
         Message ret = invoker.invoke(message);
-        assertTrue(ret.isFault());
+        assertSame(ret, message);
+        control.verify();
     }
 
-    public void testInvokeRuntimeException() throws Throwable {
-        JavaInvokerInterceptor invoker = new JavaInvokerInterceptor(runtimeMethod, component, scopeContainer, context);
-        Message message = new MessageImpl();
+    public void testSequenceContinue() throws Throwable {
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(nullParamMethod, component, scopeContainer);
+        mockCall(null, JavaInvokerInterceptor.CONTINUE);
+        message.setBody("foo");
+        control.replay();
         Message ret = invoker.invoke(message);
-        assertTrue(ret.isFault());
+        assertSame(ret, message);
+        control.verify();
     }
 
+    public void testSequenceEnd() throws Throwable {
+        JavaInvokerInterceptor<TestBean, CONTEXT> invoker =
+            new JavaInvokerInterceptor<TestBean, CONTEXT>(nullParamMethod, component, scopeContainer);
+        mockCall(null, JavaInvokerInterceptor.END);
+        message.setBody("foo");
+        control.replay();
+        Message ret = invoker.invoke(message);
+        assertSame(ret, message);
+        control.verify();
+    }
+
+    private void mockCall(Object value) throws Exception {
+        mockCall(value, JavaInvokerInterceptor.NONE);
+    }
+
+    private void mockCall(Object value, short sequence) throws Exception {
+        EasyMock.expect(message.getBody()).andReturn(value);
+        EasyMock.expect(message.getConversationSequence()).andReturn(sequence);
+        EasyMock.expect(message.getWorkContext()).andReturn(workContext);
+        EasyMock.expect(scopeContainer.getScope()).andReturn(Scope.COMPOSITE);
+        EasyMock.expect(workContext.getIdentifier(Scope.COMPOSITE)).andReturn(contextId);
+        if (sequence == JavaInvokerInterceptor.START || sequence == JavaInvokerInterceptor.NONE) {
+            EasyMock.expect(scopeContainer.getWrapper(component, contextId)).andReturn(wrapper);
+        } else if (sequence == JavaInvokerInterceptor.CONTINUE || sequence == JavaInvokerInterceptor.END) {
+            EasyMock.expect(scopeContainer.getAssociatedWrapper(component, contextId)).andReturn(wrapper);
+        } else {
+            fail();
+        }
+        EasyMock.expect(wrapper.getInstance()).andReturn(bean);
+        scopeContainer.returnWrapper(component, wrapper, contextId);
+        if (sequence == JavaInvokerInterceptor.END) {
+            scopeContainer.remove(component);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
     public void setUp() throws Exception {
+        bean = new TestBean();
+        contextId = (CONTEXT) new Object();
         echoMethod = TestBean.class.getDeclaredMethod("echo", String.class);
         arrayMethod = TestBean.class.getDeclaredMethod("arrayEcho", String[].class);
-        nullParamMethod = TestBean.class.getDeclaredMethod("nullParam", (Class[]) null);
+        nullParamMethod = TestBean.class.getDeclaredMethod("nullParam");
         primitiveMethod = TestBean.class.getDeclaredMethod("primitiveEcho", Integer.TYPE);
-        checkedMethod = TestBean.class.getDeclaredMethod("checkedException", (Class[]) null);
-        runtimeMethod = TestBean.class.getDeclaredMethod("runtimeException", (Class[]) null);
+        checkedMethod = TestBean.class.getDeclaredMethod("checkedException");
+        runtimeMethod = TestBean.class.getDeclaredMethod("runtimeException");
         assertNotNull(echoMethod);
         assertNotNull(checkedMethod);
         assertNotNull(runtimeMethod);
 
-        context = EasyMock.createNiceMock(WorkContext.class);
-        component = EasyMock.createMock(JavaAtomicComponent.class);
-        scopeContainer = EasyMock.createNiceMock(ScopeContainer.class);
-        wrapper = EasyMock.createNiceMock(InstanceWrapper.class);
-        bean = new TestBean();
-        EasyMock.replay(component);
-        EasyMock.expect(scopeContainer.getScope()).andReturn(Scope.COMPOSITE);
-        EasyMock.expect(scopeContainer.getWrapper(component)).andReturn(wrapper);
-        EasyMock.replay(scopeContainer);
-        EasyMock.expect(wrapper.getInstance()).andReturn(bean);
-        EasyMock.replay(wrapper);
-    }
-
-
-    protected void tearDown() throws Exception {
-        super.tearDown();
+        control = EasyMock.createStrictControl();
+        workContext = control.createMock(WorkContext.class);
+        component = control.createMock(AtomicComponent.class);
+        scopeContainer = control.createMock(ScopeContainer.class);
+        wrapper = control.createMock(InstanceWrapper.class);
+        message = control.createMock(Message.class);
     }
 
     private class TestBean {



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org