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