From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/FolderContributionProcessor.java | 122 ++++++++++++++++++++ .../impl/InvalidFolderContributionException.java | 38 ++++++ .../InvalidFolderContributionURIException.java | 38 ++++++ .../processor/impl/JarContributionProcessor.java | 127 +++++++++++++++++++++ 4 files changed, 325 insertions(+) create mode 100644 branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/FolderContributionProcessor.java create mode 100644 branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/InvalidFolderContributionException.java create mode 100644 branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/InvalidFolderContributionURIException.java create mode 100644 branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/JarContributionProcessor.java (limited to 'branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor') diff --git a/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/FolderContributionProcessor.java b/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/FolderContributionProcessor.java new file mode 100644 index 0000000000..8d2d91c375 --- /dev/null +++ b/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/FolderContributionProcessor.java @@ -0,0 +1,122 @@ +/* + * 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.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.contribution.ContentType; +import org.apache.tuscany.sca.contribution.processor.PackageProcessor; +import org.apache.tuscany.sca.contribution.service.ContributionException; +import org.apache.tuscany.sca.contribution.service.util.FileHelper; + +/** + * Folder contribution package processor + * + * @version $Rev$ $Date$ + */ +public class FolderContributionProcessor implements PackageProcessor { + /** + * Package-type that this package processor can handle + */ + public static final String PACKAGE_TYPE = ContentType.FOLDER; + + public FolderContributionProcessor() { + } + + public String getPackageType() { + return PACKAGE_TYPE; + } + + /** + * Recursively traverse a root directory + * + * @param fileList + * @param file + * @throws IOException + */ + private void traverse(List fileList, File file, File root) throws IOException { + if (file.isFile()) { + fileList.add(root.toURI().relativize(file.toURI())); + + } else if (file.isDirectory()) { + // FIXME: Maybe we should externalize it as a property + // Regular expression to exclude .xxx files + + String uri = root.toURI().relativize(file.toURI()).toString(); + if (uri.endsWith("/")) { + uri = uri.substring(0, uri.length() - 1); + } + fileList.add(URI.create(uri)); + + //FIXME Do we really need to use a regexp here to filter out + // file names that start one or two dots? + File[] files = file.listFiles(FileHelper.getFileFilter("[^\u002e].*", true)); + for (int i = 0; i < files.length; i++) { + traverse(fileList, files[i], root); + } + } + } + + public URL getArtifactURL(URL sourceURL, URI artifact) throws MalformedURLException { + return new URL(sourceURL, artifact.toString()); + } + + /** + * Get a list of artifact URI from the folder + * + * @return The list of artifact URI for the folder + * @throws IOException + */ + public List getArtifacts(URL packageSourceURL, InputStream inputStream) throws ContributionException, + IOException { + if (packageSourceURL == null) { + throw new IllegalArgumentException("Invalid null package source URL."); + } + + List artifacts = new ArrayList(); + + // Assume the root is a jar file + File rootFolder; + + try { + rootFolder = new File(packageSourceURL.toURI()); + if (rootFolder.isDirectory()) { + if (!rootFolder.exists()) { + throw new InvalidFolderContributionException(rootFolder.getAbsolutePath()); + } + + this.traverse(artifacts, rootFolder, rootFolder); + } + + } catch (URISyntaxException e) { + throw new InvalidFolderContributionURIException(packageSourceURL.toExternalForm(), e); + } + + return artifacts; + } +} diff --git a/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/InvalidFolderContributionException.java b/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/InvalidFolderContributionException.java new file mode 100644 index 0000000000..268ba401c1 --- /dev/null +++ b/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/InvalidFolderContributionException.java @@ -0,0 +1,38 @@ +/* + * 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 org.apache.tuscany.sca.contribution.service.ContributionException; + +/** + * Exception that indicates that the supplied XML Document invalid. + * + */ +public class InvalidFolderContributionException extends ContributionException { + + private static final long serialVersionUID = 1564255850052593282L; + + protected InvalidFolderContributionException(String componentDefinitionLocatoin) { + super(componentDefinitionLocatoin); + } + + protected InvalidFolderContributionException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/InvalidFolderContributionURIException.java b/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/InvalidFolderContributionURIException.java new file mode 100644 index 0000000000..e018f90b06 --- /dev/null +++ b/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/InvalidFolderContributionURIException.java @@ -0,0 +1,38 @@ +/* + * 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 org.apache.tuscany.sca.contribution.service.ContributionException; + +/** + * Exception that indicates that the supplied XML Document invalid. + * + */ +public class InvalidFolderContributionURIException extends ContributionException { + + private static final long serialVersionUID = 1564255850052593282L; + + protected InvalidFolderContributionURIException(String componentDefinitionLocatoin) { + super(componentDefinitionLocatoin); + } + + protected InvalidFolderContributionURIException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/JarContributionProcessor.java b/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/JarContributionProcessor.java new file mode 100644 index 0000000000..9015dc5f7f --- /dev/null +++ b/branches/sca-java-1.0.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/JarContributionProcessor.java @@ -0,0 +1,127 @@ +/* + * 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.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +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.ContentType; +import org.apache.tuscany.sca.contribution.processor.PackageProcessor; +import org.apache.tuscany.sca.contribution.service.ContributionException; + +/** + * Jar Contribution package processor + * + * @version $Rev$ $Date$ + */ +public class JarContributionProcessor implements PackageProcessor { + /** + * Package-type that this package processor can handle + */ + public static final String PACKAGE_TYPE = ContentType.JAR; + + public JarContributionProcessor() { + } + + public String getPackageType() { + return PACKAGE_TYPE; + } + + public URL getArtifactURL(URL sourceURL, URI artifact) throws MalformedURLException { + if (sourceURL.toString().startsWith("jar:")) { + return new URL(sourceURL, artifact.toString()); + } else { + return new URL("jar:" + sourceURL.toExternalForm() + "!/" + artifact); + } + } + + public List getArtifacts(URL packageSourceURL, InputStream inputStream) throws ContributionException, + IOException { + if (packageSourceURL == null) { + throw new IllegalArgumentException("Invalid null package source URL."); + } + + if (inputStream == null) { + throw new IllegalArgumentException("Invalid null source inputstream."); + } + + // Assume the root is a jar file + JarInputStream jar = new JarInputStream(inputStream); + try { + Set names = new HashSet(); + while (true) { + JarEntry entry = jar.getNextJarEntry(); + if (entry == null) { + // EOF + break; + } + + // FIXME: Maybe we should externalize the filter as a property + String name = entry.getName(); + if (!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 (!names.contains(name)) { + names.add(name); + } else { + break; + } + } + } + } + } + + // Return list of URIs + List artifacts = new ArrayList(); + for (String name: names) { + artifacts.add(URI.create(name)); + } + return artifacts; + + } finally { + jar.close(); + } + } +} -- cgit v1.2.3