From: patriot1burke Date: Thu, 10 Jan 2008 21:04:35 +0000 (+0000) Subject: Renaming artifact and packages X-Git-Tag: scannotation-1.0.4~9 X-Git-Url: https://juplo.de/gitweb/?p=scannotation;a=commitdiff_plain;h=36e6637926203201648e7892ec6ee1240807218e Renaming artifact and packages --- diff --git a/pom.xml b/pom.xml index 353ce83..82486f4 100644 --- a/pom.xml +++ b/pom.xml @@ -2,10 +2,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 org.scannotation - annotation-db + scannotation jar 1.0 - resteasy-jsr311 + scannotation http://maven.apache.org @@ -38,6 +38,14 @@ provided + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + diff --git a/src/main/java/org/scannotation/AnnotationDB.java b/src/main/java/org/scannotation/AnnotationDB.java index da19ea2..35f3a8d 100644 --- a/src/main/java/org/scannotation/AnnotationDB.java +++ b/src/main/java/org/scannotation/AnnotationDB.java @@ -6,9 +6,9 @@ import javassist.bytecode.FieldInfo; import javassist.bytecode.MethodInfo; import javassist.bytecode.ParameterAnnotationsAttribute; import javassist.bytecode.annotation.Annotation; -import org.scannotation.classpath.Filter; -import org.scannotation.classpath.IteratorFactory; -import org.scannotation.classpath.StreamIterator; +import org.scannotation.archiveiterator.Filter; +import org.scannotation.archiveiterator.IteratorFactory; +import org.scannotation.archiveiterator.StreamIterator; import java.io.BufferedInputStream; import java.io.DataInputStream; diff --git a/src/main/java/org/scannotation/ClasspathUrlFinder.java b/src/main/java/org/scannotation/ClasspathUrlFinder.java new file mode 100644 index 0000000..122dfd0 --- /dev/null +++ b/src/main/java/org/scannotation/ClasspathUrlFinder.java @@ -0,0 +1,190 @@ +package org.scannotation; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.StringTokenizer; + +/** + * Various functions to locate URLs to scan + * + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class ClasspathUrlFinder +{ + + /** + * Find the classpath URLs for a specific classpath resource. The classpath URL is extracted + * from loader.getResources() using the baseResource. + * + * @param baseResource + * @return + */ + public static URL[] findResourceBases(String baseResource, ClassLoader loader) + { + ArrayList list = new ArrayList(); + try + { + Enumeration urls = loader.getResources(baseResource); + while (urls.hasMoreElements()) + { + URL url = urls.nextElement(); + list.add(findResourceBase(url, baseResource)); + } + } + catch (IOException e) + { + throw new RuntimeException(e); + } + return list.toArray(new URL[list.size()]); + } + + /** + * Find the classpath URLs for a specific classpath resource. The classpath URL is extracted + * from loader.getResources() using the baseResource. + * + * @param baseResource + * @return + */ + public static URL[] findResourceBases(String baseResource) + { + return findResourceBases(baseResource, Thread.currentThread().getContextClassLoader()); + } + + private static URL findResourceBase(URL url, String baseResource) + { + String urlString = url.toString(); + int idx = urlString.lastIndexOf(baseResource); + urlString = urlString.substring(0, idx); + URL deployUrl = null; + try + { + deployUrl = new URL(urlString); + } + catch (MalformedURLException e) + { + throw new RuntimeException(e); + } + return deployUrl; + } + + /** + * Find the classpath URL for a specific classpath resource. The classpath URL is extracted + * from Thread.currentThread().getContextClassLoader().getResource() using the baseResource. + * + * @param baseResource + * @return + */ + public static URL findResourceBase(String baseResource) + { + return findResourceBase(baseResource, Thread.currentThread().getContextClassLoader()); + } + + /** + * Find the classpath URL for a specific classpath resource. The classpath URL is extracted + * from loader.getResource() using the baseResource. + * + * @param baseResource + * @param loader + * @return + */ + public static URL findResourceBase(String baseResource, ClassLoader loader) + { + URL url = loader.getResource(baseResource); + return findResourceBase(url, baseResource); + } + + /** + * Find the classpath for the particular class + * + * @param clazz + * @return + */ + public static URL findClassBase(Class clazz) + { + String resource = clazz.getName().replace('.', '/') + ".class"; + return findResourceBase(resource, clazz.getClassLoader()); + } + + /** + * Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH + * + * @return + */ + public static URL[] findClassPaths() + { + List list = new ArrayList(); + String classpath = System.getProperty("java.class.path"); + StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator); + + while (tokenizer.hasMoreTokens()) + { + String path = tokenizer.nextToken(); + File fp = new File(path); + if (!fp.exists()) throw new RuntimeException("File in java.class.path does not exist: " + fp); + try + { + list.add(fp.toURL()); + } + catch (MalformedURLException e) + { + throw new RuntimeException(e); + } + } + return list.toArray(new URL[list.size()]); + } + + /** + * Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH + *

