Author: jmarino
Date: Sat May 13 11:35:40 2006
New Revision: 406136

URL: http://svn.apache.org/viewcvs?rev=406136&view=rev
Log:
start of wire testcases

Modified:
    
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/DifferentInterfaceWireTestCase.java
    
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/ReferenceInjectionTestCase.java
    
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java
    
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java
    
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/DynamicTargetResolver.java

Modified: 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/DifferentInterfaceWireTestCase.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/DifferentInterfaceWireTestCase.java?rev=406136&r1=406135&r2=406136&view=diff
==============================================================================
--- 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/DifferentInterfaceWireTestCase.java
 (original)
+++ 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/DifferentInterfaceWireTestCase.java
 Sat May 13 11:35:40 2006
@@ -52,7 +52,7 @@
         ScopeContext<AtomicContext> scope = new ModuleScopeContext(ctx);
         scope.start();
         Map<String, AtomicContext> contexts = 
MockFactory.createWiredContexts("source", SourceImpl.class, Target.class, scope,
-                "target", OtherTarget.class, OtherTargetImpl.class, members, 
scope);
+                members, "target", OtherTarget.class, OtherTargetImpl.class, 
scope);
         AtomicContext sourceContext = contexts.get("source");
         Source source = (Source) sourceContext.getService(null);
         Target target = source.getTarget();

Modified: 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/ReferenceInjectionTestCase.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/ReferenceInjectionTestCase.java?rev=406136&r1=406135&r2=406136&view=diff
==============================================================================
--- 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/ReferenceInjectionTestCase.java
 (original)
+++ 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/context/ReferenceInjectionTestCase.java
 Sat May 13 11:35:40 2006
@@ -30,7 +30,7 @@
         ScopeContext<AtomicContext> scope = new ModuleScopeContext(ctx);
         scope.start();
         Map<String,AtomicContext> contexts = 
MockFactory.createWiredContexts("source",SourceImpl.class, scope,
-                "target",Target.class,TargetImpl.class,members, scope);
+                members, "target",Target.class,TargetImpl.class, scope);
         AtomicContext sourceContext = contexts.get("source");
         Source source = (Source)sourceContext.getService(null);
         Target target = source.getTarget();

Modified: 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java?rev=406136&r1=406135&r2=406136&view=diff
==============================================================================
--- 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java
 (original)
+++ 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java
 Sat May 13 11:35:40 2006
@@ -13,119 +13,162 @@
  */
 package org.apache.tuscany.container.java.integration.context;
 
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.concurrent.FutureTask;
+
 import junit.framework.TestCase;
