diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-11 09:33:43 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-11 09:33:43 +0000 |
commit | eae8b2a2e854c5de44bea5be69e6827825f2e2de (patch) | |
tree | 508c02b01a7f6cf214f284a9c34f62e9e0879ace | |
parent | 3daeedfc0ede058c6077f6655bd2ee693c922542 (diff) |
Copy the contribution service classes from the workspace modules to contribution, they can now be part of the split into contribution-xxx modules thats going on
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@773511 13f79535-47bb-0310-9956-ffa450edef68
5 files changed, 714 insertions, 0 deletions
diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java new file mode 100644 index 0000000000..2f39a6d007 --- /dev/null +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java @@ -0,0 +1,257 @@ +/* + * 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.contribution.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.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ExtendedURLArtifactProcessor; +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.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.scanner.ContributionScannerExtensionPoint; +import org.apache.tuscany.sca.contribution.scanner.impl.DirectoryContributionScanner; +import org.apache.tuscany.sca.contribution.scanner.impl.JarContributionScanner; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * URLArtifactProcessor that handles contribution files and the artifacts they contain + * and returns a contribution model. + * + * @version $Rev$ $Date$ + */ +public class ContributionContentProcessor implements ExtendedURLArtifactProcessor<Contribution>{ + private ContributionFactory contributionFactory; + private ModelResolverExtensionPoint modelResolvers; + private FactoryExtensionPoint modelFactories; + private URLArtifactProcessor<Object> artifactProcessor; + private StAXArtifactProcessor<Object> extensionProcessor; + // private UtilityExtensionPoint utilities; + private Monitor monitor; + private ContributionScannerExtensionPoint scanners; + // Marks pre-resolve phase completed + private boolean preResolved = false; + + public ContributionContentProcessor(ExtensionPointRegistry extensionPoints, StAXArtifactProcessor<Object> extensionProcessor, Monitor monitor) { + this.modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + this.modelResolvers = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class); + 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); + this.scanners = extensionPoints.getExtensionPoint(ContributionScannerExtensionPoint.class); + } + + /* + public ContributionContentProcessor(FactoryExtensionPoint modelFactories, ModelResolverExtensionPoint modelResolvers, + URLArtifactProcessor<Object> artifactProcessor, StAXArtifactProcessor<Object> 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<Contribution> 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 = scanners.getContributionScanner(contributionURL.getProtocol()); + if (scanner == null) { + 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<Artifact> artifacts = contribution.getArtifacts(); + boolean contributionMetadata = false; + List<String> 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()); + contribution.getExtensions().addAll(c.getExtensions()); + contribution.getAttributeExtensions().addAll(c.getAttributeExtensions()); + } + } + } + + // 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; + } + + /** + * A pre-resolution step, which is required for Contributions to handle the resolution of imports and exports so that + * at resolve time, imports can be followed to exports and anything exported that is required can be resolved on demand + * without the need to have already resolved the whole of the Contribution containing the export + * @param contribution - the Contribution + * @param resolver - the Resolver to use + * @throws ContributionResolveException + */ + public void preResolve(Contribution contribution, ModelResolver resolver) throws ContributionResolveException { + // Resolve the contribution model itself + ModelResolver contributionResolver = contribution.getModelResolver(); + contribution.setUnresolved(false); + contributionResolver.addModel(contribution); + + // Resolve Exports + resolveExports(contribution, contributionResolver); + // Resolve Imports + resolveImports(contribution, contributionResolver); + + preResolved = true; + } // end method preResolve + + public void resolve(Contribution contribution, ModelResolver resolver) throws ContributionResolveException { + + if( !preResolved ) preResolve( contribution, resolver); + ModelResolver contributionResolver = contribution.getModelResolver(); + + // Resolve all artifact models + for (Artifact artifact : contribution.getArtifacts()) { + Object model = artifact.getModel(); + if (model != null) { + try { + artifactProcessor.resolve(model, contributionResolver); + } catch (Throwable e) { + throw new ContributionResolveException(e); + } + } + } + + // Resolve deployable composites + List<Composite> 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); + } + } // end for + } // end method resolve + + /** + * Resolves the Exports of the contribution + * @param contribution + * @param resolver + */ + private void resolveExports(Contribution contribution, ModelResolver resolver) throws ContributionResolveException { + for (Export export: contribution.getExports()) { + if (export instanceof DefaultExport) { + // Initialize the default export's resolver + export.setModelResolver(resolver); + } else { + extensionProcessor.resolve(export, resolver); + } // end if + } // end for + + } // end method resolveExports + + /** + * Resolves the Imports of the contribution + * @param contribution + * @param resolver + */ + private void resolveImports(Contribution contribution, ModelResolver resolver) throws ContributionResolveException { + for (Import import_: contribution.getImports()) { + extensionProcessor.resolve(import_, resolver); + } // end for + } // end method resolveImports + +} // end class ContributionContentProcessor diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java new file mode 100644 index 0000000000..2984b53a06 --- /dev/null +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java @@ -0,0 +1,227 @@ +/* + * 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.contribution.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.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +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.scanner.impl.DirectoryContributionScanner; +import org.apache.tuscany.sca.contribution.scanner.impl.JarContributionScanner; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * URLArtifactProcessor that handles contribution files and returns a contribution + * info model. + * + * @version $Rev$ $Date$ + */ +public class ContributionInfoProcessor implements URLArtifactProcessor<Contribution>{ + private ContributionFactory contributionFactory; + private ModelResolverExtensionPoint modelResolvers; + private FactoryExtensionPoint modelFactories; + private URLArtifactProcessorExtensionPoint artifactProcessors; + private URLArtifactProcessor<Object> artifactProcessor; + private StAXArtifactProcessor<Object> extensionProcessor; + + public ContributionInfoProcessor(ExtensionPointRegistry extensionPoints, StAXArtifactProcessor<Object> extensionProcessor, Monitor monitor) { + this.modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.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(FactoryExtensionPoint modelFactories, ModelResolverExtensionPoint modelResolvers, URLArtifactProcessor<Object> 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<Contribution> 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()); + 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<String> 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<? extends ModelResolver>)loaderResolverClass); + } catch (ClassNotFoundException e) { + } + } + } +} diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/DirectoryContributionScanner.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/DirectoryContributionScanner.java new file mode 100644 index 0000000000..16838a95a5 --- /dev/null +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/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.contribution.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.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.scanner.ContributionScanner; + +/** + * 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<String> getArtifacts(URL contributionURL) throws ContributionReadException { + File directory = directory(contributionURL); + List<String> artifacts = new ArrayList<String>(); + 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<String> 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/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java new file mode 100644 index 0000000000..efb3e02b5a --- /dev/null +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/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.contribution.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.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.scanner.ContributionScanner; + +/** + * 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<String> 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<String> names = new HashSet<String>(); + 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<String> artifacts = new ArrayList<String>(names); + return artifacts; + + } finally { + jar.close(); + } + } catch (IOException e) { + throw new ContributionReadException(e); + } + } +} diff --git a/java/sca/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor b/java/sca/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor index cdaca4d786..560bf9abcb 100644 --- a/java/sca/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor +++ b/java/sca/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor @@ -18,3 +18,5 @@ # Implementation class for the artifact processor extension org.apache.tuscany.sca.contribution.xml.ContributionMetadataDocumentProcessor;type=/META-INF/sca-contribution.xml,model=org.apache.tuscany.sca.contribution.ContributionMetadata org.apache.tuscany.sca.contribution.xml.ContributionGeneratedMetadataDocumentProcessor;type=/META-INF/sca-contribution-generated.xml +org.apache.tuscany.sca.contribution.processor.impl.ContributionContentProcessor;type=.contribution/content,model=org.apache.tuscany.sca.contribution.Contribution +org.apache.tuscany.sca.contribution.processor.impl.ContributionInfoProcessor;type=.contribution/info |