Package org.apache.tuscany.core.context

Examples of org.apache.tuscany.core.context.CompositeContext


    public void testSessionToSession() 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.SESSION,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");
        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 source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source2);
        GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);

        Assert.assertNotNull(target2);
        Assert.assertEquals(null,target2.getString());
        Assert.assertEquals(null,source2.getGenericComponent().getString());
        source2.getGenericComponent().setString("baz");
        Assert.assertEquals("baz",source2.getGenericComponent().getString());
        Assert.assertEquals("baz",target2.getString());
       
        testCtx.publish(new RequestEnd(this,id2));

    }
View Full Code Here


        Object o = resolver.getCurrentContext().getInstance(targetQualifiedName);
        if (o != null) {
            return (T) o;
        } else {
            // walk up the hierarchy of composite contexts
            CompositeContext ctx = resolver.getCurrentContext();
            do {
                if (ctx == null) {
                    break; // reached top of context hierarchy
                }
                Context compContext = ctx.getContext(targetQualifiedName.getPartName());
                if (compContext != null) {
                    o = compContext.getInstance(targetQualifiedName);
                    if (o != null) {
                        return (T) o;
                    }
                }
                ctx = ctx.getParent();
            } while (ctx != null);
            TargetException e = new TargetException("Target reference not found");
            e.setIdentifier(targetQualifiedName.getQualifiedName());
            throw e;
        }
View Full Code Here

    public void testSessionToModule() 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.SESSION,Scope.MODULE));
        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");
        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 source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source2);
        GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);

        Assert.assertNotNull(target2);
        Assert.assertEquals("foo",target2.getString());
        Assert.assertEquals("foo",source2.getGenericComponent().getString());
        source2.getGenericComponent().setString("baz");
        Assert.assertEquals("baz",source2.getGenericComponent().getString());
        Assert.assertEquals("baz",target2.getString());
        Assert.assertEquals("baz",target.getString());
       
        testCtx.publish(new RequestEnd(this,session2));
      
    }
View Full Code Here

    public void testSessionToRequest() 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.SESSION,Scope.REQUEST));
        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,session));

        //second session
        Object session2 = new Object();
        Object id2 = new Object();
        testCtx.publish(new RequestStart(this,id2));
        testCtx.publish(new HttpSessionBound(this,session2));
        GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source);
        GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);

        Assert.assertNotNull(target2);
        Assert.assertEquals(null,target2.getString());
        source2.getGenericComponent().setString("baz");
        Assert.assertEquals("baz",target2.getString());
        Assert.assertEquals("baz",source2.getGenericComponent().getString());
       
        Assert.assertEquals("foo",target.getString());
        testCtx.publish(new RequestEnd(this,session));

    }
View Full Code Here

                .getContext(MockFactory.POLICY_BUILDER_REGISTRY).getInstance(null);
        MockSyncInterceptor mockInterceptor = new MockSyncInterceptor();
        MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false);
        registry.registerTargetBuilder(interceptorBuilder);
        runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test.module"));
        CompositeContext child = (CompositeContext) runtime.getRootContext().getContext("test.module");
        child.registerModelObject(MockFactory.createModuleWithEntryPointToExternalService());
        child.publish(new ModuleStart(this));
        Object id = new Object();
        child.publish(new RequestStart(this, id));
        HelloWorldService service1 = (HelloWorldService) child.getContext("target").getInstance(null);
        Assert.assertEquals("foo", service1.hello("foo"));

        child.publish(new RequestEnd(this, id));
        child.publish(new ModuleStop(this));
        runtime.stop();

    }
View Full Code Here

    public void testSessionToStateless() 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.SESSION,Scope.INSTANCE));
        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(null,target.getString());
        testCtx.publish(new RequestEnd(this,session));

        //second session
        Object session2 = new Object();
        Object id2 = new Object();
        testCtx.publish(new RequestStart(this,id2));
        testCtx.publish(new HttpSessionBound(this,session2));
        GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source);
        GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);

        Assert.assertNotNull(target2);
        Assert.assertEquals(null,target2.getString());
        source2.getGenericComponent().setString("baz");
        Assert.assertEquals(null,target2.getString()); //Note assumes no pooling
        Assert.assertEquals(null,source2.getGenericComponent().getString());
       
        Assert.assertEquals(null,target.getString()); //Note assumes no pooling
        testCtx.publish(new RequestEnd(this,session));

    }
View Full Code Here

    public Context createContext() throws ContextCreationException {
        if (isComposite) {
            try {
                // composite context types are themselves an instance context
                PojoObjectFactory<CompositeContext> objectFactory = new PojoObjectFactory<CompositeContext>(ctr, null, setters);
                CompositeContext ctx = objectFactory.getInstance();
                ctx.setName(name);
                // the composite has been created, now register its children
                if (module != null) {
                    try {
                        ctx.registerModelObject(module);
                    } catch (ConfigurationException e) {
                        ContextCreationException cce = new ContextCreationException("Error creating context", e);
                        cce.setIdentifier(getName());
                        throw cce;
                    }
View Full Code Here

    public void testRequestToRequest() 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.REQUEST,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,new Object()));

        //second request
        Object id2 = new Object();
        testCtx.publish(new RequestStart(this,id2));
        GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source2);
        GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);

        Assert.assertNotNull(target2);
        Assert.assertEquals(null,target2.getString());
        Assert.assertEquals(null,source2.getGenericComponent().getString());
        source2.getGenericComponent().setString("baz");
        Assert.assertEquals("baz",source2.getGenericComponent().getString());
        Assert.assertEquals("baz",target2.getString());
       
        testCtx.publish(new RequestEnd(this,new Object()));
    }
View Full Code Here

                MockFactory.SYSTEM_CHILD)).getContext(MockFactory.POLICY_BUILDER_REGISTRY).getInstance(null);
        MockSyncInterceptor mockInterceptor = new MockSyncInterceptor();
        MockInterceptorBuilder interceptorBuilder = new MockInterceptorBuilder(mockInterceptor, false);
        registry.registerTargetBuilder(interceptorBuilder);
        runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test.module"));
        CompositeContext child = (CompositeContext) runtime.getRootContext().getContext("test.module");
        child.registerModelObject(MockFactory.createModuleWithExternalService());
        child.publish(new ModuleStart(this));
        HelloWorldService source = (HelloWorldService) child.getContext("source").getInstance(null);
        Assert.assertNotNull(source);
        Assert.assertEquals(0, mockInterceptor.getCount());
        Assert.assertEquals("foo", source.hello("foo"));
        Assert.assertEquals(1, mockInterceptor.getCount());
        child.publish(new ModuleStop(this));
        runtime.stop();
    }
View Full Code Here

    public void testRequestToModule() 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.REQUEST,Scope.MODULE));
        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,new Object()));

        //second request
        Object id2 = new Object();
        testCtx.publish(new RequestStart(this,id2));
        GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
        Assert.assertNotNull(source2);
        GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);

        Assert.assertNotNull(target2);
        Assert.assertEquals("foo",target2.getString());
        Assert.assertEquals("foo",source2.getGenericComponent().getString());
        source2.getGenericComponent().setString("baz");
        Assert.assertEquals("baz",source2.getGenericComponent().getString());
        Assert.assertEquals("baz",target2.getString());
        Assert.assertEquals("baz",target.getString());
       
        testCtx.publish(new RequestEnd(this,new Object()));
    }
View Full Code Here

TOP

Related Classes of org.apache.tuscany.core.context.CompositeContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.