+import org.apache.tuscany.container.java.mock.MockFactory;
+import org.apache.tuscany.container.java.mock.components.Source;
+import org.apache.tuscany.container.java.mock.components.SourceImpl;
+import org.apache.tuscany.container.java.mock.components.Target;
+import org.apache.tuscany.container.java.mock.components.TargetImpl;
+import org.apache.tuscany.core.context.WorkContextImpl;
+import org.apache.tuscany.core.context.event.HttpSessionEnd;
+import org.apache.tuscany.core.context.event.HttpSessionStart;
+import org.apache.tuscany.core.context.event.ModuleStart;
+import org.apache.tuscany.core.context.event.ModuleStop;
+import org.apache.tuscany.core.context.scope.HttpSessionScopeContext;
+import org.apache.tuscany.core.context.scope.ModuleScopeContext;
+import org.apache.tuscany.core.context.scope.RequestScopeContext;
+import org.apache.tuscany.core.util.JavaIntrospectionHelper;
+import org.apache.tuscany.spi.context.AtomicContext;
+import org.apache.tuscany.spi.context.ScopeContext;
+import org.apache.tuscany.spi.context.WorkContext;
 
 /**
- * Tests scoping is properly handled for service references 
- * 
+ * Tests scoping is properly handled for service references
+ *
  * @version $Rev$ $Date$
  */
 public class ScopeReferenceTestCase extends TestCase {
 
-    /**
-     * Tests a module-to-module scoped wire is setup properly by the runtime
-     */
-    public void testModuleToModule() throws Exception{
-//        RuntimeContext runtime = MockFactory.createJavaRuntime();
-//        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
-//        Assert.assertNotNull(ctx);
-//        
runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-//        CompositeContext testCtx = (CompositeContext) 
runtime.getRootContext().getContext("test");
-//        Assert.assertNotNull(testCtx);
-//        testCtx.registerModelObject(MockFactory.createModule());
-//        testCtx.publish(new ModuleStart(this));
-//        GenericComponent source = 
(GenericComponent)testCtx.getContext("source").getInstance(null);
-//        Assert.assertNotNull(source);
-//        GenericComponent target = 
(GenericComponent)testCtx.getContext("target").getInstance(null);
-//        Assert.assertNotNull(target);
-//        source.getGenericComponent().getString();
-   }
+    private Map<String, Member> members;
+
 
     /**
-     * Tests a module-to-session scoped wire is setup properly by the runtime
+     * Tests a module-to-module scoped wire
      */
-    public void testModuleToSession() throws Exception{
-//        RuntimeContext runtime = MockFactory.createJavaRuntime();
-//        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
-//        Assert.assertNotNull(ctx);
-//        
runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-//        CompositeContext testCtx = (CompositeContext) 
runtime.getRootContext().getContext("test");
-//        Assert.assertNotNull(testCtx);
-//        
testCtx.registerModelObject(MockFactory.createModule(Scope.MODULE,Scope.SESSION));
-//        testCtx.publish(new ModuleStart(this));
-//
-//        // first session
-//        Object session = new Object();
-//        Object id = new Object();
-//        testCtx.publish(new RequestStart(this,id));
-//        testCtx.publish(new HttpSessionBound(this,session));
-//        GenericComponent source = 
(GenericComponent)testCtx.getContext("source").getInstance(null);
-//        Assert.assertNotNull(source);
-//        GenericComponent target = 
(GenericComponent)testCtx.getContext("target").getInstance(null);
-//        Assert.assertNotNull(target);
-//        source.getGenericComponent().setString("foo");
-//        Assert.assertEquals("foo",target.getString());
-//        testCtx.publish(new RequestEnd(this,id));
-//
-//        //second session
-//        Object session2 = new Object();
-//        Object id2 = new Object();
-//        testCtx.publish(new RequestStart(this,id2));
-//        testCtx.publish(new HttpSessionBound(this,session2));
-//        GenericComponent target2 = 
(GenericComponent)testCtx.getContext("target").getInstance(null);
-//        Assert.assertNotNull(target2);
-//        Assert.assertTrue(!"foo".equals(target2.getString()));
-//
-//        
Assert.assertTrue(!"foo".equals(source.getGenericComponent().getString()));
-//        source.getGenericComponent().setString("bar");
-//        Assert.assertEquals("bar",target2.getString());
-//        Assert.assertEquals("bar",source.getGenericComponent().getString());
-//        //testCtx.fireEvent(EventContext.SESSION_NOTIFY,session);
-//
-   }
+    @SuppressWarnings("unchecked")
+    public void testModuleToModule() throws Exception {
+        WorkContext ctx = new WorkContextImpl();
+        ScopeContext<AtomicContext> scope = new ModuleScopeContext(ctx);
+        scope.start();
+
+        Map<String, AtomicContext> contexts = 
MockFactory.createWiredContexts("source", SourceImpl.class, scope, members, 
"target", Target.class, TargetImpl.class, scope);
+        scope.onEvent(new ModuleStart(this, null));
+        AtomicContext<Source> sourceContext = (AtomicContext<Source>) 
contexts.get("source");
+        AtomicContext<Target> targetContext = (AtomicContext<Target>) 
contexts.get("target");
+        Source source = sourceContext.getService();
+        Target target = targetContext.getService();
+        assertNull(source.getTarget().getString());
+        assertNull(target.getString());
+        target.setString("foo");
+        assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+        assertEquals("foo", source.getTarget().getString());
+        scope.onEvent(new ModuleStop(this, null));
+        scope.stop();
+    }
 
     /**
-     * Tests a module-to-request scoped wire is setup properly by the runtime
+     * Tests a module-to-session scoped wire is setup properly by the runtime
      */
-    public void testModuleToRequest() throws Exception{
-//        RuntimeContext runtime = MockFactory.createJavaRuntime();
-//        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
-//        Assert.assertNotNull(ctx);
-//        
runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-//        CompositeContext testCtx = (CompositeContext) 
runtime.getRootContext().getContext("test");
-//        Assert.assertNotNull(testCtx);
-//        
testCtx.registerModelObject(MockFactory.createModule(Scope.MODULE,Scope.REQUEST));
-//        testCtx.publish(new ModuleStart(this));
-//
-//        // first request
-//        Object id = new Object();
-//        testCtx.publish(new RequestStart(this,id));
-//        GenericComponent source = 
(GenericComponent)testCtx.getContext("source").getInstance(null);
-//        Assert.assertNotNull(source);
-//        GenericComponent target = 
(GenericComponent)testCtx.getContext("target").getInstance(null);
-//        Assert.assertNotNull(target);
-//        source.getGenericComponent().setString("foo");
-//        Assert.assertEquals("foo",target.getString());
-//        testCtx.publish(new RequestEnd(this,id));
-//
-//        //second request
-//        Object id2 = new Object();
-//        testCtx.publish(new RequestStart(this,id2));
-//        GenericComponent target2 = 
(GenericComponent)testCtx.getContext("target").getInstance(null);
-//        Assert.assertNotNull(target2);
-//        Assert.assertTrue(!"foo".equals(target2.getString()));
-//
-//        
Assert.assertTrue(!"foo".equals(source.getGenericComponent().getString()));
-//        source.getGenericComponent().setString("bar");
-//        Assert.assertEquals("bar",target2.getString());
-//        Assert.assertEquals("bar",source.getGenericComponent().getString());
-
+    @SuppressWarnings("unchecked")
+    public void testModuleToSession() throws Exception {
+        WorkContext ctx = new WorkContextImpl();
+        ScopeContext<AtomicContext> moduleScope = new ModuleScopeContext(ctx);
+        moduleScope.start();
+        ScopeContext<AtomicContext> sessionScope = new 
HttpSessionScopeContext(ctx);
+        sessionScope.start();
+
+        Map<String, AtomicContext> contexts = 
MockFactory.createWiredContexts("source", SourceImpl.class,
+                moduleScope, members, "target", Target.class, 
TargetImpl.class, sessionScope);
+        moduleScope.onEvent(new ModuleStart(this, null));
+        Object session1 = new Object();
+        ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session1);
+        sessionScope.onEvent(new HttpSessionStart(this, session1));
+        AtomicContext<Source> sourceContext = (AtomicContext<Source>) 
contexts.get("source");
+        AtomicContext<Target> targetContext = (AtomicContext<Target>) 
contexts.get("target");
+        Source source = sourceContext.getService();
+        Target target = targetContext.getService();
+        assertNull(source.getTarget().getString());
+        assertNull(target.getString());
+        target.setString("foo");
+        assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+        assertEquals("foo", source.getTarget().getString());
+        ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+        sessionScope.onEvent(new HttpSessionEnd(this, session1));
+
+        //second session
+        Object session2 = new Object();
+        ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session2);
+        sessionScope.onEvent(new HttpSessionStart(this, session2));
+
+        Target target2 = targetContext.getService();
+        assertTrue(!"foo".equals(target2.getString()));
+
+        assertTrue(!"foo".equals(source.getTarget().getString()));
+        source.getTarget().setString("bar");
+        assertEquals("bar", target2.getString());
+        assertEquals("bar", source.getTarget().getString());
+        sessionScope.onEvent(new HttpSessionEnd(this, session2));
+
+        ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+        moduleScope.onEvent(new ModuleStop(this, null));
+        sessionScope.stop();
+        moduleScope.stop();
+    }
+
+    /**
+     * Tests a module-to-request scoped wire
+     */
+    @SuppressWarnings("unchecked")
+    public void testModuleToRequest() throws Exception {
+        WorkContext ctx = new WorkContextImpl();
+        ScopeContext<AtomicContext> moduleScope = new ModuleScopeContext(ctx);
+        moduleScope.start();
+        ScopeContext<AtomicContext> requestScope = new 
RequestScopeContext(ctx);
+        requestScope.start();
+
+        Map<String, AtomicContext> contexts = 
MockFactory.createWiredContexts("source", SourceImpl.class,
+                moduleScope, members, "target", Target.class, 
TargetImpl.class, requestScope);
+        moduleScope.onEvent(new ModuleStart(this, null));
+
+        AtomicContext<Source> sourceContext = (AtomicContext<Source>) 
contexts.get("source");
+        final AtomicContext<Target> targetContext = (AtomicContext<Target>) 
contexts.get("target");
+        final Source source = sourceContext.getService();
+        Target target = targetContext.getService();
+        assertNull(source.getTarget().getString());
+        assertNull(target.getString());
+        target.setString("foo");
+        assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+        assertEquals("foo", source.getTarget().getString());
+
+        // spin off another request
+        Executor executor = Executors.newSingleThreadExecutor();
+        FutureTask<Void> future = new FutureTask<Void>(new Runnable() {
+            public void run() {
+                Target target2 = targetContext.getService();
+                assertTrue(!"foo".equals(target2.getString()));
+                assertTrue(!"foo".equals(source.getTarget().getString()));
+                source.getTarget().setString("bar");
+                assertEquals("bar", target2.getString());
+                assertEquals("bar", source.getTarget().getString());
+            }
+        }, null);
+        executor.execute(future);
+        future.get();
     }
