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