diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2011-03-07 01:05:00 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2011-03-07 01:05:00 +0000 |
commit | d621604a7732497afcb0378c61ac5614a014a01d (patch) | |
tree | 17628cf68438c9338a92a9f7bb94800e7fa63836 /sandbox/sebastien/java | |
parent | 3e6e2864fabaf5fba77a0e51f0319d10e3debb35 (diff) |
Show how NodeFactory and Node can be replaced by finer grain deploy, build, activate and start methods. This would enable an SCA runtime virtual hosting environment to build a composite model, cache it, and only activate / start its components on demand when a request hits them.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1078641 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/sebastien/java')
2 files changed, 154 insertions, 29 deletions
diff --git a/sandbox/sebastien/java/vhost/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/EmbedUtil.java b/sandbox/sebastien/java/vhost/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/EmbedUtil.java index 0ccbcc9e36..7e968c0271 100644 --- a/sandbox/sebastien/java/vhost/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/EmbedUtil.java +++ b/sandbox/sebastien/java/vhost/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/EmbedUtil.java @@ -21,7 +21,15 @@ package sample.impl; import java.net.URI; import java.net.URL; -import java.util.Arrays; + +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.ServiceRuntimeException; +import org.oasisopen.sca.ServiceUnavailableException; + +import java.util.Collections; +import java.util.Properties; +import java.util.HashMap; +import java.util.List; import javax.wsdl.PortType; import javax.xml.namespace.QName; @@ -35,6 +43,8 @@ import org.apache.tuscany.sca.assembly.Contract; import org.apache.tuscany.sca.assembly.Implementation; import org.apache.tuscany.sca.assembly.Reference; import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.core.assembly.RuntimeAssemblyFactory; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; @@ -44,8 +54,14 @@ import org.apache.tuscany.sca.contribution.processor.ProcessorContext; import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver; import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint; +import org.apache.tuscany.sca.context.CompositeContext; import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.ModuleActivatorExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.deployment.Deployer; import org.apache.tuscany.sca.databinding.xml.DOMDataBinding; import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; @@ -53,9 +69,18 @@ import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLObject; -import org.apache.tuscany.sca.node.Node; -import org.apache.tuscany.sca.node.NodeFactory; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; +import org.apache.tuscany.sca.runtime.ActivationException; +import org.apache.tuscany.sca.runtime.CompositeActivator; +import org.apache.tuscany.sca.runtime.DomainRegistryFactory; +import org.apache.tuscany.sca.runtime.ExtensibleDomainRegistryFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeProperties; +import org.apache.tuscany.sca.runtime.EndpointRegistry; +import org.apache.tuscany.sca.work.WorkScheduler; /** * A few utility functions to help embed a Tuscany runtime, and a simple DSL @@ -64,39 +89,63 @@ import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; public class EmbedUtil { /** - * A runtime embedder context, which conveniently initializes a Node factory - * and gets the various registries, factories and extension points we need. + * A runtime embedder context, which conveniently gathers the various + * runtime registries, factories and extension points we need. */ static class Context { - final NodeFactory nf; final ExtensionPointRegistry epr; + final UtilityExtensionPoint uep; final FactoryExtensionPoint fep; final ContributionFactory cf; final AssemblyFactory af; final JavaInterfaceFactory jif; final WSDLFactory wif; + final MonitorFactory mf; final URLArtifactProcessorExtensionPoint apep; final ExtensibleURLArtifactProcessor aproc; final ModelResolverExtensionPoint mrep; final ProviderFactoryExtensionPoint pfep; + final Deployer depl; + final ModuleActivatorExtensionPoint maep; + final WorkScheduler ws; + final ProxyFactory pf; + final CompositeActivator ca; + final Monitor mon; + final ProcessorContext pc; + final DomainRegistryFactory drf; - Context(final NodeFactory nf) { - this.nf = nf; - epr = nf.getExtensionPointRegistry(); + Context() { + epr = new DefaultExtensionPointRegistry(); + uep = epr.getExtensionPoint(UtilityExtensionPoint.class); + mf = uep.getUtility(MonitorFactory.class); fep = epr.getExtensionPoint(FactoryExtensionPoint.class); cf = fep.getFactory(ContributionFactory.class); - af = fep.getFactory(AssemblyFactory.class); + af = new RuntimeAssemblyFactory(epr); + fep.addFactory(af); jif = fep.getFactory(JavaInterfaceFactory.class); wif = fep.getFactory(WSDLFactory.class); apep = epr.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); aproc = new ExtensibleURLArtifactProcessor(apep); mrep = epr.getExtensionPoint(ModelResolverExtensionPoint.class); pfep = epr.getExtensionPoint(ProviderFactoryExtensionPoint.class); + Properties props = new Properties(); + props.setProperty("defaultScheme", "vm"); + props.setProperty("defaultDomainName", "default"); + uep.getUtility(RuntimeProperties.class).setProperties(props); + depl = uep.getUtility(Deployer.class); + maep = epr.getExtensionPoint(ModuleActivatorExtensionPoint.class); + ws = uep.getUtility(WorkScheduler.class); + pf = uep.getUtility(ProxyFactory.class); + ca = uep.getUtility(CompositeActivator.class); + mon = mf.createMonitor(); + pc = new ProcessorContext(mon); + drf = ExtensibleDomainRegistryFactory.getInstance(epr); + drf.getEndpointRegistry(null, "default"); } } - static Context embedContext(NodeFactory nf) { - return new Context(nf); + static Context embedContext() { + return new Context(); } /** @@ -295,17 +344,94 @@ public class EmbedUtil { /** * Add a deployable composite to a contribution. */ - static Contribution deploy(final Contribution contrib, final Composite... comps) { - for(Composite c: comps) - contrib.getDeployables().add(c); + static Contribution deploy(final Contribution contrib, final Composite comp) { + contrib.getDeployables().add(comp); return contrib; } /** - * Configure a node with a list of contributions. + * Build a domain composite from a contribution. + */ + static Contribution build(final Contribution contrib, final Context ec) throws ContributionResolveException, CompositeBuilderException { + final Monitor cmon = ec.mf.setContextMonitor(ec.mon); + try { + final List<Contribution> contribs = Collections.singletonList(contrib); + Composite comp = ec.depl.build(contribs, contribs, new HashMap<QName, List<String>>(), ec.mon); + contrib.getDeployables().set(0, comp); + return contrib; + } finally { + ec.mf.setContextMonitor(cmon); + } + } + + /** + * Report the first error found in a monitor, and reset the monitor. + */ + static Context report(final Context ec) throws Throwable { + for (final Problem p : ec.mon.getProblems()) { + if ((p.getSeverity() == Problem.Severity.ERROR)) { + ec.mon.reset(); + if (p.getCause() != null) + throw p.getCause(); + throw new ServiceRuntimeException(p.toString()); + } + } + return ec; + } + + /** + * Activate the components in a composite. + */ + static CompositeContext activate(final Contribution contrib, final Context ec) throws ActivationException { + EndpointRegistry endpr = ec.drf.getEndpointRegistry("default", "default"); + final CompositeContext cc = new CompositeContext(ec.epr, endpr, contrib.getDeployables().get(0), "default", "default", null); + ec.ca.activate(cc, cc.getDomainComposite()); + return cc; + } + + /** + * Deactivate the components in a composite. */ - static Node node(final NodeFactory nf, final Contribution... contributions) { - return nf.createNode(Arrays.asList(contributions)); + static CompositeContext deactivate(final CompositeContext cc, final Context ec) throws ActivationException { + ec.ca.deactivate(cc.getDomainComposite()); + return cc; } + /** + * Start the components in a composite. + */ + static CompositeContext start(final CompositeContext cc, final Context ec) throws ActivationException { + ec.ca.start(cc, cc.getDomainComposite()); + return cc; + } + + /** + * Stop the components in a composite. + */ + static CompositeContext stop(final CompositeContext cc, final Context ec) throws ActivationException { + ec.ca.stop(cc, cc.getDomainComposite()); + return cc; + } + + /** + * Return a callable service. + */ + static <B> B service(final Class<B> i, final String s, final CompositeContext cc) { + ServiceReference<B> sr = serviceReference(i, s, cc); + if (sr == null) + throw new ServiceRuntimeException("Service not found: " + s); + return sr.getService(); + } + + /** + * Return a service reference. + */ + static <B> ServiceReference<B> serviceReference(Class<B> i, String s, CompositeContext cc) { + final String[] sn = s.split("/"); + for (Component c : cc.getDomainComposite().getComponents()) { + if (c.getName().equals(sn[0])) + return ((RuntimeComponent)c).getServiceReference(i, sn[1]); + } + throw new ServiceUnavailableException("Service has not been contributed to the domain: " + s); + } } diff --git a/sandbox/sebastien/java/vhost/samples/extending-tuscany/implementation-sample/src/test/java/sample/impl/EmbedTestCase.java b/sandbox/sebastien/java/vhost/samples/extending-tuscany/implementation-sample/src/test/java/sample/impl/EmbedTestCase.java index c7e7ef1574..2da547726a 100644 --- a/sandbox/sebastien/java/vhost/samples/extending-tuscany/implementation-sample/src/test/java/sample/impl/EmbedTestCase.java +++ b/sandbox/sebastien/java/vhost/samples/extending-tuscany/implementation-sample/src/test/java/sample/impl/EmbedTestCase.java @@ -21,26 +21,28 @@ package sample.impl; import static java.lang.System.out; import static org.junit.Assert.assertEquals; +import static sample.impl.EmbedUtil.activate; import static sample.impl.EmbedUtil.build; import static sample.impl.EmbedUtil.component; import static sample.impl.EmbedUtil.composite; import static sample.impl.EmbedUtil.contrib; +import static sample.impl.EmbedUtil.deactivate; import static sample.impl.EmbedUtil.deploy; import static sample.impl.EmbedUtil.embedContext; import static sample.impl.EmbedUtil.extensionPoints; import static sample.impl.EmbedUtil.implementation; -import static sample.impl.EmbedUtil.node; import static sample.impl.EmbedUtil.providerFactories; import static sample.impl.EmbedUtil.reference; import static sample.impl.EmbedUtil.service; +import static sample.impl.EmbedUtil.start; +import static sample.impl.EmbedUtil.stop; import static sample.impl.EmbedUtil.wsdli; import static sample.impl.TestUtil.here; import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.context.CompositeContext; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; -import org.apache.tuscany.sca.node.Node; -import org.apache.tuscany.sca.node.NodeFactory; import org.apache.tuscany.sca.provider.ImplementationProvider; import org.apache.tuscany.sca.provider.ProviderFactory; import org.apache.tuscany.sca.runtime.RuntimeComponent; @@ -64,15 +66,13 @@ import sample.WelloTest; * @version $Rev$ $Date$ */ public class EmbedTestCase { - static NodeFactory nf; static EmbedUtil.Context ec; - static Node node; + static CompositeContext cc; @SuppressWarnings("unchecked") @BeforeClass public static void setUp() throws Exception { - nf = NodeFactory.newInstance(); - ec = embedContext(nf); + ec = embedContext(); // Load the test WSDL definitions (could also construct the WSDL // and XSD models in code but that'd be quite painful, so just @@ -114,8 +114,7 @@ public class EmbedTestCase { providerFactories(ec).addProviderFactory(testProviderFactory()); // Run with it - node = node(nf, deploy(contrib, comp)); - node.start(); + cc = start(activate(build(deploy(contrib, comp), ec), ec), ec); } static ProviderFactory<SampleImplementation> testProviderFactory() { @@ -129,7 +128,7 @@ public class EmbedTestCase { @AfterClass public static void tearDown() throws Exception { - node.stop(); + deactivate(stop(cc, ec), ec); } @Test @@ -149,6 +148,6 @@ public class EmbedTestCase { } static Client client() { - return node.getService(Client.class, "client-test/Client"); + return service(Client.class, "client-test/Client", cc); } } |