-    
+
     /**
      * Tests a module-to-stateless scoped wire is setup properly by the runtime
      */
-    public void testModuleToStateless() throws Exception{
+    public void testModuleToStateless() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -156,12 +199,12 @@
 //        
Assert.assertTrue(!"foo".equals(source.getGenericComponent().getString()));
 //        source.getGenericComponent().setString("bar");
 //        Assert.assertTrue(!"bar".equals(target2.getString()));
-   }
-    
+    }
+
     /**
      * Tests a session-to-session scoped wire is setup properly by the runtime
      */
-    public void testSessionToSession() throws Exception{
+    public void testSessionToSession() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -205,11 +248,11 @@
 //
     }
 
-    
+
     /**
      * Tests a session-to-module scoped wire is setup properly by the runtime
      */
-    public void testSessionToModule() throws Exception{
+    public void testSessionToModule() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -251,13 +294,13 @@
 //        Assert.assertEquals("baz",target.getString());
 //
 //        testCtx.publish(new RequestEnd(this,session2));
-       
+
     }
 
     /**
      * Tests a session-to-request scoped wire is setup properly by the runtime
      */
-    public void testSessionToRequest() throws Exception{
+    public void testSessionToRequest() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -299,12 +342,12 @@
 //        testCtx.publish(new RequestEnd(this,session));
 
     }
