From 3a569a2f00bf172cddfd567149774ee808a2a242 Mon Sep 17 00:00:00 2001 From: nash Date: Wed, 30 Mar 2011 19:50:51 +0000 Subject: Create branch for 1.6.2 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1087059 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/ContributionDependencyBuilderImpl.java | 145 +++++++++ .../impl/ContributionContentProcessor.java | 355 +++++++++++++++++++++ .../processor/impl/ContributionInfoProcessor.java | 240 ++++++++++++++ .../scanner/impl/DirectoryContributionScanner.java | 107 +++++++ .../scanner/impl/JarContributionScanner.java | 121 +++++++ ...sca.contribution.processor.URLArtifactProcessor | 20 ++ .../workspace-validation-messages.properties | 22 ++ .../workspace-validation-messages_it.properties | 21 ++ 8 files changed, 1031 insertions(+) create mode 100644 sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/builder/impl/ContributionDependencyBuilderImpl.java create mode 100644 sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionContentProcessor.java create mode 100644 sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionInfoProcessor.java create mode 100644 sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/DirectoryContributionScanner.java create mode 100644 sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/JarContributionScanner.java create mode 100644 sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor create mode 100644 sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/workspace-validation-messages.properties create mode 100644 sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/workspace-validation-messages_it.properties (limited to 'sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main') diff --git a/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/builder/impl/ContributionDependencyBuilderImpl.java b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/builder/impl/ContributionDependencyBuilderImpl.java new file mode 100644 index 0000000000..ebfc77f5e3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/builder/impl/ContributionDependencyBuilderImpl.java @@ -0,0 +1,145 @@ +/* + * 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.workspace.builder.impl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Logger; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.DefaultImport; +import org.apache.tuscany.sca.contribution.Export; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.resolver.DefaultImportModelResolver; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; +import org.apache.tuscany.sca.monitor.impl.ProblemImpl; +import org.apache.tuscany.sca.workspace.Workspace; +import org.apache.tuscany.sca.workspace.builder.ContributionDependencyBuilder; + +/** + * A contribution dependency builder. + * + * @version $Rev$ $Date$ + */ +public class ContributionDependencyBuilderImpl implements ContributionDependencyBuilder { + private static final Logger logger = Logger.getLogger(ContributionDependencyBuilderImpl.class.getName()); + + private Monitor monitor; + + /** + * Constructs a new ContributionDependencyBuilder. + */ + public ContributionDependencyBuilderImpl(Monitor monitor) { + + this.monitor = monitor; + } + + /** + * Calculate the set of contributions that a contribution depends on. + * @param contribution + * @param workspace + * @return + */ + public List buildContributionDependencies(Contribution contribution, Workspace workspace) { + List dependencies = new ArrayList(); + Set set = new HashSet(); + + dependencies.add(contribution); + set.add(contribution); + addContributionDependencies(contribution, workspace, dependencies, set); + + Collections.reverse(dependencies); + return dependencies; + } + + /** + * Analyze a contribution and add its dependencies to the given dependency set. + * @param contribution + * @param workspace + * @param dependencies + * @param set + */ + private void addContributionDependencies(Contribution contribution, Workspace workspace, List dependencies, Set set) { + + // Go through the contribution imports + for (Import import_: contribution.getImports()) { + boolean resolved = false; + + // Go through all contribution candidates and their exports + List matchingExports = new ArrayList(); + for (Contribution dependency: workspace.getContributions()) { + if (dependency == contribution) { + // Do not self import + continue; + } + for (Export export: dependency.getExports()) { + + // If an export from a contribution matches the import in hand + // add that contribution to the dependency set + if (import_.match(export)) { + resolved = true; + matchingExports.add(export); + + if (!set.contains(dependency)) { + set.add(dependency); + dependencies.add(dependency); + + // Now add the dependencies of that contribution + addContributionDependencies(dependency, workspace, dependencies, set); + } + } + } + } + + if (resolved) { + + // Initialize the import's model resolver with a delegating model + // resolver which will delegate to the matching exports + import_.setModelResolver(new DefaultImportModelResolver(matchingExports)); + + } else { + // Record import resolution issue + if (!(import_ instanceof DefaultImport)) { + warning("UnresolvedImport", import_, import_); + } + } + } + } + + /** + * Report a warning. + * + * @param problems + * @param message + * @param model + */ + private void warning(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(getClass().getName(), "workspace-validation-messages", Severity.WARNING, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionContentProcessor.java b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionContentProcessor.java new file mode 100644 index 0000000000..64ba61dfdf --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionContentProcessor.java @@ -0,0 +1,355 @@ +/* + * 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.workspace.processor.impl; + +import java.io.File; +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; + +import org.apache.tuscany.sca.assembly.Composite; +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.ContributionMetadata; +import org.apache.tuscany.sca.contribution.DefaultExport; +import org.apache.tuscany.sca.contribution.DefaultImport; +import org.apache.tuscany.sca.contribution.Export; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ExtensibleURLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.DefaultModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint; +import org.apache.tuscany.sca.contribution.scanner.ContributionScanner; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.definitions.SCADefinitions; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.IntentAttachPointType; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.workspace.scanner.impl.DirectoryContributionScanner; +import org.apache.tuscany.sca.workspace.scanner.impl.JarContributionScanner; + +/** + * URLArtifactProcessor that handles contribution files and the artifacts they contain + * and returns a contribution model. + * + * @version $Rev$ $Date$ + */ +public class ContributionContentProcessor implements URLArtifactProcessor{ + private ExtensionPointRegistry extensionPoints; + private ContributionFactory contributionFactory; + private ModelResolverExtensionPoint modelResolvers; + private ModelFactoryExtensionPoint modelFactories; + private URLArtifactProcessor artifactProcessor; + private StAXArtifactProcessor extensionProcessor; + private UtilityExtensionPoint utilities; + private Monitor monitor = null; + private ModelResolver policyDefinitionsResolver = null; + private List policyDefinitions = null; + + private static final String COMPOSITE_FILE_EXTN = ".composite"; + + public ContributionContentProcessor(ExtensionPointRegistry extensionPoints, Monitor monitor) { + this.extensionPoints = extensionPoints; + this.modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); + this.modelResolvers = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class); + hackResolvers(modelResolvers); + this.monitor = monitor; + URLArtifactProcessorExtensionPoint artifactProcessors = extensionPoints.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + this.artifactProcessor = new ExtensibleURLArtifactProcessor(artifactProcessors, this.monitor); + + // Get and initialize artifact processors + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + XMLInputFactory inputFactory = modelFactories.getFactory(XMLInputFactory.class); + XMLOutputFactory outputFactory = modelFactories.getFactory(XMLOutputFactory.class); + this.extensionProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory, monitor); + this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); + } + + public ContributionContentProcessor(ExtensionPointRegistry extensionPoints, Monitor monitor, + ModelResolver policyDefinitionsResolver, List policyDefinitions) { + this(extensionPoints, monitor); + this.policyDefinitionsResolver = policyDefinitionsResolver; + this.policyDefinitions = policyDefinitions; + } + + /* + public ContributionContentProcessor(ModelFactoryExtensionPoint modelFactories, ModelResolverExtensionPoint modelResolvers, + URLArtifactProcessor artifactProcessor, StAXArtifactProcessor extensionProcessor, Monitor monitor) { + this.modelFactories = modelFactories; + this.modelResolvers = modelResolvers; + hackResolvers(modelResolvers); + this.artifactProcessor = artifactProcessor; + this.extensionProcessor = extensionProcessor; + this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); + this.monitor = monitor; + } + */ + + public String getArtifactType() { + return ".contribution/content"; + } + + public Class getModelType() { + return Contribution.class; + } + + public Contribution read(URL parentURL, URI contributionURI, URL contributionURL) throws ContributionReadException { + + // Create contribution model + Contribution contribution = contributionFactory.createContribution(); + contribution.setURI(contributionURI.toString()); + contribution.setLocation(contributionURL.toString()); + ModelResolver modelResolver; + if (policyDefinitionsResolver != null) { + modelResolver = new ExtensibleModelResolver(contribution, extensionPoints, modelResolvers, modelFactories, policyDefinitionsResolver); + } else { + modelResolver = new ExtensibleModelResolver(contribution, extensionPoints); + } + contribution.setModelResolver(modelResolver); + contribution.setUnresolved(true); + + // Create a contribution scanner + ContributionScanner scanner; + if ("file".equals(contributionURL.getProtocol()) && new File(contributionURL.getFile()).isDirectory()) { + scanner = new DirectoryContributionScanner(); + } else { + scanner = new JarContributionScanner(); + } + + // Scan the contribution and list the artifacts contained in it + List artifacts = contribution.getArtifacts(); + boolean contributionMetadata = false; + List compositeURIs = new ArrayList(); + List artifactURIs = scanner.getArtifacts(contributionURL); + for (String artifactURI: artifactURIs) { + if (artifactURI.endsWith(COMPOSITE_FILE_EXTN)) { + // TUSCANY-3561: need to process the composites last + compositeURIs.add(artifactURI); + } else { + URL artifactURL = scanner.getArtifactURL(contributionURL, artifactURI); + + // Add the deployed artifact model to the contribution + Artifact artifact = this.contributionFactory.createArtifact(); + artifact.setURI(artifactURI); + artifact.setLocation(artifactURL.toString()); + artifacts.add(artifact); + modelResolver.addModel(artifact); + + // Read each artifact + Object model = artifactProcessor.read(contributionURL, URI.create(artifactURI), artifactURL); + if (model != null) { + artifact.setModel(model); + + // Add the loaded model to the model resolver + modelResolver.addModel(model); + + // Add policy definitions to the list of policy definitions + if (policyDefinitionsResolver != null) { + addPolicyDefinitions(model); + } + + // Merge contribution metadata into the contribution model + if (model instanceof ContributionMetadata) { + contributionMetadata = true; + ContributionMetadata c = (ContributionMetadata)model; + contribution.getImports().addAll(c.getImports()); + contribution.getExports().addAll(c.getExports()); + contribution.getDeployables().addAll(c.getDeployables()); + contribution.getExtensions().addAll(c.getExtensions()); + contribution.getAttributeExtensions().addAll(c.getAttributeExtensions()); + } + } + } + } + + // TUSCANY-3561: process the composites last + for (String artifactURI : compositeURIs) { + URL artifactURL = scanner.getArtifactURL(contributionURL, artifactURI); + + // Add the deployed artifact model to the contribution + Artifact artifact = this.contributionFactory.createArtifact(); + artifact.setURI(artifactURI); + artifact.setLocation(artifactURL.toString()); + artifacts.add(artifact); + modelResolver.addModel(artifact); + + // Read each artifact + Object model = artifactProcessor.read(contributionURL, URI.create(artifactURI), artifactURL); + if (model != null) { + artifact.setModel(model); + + // Add the loaded model to the model resolver + modelResolver.addModel(model); + } + } + + // If no sca-contribution.xml file was provided then just consider + // all composites in the contribution as deployables + if (!contributionMetadata) { + for (Artifact artifact: artifacts) { + if (artifact.getModel() instanceof Composite) { + contribution.getDeployables().add((Composite)artifact.getModel()); + } + } + + // Add default contribution import and export + DefaultImport defaultImport = contributionFactory.createDefaultImport(); + defaultImport.setModelResolver(new DefaultModelResolver()); + contribution.getImports().add(defaultImport); + DefaultExport defaultExport = contributionFactory.createDefaultExport(); + contribution.getExports().add(defaultExport); + } + + return contribution; + } + + public void resolve(Contribution contribution, ModelResolver resolver) throws ContributionResolveException { + + // Resolve the contribution model itself + ModelResolver contributionResolver = contribution.getModelResolver(); + contribution.setUnresolved(false); + contributionResolver.addModel(contribution); + + // Resolve imports and exports + for (Export export: contribution.getExports()) { + if (export instanceof DefaultExport) { + + // Initialize the default export's resolver + export.setModelResolver(contributionResolver); + + } else { + extensionProcessor.resolve(export, contributionResolver); + } + } + for (Import import_: contribution.getImports()) { + extensionProcessor.resolve(import_, contributionResolver); + } + + // Resolve all artifact models + List composites = new ArrayList(); + for (Artifact artifact : contribution.getArtifacts()) { + // TUSCANY-3561: need to process the composites last + if (artifact.getURI().endsWith(COMPOSITE_FILE_EXTN)) { + composites.add(artifact); + } else { + Object model = artifact.getModel(); + if (model != null) { + try { + artifactProcessor.resolve(model, contributionResolver); + } catch (ContributionResolveException e) { + throw e; + } catch (Exception e) { + throw new ContributionResolveException(e); + } + } + } + } + + // TUSCANY-3561: process the composites last + for (Artifact artifact : composites) { + Object model = artifact.getModel(); + if (model != null) { + try { + artifactProcessor.resolve(model, contributionResolver); + } catch (ContributionResolveException e) { + throw e; + } catch (Exception e) { + throw new ContributionResolveException(e); + } + } + } + + // Resolve deployable composites + List deployables = contribution.getDeployables(); + for (int i = 0, n = deployables.size(); i < n; i++) { + Composite deployable = deployables.get(i); + Composite resolved = (Composite)contributionResolver.resolveModel(Composite.class, deployable); + if (resolved != deployable) { + deployables.set(i, resolved); + } + } + } + + /** + * FIXME Temporary hack for testing the ClassLoaderModelResolver. + * + * @param modelResolvers + */ + private static void hackResolvers(ModelResolverExtensionPoint modelResolvers) { + Class resolverClass = modelResolvers.getResolver(ClassReference.class); + if (resolverClass==null || !resolverClass.getName().equals("org.apache.tuscany.sca.contribution.java.impl.ClassLoaderModelResolver")) { + try { + Class loaderResolverClass = Class.forName("org.apache.tuscany.sca.contribution.java.impl.ClassLoaderModelResolver", true, ContributionContentProcessor.class.getClassLoader()); + modelResolvers.addResolver(ClassReference.class, (Class)loaderResolverClass); + } catch (ClassNotFoundException e) { + } + } + } + + /** + * The following code was copied from ContributionServiceImpl to fix TUSCANY-3171 + * + * @param model + */ + private void addPolicyDefinitions(Object model) { + + // Add policy definitions to the list of policy definitions + if (model instanceof SCADefinitions) { + policyDefinitions.add((SCADefinitions)model); + + SCADefinitions definitions = (SCADefinitions)model; + for (Intent intent : definitions.getPolicyIntents() ) { + policyDefinitionsResolver.addModel(intent); + } + + for (PolicySet policySet : definitions.getPolicySets() ) { + policyDefinitionsResolver.addModel(policySet); + } + + for (IntentAttachPointType attachPointType : definitions.getBindingTypes() ) { + policyDefinitionsResolver.addModel(attachPointType); + } + + for (IntentAttachPointType attachPointType : definitions.getImplementationTypes() ) { + policyDefinitionsResolver.addModel(attachPointType); + } + for (Object binding : definitions.getBindings() ) { + policyDefinitionsResolver.addModel(binding); + } + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionInfoProcessor.java b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionInfoProcessor.java new file mode 100644 index 0000000000..d7a05ff532 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionInfoProcessor.java @@ -0,0 +1,240 @@ +/* + * 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.workspace.processor.impl; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; +import java.util.List; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.ContributionMetadata; +import org.apache.tuscany.sca.contribution.DefaultExport; +import org.apache.tuscany.sca.contribution.DefaultImport; +import org.apache.tuscany.sca.contribution.Export; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ExtensibleURLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.DefaultModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint; +import org.apache.tuscany.sca.contribution.scanner.ContributionScanner; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.workspace.scanner.impl.DirectoryContributionScanner; +import org.apache.tuscany.sca.workspace.scanner.impl.JarContributionScanner; + +/** + * URLArtifactProcessor that handles contribution files and returns a contribution + * info model. + * + * @version $Rev$ $Date$ + */ +public class ContributionInfoProcessor implements URLArtifactProcessor{ + private ExtensionPointRegistry extensionPoints; + private ContributionFactory contributionFactory; + private ModelResolverExtensionPoint modelResolvers; + private ModelFactoryExtensionPoint modelFactories; + private URLArtifactProcessorExtensionPoint artifactProcessors; + private URLArtifactProcessor artifactProcessor; + private StAXArtifactProcessor extensionProcessor; + + public ContributionInfoProcessor(ExtensionPointRegistry extensionPoints, Monitor monitor) { + this.extensionPoints = extensionPoints; + this.modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); + this.modelResolvers = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class); + hackResolvers(modelResolvers); + URLArtifactProcessorExtensionPoint artifactProcessors = extensionPoints.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + this.artifactProcessors = artifactProcessors; + this.artifactProcessor = new ExtensibleURLArtifactProcessor(artifactProcessors, monitor); + // Get and initialize artifact processors + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + XMLInputFactory inputFactory = modelFactories.getFactory(XMLInputFactory.class); + XMLOutputFactory outputFactory = modelFactories.getFactory(XMLOutputFactory.class); + this.extensionProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory, monitor); + this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); + } + + /* + public ContributionInfoProcessor(ModelFactoryExtensionPoint modelFactories, ModelResolverExtensionPoint modelResolvers, URLArtifactProcessor artifactProcessor) { + this.modelFactories = modelFactories; + this.modelResolvers = modelResolvers; + hackResolvers(modelResolvers); + this.artifactProcessor = artifactProcessor; + this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); + } + */ + + public String getArtifactType() { + return ".contribution/info"; + } + + public Class getModelType() { + return null; + } + + public Contribution read(URL parentURL, URI contributionURI, URL contributionURL) throws ContributionReadException { + + // Create contribution model + Contribution contribution = contributionFactory.createContribution(); + contribution.setURI(contributionURI.toString()); + contribution.setLocation(contributionURL.toString()); + ModelResolver modelResolver = new ExtensibleModelResolver(contribution, extensionPoints); + contribution.setModelResolver(modelResolver); + contribution.setUnresolved(true); + + // Create a contribution scanner + ContributionScanner scanner; + if ("file".equals(contributionURL.getProtocol()) && new File(contributionURL.getFile()).isDirectory()) { + scanner = new DirectoryContributionScanner(); + } else { + scanner = new JarContributionScanner(); + } + + // Read generated and user sca-contribution.xml files + boolean contributionMetadata = false; + for (String path: new String[]{ + Contribution.SCA_CONTRIBUTION_GENERATED_META, + Contribution.SCA_CONTRIBUTION_META}) { + URL url = scanner.getArtifactURL(contributionURL, path); + try { + // Check if the file actually exists before trying to read it + URLConnection connection = url.openConnection(); + connection.setUseCaches(false); + InputStream is = connection.getInputStream(); + is.close(); + } catch (IOException e) { + continue; + } + contributionMetadata = true; + + // Read the sca-contribution.xml file + ContributionMetadata c = (ContributionMetadata)artifactProcessor.read(contributionURL, URI.create(path), url); + contribution.getImports().addAll(c.getImports()); + contribution.getExports().addAll(c.getExports()); + contribution.getDeployables().addAll(c.getDeployables()); + contribution.getExtensions().addAll(c.getExtensions()); + contribution.getAttributeExtensions().addAll(c.getAttributeExtensions()); + } + + // If no sca-contribution.xml file was provided then consider + // all composites in the contribution as deployables, and also + // read any files that are explicitly asssigned artifact processors + // as they are likely to provide relevant metadata info + if (!contributionMetadata) { + List artifactURIs; + try { + artifactURIs = scanner.getArtifacts(contributionURL); + } catch (ContributionReadException e) { + artifactURIs = null; + } + if (artifactURIs != null) { + for (String artifactURI: artifactURIs) { + boolean read = false; + if (artifactURI.endsWith(".composite")) { + read = true; + } else { + int s= artifactURI.lastIndexOf("/"); + String fileName = artifactURI.substring(s + 1); + if (artifactProcessors.getProcessor(fileName) != null) { + read = true; + } + } + if (read) { + URL artifactURL = scanner.getArtifactURL(contributionURL, artifactURI); + + // Read each artifact + Object model = artifactProcessor.read(contributionURL, URI.create(artifactURI), artifactURL); + + // In the absence of more info, consider all composites as deployable + if (model instanceof Composite) { + contribution.getDeployables().add((Composite)model); + } + } + } + } + + // Add default contribution import and export + DefaultImport defaultImport = contributionFactory.createDefaultImport(); + defaultImport.setModelResolver(new DefaultModelResolver()); + contribution.getImports().add(defaultImport); + DefaultExport defaultExport = contributionFactory.createDefaultExport(); + contribution.getExports().add(defaultExport); + } + + return contribution; + } + + public void resolve(Contribution contribution, ModelResolver resolver) throws ContributionResolveException { + + // Mark the contribution model resolved + ModelResolver contributionResolver = contribution.getModelResolver(); + contribution.setUnresolved(false); + contributionResolver.addModel(contribution); + + // Resolve imports and exports + for (Export export: contribution.getExports()) { + if (export instanceof DefaultExport) { + + // Initialize the default export's resolver + export.setModelResolver(contributionResolver); + + } else { + extensionProcessor.resolve(export, contributionResolver); + } + } + for (Import import_: contribution.getImports()) { + extensionProcessor.resolve(import_, contributionResolver); + } + + } + + /** + * FIXME Temporary hack for testing the ClassLoaderModelResolver. + * + * @param modelResolvers + */ + private static void hackResolvers(ModelResolverExtensionPoint modelResolvers) { + Class resolverClass = modelResolvers.getResolver(ClassReference.class); + if (resolverClass==null || !resolverClass.getName().equals("org.apache.tuscany.sca.contribution.java.impl.ClassLoaderModelResolver")) { + try { + Class loaderResolverClass = Class.forName("org.apache.tuscany.sca.contribution.java.impl.ClassLoaderModelResolver", true, ContributionContentProcessor.class.getClassLoader()); + modelResolvers.addResolver(ClassReference.class, (Class)loaderResolverClass); + } catch (ClassNotFoundException e) { + } + } + } +} diff --git a/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/DirectoryContributionScanner.java b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/DirectoryContributionScanner.java new file mode 100644 index 0000000000..45ba8c0609 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/DirectoryContributionScanner.java @@ -0,0 +1,107 @@ +/* + * 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.workspace.scanner.impl; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.contribution.scanner.ContributionScanner; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; + +/** + * Folder contribution processor. + * + * @version $Rev$ $Date$ + */ +public class DirectoryContributionScanner implements ContributionScanner { + + public DirectoryContributionScanner() { + } + + public String getContributionType() { + return "application/vnd.tuscany.folder"; + } + + public URL getArtifactURL(URL contributionURL, String artifact) throws ContributionReadException { + File directory = directory(contributionURL); + File file = new File(directory, artifact); + try { + return file.toURI().toURL(); + } catch (MalformedURLException e) { + throw new ContributionReadException(e); + } + } + + public List getArtifacts(URL contributionURL) throws ContributionReadException { + File directory = directory(contributionURL); + List artifacts = new ArrayList(); + try { + traverse(artifacts, directory, directory); + } catch (IOException e) { + throw new ContributionReadException(e); + } + return artifacts; + } + + /** + * Recursively traverse a root directory + * + * @param fileList + * @param file + * @param root + * @throws IOException + */ + private static void traverse(List fileList, File file, File root) throws IOException { + if (file.isFile()) { + fileList.add(root.toURI().relativize(file.toURI()).toString()); + } else if (file.isDirectory()) { + String uri = root.toURI().relativize(file.toURI()).toString(); + if (uri.endsWith("/")) { + uri = uri.substring(0, uri.length() - 1); + } + fileList.add(uri); + + File[] files = file.listFiles(); + for (File f: files) { + if (!f.getName().startsWith(".")) { + traverse(fileList, f, root); + } + } + } + } + + private static File directory(URL url) throws ContributionReadException { + File file; + try { + file = new File(url.toURI()); + } catch (URISyntaxException e) { + throw new ContributionReadException(e); + } + if (!file.exists() || !file.isDirectory()) { + throw new ContributionReadException(url.toString()); + } + return file; + } +} diff --git a/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/JarContributionScanner.java b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/JarContributionScanner.java new file mode 100644 index 0000000000..9dd1b3a15b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/JarContributionScanner.java @@ -0,0 +1,121 @@ +/* + * 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.workspace.scanner.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; + +import org.apache.tuscany.sca.contribution.scanner.ContributionScanner; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; + +/** + * JAR Contribution processor. + * + * @version $Rev$ $Date$ + */ +public class JarContributionScanner implements ContributionScanner { + + public JarContributionScanner() { + } + + public String getContributionType() { + return "application/x-compressed"; + } + + public URL getArtifactURL(URL contributionURL, String artifact) throws ContributionReadException { + try { + URL url; + if (contributionURL.toString().startsWith("jar:")) { + url = new URL(contributionURL, artifact.toString()); + } else { + url = new URL("jar:" + contributionURL.toExternalForm() + "!/" + artifact); + } + return url; + } catch (MalformedURLException e) { + throw new ContributionReadException(e); + } + } + + public List getArtifacts(URL contributionURL) throws ContributionReadException { + + // Assume the URL references a JAR file + try { + URLConnection connection = contributionURL.openConnection(); + connection.setUseCaches(false); + JarInputStream jar = new JarInputStream(connection.getInputStream()); + try { + Set names = new HashSet(); + while (true) { + JarEntry entry = jar.getNextJarEntry(); + if (entry == null) { + // EOF + break; + } + + String name = entry.getName(); + if (name.length() != 0 && !name.startsWith(".")) { + + // Trim trailing / + if (name.endsWith("/")) { + name = name.substring(0, name.length() - 1); + } + + // Add the entry name + if (!names.contains(name)) { + names.add(name); + + // Add parent folder names to the list too + for (;;) { + int s = name.lastIndexOf('/'); + if (s == -1) { + name = ""; + } else { + name = name.substring(0, s); + } + if (name.length() != 0 && !names.contains(name)) { + names.add(name); + } else { + break; + } + } + } + } + } + + // Return list of URIs + List artifacts = new ArrayList(names); + return artifacts; + + } finally { + jar.close(); + } + } catch (IOException e) { + throw new ContributionReadException(e); + } + } +} diff --git a/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor new file mode 100644 index 0000000000..41d0ebe27d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor @@ -0,0 +1,20 @@ +# 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. + +# Implementation class for the artifact processor extension +org.apache.tuscany.sca.workspace.processor.impl.ContributionContentProcessor;type=.contribution/content,model=org.apache.tuscany.sca.contribution.Contribution +org.apache.tuscany.sca.workspace.processor.impl.ContributionInfoProcessor;type=.contribution/info diff --git a/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/workspace-validation-messages.properties b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/workspace-validation-messages.properties new file mode 100644 index 0000000000..0c07a3a30f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/workspace-validation-messages.properties @@ -0,0 +1,22 @@ +# +# +# 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. +# +# + +UnresolvedImport = Unresolved import: Import = {0} diff --git a/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/workspace-validation-messages_it.properties b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/workspace-validation-messages_it.properties new file mode 100644 index 0000000000..2e8326c062 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6.2/modules/workspace-impl/src/main/resources/workspace-validation-messages_it.properties @@ -0,0 +1,21 @@ +# +# +# 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. +# +# + -- cgit v1.2.3