Added: incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/runtime/SystemBootstrapTestCase.java URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/runtime/SystemBootstrapTestCase.java?rev=385966&view=auto ============================================================================== --- incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/runtime/SystemBootstrapTestCase.java (added) +++ incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/runtime/SystemBootstrapTestCase.java Tue Mar 14 19:47:01 2006 @@ -0,0 +1,130 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" + * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.apache.tuscany.core.runtime; + +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.common.monitor.impl.NullMonitorFactory; +import org.apache.tuscany.core.builder.ContextFactoryBuilder; +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.AutowireContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.context.impl.AggregateContextImpl; +import org.apache.tuscany.core.mock.MockFactory; +import org.apache.tuscany.core.mock.component.GenericSystemComponent; +import org.apache.tuscany.core.mock.component.ModuleScopeSystemComponent; +import org.apache.tuscany.core.mock.component.ModuleScopeSystemComponentImpl; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.core.runtime.RuntimeContextImpl; +import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory; +import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl; +import org.apache.tuscany.model.assembly.Component; +import org.apache.tuscany.model.assembly.ConfiguredService; +import org.apache.tuscany.model.assembly.EntryPoint; +import org.apache.tuscany.model.assembly.ExternalService; +import org.apache.tuscany.model.assembly.Module; +import org.apache.tuscany.model.assembly.Service; +import org.apache.tuscany.model.types.java.JavaServiceContract; + +/** + * Tests bootstrapping a system module + * + * @version $Rev: 385834 $ $Date: 2006-03-14 08:57:08 -0800 (Tue, 14 Mar 2006) $ + */ +public class SystemBootstrapTestCase extends TestCase { + private List<ContextFactoryBuilder> builders; + + private SystemAssemblyFactory factory = new SystemAssemblyFactoryImpl(); + + /** + * Simulates booting a runtime process + */ + public void testBoot() throws Exception { + RuntimeContext runtimeContext = new RuntimeContextImpl(new NullMonitorFactory(), null, builders,null); + runtimeContext.start(); + + AggregateContext systemContext = runtimeContext.getSystemContext(); + Assert.assertNotNull(systemContext); + Module systemModule = MockFactory.createSystemModule(); + // MockSystemAssemblyFactory.buildModule(systemModule, systemContext); + systemContext.registerModelObject(systemModule); + + // create a test module + Component moduleComponent = MockFactory.createAggregateComponent("module"); + runtimeContext.registerModelObject(moduleComponent); + AggregateContextImpl moduleContext = (AggregateContextImpl) runtimeContext.getContext("module"); + Assert.assertNotNull(moduleContext); + ExternalService es = MockFactory.createESSystemBinding("TestServiceES", "tuscany.system/TestService1EP"); + moduleContext.registerModelObject(es); + + // start the modules and test inter-module system wires + systemContext.fireEvent(EventContext.MODULE_START, null); + moduleContext.fireEvent(EventContext.MODULE_START, null); + + Assert.assertNotNull(systemContext.locateInstance("TestService1EP")); + GenericSystemComponent testService = (GenericSystemComponent) systemContext.locateInstance("TestService1"); + Assert.assertNotNull(testService); + GenericSystemComponent testES = (GenericSystemComponent) moduleContext.locateInstance("TestServiceES"); + Assert.assertNotNull(testES); + Assert.assertSame(testService, testES); + } + + public void testRuntimeBoot() throws Exception { + RuntimeContext runtime = new RuntimeContextImpl(new NullMonitorFactory(), null, builders,null); + runtime.start(); + runtime.getRootContext(); + + AggregateContext system = runtime.getSystemContext(); + system.registerModelObject(MockFactory.createSystemModule()); + system.registerModelObject(MockFactory.createSystemAggregateComponent("module2")); + AggregateContext systemModule2 = (AggregateContext) system.getContext("module2"); + systemModule2.registerModelObject(MockFactory.createSystemChildModule()); + + EntryPoint ep = MockFactory.createEPSystemBinding("TestService2EP", ModuleScopeSystemComponent.class, "ref"); + ep.getBindings().add(factory.createSystemBinding()); + Service service = factory.createService(); + service.setName("module2/TestService2EP"); + JavaServiceContract inter = factory.createJavaServiceContract(); + inter.setInterface(ModuleScopeSystemComponentImpl.class); + service.setServiceContract(inter); + ((ConfiguredService) ep.getConfiguredReference().getTargetConfiguredServices().get(0)).setService(service); + system.registerModelObject(ep); + system.fireEvent(EventContext.MODULE_START, null); + Assert.assertNotNull(system.locateInstance("TestService1")); + Assert.assertNotNull(system.locateInstance("TestService2EP")); + + Assert.assertNotNull(((AutowireContext) system).resolveInstance(ModuleScopeSystemComponent.class)); + // create a test module + Component moduleComponent = MockFactory.createAggregateComponent("test.module"); + runtime.registerModelObject(moduleComponent); + AggregateContextImpl moduleContext = (AggregateContextImpl) runtime.getContext("test.module"); + Assert.assertNotNull(moduleContext); + ExternalService es = MockFactory.createESSystemBinding("TestService2ES", "tuscany.system/TestService2EP"); + moduleContext.registerModelObject(es); + moduleContext.fireEvent(EventContext.MODULE_START, null); + Assert.assertNotNull(moduleContext.locateInstance("TestService2ES")); + + moduleContext.fireEvent(EventContext.MODULE_STOP, null); + system.fireEvent(EventContext.MODULE_STOP, null); + runtime.stop(); + } + + protected void setUp() throws Exception { + super.setUp(); + builders = MockFactory.createSystemBuilders(); + } +}
Added: incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/system/context/AggregateNestingTestCase.java URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/system/context/AggregateNestingTestCase.java?rev=385966&view=auto ============================================================================== --- incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/system/context/AggregateNestingTestCase.java (added) +++ incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/system/context/AggregateNestingTestCase.java Tue Mar 14 19:47:01 2006 @@ -0,0 +1,96 @@ +/** + * + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.tuscany.core.system.context; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.core.context.AggregateContext; +import org.apache.tuscany.core.context.EventContext; +import org.apache.tuscany.core.mock.MockFactory; +import org.apache.tuscany.core.mock.component.Source; +import org.apache.tuscany.core.mock.component.Target; +import org.apache.tuscany.core.runtime.RuntimeContext; +import org.apache.tuscany.model.assembly.ModuleComponent; +import org.apache.tuscany.model.assembly.Scope; + +/** + * Tests registering of arbirarily deep child aggregate contexts + * + * @version $Rev$ $Date$ + */ +public class AggregateNestingTestCase extends TestCase { + + /** + * Tests registration of a 3-level deep hierarchy under the top-level system aggregate context + */ + public void testSystemContext() throws Exception { + RuntimeContext runtime = MockFactory.createCoreRuntime(); + ModuleComponent child1 = createHierarchy(); + runtime.getSystemContext().registerModelObject(child1); + AggregateContext child1Ctx = (AggregateContext) runtime.getSystemContext().getContext("child1"); + Assert.assertNotNull(child1Ctx); + child1Ctx.fireEvent(EventContext.MODULE_START, null); + analyzeLeafComponents(child1Ctx); + AggregateContext child2Ctx = (AggregateContext) child1Ctx.getContext("child2"); + Assert.assertNotNull(child2Ctx); + child2Ctx.fireEvent(EventContext.MODULE_START, null); + analyzeLeafComponents(child2Ctx); + AggregateContext child3Ctx = (AggregateContext) child2Ctx.getContext("child3"); + Assert.assertNotNull(child3Ctx); + child3Ctx.fireEvent(EventContext.MODULE_START, null); + analyzeLeafComponents(child3Ctx); + + Assert.assertNull(child1Ctx.getContext("child3")); // sanity check + } + + /** + * Tests registration of a 3-level deep hierarchy under the root application aggregate context + */ + public void testRootContext() throws Exception { + RuntimeContext runtime = MockFactory.createCoreRuntime(); + ModuleComponent child1 = createHierarchy(); + runtime.getRootContext().registerModelObject(child1); + AggregateContext child1Ctx = (AggregateContext) runtime.getRootContext().getContext("child1"); + Assert.assertNotNull(child1Ctx); + child1Ctx.fireEvent(EventContext.MODULE_START, null); + analyzeLeafComponents(child1Ctx); + AggregateContext child2Ctx = (AggregateContext) child1Ctx.getContext("child2"); + Assert.assertNotNull(child2Ctx); + child2Ctx.fireEvent(EventContext.MODULE_START, null); + analyzeLeafComponents(child2Ctx); + AggregateContext child3Ctx = (AggregateContext) child2Ctx.getContext("child3"); + Assert.assertNotNull(child3Ctx); + child3Ctx.fireEvent(EventContext.MODULE_START, null); + analyzeLeafComponents(child3Ctx); + + Assert.assertNull(child1Ctx.getContext("child3")); // sanity check + } + + private ModuleComponent createHierarchy(){ + ModuleComponent child3 = MockFactory.createSystemModuleComponentWithWiredComponents("child3", Scope.MODULE, Scope.MODULE); + ModuleComponent child2 = MockFactory.createSystemModuleComponentWithWiredComponents("child2", Scope.MODULE, Scope.MODULE); + child2.getModuleImplementation().getComponents().add(child3); + ModuleComponent child1 = MockFactory.createSystemModuleComponentWithWiredComponents("child1", Scope.MODULE, Scope.MODULE); + child1.getModuleImplementation().getComponents().add(child2); + return child1; + } + + private void analyzeLeafComponents(AggregateContext ctx) throws Exception { + Source source = (Source) ctx.locateInstance("source"); + Assert.assertNotNull(source); + Target target = source.getTarget(); + Assert.assertNotNull(target); + } +} Modified: incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/system/context/IntraAggregateWireTestCase.java URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/system/context/IntraAggregateWireTestCase.java?rev=385966&r1=385965&r2=385966&view=diff ============================================================================== --- incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/system/context/IntraAggregateWireTestCase.java (original) +++ incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/system/context/IntraAggregateWireTestCase.java Tue Mar 14 19:47:01 2006 @@ -36,7 +36,7 @@ public void testModuleToModuleScope() throws Exception { SystemAggregateContext context = createContext(); context.start(); - context.registerModelObject(MockFactory.createSystemModuleWithWiredComponents(Scope.MODULE, Scope.MODULE)); + context.registerModelObject(MockFactory.createSystemModuleWithWiredComponents("system.module",Scope.MODULE, Scope.MODULE)); context.fireEvent(EventContext.MODULE_START, null); Source source = (Source) context.getContext("source").getImplementationInstance(); Assert.assertNotNull(source); @@ -53,7 +53,7 @@ public void testStatelessToModuleScope() throws Exception { SystemAggregateContext context = createContext(); context.start(); - context.registerModelObject(MockFactory.createSystemModuleWithWiredComponents(Scope.INSTANCE, Scope.MODULE)); + context.registerModelObject(MockFactory.createSystemModuleWithWiredComponents("system.module",Scope.INSTANCE, Scope.MODULE)); context.fireEvent(EventContext.MODULE_START, null); Source source = (Source) context.getContext("source").getImplementationInstance(); Assert.assertNotNull(source); @@ -70,7 +70,7 @@ public void testModuleToStatelessScope() throws Exception { SystemAggregateContext context = createContext(); context.start(); - context.registerModelObject(MockFactory.createSystemModuleWithWiredComponents(Scope.MODULE, Scope.INSTANCE)); + context.registerModelObject(MockFactory.createSystemModuleWithWiredComponents("system.module",Scope.MODULE, Scope.INSTANCE)); context.fireEvent(EventContext.MODULE_START, null); Source source = (Source) context.getContext("source").getImplementationInstance(); Assert.assertNotNull(source); @@ -89,7 +89,7 @@ public void testMultiplicity() throws Exception { SystemAggregateContext context = createContext(); context.start(); - context.registerModelObject(MockFactory.createSystemModuleWithWiredComponents(Scope.MODULE, Scope.MODULE)); + context.registerModelObject(MockFactory.createSystemModuleWithWiredComponents("system.module",Scope.MODULE, Scope.MODULE)); context.fireEvent(EventContext.MODULE_START, null); Source source = (Source) context.getContext("source").getImplementationInstance(); Assert.assertNotNull(source); Modified: incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/AggregateImpl.java URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/AggregateImpl.java?rev=385966&r1=385965&r2=385966&view=diff ============================================================================== --- incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/AggregateImpl.java (original) +++ incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/AggregateImpl.java Tue Mar 14 19:47:01 2006 @@ -46,18 +46,29 @@ * An implementation of Aggregate. */ public abstract class AggregateImpl extends ExtensibleImpl implements Aggregate { - + private String name; - private List<Component> components=new ArrayList<Component>(); + + private List<Component> components = new ArrayList<Component>(); + private Map<String, Component> componentsMap; - private List<EntryPoint> entryPoints=new ArrayList<EntryPoint>(); + + private List<EntryPoint> entryPoints = new ArrayList<EntryPoint>(); + private Map<String, EntryPoint> entryPointsMap; - private List<ExternalService> externalServices=new ArrayList<ExternalService>(); + + private List<ExternalService> externalServices = new ArrayList<ExternalService>(); + private Map<String, ExternalService> externalServicesMap; + private List<AggregatePart> aggregateParts; - private List<Wire> wires=new ArrayList<Wire>(); - private List<Import> wsdlImports=new ArrayList<Import>(); + + private List<Wire> wires = new ArrayList<Wire>(); + + private List<Import> wsdlImports = new ArrayList<Import>(); + private Map<String, List<Import>> wsdlImportsMap; + private AssemblyModelContext modelContext; /** @@ -78,7 +89,7 @@ */ public void setName(String newName) { checkNotFrozen(); - name=newName; + name = newName; } /** @@ -140,14 +151,14 @@ public List<Wire> getWires() { return wires; } - + /** * @see org.apache.tuscany.model.assembly.Aggregate#getWSDLImports() */ public List<Import> getWSDLImports() { return wsdlImports; } - + /** * @see org.apache.tuscany.model.assembly.Aggregate#getWSDLImports(java.lang.String) */ @@ -155,7 +166,7 @@ checkInitialized(); return wsdlImportsMap.get(namespace); } - + /** * @see org.apache.tuscany.model.assembly.Aggregate#getAssemblyModelContext() */ @@ -163,7 +174,7 @@ checkInitialized(); return modelContext; } - + /** * @see org.apache.tuscany.model.assembly.Aggregate#getConfiguredService(org.apache.tuscany.model.assembly.ServiceURI) */ @@ -199,35 +210,35 @@ if (isInitialized()) return; super.initialize(modelContext); - + // Save the model context - this.modelContext=modelContext; - + this.modelContext = modelContext; + // Populate map of WSDL imports - ResourceLoader resourceLoader=modelContext.getApplicationResourceLoader(); + ResourceLoader resourceLoader = modelContext.getApplicationResourceLoader(); wsdlImportsMap = new HashMap<String, List<Import>>(); for (Import wsdlImport : wsdlImports) { - String namespace=wsdlImport.getNamespaceURI(); - List<Import> list=wsdlImportsMap.get(namespace); - if (list==null) { - list=new ArrayList<Import>(); + String namespace = wsdlImport.getNamespaceURI(); + List<Import> list = wsdlImportsMap.get(namespace); + if (list == null) { + list = new ArrayList<Import>(); wsdlImportsMap.put(namespace, list); } list.add(wsdlImport); - + // Load the WSDL definition if necessary - if (wsdlImport.getDefinition()==null) { - String location=wsdlImport.getLocationURI(); + if (wsdlImport.getDefinition() == null) { + String location = wsdlImport.getLocationURI(); Definition definition; - ClassLoader ccl=Thread.currentThread().getContextClassLoader(); + ClassLoader ccl = Thread.currentThread().getContextClassLoader(); try { - URL url=resourceLoader.getResource(location); - if (url==null) - throw new IllegalArgumentException("Cannot find "+location); + URL url = resourceLoader.getResource(location); + if (url == null) + throw new IllegalArgumentException("Cannot find " + location); definition = modelContext.getAssemblyLoader().loadDefinition(url.toString()); Thread.currentThread().setContextClassLoader(modelContext.getApplicationResourceLoader().getClassLoader()); - XSDHelper xsdHelper=SDOUtil.createXSDHelper(modelContext.getTypeHelper()); - xsdHelper.define (url.openStream(), null); + XSDHelper xsdHelper = SDOUtil.createXSDHelper(modelContext.getTypeHelper()); + xsdHelper.define(url.openStream(), null); } catch (IOException e) { throw new IllegalArgumentException(e); } finally { @@ -244,21 +255,21 @@ componentsMap.put(component.getName(), component); aggregateParts.add(component); component.initialize(modelContext); - ((AggregatePartImpl)component).setAggregate(this); + ((AggregatePartImpl) component).setAggregate(this); } entryPointsMap = new HashMap<String, EntryPoint>(); for (EntryPoint entryPoint : entryPoints) { entryPointsMap.put(entryPoint.getName(), entryPoint); aggregateParts.add(entryPoint); entryPoint.initialize(modelContext); - ((AggregatePartImpl)entryPoint).setAggregate(this); + ((AggregatePartImpl) entryPoint).setAggregate(this); } externalServicesMap = new HashMap<String, ExternalService>(); for (ExternalService externalService : externalServices) { externalServicesMap.put(externalService.getName(), externalService); aggregateParts.add(externalService); externalService.initialize(modelContext); - ((AggregatePartImpl)externalService).setAggregate(this); + ((AggregatePartImpl) externalService).setAggregate(this); } for (Wire wire : wires) { wire.initialize(modelContext); @@ -272,17 +283,17 @@ if (isFrozen()) return; super.freeze(); - + // Freeze lists - wsdlImports=Collections.unmodifiableList(wsdlImports); + wsdlImports = Collections.unmodifiableList(wsdlImports); freeze(wsdlImports); - components=Collections.unmodifiableList(components); + components = Collections.unmodifiableList(components); freeze(components); - entryPoints=Collections.unmodifiableList(entryPoints); + entryPoints = Collections.unmodifiableList(entryPoints); freeze(entryPoints); - externalServices=Collections.unmodifiableList(externalServices); + externalServices = Collections.unmodifiableList(externalServices); freeze(externalServices); - wires=Collections.unmodifiableList(wires); + wires = Collections.unmodifiableList(wires); freeze(wires); } @@ -292,14 +303,21 @@ public boolean accept(AssemblyModelVisitor visitor) { if (!super.accept(visitor)) return false; - - if (!accept(aggregateParts, visitor)) + + // if (!accept(aggregateParts, visitor)) + // return false; + if (!accept(components, visitor)) + return false; + if (!accept(entryPoints, visitor)) + return false; + + if (!accept(externalServices, visitor)) return false; - + if (!accept(wires, visitor)) return false; - + return true; } -} //ModuleImpl +} // ModuleImpl Modified: incubator/tuscany/java/sca/tomcat/src/main/java/org/apache/tuscany/tomcat/TuscanyContextListener.java URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/tomcat/src/main/java/org/apache/tuscany/tomcat/TuscanyContextListener.java?rev=385966&r1=385965&r2=385966&view=diff ============================================================================== --- incubator/tuscany/java/sca/tomcat/src/main/java/org/apache/tuscany/tomcat/TuscanyContextListener.java (original) +++ incubator/tuscany/java/sca/tomcat/src/main/java/org/apache/tuscany/tomcat/TuscanyContextListener.java Tue Mar 14 19:47:01 2006 @@ -121,7 +121,7 @@ AggregateContext rootContext = runtime.getRootContext(); rootContext.registerModelObject(moduleComponent); moduleContext = (AggregateContext)rootContext.getContext(moduleComponent.getName()); - moduleContext.registerModelObject(moduleComponent.getComponentImplementation()); + //moduleContext.registerModelObject(moduleComponent.getComponentImplementation()); } finally { Thread.currentThread().setContextClassLoader(oldCl); }