-    
-    
+
+
     /**
      * Tests a session-to-stateless scoped wire is setup properly by the 
runtime
      */
-    public void testSessionToStateless() throws Exception{
+    public void testSessionToStateless() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -350,7 +393,7 @@
     /**
      * Tests a request-to-request scoped wire is setup properly by the runtime
      */
-    public void testRequestToRequest() throws Exception{
+    public void testRequestToRequest() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -391,7 +434,7 @@
     /**
      * Tests a request-to-module scoped wire is setup properly by the runtime
      */
-    public void testRequestToModule() throws Exception{
+    public void testRequestToModule() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -433,7 +476,7 @@
     /**
      * Tests a request-to-session scoped wire is setup properly by the runtime
      */
-    public void testRequestToSession() throws Exception{
+    public void testRequestToSession() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -492,11 +535,11 @@
 
     }
 
-    
+
     /**
      * Tests a request-to-stateless scoped wire is setup properly by the 
runtime
      */
-    public void testRequestToStateless() throws Exception{
+    public void testRequestToStateless() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -534,11 +577,11 @@
 //        testCtx.publish(new RequestEnd(this,new Object()));
     }
 
-    
+
     /**
      * Tests a stateless-to-stateless scoped wire is setup properly by the 
runtime
      */
-    public void testStatelessToStateless() throws Exception{
+    public void testStatelessToStateless() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -575,11 +618,11 @@
 //
 //        testCtx.publish(new RequestEnd(this,new Object()));
     }
-    
+
     /**
      * Tests a stateless-to-request scoped wire is setup properly by the 
runtime
      */
-    public void testStatelessToRequest() throws Exception{
+    public void testStatelessToRequest() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -619,12 +662,12 @@
 //        Assert.assertEquals("baz",target2.getString());
 //
 //        testCtx.publish(new RequestEnd(this,new Object()));
-      }
+    }
 
     /**
      * Tests a stateless-to-session scoped wire is setup properly by the 
runtime
      */
-    public void testStatelessToSession() throws Exception{
+    public void testStatelessToSession() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -683,11 +726,11 @@
 //
     }
 
-    
+
     /**
      * Tests a stateless-to-module scoped wire is setup properly by the runtime
      */
-    public void testStatelessToModule() throws Exception{
+    public void testStatelessToModule() throws Exception {
 //        RuntimeContext runtime = MockFactory.createJavaRuntime();
 //        Context ctx = 
runtime.getSystemContext().getContext("tuscany.system.child");
 //        Assert.assertNotNull(ctx);
@@ -724,6 +767,18 @@
 //        testCtx.publish(new RequestEnd(this,new Object()));
 //
     }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        members = new HashMap<String, Member>();
+        Method[] methods = SourceImpl.class.getMethods();
+        for (Method method : methods) {
+            if (method.getName().startsWith("set")) {
+                
members.put(JavaIntrospectionHelper.toPropertyName(method.getName()), method);
+            }
+        }
+    }
+
 
 }
 

Modified: 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java?rev=406136&r1=406135&r2=406136&view=diff
==============================================================================
--- 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java
 (original)
+++ 
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java
 Sat May 13 11:35:40 2006
@@ -24,6 +24,8 @@
 import org.apache.tuscany.spi.builder.BuilderConfigException;
 import org.apache.tuscany.spi.context.AtomicContext;
 import org.apache.tuscany.spi.context.ScopeContext;
+import org.apache.tuscany.spi.wire.Interceptor;
+import org.apache.tuscany.spi.wire.MessageHandler;
 import org.apache.tuscany.spi.wire.SourceInvocationChain;
 import org.apache.tuscany.spi.wire.SourceWire;
 import org.apache.tuscany.spi.wire.TargetInvocationChain;
@@ -48,55 +50,25 @@
         return new JavaAtomicContext(name, serviceInterfaces, 
createObjectFactory(clazz, null), eagerInit, initInvoker, destroyInvoker, 
injectors, members);
     }
 
