You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/03/01 01:08:42 UTC
svn commit: r513075 - in
/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src:
main/java/org/apache/tuscany/core/bootstrap/
main/java/org/apache/tuscany/core/services/deployment/
main/java/org/apache/tuscany/core/services/deployment/con...
Author: rfeng
Date: Wed Feb 28 16:08:41 2007
New Revision: 513075
URL: http://svn.apache.org/viewvc?view=rev&rev=513075
Log:
[sca-integration-branch] Make sure input streams are closed, fix artifact URI, and content types
Modified:
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/bootstrap/DefaultSCAContainer.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImpl.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionServiceImpl.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/FolderContributionProcessor.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/JarContributionProcessor.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/ScdlContributionProcessor.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/FileHelper.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOHelper.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImplTestCase.java
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/bootstrap/DefaultSCAContainer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/bootstrap/DefaultSCAContainer.java?view=diff&rev=513075&r1=513074&r2=513075
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/bootstrap/DefaultSCAContainer.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/bootstrap/DefaultSCAContainer.java Wed Feb 28 16:08:41 2007
@@ -142,7 +142,7 @@
// lresende - contribution
URL contributionLocation = getContributionLocation(applicationSCDL, compositePath);
URI contributionId = this.contributionService.contribute(contributionLocation, false);
- URI compositeDefinitionId = new URI(contributionId + FileHelper.getName(applicationSCDL.toString()));
+ URI compositeDefinitionId = contributionId.resolve(compositePath);
component =
(CompositeComponent)this.assemblyService.addCompositeToDomain(contributionId, compositeDefinitionId);
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImpl.java?view=diff&rev=513075&r1=513074&r2=513075
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImpl.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImpl.java Wed Feb 28 16:08:41 2007
@@ -50,11 +50,11 @@
* extensions
*/
private void init() {
- contentTypeRegistry.put("SCDL", "application/v.tuscany.scdl");
- contentTypeRegistry.put("COMPOSITE", "application/v.tuscany.scdl");
- contentTypeRegistry.put("WSDL", "application/v.tuscany.wsdl");
+ contentTypeRegistry.put("SCDL", "application/vnd.tuscany.scdl");
+ contentTypeRegistry.put("COMPOSITE", "application/vnd.tuscany.scdl");
+ contentTypeRegistry.put("WSDL", "application/vnd.tuscany.wsdl");
contentTypeRegistry.put("JAR", "application/x-compressed");
- contentTypeRegistry.put("FOLDER", "application/v.tuscany.folder");
+ contentTypeRegistry.put("FOLDER", "application/vnd.tuscany.folder");
}
protected String resolveContentyTypeByExtension(URL resourceURL) {
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionServiceImpl.java?view=diff&rev=513075&r1=513074&r2=513075
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionServiceImpl.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionServiceImpl.java Wed Feb 28 16:08:41 2007
@@ -132,7 +132,7 @@
public void addDeploymentComposite(URI contribution, Object composite) {
CompositeComponentType model = (CompositeComponentType)composite;
- URI compositeURI = URI.create(contribution.toString() + "/" + model.getName() + ".composite");
+ URI compositeURI = contribution.resolve(model.getName() + ".composite");
DeployedArtifact artifact = new DeployedArtifact(compositeURI);
// FIXME: the namespace should be from the CompositeComponentType model
artifact.addModelObject(composite.getClass(), null, composite);
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/FolderContributionProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/FolderContributionProcessor.java?view=diff&rev=513075&r1=513074&r2=513075
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/FolderContributionProcessor.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/FolderContributionProcessor.java Wed Feb 28 16:08:41 2007
@@ -30,6 +30,7 @@
import org.apache.tuscany.core.services.deployment.ContentTypeDescriberImpl;
import org.apache.tuscany.core.util.FileHelper;
+import org.apache.tuscany.core.util.IOHelper;
import org.apache.tuscany.host.deployment.DeploymentException;
import org.apache.tuscany.spi.deployer.ContentTypeDescriber;
import org.apache.tuscany.spi.deployer.ContributionProcessor;
@@ -38,16 +39,16 @@
import org.apache.tuscany.spi.model.DeployedArtifact;
public class FolderContributionProcessor extends ContributionProcessorExtension implements ContributionProcessor {
- public static final String CONTENT_TYPE = "application/v.tuscany.folder";
+ public static final String CONTENT_TYPE = "application/vnd.tuscany.folder";
@Override
public String getContentType() {
return CONTENT_TYPE;
}
-
/**
* Recursively traverse a root directory
+ *
* @param fileList
* @param root
* @throws IOException
@@ -55,28 +56,29 @@
private void traverse(List<URL> fileList, File root) throws IOException {
if (root.isFile()) {
fileList.add(root.toURL());
- } else if (root.isDirectory() && !root.getName().equals(".svn")) {
- File[] files = root.listFiles();
+ } else if (root.isDirectory()) {
+ // FIXME: Maybe we should externalize it as a property
+ // Regular expression to exclude .xxx files
+ File[] files = root.listFiles(FileHelper.getFileFilter("[^\u002e].*", true));
for (int i = 0; i < files.length; i++) {
traverse(fileList, files[i]);
}
}
}
-
/**
* Get a list of files from the directory
*
* @return
* @throws IOException
*/
- protected List<URL> getArtifacts(URL rootURL, InputStream sourceInputStream)
- throws DeploymentException, IOException {
+ protected List<URL> getArtifacts(URL rootURL, InputStream sourceInputStream) throws DeploymentException,
+ IOException {
List<URL> artifacts = new ArrayList<URL>();
// Assume the root is a jar file
File rootFolder;
-
+
try {
rootFolder = new File(rootURL.toURI());
if (rootFolder.isDirectory()) {
@@ -88,10 +90,9 @@
}
return artifacts;
- }
-
-
- public void processContent(Contribution contribution, URI source, InputStream inputStream)
+ }
+
+ public void processContent(Contribution contribution, URI source, InputStream inputStream)
throws DeploymentException, IOException {
if (contribution == null) {
throw new IllegalArgumentException("Invalid null contribution.");
@@ -103,40 +104,34 @@
URL contributionURL = contribution.getArtifact(source).getLocation();
-
for (URL artifactURL : getArtifacts(contributionURL, inputStream)) {
-
- URI artifactURI;
- try {
- artifactURI = new URI(contribution.getUri().toString() + FileHelper.getName(artifactURL.getPath()));
- DeployedArtifact artifact = new DeployedArtifact(artifactURI);
- artifact.setLocation(artifactURL);
- contribution.addArtifact(artifact);
-
- //TODO: remove this... for debug purpose only
- ContentTypeDescriber contentTypeDescriber = new ContentTypeDescriberImpl();
- String contentType = contentTypeDescriber.getContentType(artifactURL, null);
- //System.out.println("File : " + artifactURL);
- //System.out.println("Type : " + contentType);
-
-
- //just process scdl for now
- if ("application/v.tuscany.scdl".equals(contentType) /*|| "application/java-vm".equals(contentType)*/) {
- this.registry.processContent(contribution, artifactURI, artifactURL.openStream());
+ String artifactPath = artifactURL.toExternalForm().substring(contributionURL.toExternalForm().length());
+ URI artifactURI = contribution.getUri().resolve(artifactPath);
+ DeployedArtifact artifact = new DeployedArtifact(artifactURI);
+ artifact.setLocation(artifactURL);
+ contribution.addArtifact(artifact);
+
+ // TODO: remove this... for debug purpose only
+ ContentTypeDescriber contentTypeDescriber = new ContentTypeDescriberImpl();
+ String contentType = contentTypeDescriber.getContentType(artifactURL, null);
+ // System.out.println("File : " + artifactURL);
+ // System.out.println("Type : " + contentType);
+
+ // just process scdl for now
+ if ("application/vnd.tuscany.scdl".equals(contentType)) {
+ InputStream is = artifactURL.openStream();
+ try {
+ this.registry.processContent(contribution, artifactURI, is);
+ } finally {
+ IOHelper.closeQuietly(is);
}
- } catch (URISyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
}
-
}
-
}
- public void processModel(Contribution contribution, URI source, Object modelObject)
- throws DeploymentException, IOException {
- // TODO Auto-generated method stub
-
+ public void processModel(Contribution contribution, URI source, Object modelObject) throws DeploymentException,
+ IOException {
+ // NOOP
}
}
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/JarContributionProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/JarContributionProcessor.java?view=diff&rev=513075&r1=513074&r2=513075
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/JarContributionProcessor.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/JarContributionProcessor.java Wed Feb 28 16:08:41 2007
@@ -23,7 +23,6 @@
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;
@@ -31,7 +30,7 @@
import java.util.jar.JarInputStream;
import org.apache.tuscany.core.services.deployment.ContentTypeDescriberImpl;
-import org.apache.tuscany.core.util.FileHelper;
+import org.apache.tuscany.core.util.IOHelper;
import org.apache.tuscany.host.deployment.DeploymentException;
import org.apache.tuscany.spi.deployer.ContentTypeDescriber;
import org.apache.tuscany.spi.deployer.ContributionProcessor;
@@ -69,7 +68,10 @@
continue;
}
- artifacts.add(new URL(rootURL, entry.getName()));
+ // FIXME: Maybe we should externalize the filter as a property
+ if (!entry.getName().startsWith(".")) {
+ artifacts.add(new URL(rootURL, entry.getName()));
+ }
}
} finally {
jar.close();
@@ -106,33 +108,29 @@
for (URL artifactURL : getArtifacts(sourceURL, inputStream)) {
URI artifactURI;
-
- try {
- artifactURI = new URI(source.toString() + FileHelper.getName(artifactURL.getPath()));
- DeployedArtifact artifact = new DeployedArtifact(artifactURI);
- artifact.setLocation(artifactURL);
- contribution.addArtifact(artifact);
-
-
- //TODO: remove this... for debug purpose only
- ContentTypeDescriber contentTypeDescriber = new ContentTypeDescriberImpl();
- String contentType = contentTypeDescriber.getContentType(artifactURL, null);
- //System.out.println("File : " + artifactURL);
- //System.out.println("Type : " + contentType);
-
-
- //just process scdl for now
- if ("application/v.tuscany.scdl".equals(contentType)
- /* || "application/java-vm".equals(contentType) */) {
+
+ String artifactPath = artifactURL.toExternalForm().substring(sourceURL.toExternalForm().length());
+ artifactURI = contribution.getUri().resolve(artifactPath);
+ DeployedArtifact artifact = new DeployedArtifact(artifactURI);
+ artifact.setLocation(artifactURL);
+ contribution.addArtifact(artifact);
+
+ // TODO: remove this... for debug purpose only
+ ContentTypeDescriber contentTypeDescriber = new ContentTypeDescriberImpl();
+ String contentType = contentTypeDescriber.getContentType(artifactURL, null);
+ // System.out.println("File : " + artifactURL);
+ // System.out.println("Type : " + contentType);
+
+ // just process scdl for now
+ if ("application/vnd.tuscany.scdl".equals(contentType)
+ /* || "application/java-vm".equals(contentType) */) {
+ InputStream is = IOHelper.getInputStream(artifactURL);
+ try {
this.registry.processContent(contribution, artifactURI, artifactURL.openStream());
+ } finally {
+ IOHelper.closeQuietly(is);
}
- } catch (URISyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
}
-
-
-
}
}
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/ScdlContributionProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/ScdlContributionProcessor.java?view=diff&rev=513075&r1=513074&r2=513075
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/ScdlContributionProcessor.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/contribution/ScdlContributionProcessor.java Wed Feb 28 16:08:41 2007
@@ -40,16 +40,10 @@
import org.apache.tuscany.spi.model.Contribution;
public class ScdlContributionProcessor extends ContributionProcessorExtension implements ContributionProcessor {
- public static final String CONTENT_TYPE = "application/v.tuscany.scdl";
-
- private final LoaderRegistry registry;
+ public static final String CONTENT_TYPE = "application/vnd.tuscany.scdl";
protected XMLInputFactory xmlFactory;
-
- @Override
- public String getContentType() {
- return CONTENT_TYPE;
- }
+ private final LoaderRegistry registry;
public ScdlContributionProcessor(@Autowire LoaderRegistry registry) {
super();
@@ -57,6 +51,11 @@
this.xmlFactory = XMLInputFactory.newInstance("javax.xml.stream.XMLInputFactory", getClass().getClassLoader());
}
+ @Override
+ public String getContentType() {
+ return CONTENT_TYPE;
+ }
+
public void processContent(Contribution contribution, URI source, InputStream inputStream)
throws DeploymentException, IOException {
if (source == null) {
@@ -93,8 +92,6 @@
public void processModel(Contribution contribution, URI source, Object modelObject) throws DeploymentException,
IOException {
- // TODO Auto-generated method stub
-
}
}
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/FileHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/FileHelper.java?view=diff&rev=513075&r1=513074&r2=513075
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/FileHelper.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/FileHelper.java Wed Feb 28 16:08:41 2007
@@ -20,9 +20,11 @@
package org.apache.tuscany.core.util;
import java.io.File;
+import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
+import java.util.regex.Pattern;
public class FileHelper {
/**
@@ -39,21 +41,24 @@
* The Windows separator character.
*/
private static final char WINDOWS_SEPARATOR = '\\';
-
+
protected FileHelper() {
-
+
}
+
/**
* Returns the index of the last directory separator character.
* <p>
- * This method will handle a file in either Unix or Windows format.
- * The position of the last forward or backslash is returned.
+ * This method will handle a file in either Unix or Windows format. The
+ * position of the last forward or backslash is returned.
* <p>
- * The output will be the same irrespective of the machine that the code is running on.
- *
- * @param filename the filename to find the last path separator in, null returns -1
- * @return the index of the last separator character, or -1 if there
- * is no such character
+ * The output will be the same irrespective of the machine that the code is
+ * running on.
+ *
+ * @param filename the filename to find the last path separator in, null
+ * returns -1
+ * @return the index of the last separator character, or -1 if there is no
+ * such character
*/
public static int indexOfLastSeparator(String filename) {
if (filename == null) {
@@ -63,19 +68,22 @@
int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
return Math.max(lastUnixPos, lastWindowsPos);
}
-
+
/**
- * Returns the index of the last extension separator character, which is a dot.
+ * Returns the index of the last extension separator character, which is a
+ * dot.
* <p>
- * This method also checks that there is no directory separator after the last dot.
- * To do this it uses {@link #indexOfLastSeparator(String)} which will
- * handle a file in either Unix or Windows format.
+ * This method also checks that there is no directory separator after the
+ * last dot. To do this it uses {@link #indexOfLastSeparator(String)} which
+ * will handle a file in either Unix or Windows format.
* <p>
- * The output will be the same irrespective of the machine that the code is running on.
- *
- * @param filename the filename to find the last path separator in, null returns -1
- * @return the index of the last separator character, or -1 if there
- * is no such character
+ * The output will be the same irrespective of the machine that the code is
+ * running on.
+ *
+ * @param filename the filename to find the last path separator in, null
+ * returns -1
+ * @return the index of the last separator character, or -1 if there is no
+ * such character
*/
public static int indexOfExtension(String filename) {
if (filename == null) {
@@ -85,23 +93,27 @@
int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos ? -1 : extensionPos;
}
-
+
/**
* Gets the name minus the path from a full filename.
* <p>
- * This method will handle a file in either Unix or Windows format.
- * The text after the last forward or backslash is returned.
+ * This method will handle a file in either Unix or Windows format. The text
+ * after the last forward or backslash is returned.
+ *
* <pre>
- * a/b/c.txt --> c.txt
- * a.txt --> a.txt
- * a/b/c --> c
- * a/b/c/ --> ""
+ * a/b/c.txt --> c.txt
+ * a.txt --> a.txt
+ * a/b/c --> c
+ * a/b/c/ --> ""
* </pre>
+ *
* <p>
- * The output will be the same irrespective of the machine that the code is running on.
- *
- * @param fileName the filename to query, null returns null
- * @return the name of the file without the path, or an empty string if none exists
+ * The output will be the same irrespective of the machine that the code is
+ * running on.
+ *
+ * @param fileName the filename to query, null returns null
+ * @return the name of the file without the path, or an empty string if none
+ * exists
*/
public static String getName(String fileName) {
if (fileName == null) {
@@ -110,21 +122,24 @@
int index = indexOfLastSeparator(fileName);
return fileName.substring(index + 1);
}
-
+
/**
* Gets the extension of a filename.
* <p>
* This method returns the textual part of the filename after the last dot.
* There must be no directory separator after the dot.
+ *
* <pre>
- * foo.txt --> "txt"
- * a/b/c.jpg --> "jpg"
- * a/b.txt/c --> ""
- * a/b/c --> ""
+ * foo.txt --> "txt"
+ * a/b/c.jpg --> "jpg"
+ * a/b.txt/c --> ""
+ * a/b/c --> ""
* </pre>
+ *
* <p>
- * The output will be the same irrespective of the machine that the code is running on.
- *
+ * The output will be the same irrespective of the machine that the code is
+ * running on.
+ *
* @param filename the filename to retrieve the extension of.
* @return the extension of the file or an empty string if none exists.
*/
@@ -139,13 +154,13 @@
return filename.substring(index + 1);
}
}
-
+
/**
* Make a directory, including any necessary but nonexistent parent
- * directories. If there already exists a file with specified name or
- * the directory cannot be created then an exception is thrown.
- *
- * @param directory directory to create, not null
+ * directories. If there already exists a file with specified name or the
+ * directory cannot be created then an exception is thrown.
+ *
+ * @param directory directory to create, not null
* @throws NullPointerException if the directory is null
* @throws IOException if the directory cannot be created
*/
@@ -153,21 +168,17 @@
if (directory.exists()) {
if (directory.isFile()) {
String message =
- "File "
- + directory
- + " exists and is "
- + "not a directory. Unable to create directory.";
+ "File " + directory + " exists and is " + "not a directory. Unable to create directory.";
throw new IOException(message);
}
} else {
if (!directory.mkdirs()) {
- String message =
- "Unable to create directory " + directory;
+ String message = "Unable to create directory " + directory;
throw new IOException(message);
}
}
}
-
+
/**
* Delete a file. If file is a directory, delete it and all sub-directories.
* <p>
@@ -175,10 +186,10 @@
* <ul>
* <li>A directory to be deleted does not have to be empty.</li>
* <li>You get exceptions when a file or directory cannot be deleted.
- * (java.io.File methods returns a boolean)</li>
+ * (java.io.File methods returns a boolean)</li>
* </ul>
- *
- * @param file file or directory to delete, not null
+ *
+ * @param file file or directory to delete, not null
* @throws NullPointerException if the directory is null
* @throws IOException in case deletion is unsuccessful
*/
@@ -190,36 +201,33 @@
throw new FileNotFoundException("File does not exist: " + file);
}
if (!file.delete()) {
- String message =
- "Unable to delete file: " + file;
+ String message = "Unable to delete file: " + file;
throw new IOException(message);
}
}
}
-
+
/**
* Recursively delete a directory.
- *
- * @param directory directory to delete
+ *
+ * @param directory directory to delete
* @throws IOException in case deletion is unsuccessful
*/
- public static void deleteDirectory(File directory)
- throws IOException {
+ public static void deleteDirectory(File directory) throws IOException {
if (!directory.exists()) {
return;
}
cleanDirectory(directory);
if (!directory.delete()) {
- String message =
- "Unable to delete directory " + directory + ".";
+ String message = "Unable to delete directory " + directory + ".";
throw new IOException(message);
}
}
/**
* Clean a directory without deleting it.
- *
+ *
* @param directory directory to clean
* @throws IOException in case cleaning is unsuccessful
*/
@@ -235,7 +243,7 @@
}
File[] files = directory.listFiles();
- if (files == null) { // null if security restricted
+ if (files == null) { // null if security restricted
throw new IOException("Failed to list contents of " + directory);
}
@@ -253,17 +261,17 @@
throw exception;
}
}
-
+
/**
* Convert from a <code>URL</code> to a <code>File</code>.
* <p>
- * From version 1.1 this method will decode the URL.
- * Syntax such as <code>file:///my%20docs/file.txt</code> will be
- * correctly decoded to <code>/my docs/file.txt</code>.
- *
- * @param url the file URL to convert, null returns null
+ * From version 1.1 this method will decode the URL. Syntax such as
+ * <code>file:///my%20docs/file.txt</code> will be correctly decoded to
+ * <code>/my docs/file.txt</code>.
+ *
+ * @param url the file URL to convert, null returns null
* @return the equivalent <code>File</code> object, or <code>null</code>
- * if the URL's protocol is not <code>file</code>
+ * if the URL's protocol is not <code>file</code>
* @throws IllegalArgumentException if the file is incorrectly encoded
*/
public static File toFile(URL url) {
@@ -275,11 +283,57 @@
while ((pos = filename.indexOf('%', pos)) >= 0) {
if (pos + 2 < filename.length()) {
String hexStr = filename.substring(pos + 1, pos + 3);
- char ch = (char) Integer.parseInt(hexStr, 16);
+ char ch = (char)Integer.parseInt(hexStr, 16);
filename = filename.substring(0, pos) + ch + filename.substring(pos + 3);
}
}
return new File(filename);
}
}
+
+ public static FileFilter getFileFilter(String regExp, boolean ignoreCase) {
+ return new RegExpFilter(regExp, ignoreCase);
+ }
+
+ /**
+ * A regular-expression based resource filter
+ */
+ public static class RegExpFilter implements FileFilter {
+ private Pattern pattern;
+
+ public RegExpFilter(Pattern pattern) {
+ this.pattern = pattern;
+ }
+
+ public RegExpFilter(String patternStr, boolean ignoreCase) {
+ this.pattern = Pattern.compile(patternStr, ignoreCase ? Pattern.CASE_INSENSITIVE : 0);
+ }
+
+ public boolean accept(File file) {
+ return pattern.matcher(file.getName()).matches();
+ }
+
+ /**
+ * Convert wildcard into a regex pattern
+ *
+ * @param str
+ * @return
+ */
+ public static RegExpFilter getWildcardFilter(String str, boolean ignoreCase) {
+ StringBuffer buffer = new StringBuffer();
+ for (int i = 0; i < str.length(); i++) {
+ char ch = str.charAt(i);
+ if (ch == '?') {
+ buffer.append('.');
+ } else if (ch == '*') {
+ buffer.append(".*");
+ } else {
+ buffer.append(ch);
+ }
+ }
+ return new RegExpFilter(buffer.toString(), ignoreCase);
+ }
+
+ }
+
}
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOHelper.java?view=diff&rev=513075&r1=513074&r2=513075
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOHelper.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOHelper.java Wed Feb 28 16:08:41 2007
@@ -22,6 +22,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.util.jar.JarFile;
public class IOHelper {
/**
@@ -93,4 +96,89 @@
}
return count;
}
+
+ public static InputStream getInputStream(URL url) throws IOException {
+ return new SafeURLInputStream(url);
+ }
+
+ /**
+ * This class is a workaround for URL stream issue as illustrated below.
+ * InputStream is=url.getInputStream(); is.close(); // This line doesn't close
+ * the JAR file if the URL is a jar entry like "jar:file:/a.jar!/my.composite" We
+ * also need to turn off the JarFile cache.
+ *
+ * @see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4950148
+ *
+ * @version $Rev$ $Date$
+ */
+ public static class SafeURLInputStream extends InputStream {
+ private JarFile jarFile;
+ private InputStream is;
+
+ public SafeURLInputStream(URL url) throws IOException {
+ String protocol = url.getProtocol();
+ if (protocol != null && (protocol.equals("jar"))) {
+ JarURLConnection connection = (JarURLConnection)url.openConnection();
+ // We cannot use cache
+ connection.setUseCaches(false);
+ try {
+ is = connection.getInputStream();
+ } catch (IOException e) {
+ throw e;
+ }
+ jarFile = connection.getJarFile();
+ } else {
+ is = url.openStream();
+ }
+ }
+
+ public SafeURLInputStream(JarURLConnection connection) throws IOException {
+ // We cannot use cache
+ connection.setUseCaches(false);
+ is = connection.getInputStream();
+ jarFile = connection.getJarFile();
+ }
+
+ public int available() throws IOException {
+ return is.available();
+ }
+
+ public void close() throws IOException {
+ is.close();
+ // We need to close the JAR file
+ if (jarFile != null) {
+ jarFile.close();
+ }
+ }
+
+ public synchronized void mark(int readlimit) {
+ is.mark(readlimit);
+ }
+
+ public boolean markSupported() {
+ return is.markSupported();
+ }
+
+ public int read() throws IOException {
+ return is.read();
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ return is.read(b, off, len);
+ }
+
+ public int read(byte[] b) throws IOException {
+ return is.read(b);
+ }
+
+ public synchronized void reset() throws IOException {
+ is.reset();
+ }
+
+ public long skip(long n) throws IOException {
+ return is.skip(n);
+ }
+
+ }
+
}
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImplTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImplTestCase.java?view=diff&rev=513075&r1=513074&r2=513075
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImplTestCase.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContentTypeDescriberImplTestCase.java Wed Feb 28 16:08:41 2007
@@ -27,7 +27,7 @@
public void testResolveContentType() throws Exception {
URL artifactURL = getClass().getResource("test.scdl");
- assertEquals("application/v.tuscany.scdl", contentTypeBuilder.getContentType(artifactURL, null));
+ assertEquals("application/vnd.tuscany.scdl", contentTypeBuilder.getContentType(artifactURL, null));
}
@@ -38,8 +38,8 @@
public void testDefaultContentType() throws Exception {
URL artifactURL = getClass().getResource("test.ext");
- assertEquals("application/v.tuscany.ext",
- contentTypeBuilder.getContentType(artifactURL, "application/v.tuscany.ext"));
+ assertEquals("application/vnd.tuscany.ext",
+ contentTypeBuilder.getContentType(artifactURL, "application/vnd.tuscany.ext"));
}
protected void setUp() throws Exception {
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org