1 | |
package liquibase.resource; |
2 | |
|
3 | |
import liquibase.util.StringUtils; |
4 | |
|
5 | |
import java.io.IOException; |
6 | |
import java.io.InputStream; |
7 | |
import java.net.URL; |
8 | |
import java.net.URLClassLoader; |
9 | |
import java.util.ArrayList; |
10 | |
import java.util.Enumeration; |
11 | |
import java.util.List; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
public class ClassLoaderResourceAccessor implements ResourceAccessor { |
19 | |
private ClassLoader classLoader; |
20 | |
|
21 | 32 | public ClassLoaderResourceAccessor() { |
22 | 32 | this.classLoader = getClass().getClassLoader(); |
23 | 32 | } |
24 | |
|
25 | 2 | public ClassLoaderResourceAccessor(ClassLoader classLoader) { |
26 | 2 | this.classLoader = classLoader; |
27 | 2 | } |
28 | |
|
29 | |
public InputStream getResourceAsStream(String file) throws IOException { |
30 | 28 | return classLoader.getResourceAsStream(file); |
31 | |
} |
32 | |
|
33 | |
public Enumeration<URL> getResources(String packageName) throws IOException { |
34 | 9 | return classLoader.getResources(packageName); |
35 | |
} |
36 | |
|
37 | |
public ClassLoader toClassLoader() { |
38 | 26 | return classLoader; |
39 | |
} |
40 | |
|
41 | |
@Override |
42 | |
public String toString() { |
43 | |
String description; |
44 | 0 | if (classLoader instanceof URLClassLoader) { |
45 | 0 | List<String> urls = new ArrayList<String>(); |
46 | 0 | for (URL url : ((URLClassLoader) classLoader).getURLs()) { |
47 | 0 | urls.add(url.toExternalForm()); |
48 | |
} |
49 | 0 | description = StringUtils.join(urls, ","); |
50 | 0 | } else { |
51 | 0 | description = classLoader.getClass().getName(); |
52 | |
} |
53 | 0 | return getClass().getName() + "(" + description + ")"; |
54 | |
|
55 | |
} |
56 | |
} |