-    public static <T> TargetWire<T> createTargetWireFactory(String 
serviceName, Class<T> interfaze) throws WireFactoryInitException {
-        TargetWire<T> wire = new JDKTargetWire<T>();
-        wire.setBusinessInterface(interfaze);
-        wire.setServiceName(serviceName);
-        
wire.setInvocationChains(createTargetInvocationConfigurations(interfaze));
-        wire.initialize();
-        return wire;
-    }
-
-    public static Map<Method, TargetInvocationChain> 
createTargetInvocationConfigurations(Class<?> interfaze) {
-        Map<Method, TargetInvocationChain> invocations = new 
MethodHashMap<TargetInvocationChain>();
-        Method[] methods = interfaze.getMethods();
-        for (Method method : methods) {
-            TargetInvocationChain iConfig = new 
TargetInvocationChainImpl(method);
-            // add tail interceptor
-            iConfig.addInterceptor(new InvokerInterceptor());
-            invocations.put(method, iConfig);
-        }
-        return invocations;
-    }
-
-    public static <T> SourceWire<T> createSourceWireFactory(String refName, 
Class<T> interfaze) throws WireFactoryInitException {
-        SourceWire<T> wire = new JDKSourceWire<T>();
-        wire.setReferenceName(refName);
-        
wire.setInvocationChains(createSourceInvocationConfigurations(interfaze));
-        wire.setBusinessInterface(interfaze);
-        wire.initialize();
-        return wire;
-    }
-
     /**
      * Wires two contexts together where the reference interface is the same 
as target service
      *
      * @param sourceName
      * @param sourceClass
      * @param sourceScope
+     * @param members
      * @param targetName
      * @param targetService
      * @param targetClass
-     * @param members
      * @param targetScope
      * @return
      * @throws Exception
      */
     public static Map<String, AtomicContext> createWiredContexts(String 
sourceName, Class<?> sourceClass,
                                                                  
ScopeContext<AtomicContext> sourceScope,
-                                                                 String 
targetName, Class<?> targetService, Class<?> targetClass,
-                                                                 Map<String, 
Member> members, ScopeContext<AtomicContext> targetScope) throws Exception {
-        return createWiredContexts(sourceName, sourceClass, targetService, 
sourceScope, targetName, targetService, targetClass, members, targetScope);
+                                                                 Map<String, 
Member> members, String targetName, Class<?> targetService, Class<?> 
targetClass,
+                                                                 
ScopeContext<AtomicContext> targetScope) throws Exception {
+        return createWiredContexts(sourceName, sourceClass, targetService, 
sourceScope, members, targetName, targetService, targetClass, targetScope);
 
     }
 
