Package org.apache.tuscany.core.context.scope

Examples of org.apache.tuscany.core.context.scope.ModuleScopeContext


    public SystemScopeStrategy() {
    }

    public Map<Scope, ScopeContext> getScopeContexts(EventContext eventContext) {
        ScopeContext aggregrateScope = new CompositeScopeContext(eventContext);
        ScopeContext moduleScoper = new ModuleScopeContext(eventContext);
        ScopeContext statelessScope = new StatelessScopeContext(eventContext);
        Map<Scope, ScopeContext> scopes = new HashMap<Scope, ScopeContext>();
        scopes.put(Scope.AGGREGATE, aggregrateScope);
        scopes.put(Scope.MODULE, moduleScoper);
        scopes.put(Scope.INSTANCE, statelessScope);
View Full Code Here


    JavaContextFactoryBuilder builder;

    public void testInitDestroy() throws Exception {
        EventContext ctx = new EventContextImpl();
        ModuleScopeContext scope = new ModuleScopeContext(ctx);
        scope.registerFactories(createComponents());
        scope.start();
        scope.onEvent(new ModuleStart(this));
        ModuleScopeInitDestroyComponent initDestroy = (ModuleScopeInitDestroyComponent) scope.getContext(
                "TestServiceInitDestroy").getInstance(null);
        Assert.assertNotNull(initDestroy);
        ModuleScopeInitOnlyComponent initOnly = (ModuleScopeInitOnlyComponent) scope.getContext("TestServiceInitOnly")
                .getInstance(null);
        Assert.assertNotNull(initOnly);
        ModuleScopeDestroyOnlyComponent destroyOnly = (ModuleScopeDestroyOnlyComponent) scope.getContext(
                "TestServiceDestroyOnly").getInstance(null);
        Assert.assertNotNull(destroyOnly);

        Assert.assertTrue(initDestroy.isInitialized());
        Assert.assertTrue(initOnly.isInitialized());
        Assert.assertFalse(initDestroy.isDestroyed());
        Assert.assertFalse(destroyOnly.isDestroyed());

        // expire module
        scope.onEvent(new ModuleStop(this));

        Assert.assertTrue(initDestroy.isDestroyed());
        Assert.assertTrue(destroyOnly.isDestroyed());

        scope.stop();
    }
View Full Code Here

        scope.stop();
    }

    public void testEagerInit() throws Exception {
        EventContext ctx = new EventContextImpl();
        ModuleScopeContext scope = new ModuleScopeContext(ctx);
        scope.registerFactories(createEagerInitComponents());
        scope.start();
        scope.onEvent(new ModuleStart(this));
        ModuleScopeEagerInitDestroyComponent initDestroy = (ModuleScopeEagerInitDestroyComponent) scope.getContext(
                "TestServiceEagerInitDestroy").getInstance(null);
        Assert.assertNotNull(initDestroy);
        ModuleScopeEagerInitComponent initOnly = (ModuleScopeEagerInitComponent) scope
                .getContext("TestServiceEagerInit").getInstance(null);
        Assert.assertNotNull(initOnly);

        Assert.assertTrue(initDestroy.isInitialized());
        Assert.assertTrue(initOnly.isInitialized());
        Assert.assertFalse(initDestroy.isDestroyed());

        // expire module
        scope.onEvent(new ModuleStop(this));

        Assert.assertTrue(initDestroy.isDestroyed());

        scope.stop();

    }
View Full Code Here

    }

    public void testDestroyOrder() throws Exception {
        EventContext ctx = new EventContextImpl();
        ModuleScopeContext scope = new ModuleScopeContext(ctx);
        scope.registerFactories(createOrderedInitComponents());
        scope.start();
        scope.onEvent(new ModuleStart(this));
        OrderedInitPojo one = (OrderedInitPojo) scope.getContext("one").getInstance(null);
        Assert.assertNotNull(one);
        Assert.assertEquals(1, one.getNumberInstantiated());
        Assert.assertEquals(1, one.getInitOrder());

        OrderedInitPojo two = (OrderedInitPojo) scope.getContext("two").getInstance(null);
        Assert.assertNotNull(two);
        Assert.assertEquals(2, two.getNumberInstantiated());
        Assert.assertEquals(2, two.getInitOrder());

        OrderedInitPojo three = (OrderedInitPojo) scope.getContext("three").getInstance(null);
        Assert.assertNotNull(three);
        Assert.assertEquals(3, three.getNumberInstantiated());
        Assert.assertEquals(3, three.getInitOrder());

        // expire module
        scope.onEvent(new ModuleStop(this));
        Assert.assertEquals(0, one.getNumberInstantiated());
        scope.stop();
    }
