From ed50ceeecd37525c923683c8e81c7edac2dba497 Mon Sep 17 00:00:00 2001 From: antelder Date: Fri, 21 May 2010 14:49:55 +0000 Subject: Update for Section10 interface updates and start to try to get it to work more as the spec describes so without nodes and to have installed contributions and deploying/undeploying composites. (The tests getService methods no longer work as the scaclient factory impl expects Nodes to have been created) git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@947031 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/something/impl/InstalledContribution.java | 57 ++++ .../tuscany/sca/something/impl/Section10Impl.java | 316 +++++++++++++++------ 2 files changed, 291 insertions(+), 82 deletions(-) create mode 100644 sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/InstalledContribution.java (limited to 'sca-java-2.x/contrib/modules/section10/src/main') diff --git a/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/InstalledContribution.java b/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/InstalledContribution.java new file mode 100644 index 0000000000..f793bacc53 --- /dev/null +++ b/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/InstalledContribution.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.sca.something.impl; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.Contribution; + +public class InstalledContribution { + private String uri; + private String url; + private Contribution contribution; + private List deployedComposites = new ArrayList(); + + public InstalledContribution(String uri, String url, Contribution contribution) { + this.uri = uri; + this.url = url; + this.contribution = contribution; + } + public Contribution getContribution() { + return contribution; + } + public void setContribution(Contribution contribution) { + this.contribution = contribution; + } + public String getURI() { + return uri; + } + public String getURL() { + return url; + } + public List getDeployedComposites() { + return deployedComposites; + } + + // TODO: there likely needs to be ways to get at depending/dependent contributions + +} diff --git a/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/Section10Impl.java b/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/Section10Impl.java index 7d7e6d902c..3c838c6ae7 100644 --- a/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/Section10Impl.java +++ b/sca-java-2.x/contrib/modules/section10/src/main/java/org/apache/tuscany/sca/something/impl/Section10Impl.java @@ -19,36 +19,99 @@ package org.apache.tuscany.sca.something.impl; +import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.Reader; +import java.net.MalformedURLException; import java.net.URI; -import java.util.Collections; +import java.net.URL; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import javax.xml.namespace.QName; +import javax.xml.stream.XMLOutputFactory; -import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.common.java.io.IOHelper; +import org.apache.tuscany.sca.context.CompositeContext; +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.deployment.Deployer; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; import org.apache.tuscany.sca.node.NodeFactory; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; import org.apache.tuscany.sca.node.impl.NodeFactoryImpl; -import org.apache.tuscany.sca.node.impl.NodeImpl; +import org.apache.tuscany.sca.runtime.CompositeActivator; +import org.apache.tuscany.sca.runtime.DomainRegistryFactory; +import org.apache.tuscany.sca.runtime.EndpointRegistry; +import org.apache.tuscany.sca.runtime.ExtensibleDomainRegistryFactory; import org.apache.tuscany.sca.something.Section10; import org.oasisopen.sca.NoSuchDomainException; +import org.oasisopen.sca.NoSuchServiceException; +import org.oasisopen.sca.ServiceRuntimeException; import org.oasisopen.sca.client.SCAClientFactory; -import org.w3c.dom.Document; public class Section10Impl implements Section10 { - private NodeFactory nodeFactory; - private Map installedContributions = new HashMap(); + protected NodeFactoryImpl nodeFactory; + protected Map installedContributions = new HashMap(); public Section10Impl(Properties config) { - this.nodeFactory = NodeFactory.newInstance(config); + this.nodeFactory = (NodeFactoryImpl)NodeFactory.newInstance(config); ((NodeFactoryImpl)nodeFactory).start(); } + public String installContribution(String contributionURL) throws ContributionReadException, MalformedURLException { + installContribution(contributionURL, contributionURL, null, true); + return contributionURL; + } + + public String installContribution(String contributionURL, boolean autoDeploy) throws ContributionReadException, MalformedURLException { + installContribution(contributionURL, contributionURL, null, autoDeploy); + return contributionURL; + } + + public void installContribution(String uri, String contributionURL) throws ContributionReadException, MalformedURLException { + installContribution(uri, contributionURL, null, true); + } + + public void installContribution(String uri, String contributionURL, List dependentContributionURIs) throws ContributionReadException, MalformedURLException { + installContribution(uri, contributionURL, dependentContributionURIs, true); + } + + public void installContribution(String uri, String contributionURL, List dependentContributionURIs, boolean autoDeploy) throws ContributionReadException, MalformedURLException { + Deployer deployer = nodeFactory.getDeployer(); + Monitor monitor = deployer.createMonitor(); + URL url = getLocationAsURL(contributionURL); + Contribution contribution = deployer.loadContribution(URI.create(uri), url, monitor); + InstalledContribution ic = new InstalledContribution(uri, url.toString(), contribution); + installedContributions.put(uri, ic); + if (autoDeploy) { + for (Composite c : contribution.getDeployables()) { + try { + deployComposite(c, ic); + } catch (Throwable e) { + // TODO tidy up the exceptions + throw new RuntimeException(e); + } + } + } + } + public String addDeploymentComposite(String contriubutionURI, String compositeXML) { // TODO Auto-generated method stub return null; @@ -60,132 +123,221 @@ public class Section10Impl implements Section10 { } public void addToDomainLevelComposite(String compositeURI) { - String contributionURI = getContributionUriFromArtifactUri(compositeURI); - String artifactURI = compositeURI.substring(contributionURI.length()+1); - // TODO: now what? there doesn't appear to be any way yet to do this with the exsiting Node API + String contributionURI = getContributionUriForArtifact(compositeURI); + InstalledContribution ic = installedContributions.get(contributionURI); + if (ic == null) { + throw new IllegalArgumentException("Contribution not installed: " + contributionURI); + } + String relativeURI = compositeURI.substring(contributionURI.length()+1); + for (Artifact a : ic.getContribution().getArtifacts()) { + if (a.getURI().equals(relativeURI)) { + Composite c = (Composite) a.getModel(); + try { + deployComposite(c, ic); + } catch (Throwable e) { + // TODO tidy up the exceptions + throw new RuntimeException(e); + } + return; + } + } + throw new IllegalArgumentException("composite not found: " + compositeURI); } - protected String getContributionUriFromArtifactUri(String artifactURI) { - String contributionURI = null; - for (String uri : installedContributions.keySet()) { - if (artifactURI.startsWith(uri)) { - contributionURI = uri; - break; + public void removeFromDomainLevelComposite(String compositeURI) { + String contributionURI = getContributionUriForArtifact(compositeURI); + InstalledContribution ic = installedContributions.get(contributionURI); + String relativeURI = compositeURI.substring(contributionURI.length()+1); + for (Composite c : ic.getDeployedComposites()) { + if (relativeURI.equals(c.getURI())) { + undeployComposite(c, ic); + return; } } - if (contributionURI == null) { - throw new IllegalArgumentException("no contribution found for: " + artifactURI); - } - return contributionURI; + throw new IllegalStateException("composite not deployed: " + compositeURI); } - public void deployContribution(String contributionURI) { + public Composite getDomainLevelComposite() { // TODO Auto-generated method stub + return null; + } + + public String getDomainLevelCompositeAsString() { + StAXArtifactProcessorExtensionPoint xmlProcessors = getExtensionRegistry().getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + StAXArtifactProcessor compositeProcessor = xmlProcessors.getProcessor(Composite.class); + return writeComposite(getDomainLevelComposite(), compositeProcessor); } - public Document getDomainLevelComposite() { + public Object getQNameDefinition(String contributionURI, QName definition, QName symbolSpace) { // TODO Auto-generated method stub return null; } - public String getDomainLevelCompositeAsString() { + public void removeContribution(String contributionURI) { + // TODO: what about dependent contributions + InstalledContribution ic = installedContributions.remove(contributionURI); + if (ic == null) { + throw new IllegalArgumentException("Contribution not installed: " + contributionURI); + } + for (Composite c : ic.getDeployedComposites()) { + undeployComposite(c, ic); + } + } + + public void updateContribution(String uri, String contributionURL) { + // TODO Auto-generated method stub + // is this just removeContribution/installContribution? + } + + public String updateDeploymentComposite(String uri, String compositeXML) { // TODO Auto-generated method stub + // is this removeFromDomainLevelComposite/addDeploymentComposite return null; } - public Object getQNameDefinition(String contributionURI, QName definition, QName symbolSpace) { + public String updateDeploymentComposite(String uri, Reader compositeXML) { // TODO Auto-generated method stub + // is this removeFromDomainLevelComposite/addDeploymentComposite return null; } - public SCAClientFactory getSCAClientFactory() { + public void shutdown() { + nodeFactory.destroy(); + } + + public T getService(Class interfaze, String serviceURI) throws NoSuchServiceException { try { - return SCAClientFactory.newInstance(URI.create(getDomainName())); + return SCAClientFactory.newInstance(URI.create(getDomainName())).getService(interfaze, serviceURI); } catch (NoSuchDomainException e) { + // shouldn't ever happen as we know this is the domain so it must exist throw new IllegalStateException(e); } } - - private String getDomainName() { + + public String getDomainName() { // TODO Auto-generated method stub return "default"; } - public void installContribution(String uri, String contributionURL, List dependentContributionURIs) { - if (dependentContributionURIs == null) { - dependentContributionURIs = Collections.emptyList(); - } - String[] cs = new String[dependentContributionURIs.size()+1]; - cs[0] = contributionURL; - for (int i=1; i allContributions = new ArrayList(); + for (InstalledContribution ics : installedContributions.values()) { + allContributions.add(ics.getContribution()); + } + + List contributions = new ArrayList(); + contributions.add(ic.getContribution()); + contributions.get(0).getDeployables().clear(); + contributions.get(0).getDeployables().add(c); + domainComposite = nodeFactory.getDeployer().build(contributions, allContributions, new HashMap>(), monitor); + analyzeProblems(monitor); + + compositeContext = new CompositeContext(getExtensionRegistry(), + endpointRegistry, + domainComposite, + getDomainName(), + "whatIsThisURIfor", + nodeFactory.getDeployer().getSystemDefinitions()); + + CompositeContext.setThreadCompositeContext(compositeContext); + } finally { + monitorFactory.setContextMonitor(tcm); + } + compositeActivator.activate(compositeContext, domainComposite); + compositeActivator.start(compositeContext, domainComposite); + + ic.getDeployedComposites().add(c); } - public void stopComponent(String componentURI) { + protected void undeployComposite(Composite c, InstalledContribution ic) { + ic.getDeployedComposites().remove(c); // TODO Auto-generated method stub - } - public void undeployContribution(String contributionURI) { - // TODO Auto-generated method stub - + protected ExtensionPointRegistry getExtensionRegistry() { + ExtensionPointRegistry reg = nodeFactory.getExtensionPointRegistry(); + return reg; } - public void updateContribution(String uri, String contributionURL) { - // TODO Auto-generated method stub + protected String writeComposite(Composite composite, StAXArtifactProcessor compositeProcessor){ + XMLOutputFactory outputFactory = getExtensionRegistry().getExtensionPoint(FactoryExtensionPoint.class).getFactory(XMLOutputFactory.class); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + compositeProcessor.write(composite, outputFactory.createXMLStreamWriter(bos), new ProcessorContext(getExtensionRegistry())); + } catch(Exception e) { + throw new RuntimeException(e); + } + + String result = bos.toString(); + + // write out and nested composites + for (Component component : composite.getComponents()) { + if (component.getImplementation() instanceof Composite) { + result += "\n\n"; + result += writeComposite((Composite)component.getImplementation(), compositeProcessor); + } + } + return result; } - public String updateDeploymentComposite(String uri, String compositeXML) { - // TODO Auto-generated method stub - return null; + protected URL getLocationAsURL(String location) throws MalformedURLException { + URI uri = IOHelper.createURI(location); + if (uri.getScheme() == null) { + uri = new File(location).toURI(); + } + URL url = uri.toURL(); + return url; } - - public String updateDeploymentComposite(String uri, Reader compositeXML) { - // TODO Auto-generated method stub - return null; + + protected void analyzeProblems(Monitor monitor) throws Throwable { + try { + for (Problem problem : monitor.getProblems()) { + if ((problem.getSeverity() == Severity.ERROR)) { + if (problem.getCause() != null) { + throw problem.getCause(); + } else { + throw new ServiceRuntimeException(problem.toString()); + } + } + } + } finally { + // FIXME: Clear problems so that the monitor is clean again + monitor.reset(); + } } - } -- cgit v1.2.3