From a63b5346cc43457b0efccb238f15367f36244291 Mon Sep 17 00:00:00 2001 From: tjordahl Date: Thu, 11 Aug 2011 21:51:50 +0000 Subject: [PATCH] Apply patch for bugs 2030388 and 3134533: FileProtocolIteratorFactory creates File from a URL wrong and doesn't handle spaces. Add new AnnotationDB setting: ignoreBadURLs. Setting this to true will prevent AnnotationDB::scanArchives() from throwing an IOException when handed a bad URL, it will continue to process the remaining URLs passed to it and skip the bad ones. --- .../java/org/scannotation/AnnotationDB.java | 38 +++++++++++++++---- .../FileProtocolIteratorFactory.java | 15 +++++++- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/scannotation/AnnotationDB.java b/src/main/java/org/scannotation/AnnotationDB.java index 230bd6e..bf27515 100644 --- a/src/main/java/org/scannotation/AnnotationDB.java +++ b/src/main/java/org/scannotation/AnnotationDB.java @@ -50,6 +50,7 @@ public class AnnotationDB implements Serializable protected transient boolean scanFieldAnnotations = true; protected transient String[] ignoredPackages = {"javax", "java", "sun", "com.sun", "javassist"}; protected transient String[] scanPackages = null; + protected transient boolean ignoreBadURLs = false; public class CrossReferenceException extends Exception { @@ -110,7 +111,9 @@ public class AnnotationDB implements Serializable * This method will cross reference annotations in the annotation index with any meta-annotations that they have * and create additional entries as needed. For example: * - * @HttpMethod("GET") public @interface GET {} + *
+    * @ HttpMethod("GET") public @interface GET {}
+    * 
*

* The HttpMethod index will have additional classes added to it for any classes annotated with annotations that * have the HttpMethod meta-annotation. @@ -168,7 +171,6 @@ public class AnnotationDB implements Serializable * a class's implemented interfaces. The cross references will be added to the annotationIndex and * classIndex indexes * - * @param ignoredPackages var arg list of packages to ignore * @throws CrossReferenceException an Exception thrown if referenced interfaces haven't been scanned */ public void crossReferenceImplementedInterfaces() throws CrossReferenceException @@ -293,8 +295,18 @@ public class AnnotationDB implements Serializable this.scanFieldAnnotations = scanFieldAnnotations; } - - /** + /** + * Whether or not you want AnnotationDB to ignore bad URLs passed to scanArchives. + * Default is to throw an IOException. + * + * @param ignoreBadURLs + */ + public void setIgnoreBadURLs(boolean ignoreBadURLs) + { + this.ignoreBadURLs = ignoreBadURLs; + } + + /** * Scan a url that represents an "archive" this is a classpath directory or jar file * * @param urls variable list of URLs to scan as archives @@ -320,10 +332,20 @@ public class AnnotationDB implements Serializable } }; - StreamIterator it = IteratorFactory.create(url, filter); - - InputStream stream; - while ((stream = it.next()) != null) scanClass(stream); + try + { + StreamIterator it = IteratorFactory.create(url, filter); + + InputStream stream; + while ((stream = it.next()) != null) scanClass(stream); + } + catch (IOException e) + { + if (ignoreBadURLs) + continue; + else + throw e; + } } } diff --git a/src/main/java/org/scannotation/archiveiterator/FileProtocolIteratorFactory.java b/src/main/java/org/scannotation/archiveiterator/FileProtocolIteratorFactory.java index a8c0a40..5a5979b 100644 --- a/src/main/java/org/scannotation/archiveiterator/FileProtocolIteratorFactory.java +++ b/src/main/java/org/scannotation/archiveiterator/FileProtocolIteratorFactory.java @@ -2,6 +2,7 @@ package org.scannotation.archiveiterator; import java.io.File; import java.io.IOException; +import java.net.URISyntaxException; import java.net.URL; /** @@ -13,8 +14,18 @@ public class FileProtocolIteratorFactory implements DirectoryIteratorFactory public StreamIterator create(URL url, Filter filter) throws IOException { - File f = new File(url.getPath()); - if (f.isDirectory()) + // See http://weblogs.java.net/blog/2007/04/25/how-convert-javaneturl-javaiofile + File f; + try + { + f = new File(url.toURI()); + } + catch (URISyntaxException e) + { + f = new File(url.getPath()); + } + + if (f.isDirectory()) { return new FileIterator(f, filter); } -- 2.20.1