From 23297fa3b1f66d74325c8352cb8f5aae599a3090 Mon Sep 17 00:00:00 2001 From: antelder Date: Mon, 11 Aug 2008 07:29:53 +0000 Subject: Start branch for 1.3.1, the only changes in this commit are for the version git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@684661 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/ContributionDependencyBuilderImpl.java | 145 +++++++++++++ .../impl/ContributionContentProcessor.java | 230 +++++++++++++++++++++ .../processor/impl/ContributionInfoProcessor.java | 225 ++++++++++++++++++++ .../scanner/impl/DirectoryContributionScanner.java | 107 ++++++++++ .../scanner/impl/JarContributionScanner.java | 121 +++++++++++ 5 files changed, 828 insertions(+) create mode 100644 branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/builder/impl/ContributionDependencyBuilderImpl.java create mode 100644 branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionContentProcessor.java create mode 100644 branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionInfoProcessor.java create mode 100644 branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/DirectoryContributionScanner.java create mode 100644 branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/JarContributionScanner.java (limited to 'branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org') diff --git a/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/builder/impl/ContributionDependencyBuilderImpl.java b/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/builder/impl/ContributionDependencyBuilderImpl.java new file mode 100644 index 0000000000..016c764d47 --- /dev/null +++ b/branches/sca-java-1.3.1/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.assembly.builder.impl.ProblemImpl; +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.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/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionContentProcessor.java b/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionContentProcessor.java new file mode 100644 index 0000000000..880e95480e --- /dev/null +++ b/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionContentProcessor.java @@ -0,0 +1,230 @@ +/* + * 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.List; + +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.ExtensibleURLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +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.core.UtilityExtensionPoint; +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 the artifacts they contain + * and returns a contribution model. + * + * @version $Rev$ $Date$ + */ +public class ContributionContentProcessor implements URLArtifactProcessor{ + private ContributionFactory contributionFactory; + private ModelResolverExtensionPoint modelResolvers; + private ModelFactoryExtensionPoint modelFactories; + private URLArtifactProcessor artifactProcessor; + private StAXArtifactProcessor extensionProcessor; + private UtilityExtensionPoint utilities; + private Monitor monitor = null; + + public ContributionContentProcessor(ExtensionPointRegistry extensionPoints, StAXArtifactProcessor extensionProcessor, Monitor monitor) { + 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); + this.extensionProcessor = extensionProcessor; + this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); + } + + 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 = new ExtensibleModelResolver(contribution, modelResolvers, modelFactories); + 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 artifactURIs = scanner.getArtifacts(contributionURL); + for (String artifactURI: artifactURIs) { + 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); + + // 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()); + } + } + } + + // 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 + for (Artifact artifact : contribution.getArtifacts()) { + Object model = artifact.getModel(); + if (model != null) { + try { + artifactProcessor.resolve(model, contributionResolver); + } catch (Exception e) { + //FIXME this shouldn't happen + } + } + } + + // 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) { + } + } + } +} diff --git a/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionInfoProcessor.java b/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionInfoProcessor.java new file mode 100644 index 0000000000..6ef0fa753d --- /dev/null +++ b/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/processor/impl/ContributionInfoProcessor.java @@ -0,0 +1,225 @@ +/* + * 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 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.ExtensibleURLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +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 ContributionFactory contributionFactory; + private ModelResolverExtensionPoint modelResolvers; + private ModelFactoryExtensionPoint modelFactories; + private URLArtifactProcessorExtensionPoint artifactProcessors; + private URLArtifactProcessor artifactProcessor; + private StAXArtifactProcessor extensionProcessor; + + public ContributionInfoProcessor(ExtensionPointRegistry extensionPoints, StAXArtifactProcessor extensionProcessor, Monitor monitor) { + 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); + this.extensionProcessor = extensionProcessor; + 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, modelResolvers, modelFactories); + 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()); + } + + // 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/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/DirectoryContributionScanner.java b/branches/sca-java-1.3.1/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/branches/sca-java-1.3.1/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/branches/sca-java-1.3.1/modules/workspace-impl/src/main/java/org/apache/tuscany/sca/workspace/scanner/impl/JarContributionScanner.java b/branches/sca-java-1.3.1/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/branches/sca-java-1.3.1/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); + } + } +} -- cgit v1.2.3