+ * paths is used as a filter to only include paths that have the specific relative file within it + * + * @param paths comma list of files that should exist in a particular path + * @return + */ + public static URL[] findClassPaths(String... paths) + { + ArrayList list = new ArrayList(); + + String classpath = System.getProperty("java.class.path"); + StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator); + for (int i = 0; i < paths.length; i++) + { + paths[i] = paths[i].trim(); + } + + while (tokenizer.hasMoreTokens()) + { + String path = tokenizer.nextToken().trim(); + boolean found = false; + for (String wantedPath : paths) + { + if (path.endsWith(File.separator + wantedPath)) + { + found = true; + break; + } + } + if (!found) continue; + File fp = new File(path); + if (!fp.exists()) throw new RuntimeException("File in java.class.path does not exists: " + fp); + try + { + list.add(fp.toURL()); + } + catch (MalformedURLException e) + { + throw new RuntimeException(e); + } + } + return list.toArray(new URL[list.size()]); + } + + +} + diff --git a/src/main/java/org/scannotation/WarUrlFinder.java b/src/main/java/org/scannotation/WarUrlFinder.java new file mode 100644 index 0000000..f253764 --- /dev/null +++ b/src/main/java/org/scannotation/WarUrlFinder.java @@ -0,0 +1,74 @@ +package org.scannotation; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Set; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class WarUrlFinder +{ + public static URL[] findWebInfLibClasspaths(ServletContextEvent servletContextEvent) + { + ServletContext servletContext = servletContextEvent.getServletContext(); + return findWebInfLibClasspaths(servletContext); + } + + public static URL[] findWebInfLibClasspaths(ServletContext servletContext) + { + ArrayList list = new ArrayList(); + Set libJars = servletContext.getResourcePaths("/WEB-INF/lib"); + for (Object jar : libJars) + { + try + { + list.add(servletContext.getResource((String) jar)); + } + catch (MalformedURLException e) + { + throw new RuntimeException(e); + } + } + return list.toArray(new URL[list.size()]); + } + + public static URL findWebInfClassesPath(ServletContextEvent servletContextEvent) + { + ServletContext servletContext = servletContextEvent.getServletContext(); + return findWebInfClassesPath(servletContext); + } + + /** + * Find the URL pointing to "/WEB-INF/classes" This method may not work in conjunction with IteratorFactory + * if your servlet container does not extract the /WEB-INF/classes into a real file-based directory + * + * @param servletContext + * @return + */ + public static URL findWebInfClassesPath(ServletContext servletContext) + { + Set libJars = servletContext.getResourcePaths("/WEB-INF/classes"); + for (Object jar : libJars) + { + try + { + URL url = servletContext.getResource((String) jar); + String urlString = url.toString(); + int index = urlString.lastIndexOf("/WEB-INF/classes/"); + urlString = urlString.substring(0, index + "/WEB-INF/classes/".length()); + return new URL(urlString); + } + catch (MalformedURLException e) + { + throw new RuntimeException(e); + } + } + return null; + + } +} diff --git a/src/main/java/org/scannotation/archiveiterator/DirectoryIteratorFactory.java b/src/main/java/org/scannotation/archiveiterator/DirectoryIteratorFactory.java new file mode 100644 index 0000000..699074d --- /dev/null +++ b/src/main/java/org/scannotation/archiveiterator/DirectoryIteratorFactory.java @@ -0,0 +1,13 @@ +package org.scannotation.archiveiterator; + +import java.io.IOException; +import java.net.URL; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public interface DirectoryIteratorFactory +{ + StreamIterator create(URL url, Filter filter) throws IOException; +} diff --git a/src/main/java/org/scannotation/archiveiterator/FileIterator.java b/src/main/java/org/scannotation/archiveiterator/FileIterator.java new file mode 100644 index 0000000..aa3593b --- /dev/null +++ b/src/main/java/org/scannotation/archiveiterator/FileIterator.java @@ -0,0 +1,68 @@ +package org.scannotation.archiveiterator; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class FileIterator implements StreamIterator +{ + private ArrayList files; + private int index = 0; + + public FileIterator(File file, Filter filter) + { + files = new ArrayList(); + try + { + create(files, file, filter); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } + + protected static void create(List list, File dir, Filter filter) throws Exception + { + File[] files = dir.listFiles(); + for (int i = 0; i < files.length; i++) + { + if (files[i].isDirectory()) + { + create(list, files[i], filter); + } + else + { + if (filter == null || filter.accepts(files[i].getAbsolutePath())) + { + list.add(files[i]); + } + } + } + } + + public InputStream next() + { + if (index >= files.size()) return null; + File fp = (File) files.get(index++); + try + { + return new FileInputStream(fp); + } + catch (FileNotFoundException e) + { + throw new RuntimeException(e); + } + } + + public void close() + { + } +} diff --git a/src/main/java/org/scannotation/archiveiterator/FileProtocolIteratorFactory.java b/src/main/java/org/scannotation/archiveiterator/FileProtocolIteratorFactory.java new file mode 100644 index 0000000..a8c0a40 --- /dev/null +++ b/src/main/java/org/scannotation/archiveiterator/FileProtocolIteratorFactory.java @@ -0,0 +1,26 @@ +package org.scannotation.archiveiterator; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class FileProtocolIteratorFactory implements DirectoryIteratorFactory +{ + + public StreamIterator create(URL url, Filter filter) throws IOException + { + File f = new File(url.getPath()); + if (f.isDirectory()) + { + return new FileIterator(f, filter); + } + else + { + return new JarIterator(url.openStream(), filter); + } + } +} diff --git a/src/main/java/org/scannotation/archiveiterator/Filter.java b/src/main/java/org/scannotation/archiveiterator/Filter.java new file mode 100644 index 0000000..b1eecfb --- /dev/null +++ b/src/main/java/org/scannotation/archiveiterator/Filter.java @@ -0,0 +1,10 @@ +package org.scannotation.archiveiterator; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public interface Filter +{ + boolean accepts(String filename); +} diff --git a/src/main/java/org/scannotation/archiveiterator/InputStreamWrapper.java b/src/main/java/org/scannotation/archiveiterator/InputStreamWrapper.java new file mode 100644 index 0000000..5adb77d --- /dev/null +++ b/src/main/java/org/scannotation/archiveiterator/InputStreamWrapper.java @@ -0,0 +1,72 @@ +package org.scannotation.archiveiterator; + +import java.io.InputStream; +import java.io.IOException; + +/** + * Delegate to everything but close(). This object will not close the stream + * + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class InputStreamWrapper extends InputStream +{ + private InputStream delegate; + + public InputStreamWrapper(InputStream delegate) + { + this.delegate = delegate; + } + + public int read() + throws IOException + { + return delegate.read(); + } + + public int read(byte[] bytes) + throws IOException + { + return delegate.read(bytes); + } + + public int read(byte[] bytes, int i, int i1) + throws IOException + { + return delegate.read(bytes, i, i1); + } + + public long skip(long l) + throws IOException + { + return delegate.skip(l); + } + + public int available() + throws IOException + { + return delegate.available(); + } + + public void close() + throws IOException + { + // ignored + } + + public void mark(int i) + { + delegate.mark(i); + } + + public void reset() + throws IOException + { + delegate.reset(); + } + + public boolean markSupported() + { + return delegate.markSupported(); + } +} diff --git a/src/main/java/org/scannotation/archiveiterator/IteratorFactory.java b/src/main/java/org/scannotation/archiveiterator/IteratorFactory.java new file mode 100644 index 0000000..5006ea7 --- /dev/null +++ b/src/main/java/org/scannotation/archiveiterator/IteratorFactory.java @@ -0,0 +1,43 @@ +package org.scannotation.archiveiterator; + +import java.io.IOException; +import java.net.URL; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class IteratorFactory +{ + private static final ConcurrentHashMap registry = new ConcurrentHashMap(); + + static + { + registry.put("file", new FileProtocolIteratorFactory()); + } + + + public static StreamIterator create(URL url, Filter filter) throws IOException + { + String urlString = url.toString(); + if (urlString.endsWith("!/")) + { + urlString = urlString.substring(4); + urlString = urlString.substring(0, urlString.length() - 2); + url = new URL(urlString); + } + + + if (!urlString.endsWith("/")) + { + return new JarIterator(url.openStream(), filter); + } + else + { + DirectoryIteratorFactory factory = registry.get(url.getProtocol()); + if (factory == null) throw new IOException("Unable to scan directory of protocol: " + url.getProtocol()); + return factory.create(url, filter); + } + } +} diff --git a/src/main/java/org/scannotation/archiveiterator/JarIterator.java b/src/main/java/org/scannotation/archiveiterator/JarIterator.java new file mode 100644 index 0000000..67291a6 --- /dev/null +++ b/src/main/java/org/scannotation/archiveiterator/JarIterator.java @@ -0,0 +1,77 @@ +package org.scannotation.archiveiterator; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class JarIterator implements StreamIterator +{ + JarInputStream jar; + JarEntry next; + Filter filter; + boolean initial = true; + boolean closed = false; + + public JarIterator(File file, Filter filter) throws IOException + { + this(new FileInputStream(file), filter); + } + + + public JarIterator(InputStream is, Filter filter) throws IOException + { + this.filter = filter; + jar = new JarInputStream(is); + } + + private void setNext() + { + initial = true; + try + { + if (next != null) jar.closeEntry(); + next = null; + do + { + next = jar.getNextJarEntry(); + } while (next != null && (next.isDirectory() || (filter == null || !filter.accepts(next.getName())))); + if (next == null) + { + close(); + } + } + catch (IOException e) + { + throw new RuntimeException("failed to browse jar", e); + } + } + + public InputStream next() + { + if (closed || (next == null && !initial)) return null; + setNext(); + if (next == null) return null; + return new InputStreamWrapper(jar); + } + + public void close() + { + try + { + closed = true; + jar.close(); + } + catch (IOException ignored) + { + + } + + } +} diff --git a/src/main/java/org/scannotation/archiveiterator/StreamIterator.java b/src/main/java/org/scannotation/archiveiterator/StreamIterator.java new file mode 100644 index 0000000..9d4e4f1 --- /dev/null +++ b/src/main/java/org/scannotation/archiveiterator/StreamIterator.java @@ -0,0 +1,25 @@ +package org.scannotation.archiveiterator; + +import java.io.InputStream; + +/** + * Simpler iterator than java.util.iterator. Things like JarInputStream does not allow you to implement hasNext() + * + * @author Bill Burke + * @version $Revision: 1 $ + */ +public interface StreamIterator +{ + /** + * User is resposible for closing the InputStream returned + * + * @return null if no more streams left to iterate on + */ + InputStream next(); + + /** + * Cleanup any open resources of the iterator + * + */ + void close(); +} diff --git a/src/main/java/org/scannotation/classpath/ClasspathUrlFinder.java b/src/main/java/org/scannotation/classpath/ClasspathUrlFinder.java deleted file mode 100644 index f56be22..0000000 --- a/src/main/java/org/scannotation/classpath/ClasspathUrlFinder.java +++ /dev/null @@ -1,190 +0,0 @@ -package org.scannotation.classpath; - -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; -import java.util.StringTokenizer; - -/** - * Various functions to locate URLs to scan - * - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class ClasspathUrlFinder -{ - - /** - * Find the classpath URLs for a specific classpath resource. The classpath URL is extracted - * from loader.getResources() using the baseResource. - * - * @param baseResource - * @return - */ - public static URL[] findResourceBases(String baseResource, ClassLoader loader) - { - ArrayList list = new ArrayList(); - try - { - Enumeration urls = loader.getResources(baseResource); - while (urls.hasMoreElements()) - { - URL url = urls.nextElement(); - list.add(findResourceBase(url, baseResource)); - } - } - catch (IOException e) - { - throw new RuntimeException(e); - } - return list.toArray(new URL[list.size()]); - } - - /** - * Find the classpath URLs for a specific classpath resource. The classpath URL is extracted - * from loader.getResources() using the baseResource. - * - * @param baseResource - * @return - */ - public static URL[] findResourceBases(String baseResource) - { - return findResourceBases(baseResource, Thread.currentThread().getContextClassLoader()); - } - - private static URL findResourceBase(URL url, String baseResource) - { - String urlString = url.toString(); - int idx = urlString.lastIndexOf(baseResource); - urlString = urlString.substring(0, idx); - URL deployUrl = null; - try - { - deployUrl = new URL(urlString); - } - catch (MalformedURLException e) - { - throw new RuntimeException(e); - } - return deployUrl; - } - - /** - * Find the classpath URL for a specific classpath resource. The classpath URL is extracted - * from Thread.currentThread().getContextClassLoader().getResource() using the baseResource. - * - * @param baseResource - * @return - */ - public static URL findResourceBase(String baseResource) - { - return findResourceBase(baseResource, Thread.currentThread().getContextClassLoader()); - } - - /** - * Find the classpath URL for a specific classpath resource. The classpath URL is extracted - * from loader.getResource() using the baseResource. - * - * @param baseResource - * @param loader - * @return - */ - public static URL findResourceBase(String baseResource, ClassLoader loader) - { - URL url = loader.getResource(baseResource); - return findResourceBase(url, baseResource); - } - - /** - * Find the classpath for the particular class - * - * @param clazz - * @return - */ - public static URL findClassBase(Class clazz) - { - String resource = clazz.getName().replace('.', '/') + ".class"; - return findResourceBase(resource, clazz.getClassLoader()); - } - - /** - * Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH - * - * @return - */ - public static URL[] findClassPaths() - { - List list = new ArrayList(); - String classpath = System.getProperty("java.class.path"); - StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator); - - while (tokenizer.hasMoreTokens()) - { - String path = tokenizer.nextToken(); - File fp = new File(path); - if (!fp.exists()) throw new RuntimeException("File in java.class.path does not exist: " + fp); - try - { - list.add(fp.toURL()); - } - catch (MalformedURLException e) - { - throw new RuntimeException(e); - } - } - return list.toArray(new URL[list.size()]); - } - - /** - * Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH - *

- * paths is used as a filter to only include paths that have the specific relative file within it - * - * @param paths comma list of files that should exist in a particular path - * @return - */ - public static URL[] findClassPaths(String... paths) - { - ArrayList list = new ArrayList(); - - String classpath = System.getProperty("java.class.path"); - StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator); - for (int i = 0; i < paths.length; i++) - { - paths[i] = paths[i].trim(); - } - - while (tokenizer.hasMoreTokens()) - { - String path = tokenizer.nextToken().trim(); - boolean found = false; - for (String wantedPath : paths) - { - if (path.endsWith(File.separator + wantedPath)) - { - found = true; - break; - } - } - if (!found) continue; - File fp = new File(path); - if (!fp.exists()) throw new RuntimeException("File in java.class.path does not exists: " + fp); - try - { - list.add(fp.toURL()); - } - catch (MalformedURLException e) - { - throw new RuntimeException(e); - } - } - return list.toArray(new URL[list.size()]); - } - - -} - diff --git a/src/main/java/org/scannotation/classpath/DirectoryIteratorFactory.java b/src/main/java/org/scannotation/classpath/DirectoryIteratorFactory.java deleted file mode 100644 index 7239d1b..0000000 --- a/src/main/java/org/scannotation/classpath/DirectoryIteratorFactory.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.scannotation.classpath; - -import java.io.IOException; -import java.net.URL; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public interface DirectoryIteratorFactory -{ - StreamIterator create(URL url, Filter filter) throws IOException; -} diff --git a/src/main/java/org/scannotation/classpath/FileIterator.java b/src/main/java/org/scannotation/classpath/FileIterator.java deleted file mode 100644 index b69709c..0000000 --- a/src/main/java/org/scannotation/classpath/FileIterator.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.scannotation.classpath; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class FileIterator implements StreamIterator -{ - private ArrayList files; - private int index = 0; - - public FileIterator(File file, Filter filter) - { - files = new ArrayList(); - try - { - create(files, file, filter); - } - catch (Exception e) - { - throw new RuntimeException(e); - } - } - - protected static void create(List list, File dir, Filter filter) throws Exception - { - File[] files = dir.listFiles(); - for (int i = 0; i < files.length; i++) - { - if (files[i].isDirectory()) - { - create(list, files[i], filter); - } - else - { - if (filter == null || filter.accepts(files[i].getAbsolutePath())) - { - list.add(files[i]); - } - } - } - } - - public InputStream next() - { - if (index >= files.size()) return null; - File fp = (File) files.get(index++); - try - { - return new FileInputStream(fp); - } - catch (FileNotFoundException e) - { - throw new RuntimeException(e); - } - } - - public void close() - { - } -} diff --git a/src/main/java/org/scannotation/classpath/FileProtocolIteratorFactory.java b/src/main/java/org/scannotation/classpath/FileProtocolIteratorFactory.java deleted file mode 100644 index 5d3b44c..0000000 --- a/src/main/java/org/scannotation/classpath/FileProtocolIteratorFactory.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.scannotation.classpath; - -import java.io.File; -import java.io.IOException; -import java.net.URL; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class FileProtocolIteratorFactory implements DirectoryIteratorFactory -{ - - public StreamIterator create(URL url, Filter filter) throws IOException - { - File f = new File(url.getPath()); - if (f.isDirectory()) - { - return new FileIterator(f, filter); - } - else - { - return new JarIterator(url.openStream(), filter); - } - } -} diff --git a/src/main/java/org/scannotation/classpath/Filter.java b/src/main/java/org/scannotation/classpath/Filter.java deleted file mode 100644 index 208b313..0000000 --- a/src/main/java/org/scannotation/classpath/Filter.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.scannotation.classpath; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public interface Filter -{ - boolean accepts(String filename); -} diff --git a/src/main/java/org/scannotation/classpath/InputStreamWrapper.java b/src/main/java/org/scannotation/classpath/InputStreamWrapper.java deleted file mode 100644 index c19d0e7..0000000 --- a/src/main/java/org/scannotation/classpath/InputStreamWrapper.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.scannotation.classpath; - -import java.io.InputStream; -import java.io.IOException; - -/** - * Delegate to everything but close(). This object will not close the stream - * - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class InputStreamWrapper extends InputStream -{ - private InputStream delegate; - - public InputStreamWrapper(InputStream delegate) - { - this.delegate = delegate; - } - - public int read() - throws IOException - { - return delegate.read(); - } - - public int read(byte[] bytes) - throws IOException - { - return delegate.read(bytes); - } - - public int read(byte[] bytes, int i, int i1) - throws IOException - { - return delegate.read(bytes, i, i1); - } - - public long skip(long l) - throws IOException - { - return delegate.skip(l); - } - - public int available() - throws IOException - { - return delegate.available(); - } - - public void close() - throws IOException - { - // ignored - } - - public void mark(int i) - { - delegate.mark(i); - } - - public void reset() - throws IOException - { - delegate.reset(); - } - - public boolean markSupported() - { - return delegate.markSupported(); - } -} diff --git a/src/main/java/org/scannotation/classpath/IteratorFactory.java b/src/main/java/org/scannotation/classpath/IteratorFactory.java deleted file mode 100644 index adbc091..0000000 --- a/src/main/java/org/scannotation/classpath/IteratorFactory.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.scannotation.classpath; - -import java.io.IOException; -import java.net.URL; -import java.util.concurrent.ConcurrentHashMap; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class IteratorFactory -{ - private static final ConcurrentHashMap registry = new ConcurrentHashMap(); - - static - { - registry.put("file", new FileProtocolIteratorFactory()); - } - - - public static StreamIterator create(URL url, Filter filter) throws IOException - { - String urlString = url.toString(); - if (urlString.endsWith("!/")) - { - urlString = urlString.substring(4); - urlString = urlString.substring(0, urlString.length() - 2); - url = new URL(urlString); - } - - - if (!urlString.endsWith("/")) - { - return new JarIterator(url.openStream(), filter); - } - else - { - DirectoryIteratorFactory factory = registry.get(url.getProtocol()); - if (factory == null) throw new IOException("Unable to scan directory of protocol: " + url.getProtocol()); - return factory.create(url, filter); - } - } -} diff --git a/src/main/java/org/scannotation/classpath/JarIterator.java b/src/main/java/org/scannotation/classpath/JarIterator.java deleted file mode 100644 index dd4cf35..0000000 --- a/src/main/java/org/scannotation/classpath/JarIterator.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.scannotation.classpath; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.jar.JarEntry; -import java.util.jar.JarInputStream; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class JarIterator implements StreamIterator -{ - JarInputStream jar; - JarEntry next; - Filter filter; - boolean initial = true; - boolean closed = false; - - public JarIterator(File file, Filter filter) throws IOException - { - this(new FileInputStream(file), filter); - } - - - public JarIterator(InputStream is, Filter filter) throws IOException - { - this.filter = filter; - jar = new JarInputStream(is); - } - - private void setNext() - { - initial = true; - try - { - if (next != null) jar.closeEntry(); - next = null; - do - { - next = jar.getNextJarEntry(); - } while (next != null && (next.isDirectory() || (filter == null || !filter.accepts(next.getName())))); - if (next == null) - { - close(); - } - } - catch (IOException e) - { - throw new RuntimeException("failed to browse jar", e); - } - } - - public InputStream next() - { - if (closed || (next == null && !initial)) return null; - setNext(); - if (next == null) return null; - return new InputStreamWrapper(jar); - } - - public void close() - { - try - { - closed = true; - jar.close(); - } - catch (IOException ignored) - { - - } - - } -} diff --git a/src/main/java/org/scannotation/classpath/StreamIterator.java b/src/main/java/org/scannotation/classpath/StreamIterator.java deleted file mode 100644 index 410df8f..0000000 --- a/src/main/java/org/scannotation/classpath/StreamIterator.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.scannotation.classpath; - -import java.io.InputStream; - -/** - * Simpler iterator than java.util.iterator. Things like JarInputStream does not allow you to implement hasNext() - * - * @author Bill Burke - * @version $Revision: 1 $ - */ -public interface StreamIterator -{ - /** - * User is resposible for closing the InputStream returned - * - * @return null if no more streams left to iterate on - */ - InputStream next(); - - /** - * Cleanup any open resources of the iterator - * - */ - void close(); -} diff --git a/src/main/java/org/scannotation/classpath/WarUrlFinder.java b/src/main/java/org/scannotation/classpath/WarUrlFinder.java deleted file mode 100644 index 73d3372..0000000 --- a/src/main/java/org/scannotation/classpath/WarUrlFinder.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.scannotation.classpath; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Set; - -/** - * @author Bill Burke - * @version $Revision: 1 $ - */ -public class WarUrlFinder -{ - public static URL[] findWebInfLibClasspaths(ServletContextEvent servletContextEvent) - { - ServletContext servletContext = servletContextEvent.getServletContext(); - return findWebInfLibClasspaths(servletContext); - } - - public static URL[] findWebInfLibClasspaths(ServletContext servletContext) - { - ArrayList list = new ArrayList(); - Set libJars = servletContext.getResourcePaths("/WEB-INF/lib"); - for (Object jar : libJars) - { - try - { - list.add(servletContext.getResource((String) jar)); - } - catch (MalformedURLException e) - { - throw new RuntimeException(e); - } - } - return list.toArray(new URL[list.size()]); - } - - public static URL findWebInfClassesPath(ServletContextEvent servletContextEvent) - { - ServletContext servletContext = servletContextEvent.getServletContext(); - return findWebInfClassesPath(servletContext); - } - - /** - * Find the URL pointing to "/WEB-INF/classes" This method may not work in conjunction with IteratorFactory - * if your servlet container does not extract the /WEB-INF/classes into a real file-based directory - * - * @param servletContext - * @return - */ - public static URL findWebInfClassesPath(ServletContext servletContext) - { - Set libJars = servletContext.getResourcePaths("/WEB-INF/classes"); - for (Object jar : libJars) - { - try - { - URL url = servletContext.getResource((String) jar); - String urlString = url.toString(); - int index = urlString.lastIndexOf("/WEB-INF/classes/"); - urlString = urlString.substring(0, index + "/WEB-INF/classes/".length()); - return new URL(urlString); - } - catch (MalformedURLException e) - { - throw new RuntimeException(e); - } - } - return null; - - } -} diff --git a/src/test/java/org/scannotation/test/TestSmoke.java b/src/test/java/org/scannotation/test/TestSmoke.java index 8f914aa..8a9331d 100644 --- a/src/test/java/org/scannotation/test/TestSmoke.java +++ b/src/test/java/org/scannotation/test/TestSmoke.java @@ -4,7 +4,7 @@ import com.titan.domain.Address; import org.junit.Assert; import org.junit.Test; import org.scannotation.AnnotationDB; -import org.scannotation.classpath.ClasspathUrlFinder; +import org.scannotation.ClasspathUrlFinder; import java.io.IOException; import java.net.URL; @@ -41,6 +41,7 @@ public class TestSmoke if (System.getProperty("java.class.path").indexOf("titan-cruise-1.0.jar") == -1) { System.err.println("WARNING!!!!!!!! CANNOT TEST testFindClasspaths(): This is a Maven2 and Surefire problem in that it doesn't set java.class.path correctly. I run this test within the IDE"); + return; } URL[] urls = ClasspathUrlFinder.findClassPaths("titan-cruise-1.0.jar"); @@ -56,6 +57,7 @@ public class TestSmoke if (System.getProperty("java.class.path").indexOf("titan-cruise-1.0.jar") == -1) { System.err.println("WARNING!!!!!!! CANNOT TEST testFindClasspaths2(): This is a Maven2 and Surefire problem in that it doesn't set java.class.path correctly. I run this test within the IDE"); + return; } URL[] urls = ClasspathUrlFinder.findClassPaths();