@@ -107,25 +79,42 @@
      * @param sourceClass
      * @param sourceReferenceClass
      * @param sourceScope
+     * @param members
      * @param targetName
      * @param targetService
      * @param targetClass
-     * @param members
      * @param targetScope
      * @return
      * @throws Exception
      */
     public static Map<String, AtomicContext> createWiredContexts(String 
sourceName, Class<?> sourceClass, Class<?> sourceReferenceClass,
                                                                  
ScopeContext<AtomicContext> sourceScope,
+                                                                 Map<String, 
Member> members, String targetName,
+                                                                 Class<?> 
targetService, Class<?> targetClass,
+                                                                 
ScopeContext<AtomicContext> targetScope) throws Exception {
+        return 
createWiredContexts(sourceName,sourceClass,sourceReferenceClass,sourceScope,null,null,null,members,targetName,targetService,
+                                                                  
targetClass,targetScope,null,null,null);
+    }
+
+    public static Map<String, AtomicContext> createWiredContexts(String 
sourceName, Class<?> sourceClass, Class<?> sourceReferenceClass,
+                                                                 
ScopeContext<AtomicContext> sourceScope,
+                                                                 Interceptor 
sourceHeadInterceptor,
+                                                                 
MessageHandler sourceHeadRequestHandler,
+                                                                 
MessageHandler sourceHeadResponseHandler,
+                                                                 Map<String, 
Member> members,
                                                                  String 
targetName, Class<?> targetService, Class<?> targetClass,
-                                                                 Map<String, 
Member> members, ScopeContext<AtomicContext> targetScope) throws Exception {
+                                                                 
ScopeContext<AtomicContext> targetScope,
+                                                                 Interceptor 
targetHeadInterceptor,
+                                                                 
MessageHandler targetRequestHeadHandler,
+                                                                 
MessageHandler targetResponseHeadHandler) throws Exception {
         JavaAtomicContext targetContext = createJavaAtomicContext(targetName, 
targetClass);
-        TargetWire targetWire = 
createTargetWireFactory(targetService.getName().substring(
-                targetService.getName().lastIndexOf('.') + 1), targetService);
+        TargetWire targetWire = 
createTargetWire(targetService.getName().substring(
+                targetService.getName().lastIndexOf('.') + 1), 
targetService,targetHeadInterceptor,targetRequestHeadHandler,targetResponseHeadHandler);
         targetContext.addTargetWire(targetWire);
 
         JavaAtomicContext sourceContext = createJavaAtomicContext(sourceName, 
sourceClass, false, null, null, null, members);
-        SourceWire sourceWire = createSourceWireFactory(targetName, 
sourceReferenceClass);
+        SourceWire sourceWire = createSourceWire(targetName, 
sourceReferenceClass,sourceHeadInterceptor,
+                sourceHeadRequestHandler,sourceHeadResponseHandler);
         sourceContext.addSourceWire(sourceWire);
         targetScope.register(targetContext);
         sourceContext.setScopeContext(sourceScope);
@@ -138,7 +127,6 @@
         return contexts;
     }
 
