From d587bce05e96fde5fac8aa5ca76d997ce34bd0d0 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 13 Sep 2010 18:03:53 +0000 Subject: Cleanup EmbedUtil a bit. Use a builder pattern and closures to pass a node factory context around instead of being kept in a static. Change the DOM util to use a similar builder pattern as well. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@996620 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/main/java/sample/impl/EmbedUtil.java | 306 +++++++++++++-------- .../src/test/java/sample/Xutil.java | 43 +-- .../src/test/java/sample/impl/EmbedTestCase.java | 24 +- 3 files changed, 226 insertions(+), 147 deletions(-) (limited to 'sandbox/sebastien') diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/main/java/sample/impl/EmbedUtil.java b/sandbox/sebastien/java/embed/samples/implementation-extension/src/main/java/sample/impl/EmbedUtil.java index 1db0e9a717..0ccbcc9e36 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/main/java/sample/impl/EmbedUtil.java +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/main/java/sample/impl/EmbedUtil.java @@ -19,9 +19,7 @@ package sample.impl; -import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; import java.util.Arrays; @@ -40,7 +38,6 @@ import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; -import org.apache.tuscany.sca.contribution.processor.ContributionException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.processor.ExtensibleURLArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.ProcessorContext; @@ -67,139 +64,232 @@ import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; public class EmbedUtil { /** - * Initialize a Node factory and get the various registries, factories and - * extension points we need. + * A runtime embedder context, which conveniently initializes a Node factory + * and gets the various registries, factories and extension points we need. */ - static final NodeFactory nf; - static final ExtensionPointRegistry epr; - static final FactoryExtensionPoint fep; - static final ContributionFactory cf; - static final AssemblyFactory af; - static final JavaInterfaceFactory jif; - static final WSDLFactory wif; - static final URLArtifactProcessorExtensionPoint apep; - static final ExtensibleURLArtifactProcessor aproc; - static final ModelResolverExtensionPoint mrep; - static final ProviderFactoryExtensionPoint pfep; - static { - nf = NodeFactory.newInstance(); - epr = nf.getExtensionPointRegistry(); - fep = epr.getExtensionPoint(FactoryExtensionPoint.class); - cf = fep.getFactory(ContributionFactory.class); - af = fep.getFactory(AssemblyFactory.class); - 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); + static class Context { + final NodeFactory nf; + final ExtensionPointRegistry epr; + final FactoryExtensionPoint fep; + final ContributionFactory cf; + final AssemblyFactory af; + final JavaInterfaceFactory jif; + final WSDLFactory wif; + final URLArtifactProcessorExtensionPoint apep; + final ExtensibleURLArtifactProcessor aproc; + final ModelResolverExtensionPoint mrep; + final ProviderFactoryExtensionPoint pfep; + + Context(final NodeFactory nf) { + this.nf = nf; + epr = nf.getExtensionPointRegistry(); + fep = epr.getExtensionPoint(FactoryExtensionPoint.class); + cf = fep.getFactory(ContributionFactory.class); + af = fep.getFactory(AssemblyFactory.class); + 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); + } } - + + static Context embedContext(NodeFactory nf) { + return new Context(nf); + } + /** * A mini DSL to help build and assemble contributions and SCDL composites. */ - static Contribution contrib(final String uri, final String loc, final Artifact... artifacts) { - final Contribution c = cf.createContribution(); - c.setURI(uri); - c.setLocation(loc); - c.setModelResolver(new ExtensibleModelResolver(c, mrep, fep)); - for(Artifact a: artifacts) - c.getArtifacts().add(a); - return c; - } - - static Artifact artifact(final String uri, final Object model) { - final Artifact a = cf.createArtifact(); - a.setURI(uri); - a.setModel(model); - return a; - } - - static Composite composite(final String ns, final String name, final Component... components) { - final Composite compos = af.createComposite(); - compos.setName(new QName(ns, name)); - for(final Component c: components) - compos.getComponents().add(c); - return compos; - } - - static Component component(final String name, final Implementation impl, final ComponentReference... references) { - final Component c = af.createComponent(); - c.setName(name); - c.setImplementation(impl); - for(ComponentReference r: references) - c.getReferences().add(r); - return c; - } - - static SampleImplementation implementation(final Class clazz, final Contract... contracts) { - final SampleImplementation impl = ImplUtil.implementation(clazz.getName()); - impl.clazz = clazz; - impl.setUnresolved(false); - for(final Contract c: contracts) { - if(c instanceof Service) - impl.getServices().add((Service)c); - else - impl.getReferences().add((Reference)c); - } - return impl; + interface Builder { + T build(Context ec); + } + + static T build(final Builder builder, final Context ec) { + return builder.build(ec); + } + + /** + * Return a contribution builder. + */ + static Builder contrib(final String uri, final String loc, final Builder... artifacts) { + return new Builder() { + public Contribution build(final Context ec) { + final Contribution c = ec.cf.createContribution(); + c.setURI(uri); + c.setLocation(loc); + c.setModelResolver(new ExtensibleModelResolver(c, ec.mrep, ec.fep)); + for(Builder a: artifacts) + c.getArtifacts().add(a.build(ec)); + return c; + } + }; + } + + /** + * Return an artifact builder. + */ + static Builder artifact(final String uri, final Object model) { + return new Builder() { + public Artifact build(final Context ec) { + final Artifact a = ec.cf.createArtifact(); + a.setURI(uri); + a.setModel(model); + return a; + } + }; } - static Reference reference(final String name, final Class c) throws InvalidInterfaceException { - return ImplUtil.reference(name, c, jif, af); + /** + * Return a composite builder. + */ + static Builder composite(final String ns, final String name, final Builder... components) { + return new Builder() { + public Composite build(final Context ec) { + final Composite compos = ec.af.createComposite(); + compos.setName(new QName(ns, name)); + for(final Builder c: components) + compos.getComponents().add(c.build(ec)); + return compos; + } + }; + } + + /** + * Return a component builder. + */ + static Builder component(final String name, final Builder impl, final Builder... references) { + return new Builder() { + public Component build(final Context ec) { + final Component c = ec.af.createComponent(); + c.setName(name); + c.setImplementation(impl.build(ec)); + for(Builder r: references) + c.getReferences().add(r.build(ec)); + return c; + } + }; } - static Reference reference(final String name, final WSDLInterface c) { - return ImplUtil.reference(name, c, wif, af); + /** + * Return an implementation builder. + */ + static Builder implementation(final Class clazz, final Builder... contracts) { + return new Builder() { + public SampleImplementation build(final Context ec) { + final SampleImplementation impl = ImplUtil.implementation(clazz.getName()); + impl.clazz = clazz; + impl.setUnresolved(false); + for(final Builder b: contracts) { + Contract c = b.build(ec); + if(c instanceof Service) + impl.getServices().add((Service)c); + else + impl.getReferences().add((Reference)c); + } + return impl; + } + }; } - static ComponentReference reference(final String name, final String target) { - final ComponentReference r = af.createComponentReference(); - r.setName(name); - final ComponentService s = af.createComponentService(); - s.setUnresolved(true); - s.setName(target); - r.getTargets().add(s); - return r; + /** + * Return a reference builder. + */ + static Builder reference(final String name, final Class c) { + return new Builder() { + public Reference build(final Context ec) { + try { + return ImplUtil.reference(name, c, ec.jif, ec.af); + } catch(InvalidInterfaceException e) { + throw new RuntimeException(e); + } + } + }; } - static Service service(final Class c) throws InvalidInterfaceException { - return ImplUtil.service(c, jif, af); + static Builder reference(final String name, final WSDLInterface c) { + return new Builder() { + public Reference build(final Context ec) { + return ImplUtil.reference(name, c, ec.wif, ec.af); + } + }; } - static Service service(final WSDLInterface c) { - return ImplUtil.service(c, wif, af); + static Builder reference(final String name, final String target) { + return new Builder() { + public ComponentReference build(final Context ec) { + final ComponentReference r = ec.af.createComponentReference(); + r.setName(name); + final ComponentService s = ec.af.createComponentService(); + s.setUnresolved(true); + s.setName(target); + r.getTargets().add(s); + return r; + } + }; } /** - * Load a WSDL into a contribution. + * Return a service builder. */ - static WSDLInterface wsdli(final String uri, final String ns, final String name, final Contribution c) throws InvalidInterfaceException, ContributionException, IOException, URISyntaxException { - final ProcessorContext ctx = new ProcessorContext(); - final WSDLDefinition wd = aproc.read(null, new URI(uri), new URL(new URL(c.getLocation()), uri), ctx, WSDLDefinition.class); - c.getModelResolver().addModel(wd, ctx); - c.getModelResolver().resolveModel(WSDLDefinition.class, wd, ctx); - final WSDLObject pt = wd.getWSDLObject(PortType.class, new QName(ns, name)); - if(pt == null) - throw new ContributionResolveException("Couldn't find " + name); - final WSDLInterface nwi = wif.createWSDLInterface(pt.getElement(), wd, c.getModelResolver(), null); - nwi.setWsdlDefinition(wd); - nwi.resetDataBinding(DOMDataBinding.NAME); - return nwi; + static Builder service(final Class c) { + return new Builder() { + public Service build(final Context ec) { + try { + return ImplUtil.service(c, ec.jif, ec.af); + } catch(InvalidInterfaceException e) { + throw new RuntimeException(e); + } + } + }; + } + + static Builder service(final WSDLInterface c) { + return new Builder() { + public Service build(final Context ec) { + return ImplUtil.service(c, ec.wif, ec.af); + } + }; } + /** + * Return a WSDLInterface builder which loads a WSDL into a contribution. + */ + static Builder wsdli(final String uri, final String ns, final String name, final Contribution c) { + return new Builder() { + public WSDLInterface build(final Context ec) { + try { + final ProcessorContext ctx = new ProcessorContext(); + final WSDLDefinition wd = ec.aproc.read(null, new URI(uri), new URL(new URL(c.getLocation()), uri), ctx, WSDLDefinition.class); + c.getModelResolver().addModel(wd, ctx); + c.getModelResolver().resolveModel(WSDLDefinition.class, wd, ctx); + final WSDLObject pt = wd.getWSDLObject(PortType.class, new QName(ns, name)); + if(pt == null) + throw new ContributionResolveException("Couldn't find " + name); + final WSDLInterface nwi = ec.wif.createWSDLInterface(pt.getElement(), wd, c.getModelResolver(), null); + nwi.setWsdlDefinition(wd); + nwi.resetDataBinding(DOMDataBinding.NAME); + return nwi; + } catch(Exception e) { + throw new RuntimeException(e); + } + } + }; + } + /** * Return the extension point registry used by our nodes. */ - static ExtensionPointRegistry extensionPoints() { - return epr; + static ExtensionPointRegistry extensionPoints(final Context ec) { + return ec.epr; } /** * Return the provider factory extension point used by our nodes. */ - static ProviderFactoryExtensionPoint providerFactories() { - return pfep; + static ProviderFactoryExtensionPoint providerFactories(final Context ec) { + return ec.pfep; } /** @@ -214,7 +304,7 @@ public class EmbedUtil { /** * Configure a node with a list of contributions. */ - static Node node(final Contribution... contributions) { + static Node node(final NodeFactory nf, final Contribution... contributions) { return nf.createNode(Arrays.asList(contributions)); } diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/Xutil.java b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/Xutil.java index bdad745b6f..ce89f9dd8c 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/Xutil.java +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/Xutil.java @@ -45,18 +45,15 @@ import org.w3c.dom.NodeList; * Just for fun, a little bit of magic code and utility functions to help work with XML DOM. */ class Xutil { - static class NodeBuilder { - String ns; - String name; - NodeBuilder[] children; - String text; + interface NodeBuilder { + Node build(Document doc); } /** * Convert a name and a list of children to a document element. */ static Element xdom(String ns, String name, final NodeBuilder... nodes) { - return (Element)node(elem(ns, name, nodes), db.newDocument()); + return (Element)elem(ns, name, nodes).build(db.newDocument()); } /** @@ -64,21 +61,17 @@ class Xutil { */ static NodeBuilder elem(final String uri, final String n, final NodeBuilder... nodes) { return new NodeBuilder() { - { - this.ns = uri; - this.name = n; - this.children = nodes; + public Node build(Document doc) { + final Element e = doc.createElementNS(uri, n); + for(final NodeBuilder n: nodes) + e.appendChild(n.build(doc)); + return e; } }; } static NodeBuilder elem(final String n, final NodeBuilder... nodes) { - return new NodeBuilder() { - { - this.name = n; - this.children = nodes; - } - }; + return elem(null, n, nodes); } /** @@ -86,8 +79,8 @@ class Xutil { */ static NodeBuilder text(final String t) { return new NodeBuilder() { - { - this.text = t; + public Node build(final Document doc) { + return doc.createTextNode(t); } }; } @@ -102,18 +95,6 @@ class Xutil { } } - private static Element link(final Element e, final Document doc, final NodeBuilder... nodes) { - for(final NodeBuilder c: nodes) - e.appendChild(node(c, doc)); - return e; - } - - private static Node node(NodeBuilder node, Document doc) { - if(node.text != null) - return doc.createTextNode(node.text); - return link(doc.createElementNS(node.ns, node.name), doc, node.children); - } - /** * Convert an element to XML. */ @@ -144,7 +125,7 @@ class Xutil { } /** - * A pure Java FP-style alternative to xpath. + * A pure Java FP-style alternative to xpath for DOM. */ interface Mapper { T map(final Element e); diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java index f30b5c9853..406dbabee0 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java @@ -21,10 +21,12 @@ package sample.impl; import static java.lang.System.out; import static org.junit.Assert.assertEquals; +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.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; @@ -38,6 +40,7 @@ import org.apache.tuscany.sca.assembly.Composite; 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; @@ -61,23 +64,28 @@ import sample.WelloTest; * @version $Rev$ $Date$ */ public class EmbedTestCase { + static NodeFactory nf; + static EmbedUtil.Context ec; static Node node; + @SuppressWarnings("unchecked") @BeforeClass public static void setUp() throws Exception { + nf = NodeFactory.newInstance(); + ec = embedContext(nf); // Load the test WSDL definitions (could also construct the WSDL // and XSD models in code but that'd be quite painful, so just // load them from XML for now) - final Contribution contrib = contrib("test", here()); - WSDLInterface Hello_wsdl = wsdli("Hello.wsdl", "http://sample/hello", "Hello", contrib); - WSDLInterface Upper_wsdl = wsdli("Upper.wsdl", "http://sample/upper", "Upper", contrib); + final Contribution contrib = build(contrib("test", here()), ec); + WSDLInterface Hello_wsdl = build(wsdli("Hello.wsdl", "http://sample/hello", "Hello", contrib), ec); + WSDLInterface Upper_wsdl = build(wsdli("Upper.wsdl", "http://sample/upper", "Upper", contrib), ec); // Assemble a test composite model (see EmbedUtil // for the little DSL used here, much more concise // than using the assembly model interfaces) final Composite comp = - composite("http://sample", "test", + build(composite("http://sample", "test", component("client-test", implementation(ClientTest.class, service(Client.class), @@ -97,19 +105,19 @@ public class EmbedTestCase { reference("upper", "upper-test")), component("upper-test", implementation(UpperTest.class, - service(Upper.class)))); + service(Upper.class)))), ec); // Register a test instance of our sample implementation ProviderFactory - providerFactories().addProviderFactory(testProviderFactory()); + providerFactories(ec).addProviderFactory(testProviderFactory()); // Run with it - node = node(deploy(contrib, comp)); + node = node(nf, deploy(contrib, comp)); node.start(); } static ProviderFactory testProviderFactory() { // This shows how to get called when a provider is created - return new SampleProviderFactory(extensionPoints()) { + return new SampleProviderFactory(extensionPoints(ec)) { public ImplementationProvider createImplementationProvider(RuntimeComponent comp, SampleImplementation impl) { out.println("Creating a provider for component " + comp.getName()); return super.createImplementationProvider(comp, impl); -- cgit v1.2.3