Renaming artifact and packages
[scannotation] / src / main / java / org / scannotation / ClasspathUrlFinder.java
1 package org.scannotation;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.Enumeration;
9 import java.util.List;
10 import java.util.StringTokenizer;
11
12 /**
13  * Various functions to locate URLs to scan
14  *
15  * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
16  * @version $Revision: 1 $
17  */
18 public class ClasspathUrlFinder
19 {
20
21    /**
22     * Find the classpath URLs for a specific classpath resource.  The classpath URL is extracted
23     * from loader.getResources() using the baseResource.
24     *
25     * @param baseResource
26     * @return
27     */
28    public static URL[] findResourceBases(String baseResource, ClassLoader loader)
29    {
30       ArrayList<URL> list = new ArrayList<URL>();
31       try
32       {
33          Enumeration<URL> urls = loader.getResources(baseResource);
34          while (urls.hasMoreElements())
35          {
36             URL url = urls.nextElement();
37             list.add(findResourceBase(url, baseResource));
38          }
39       }
40       catch (IOException e)
41       {
42          throw new RuntimeException(e);
43       }
44       return list.toArray(new URL[list.size()]);
45    }
46
47    /**
48     * Find the classpath URLs for a specific classpath resource.  The classpath URL is extracted
49     * from loader.getResources() using the baseResource.
50     *
51     * @param baseResource
52     * @return
53     */
54    public static URL[] findResourceBases(String baseResource)
55    {
56       return findResourceBases(baseResource, Thread.currentThread().getContextClassLoader());
57    }
58
59    private static URL findResourceBase(URL url, String baseResource)
60    {
61       String urlString = url.toString();
62       int idx = urlString.lastIndexOf(baseResource);
63       urlString = urlString.substring(0, idx);
64       URL deployUrl = null;
65       try
66       {
67          deployUrl = new URL(urlString);
68       }
69       catch (MalformedURLException e)
70       {
71          throw new RuntimeException(e);
72       }
73       return deployUrl;
74    }
75
76    /**
77     * Find the classpath URL for a specific classpath resource.  The classpath URL is extracted
78     * from Thread.currentThread().getContextClassLoader().getResource() using the baseResource.
79     *
80     * @param baseResource
81     * @return
82     */
83    public static URL findResourceBase(String baseResource)
84    {
85       return findResourceBase(baseResource, Thread.currentThread().getContextClassLoader());
86    }
87
88    /**
89     * Find the classpath URL for a specific classpath resource.  The classpath URL is extracted
90     * from loader.getResource() using the baseResource.
91     *
92     * @param baseResource
93     * @param loader
94     * @return
95     */
96    public static URL findResourceBase(String baseResource, ClassLoader loader)
97    {
98       URL url = loader.getResource(baseResource);
99       return findResourceBase(url, baseResource);
100    }
101
102    /**
103     * Find the classpath for the particular class
104     *
105     * @param clazz
106     * @return
107     */
108    public static URL findClassBase(Class clazz)
109    {
110       String resource = clazz.getName().replace('.', '/') + ".class";
111       return findResourceBase(resource, clazz.getClassLoader());
112    }
113
114    /**
115     * Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH
116     *
117     * @return
118     */
119    public static URL[] findClassPaths()
120    {
121       List<URL> list = new ArrayList<URL>();
122       String classpath = System.getProperty("java.class.path");
123       StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
124
125       while (tokenizer.hasMoreTokens())
126       {
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);
130          try
131          {
132             list.add(fp.toURL());
133          }
134          catch (MalformedURLException e)
135          {
136             throw new RuntimeException(e);
137          }
138       }
139       return list.toArray(new URL[list.size()]);
140    }
141
142    /**
143     * Uses the java.class.path system property to obtain a list of URLs that represent the CLASSPATH
144     * <p/>
145     * paths is used as a filter to only include paths that have the specific relative file within it
146     *
147     * @param paths comma list of files that should exist in a particular path
148     * @return
149     */
150    public static URL[] findClassPaths(String... paths)
151    {
152       ArrayList<URL> list = new ArrayList<URL>();
153
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++)
157       {
158          paths[i] = paths[i].trim();
159       }
160
161       while (tokenizer.hasMoreTokens())
162       {
163          String path = tokenizer.nextToken().trim();
164          boolean found = false;
165          for (String wantedPath : paths)
166          {
167             if (path.endsWith(File.separator + wantedPath))
168             {
169                found = true;
170                break;
171             }
172          }
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);
176          try
177          {
178             list.add(fp.toURL());
179          }
180          catch (MalformedURLException e)
181          {
182             throw new RuntimeException(e);
183          }
184       }
185       return list.toArray(new URL[list.size()]);
186    }
187
188
189 }
190