-
     /**
      * Wires two contexts using a multiplicity reference
      *
@@ -159,12 +147,12 @@
                                                                      String 
targetName, Class<?> targetService, Class<?> targetClass,
                                                                      
Map<String, Member> members, ScopeContext<AtomicContext> targetScope) throws 
Exception {
         JavaAtomicContext targetContext = createJavaAtomicContext(targetName, 
targetClass);
-        TargetWire targetWire = 
createTargetWireFactory(targetService.getName().substring(
-                targetService.getName().lastIndexOf('.') + 1), targetService);
+        TargetWire targetWire = 
createTargetWire(targetService.getName().substring(
+                targetService.getName().lastIndexOf('.') + 1), 
targetService,null,null,null);
         targetContext.addTargetWire(targetWire);
 
         JavaAtomicContext sourceContext = createJavaAtomicContext(sourceName, 
sourceClass, false, null, null, null, members);
-        SourceWire sourceWire = createSourceWireFactory(targetName, 
sourceReferenceClass);
+        SourceWire sourceWire = createSourceWire(targetName, 
sourceReferenceClass,null,null,null);
         List<SourceWire> factories = new ArrayList<SourceWire>();
         factories.add(sourceWire);
         sourceContext.addSourceWires(sourceReferenceClass, factories);
@@ -179,6 +167,31 @@
         return contexts;
     }
 
+    public static <T> TargetWire<T> createTargetWire(String serviceName, 
Class<T> interfaze,
+                                                     Interceptor 
headInterceptor,
+                                                     MessageHandler 
headRequestHandler,
+                                                     MessageHandler 
headResponseHandler) throws WireFactoryInitException {
+        TargetWire<T> wire = new JDKTargetWire<T>();
+        wire.setBusinessInterface(interfaze);
+        wire.setServiceName(serviceName);
+        
wire.setInvocationChains(createTargetInvocationChains(interfaze,headInterceptor,headRequestHandler,headResponseHandler));
+        wire.initialize();
+        return wire;
+    }
+
+    public static <T> SourceWire<T> createSourceWire(String refName, Class<T> 
interfaze,
+                                                     Interceptor 
headInterceptor,
+                                                     MessageHandler 
headRequestHandler,
+                                                     MessageHandler 
headResponseHandler) throws WireFactoryInitException {
+
+        SourceWire<T> wire = new JDKSourceWire<T>();
+        wire.setReferenceName(refName);
+        
wire.setInvocationChains(createSourceInvocationChains(interfaze,headInterceptor,headRequestHandler,headResponseHandler));
+        wire.setBusinessInterface(interfaze);
+        wire.initialize();
+        return wire;
+    }
+
 
     /**
      * TODO refactor
@@ -193,8 +206,7 @@
         if (targetWire != null) {
             // if null, the target side has no interceptors or handlers
             Map<Method, TargetInvocationChain> targetInvocationConfigs = 
targetWire.getInvocationChains();
-            for (SourceInvocationChain sourceInvocationConfig : 
sourceWire.getInvocationChains().values())
-            {
+            for (SourceInvocationChain sourceInvocationConfig : 
sourceWire.getInvocationChains().values()) {
                 // match wire chains
                 TargetInvocationChain targetInvocationConfig = 
targetInvocationConfigs.get(sourceInvocationConfig.getMethod());
                 if (targetInvocationConfig == null) {
@@ -233,20 +245,65 @@
         }
     }
 
+    public static Map<Method, SourceInvocationChain> 
createSourceInvocationChains(Class<?> interfaze) {
+        return createSourceInvocationChains(interfaze, null, null, null);
+    }
 
-    public static Map<Method, SourceInvocationChain> 
createSourceInvocationConfigurations(Class<?> interfaze) {
+    public static Map<Method, SourceInvocationChain> 
createSourceInvocationChains(Class<?> interfaze,
+                                                                               
   Interceptor headInterceptor, MessageHandler headRequestHandler,
+                                                                               
   MessageHandler headResponseHandler) {
         Map<Method, SourceInvocationChain> invocations = new HashMap<Method, 
SourceInvocationChain>();
         Method[] methods = interfaze.getMethods();
         for (Method method : methods) {
-            invocations.put(method, new SourceInvocationChainImpl(method));
+            SourceInvocationChain chain = new 
SourceInvocationChainImpl(method);
+            if (headInterceptor != null) {
+                chain.addInterceptor(headInterceptor);
+            }
+            if (headRequestHandler != null) {
+                chain.addRequestHandler(headRequestHandler);
+            }
+            if (headResponseHandler != null) {
+                chain.addRequestHandler(headResponseHandler);
+            }
+            invocations.put(method, chain);
         }
         return invocations;
     }
 
-    private static <T> ObjectFactory<T> createObjectFactory(Class<T> clazz, 
List<Injector> injectors) throws NoSuchMethodException {
-        Constructor<T> ctr = clazz.getConstructor((Class<T>[]) null);
-        return new PojoObjectFactory<T>(ctr, null, injectors);
+    public static Map<Method, TargetInvocationChain> 
createTargetInvocationChains(Class<?> interfaze) {
+        return createTargetInvocationChains(interfaze, null, null, null);
     }
 
+    public static Map<Method, TargetInvocationChain> 
createTargetInvocationChains(Class<?> interfaze,
+                                                                               
   Interceptor headInterceptor, MessageHandler headRequestHandler,
+                                                                               
   MessageHandler headResponseHandler) {
+            Map<Method, TargetInvocationChain> invocations = new 
MethodHashMap<TargetInvocationChain>();
+            Method[] methods = interfaze.getMethods();
+            for (Method method : methods) {
+                TargetInvocationChain chain = new 
TargetInvocationChainImpl(method);
+                if (headInterceptor != null) {
+                    chain.addInterceptor(headInterceptor);
+                }
+                if (headRequestHandler != null) {
+                    chain.addRequestHandler(headRequestHandler);
+                }
+                if (headResponseHandler != null) {
+                    chain.addRequestHandler(headResponseHandler);
+                }
+                // add tail interceptor
+                chain.addInterceptor(new InvokerInterceptor());
+                invocations.put(method, chain);
+            }
+            return invocations;
+        }
+
 
-}
+        private static <T> ObjectFactory<T> createObjectFactory
+        (Class < T > clazz, List < Injector > injectors) throws 
NoSuchMethodException
+        {
+            Constructor<T> ctr = clazz.getConstructor((Class<T>[]) null);
+            return new PojoObjectFactory<T>(ctr, null, injectors);
+        }
+
+
+    }

Modified: 
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/DynamicTargetResolver.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/DynamicTargetResolver.java?rev=406136&r1=406135&r2=406136&view=diff
==============================================================================
--- 
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/DynamicTargetResolver.java
 (original)
+++ 
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/DynamicTargetResolver.java
 Sat May 13 11:35:40 2006
@@ -37,7 +37,7 @@
         if (ctx instanceof AtomicContext) {
             return (T) ((AtomicContext) 
ctx).getService(targetName.getPortName());
         } else if (ctx instanceof ServiceContext) {
-            return (T) ((ServiceContext) ctx).getService();
+            return (T) ctx.getService();
         } else if (ctx instanceof CompositeContext) {
             ServiceContext serviceContext = ((CompositeContext) 
ctx).getServiceContext(targetName.getPortName());
             if (serviceContext == null) {
@@ -48,7 +48,7 @@
             }
             return (T) serviceContext.getService();
         } else if (ctx instanceof ReferenceContext){
-            return (T) ((ReferenceContext) ctx).getService();
+            return (T) ctx.getService();
         }else {
             UnknownTargetTypeException e = new 
UnknownTargetTypeException(ctx.getClass().getName());
             e.setIdentifier(targetName.getQualifiedName());


Reply via email to