View Full Code Here

        ScopeStrategy strategy = new DefaultScopeStrategy();
        DefaultWireBuilder wireBuilder = new DefaultWireBuilder();
        wireBuilder.addWireBuilder(javaWireBuilder);
        Module module = MockFactory.createModule();
        EventContext eCtx = new EventContextImpl();
        ScopeContext scopeContext = new ModuleScopeContext(eCtx);
        scopeContext.start();
        scopeContext.onEvent(new ModuleStart(this));
        List<Component> components = module.getComponents();
        Map<String, Component> compMap = new HashMap<String, Component>(components.size());

        for (Component component : components) {
            compMap.put(component.getName(), component);
            builder.build(component);
            ContextFactory contextFactory = (ContextFactory) component.getContextFactory();
            Assert.assertNotNull(contextFactory);
        }
        for (Component component : components) {
            ContextFactory<Context> source = (ContextFactory<Context>) component.getContextFactory();
            Assert.assertNotNull(source);
            for (SourceWireFactory pFactory : source.getSourceWireFactories()) {
                WireConfiguration pConfig = pFactory.getConfiguration();
                Component target = compMap.get(pConfig.getTargetName().getPartName());

                if (target != null) {
                    ContextFactory targetConfig = (ContextFactory) target.getContextFactory();
                    boolean downScope = strategy.downScopeReference(source.getScope(), targetConfig.getScope());
                    wireBuilder.connect(pFactory, targetConfig.getTargetWireFactory(pFactory.getConfiguration().getTargetName()
                            .getPortName()), targetConfig.getClass(), downScope, scopeContext);
                }
                pFactory.initialize();
            }
            scopeContext.registerFactory(source);
        }
        for (Component component : components) {
            ContextFactory config = (ContextFactory) component.getContextFactory();
            Context context = config.createContext();
            if ("source".equals(component.getName())) {
View Full Code Here

        scope.stop();
    }

    public void testEagerInitDestroyOrder() throws Exception {
        EventContext ctx = new EventContextImpl();
        ModuleScopeContext scope = new ModuleScopeContext(ctx);
        scope.registerFactories(createOrderedEagerInitComponents());
        scope.start();
        scope.onEvent(new ModuleStart(this));
        OrderedEagerInitPojo one = (OrderedEagerInitPojo) scope.getContext("one").getInstance(null);
        Assert.assertNotNull(one);

        OrderedEagerInitPojo two = (OrderedEagerInitPojo) scope.getContext("two").getInstance(null);
        Assert.assertNotNull(two);

        OrderedEagerInitPojo three = (OrderedEagerInitPojo) scope.getContext("three").getInstance(null);
        Assert.assertNotNull(three);

        // expire module
        scope.onEvent(new ModuleStop(this));
        Assert.assertEquals(0, one.getNumberInstantiated());
        scope.stop();
    }
View Full Code Here

    /**
     * Tests instance identity is properly maintained
     */
    public void testInstanceManagement() throws Exception {
        EventContext ctx = new EventContextImpl();
        ModuleScopeContext scope = new ModuleScopeContext(ctx);
        scope.registerFactories(createConfigurations());
        scope.start();
        // first request
        scope.onEvent(new ModuleStart(this));
        ModuleScopeComponentImpl comp1 = (ModuleScopeComponentImpl) scope.getContext("TestService1").getInstance(null);
        Assert.assertNotNull(comp1);
        // second request
        ModuleScopeComponentImpl comp2 = (ModuleScopeComponentImpl) scope.getContext("TestService1").getInstance(null);
        Assert.assertNotNull(comp2);
        Assert.assertSame(comp1, comp2);
        scope.onEvent(new ModuleStop(this));
        scope.stop();
    }
View Full Code Here

        scope.stop();
    }

    public void testSetNullComponents() throws Exception {
        EventContext ctx = new EventContextImpl();
        ModuleScopeContext scope = new ModuleScopeContext(ctx);
        scope.registerFactories(createConfigurations());
        scope.start();
        scope.onEvent(new ModuleStart(this));
        scope.onEvent(new ModuleStop(this));
        scope.stop();
    }
View Full Code Here

        scope.stop();
    }

    public void testRegisterContextBeforeStart() throws Exception {
        EventContext ctx = new EventContextImpl();
        ModuleScopeContext scope = new ModuleScopeContext(ctx);
        scope.registerFactories(createConfigurations());
        scope.start();
        scope.registerFactory(createConfiguration("NewTestService"));
        scope.onEvent(new ModuleStart(this));
        ModuleScopeInitDestroyComponent comp2 = (ModuleScopeInitDestroyComponent) scope.getContext("NewTestService").getInstance(null);
        Assert.assertNotNull(comp2);
        Assert.assertTrue(comp2.isInitialized());
        scope.onEvent(new ModuleStop(this));
        Assert.assertTrue(comp2.isDestroyed());
        scope.stop();
    }
View Full Code Here

        scope.stop();
    }

    public void testRegisterContextAfterStart() throws Exception {
        EventContext ctx = new EventContextImpl();
        ModuleScopeContext scope = new ModuleScopeContext(ctx);
        scope.start();
        scope.registerFactory(createConfiguration("NewTestService"));
        scope.onEvent(new ModuleStart(this));
        scope.registerFactories(createConfigurations());
        ModuleScopeInitDestroyComponent comp2 = (ModuleScopeInitDestroyComponent) scope.getContext("NewTestService").getInstance(null);
        Assert.assertNotNull(comp2);
        Assert.assertTrue(comp2.isInitialized());
        scope.onEvent(new ModuleStop(this));
        Assert.assertTrue(comp2.isDestroyed());
        scope.stop();
    }
View Full Code Here

TOP

Related Classes of org.apache.tuscany.core.context.scope.ModuleScopeContext

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.