X-Git-Url: https://juplo.de/gitweb/?p=scannotation;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fscannotation%2Fclasspath%2FClasspathUrlFinder.java;fp=src%2Fmain%2Fjava%2Forg%2Fscannotation%2Fclasspath%2FClasspathUrlFinder.java;h=0000000000000000000000000000000000000000;hp=f56be223665fed92423a99ba951650c2bd224d49;hb=36e6637926203201648e7892ec6ee1240807218e;hpb=58b6663aae5313b41167d92851981ca549cbb461 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()]); - } - - -} -