1 package org.scannotation.classpath;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
7 import java.util.ArrayList;
8 import java.util.Enumeration;
10 import java.util.StringTokenizer;
13 * Various functions to locate URLs to scan
15 * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
16 * @version $Revision: 1 $
18 public class ClasspathUrlFinder
22 * Find the classpath URLs for a specific classpath resource. The classpath URL is extracted
23 * from loader.getResources() using the baseResource.
28 public static URL[] findResourceBases(String baseResource, ClassLoader loader)
30 ArrayList<URL> list = new ArrayList<URL>();
33 Enumeration<URL> urls = loader.getResources(baseResource);
34 while (urls.hasMoreElements())
36 URL url = urls.nextElement();
37 list.add(findResourceBase(url, baseResource));
42 throw new RuntimeException(e);
44 return list.toArray(new URL[list.size()]);
48 * Find the classpath URLs for a specific classpath resource. The classpath URL is extracted
49 * from loader.getResources() using the baseResource.
54 public static URL[] findResourceBases(String baseResource)
56 return findResourceBases(baseResource, Thread.currentThread().getContextClassLoader());
59 private static URL findResourceBase(URL url, String baseResource)
61 String urlString = url.toString();
62 int idx = urlString.lastIndexOf(baseResource);
63 urlString = urlString.substring(0, idx);
67 deployUrl = new URL(urlString);
69 catch (MalformedURLException e)
71 throw new RuntimeException(e);
77 * Find the classpath URL for a specific classpath resource. The classpath URL is extracted
78 * from Thread.currentThread().getContextClassLoader().getResource() using the baseResource.
83 public static URL findResourceBase(String baseResource)
85 return findResourceBase(baseResource, Thread.currentThread().getContextClassLoader());
89 * Find the classpath URL for a specific classpath resource. The classpath URL is extracted
90 * from loader.getResource() using the baseResource.
96 public static URL findResourceBase(String baseResource, ClassLoader loader)
98 URL url = loader.getResource(baseResource);
99 return findResourceBase(url, baseResource);
103 * Find the classpath for the particular class
108 public static URL findClassBase(Class clazz)
110 String resource = clazz.getName().replace('.', '/') + ".class";
111 return findResourceBase(resource, clazz.getClassLoader());
115 * Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH
119 public static URL[] findClassPaths()
121 List<URL> list = new ArrayList<URL>();
122 String classpath = System.getProperty("java.class.path");
123 StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
125 while (tokenizer.hasMoreTokens())
127 String path = tokenizer.nextToken();
128 File fp = new File(path);
129 if (!fp.exists()) throw new RuntimeException("File in java.class.path does not exist: " + fp);
132 list.add(fp.toURL());
134 catch (MalformedURLException e)
136 throw new RuntimeException(e);
139 return list.toArray(new URL[list.size()]);
143 * Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH
145 * paths is used as a filter to only include paths that have the specific relative file within it
147 * @param paths comma list of files that should exist in a particular path
150 public static URL[] findClassPaths(String... paths)
152 ArrayList<URL> list = new ArrayList<URL>();
154 String classpath = System.getProperty("java.class.path");
155 StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
156 for (int i = 0; i < paths.length; i++)
158 paths[i] = paths[i].trim();
161 while (tokenizer.hasMoreTokens())
163 String path = tokenizer.nextToken().trim();
164 boolean found = false;
165 for (String wantedPath : paths)
167 if (path.endsWith(File.separator + wantedPath))
173 if (!found) continue;
174 File fp = new File(path);
175 if (!fp.exists()) throw new RuntimeException("File in java.class.path does not exists: " + fp);
178 list.add(fp.toURL());
180 catch (MalformedURLException e)
182 throw new RuntimeException(e);
185 return list.toArray(new URL[list.size()]);