From 9686b7a1c9319ff1dab18db1e4248b3481a5f797 Mon Sep 17 00:00:00 2001 From: antelder Date: Fri, 13 May 2011 13:28:59 +0000 Subject: A refactor of NodeImpl to simplify and remove the use of InstalledContribution. This is in a seperate class name NodeImpl2 for now as its getting a bit tanlged so this is stripped down to the bare minimum for now just to get it going again git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1102714 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tuscany/sca/impl/NodeImpl2.java | 292 +++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 sca-java-2.x/trunk/modules/domain-node/src/main/java/org/apache/tuscany/sca/impl/NodeImpl2.java (limited to 'sca-java-2.x/trunk/modules/domain-node/src') diff --git a/sca-java-2.x/trunk/modules/domain-node/src/main/java/org/apache/tuscany/sca/impl/NodeImpl2.java b/sca-java-2.x/trunk/modules/domain-node/src/main/java/org/apache/tuscany/sca/impl/NodeImpl2.java new file mode 100644 index 0000000000..7bc17c959d --- /dev/null +++ b/sca-java-2.x/trunk/modules/domain-node/src/main/java/org/apache/tuscany/sca/impl/NodeImpl2.java @@ -0,0 +1,292 @@ +/* + * 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.impl; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.TuscanyRuntime; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.common.java.io.IOHelper; +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.Export; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.namespace.NamespaceExport; +import org.apache.tuscany.sca.contribution.namespace.NamespaceImport; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.deployment.Deployer; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.ValidationException; +import org.apache.tuscany.sca.runtime.ActivationException; +import org.apache.tuscany.sca.runtime.CompositeActivator; +import org.apache.tuscany.sca.runtime.EndpointRegistry; +import org.apache.tuscany.sca.runtime.InstalledContribution; + +public class NodeImpl2 { + + private String domainName; + private Deployer deployer; + private CompositeActivator compositeActivator; + private EndpointRegistry endpointRegistry; + private ExtensionPointRegistry extensionPointRegistry; + private TuscanyRuntime tuscanyRuntime; + + private Map loadedContributions = new HashMap(); + + private Map startedComposites = new HashMap(); + private Map stoppedComposites = new HashMap(); + + public NodeImpl2(String domainName, + Deployer deployer, + CompositeActivator compositeActivator, + EndpointRegistry endpointRegistry, + ExtensionPointRegistry extensionPointRegistry, + TuscanyRuntime tuscanyRuntime) { + this.domainName = domainName; + this.deployer = deployer; + this.compositeActivator = compositeActivator; + this.endpointRegistry = endpointRegistry; + this.extensionPointRegistry = extensionPointRegistry; + this.tuscanyRuntime = tuscanyRuntime; + } + + public String installContribution(String contributionURL) throws ContributionReadException, ActivationException, ValidationException { + return installContribution(null, contributionURL); + } + + public String installContribution(String uri, String contributionURL) throws ContributionReadException, ActivationException, ValidationException { + if (uri == null) { + uri = getDefaultContributionURI(contributionURL); + } + + InstalledContribution ic = new InstalledContribution(); + ic.setURI(uri); + ic.setURL(contributionURL); + + peekIntoContribution(ic); + + endpointRegistry.installContribution(ic); + + return uri; + } + + /** + * Peek into the contribution to find its attributes. + * ASM12032 and ASM12033 say no error checking should be done during install and that should happen later, but + * we would still like to know about deployables and exports so peek into the contribution to try to get those, + * and just ignore any errors they might happen while doing that. + */ + protected void peekIntoContribution(InstalledContribution ic) throws ContributionReadException, ValidationException { + Contribution contribution = loadContribution(ic); + + // deployables + for (Composite composite : contribution.getDeployables()) { + ic.getDeployables().add(composite.getURI()); + } + + // Exports + for (Export export : contribution.getExports()) { + if (export instanceof JavaExport) { + ic.getJavaExports().add(((JavaExport)export).getPackage()); + } else if (export instanceof NamespaceExport) { + ic.getNamespaceExports().add(((NamespaceExport)export).getNamespace()); + } // TODO: Handle these and others in a more extensible way + } + + } + + public List getDeployableComposites(String contributionURI) { + InstalledContribution ic = endpointRegistry.getInstalledContribution(contributionURI); + return new ArrayList(ic.getDeployables()); + } + + public List getInstalledContributionURIs() { + return new ArrayList(endpointRegistry.getInstalledContributionURIs()); + } + + public Contribution getContribution(String contributionURI) throws ContributionReadException, ValidationException { + return loadContribution(getInstalledContribution(contributionURI)); + } + + public void validateContribution(String contributionURI) throws ContributionReadException, ValidationException { + InstalledContribution ic = getInstalledContribution(contributionURI); + Contribution contribution = loadContribution(ic); + + List dependentContributions = calculateDependentContributions(ic); + + Monitor monitor = deployer.createMonitor(); + try { + deployer.resolve(contribution, dependentContributions, monitor); + } catch (Exception e) { + throw new RuntimeException(e); + } + monitor.analyzeProblems(); + } + + public Map> getStartedComposites() { + return endpointRegistry.getRunningCompositeNames(); + } + + public void startComposite(String contributionURI, String compositeURI) throws ActivationException, ValidationException, ContributionReadException { + String key = contributionURI+"/"+compositeURI; + if (startedComposites.containsKey(key)) { + throw new IllegalStateException("composite already started: " + compositeURI); + } + DeployedComposite dc = stoppedComposites.remove(key); + if (dc != null) { + dc.start(); + startedComposites.put(key, dc); + } else { + InstalledContribution ic = getInstalledContribution(contributionURI); + Contribution contribution = loadContribution(ic); + Composite composite = getComposite(contribution, compositeURI); + List dependentContributions = calculateDependentContributions(ic); + dc = new DeployedComposite(composite, contribution, dependentContributions, deployer, compositeActivator, endpointRegistry, extensionPointRegistry); + dc.start(); + startedComposites.put(key, dc); + } + } + + public void stopComposite(String contributionURI, String compositeURI) throws ActivationException { + String key = contributionURI+"/"+compositeURI; + DeployedComposite dc = startedComposites.remove(key); + if (dc == null) { + throw new IllegalStateException("composite not started: " + compositeURI); + } + dc.stop(); + stoppedComposites.put(key, dc); + } + + protected InstalledContribution getInstalledContribution(String contributionURI) { + InstalledContribution ic = endpointRegistry.getInstalledContribution(contributionURI); + if (ic == null) { + throw new IllegalArgumentException("Contribution not installed: " + contributionURI); + } + return ic; + } + + protected Contribution loadContribution(InstalledContribution ic) throws ContributionReadException, ValidationException { + Contribution contribution = loadedContributions.get(ic.getURI()); + if (contribution == null) { + Monitor monitor = deployer.createMonitor(); + contribution = deployer.loadContribution(IOHelper.createURI(ic.getURI()), IOHelper.getLocationAsURL(ic.getURL()), monitor); + monitor.analyzeProblems(); + loadedContributions.put(ic.getURI(), contribution); + fixDeployedCompositeURIs(contribution); + } + return contribution; + } + + protected List calculateDependentContributions(InstalledContribution ic) throws ContributionReadException, ValidationException { + List dependentContributions = new ArrayList(); + Contribution c = loadContribution(ic); + if (ic.getDependentContributionURIs() != null) { + // if the install specified dependent uris use just those contributions + for (String uri : ic.getDependentContributionURIs()) { + InstalledContribution dependee = endpointRegistry.getInstalledContribution(uri); + if (dependee != null) { + dependentContributions.add(loadContribution(dependee)); + } + } + } else { + // TODO: otherwise find from the registry which contributions export the required resources + for (Import imprt : c.getImports()) { + // TODO: Handle Imports in a more extensible way + if (imprt instanceof JavaImport) { +// ic.getJavaExports().add(((JavaExport)export).getPackage()); + } else if (imprt instanceof NamespaceImport) { +// ic.getNamespaceExports().add(((NamespaceExport)export).getNamespace()); + } +// dependentContributions.add(ics.getContribution()); + } + } + return dependentContributions; + } + + protected Composite getComposite(Contribution contribution, String compositeURI) { + for (Artifact a : contribution.getArtifacts()) { + if (a.getURI().equals(compositeURI)) { + return (Composite) a.getModel(); + } + } + throw new IllegalArgumentException("composite not found: " + compositeURI); + } + + /** + * Deployable composites don't have the uri set so get it from the artifact in the contribution + * // TODO: fix the Tuscany code so this uri is correctly set and this method isn't needed + */ + private void fixDeployedCompositeURIs(Contribution contribution) { + int i = contribution.getDependencies().size(); + for (Artifact a : contribution.getArtifacts()) { + if (a.getModel() != null) { + if (a.getModel() instanceof Composite) { + Composite cm = a.getModel(); + for (Composite c : contribution.getDeployables()) { + if (c.getName().equals(cm.getName())) { + c.setURI(cm.getURI()); + i = i-1; + if (i < 1) { + return; + } + } + } + } + } + } + } + + /** + * Returns a default URI for a contribution based on the contribution URL + */ + protected String getDefaultContributionURI(String contributionURL) { + String uri = null; + try { + File f = new File(contributionURL); + if ("classes".equals(f.getName()) && "target".equals(f.getParentFile().getName())) { + uri = f.getParentFile().getParentFile().getName(); + } else { + uri = f.getName(); + } + } catch (Exception e) { + // ignore + } + if (uri == null) { + uri = contributionURL; + } + if (uri.endsWith(".zip") || uri.endsWith(".jar")) { + uri = uri.substring(0, uri.length() - 4); + } + if (uri.endsWith("SNAPSHOT")) { + uri = uri.substring(0, uri.lastIndexOf('-')); + uri = uri.substring(0, uri.lastIndexOf('-')); + } + return uri; + } +} -- cgit v1.2.3