|
1 |
| package com.agical.rdoc.core.resourcemanager.impl; |
|
2 |
| |
|
3 |
| import java.io.File; |
|
4 |
| import java.io.IOException; |
|
5 |
| import java.io.InputStream; |
|
6 |
| import java.net.MalformedURLException; |
|
7 |
| import java.net.URL; |
|
8 |
| import java.util.Enumeration; |
|
9 |
| import java.util.Iterator; |
|
10 |
| import java.util.Properties; |
|
11 |
| |
|
12 |
| import com.agical.rdoc.core.exception.RDocConfigurationSystemException; |
|
13 |
| import com.agical.rdoc.core.resourcemanager.NoSuchResourceException; |
|
14 |
| import com.agical.rdoc.core.resourcemanager.ResourceProvider; |
|
15 |
| import com.agical.rdoc.core.resourcemanager.ResourceRoot; |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| public class PropertiesResourceProvider implements ResourceProvider { |
|
23 |
| |
|
24 |
| private File executionDir; |
|
25 |
| private Properties properties; |
|
26 |
| private ResourceRoot[] sourceRoots; |
|
27 |
| private String propertiesFileName; |
|
28 |
| private final BasedirProvider basedirProvider; |
|
29 |
| |
|
30 |
14
| public PropertiesResourceProvider(String propertiesFileName, BasedirProvider basedirProvider) {
|
|
31 |
14
| super();
|
|
32 |
14
| this.propertiesFileName = propertiesFileName;
|
|
33 |
14
| this.basedirProvider = basedirProvider;
|
|
34 |
| } |
|
35 |
| |
|
36 |
32
| public ResourceRoot[] getRoots() {
|
|
37 |
32
| if( sourceRoots == null ) {
|
|
38 |
9
| properties = new Properties();
|
|
39 |
9
| InputStream inputStream = null;
|
|
40 |
9
| try {
|
|
41 |
9
| Enumeration urls = Thread.currentThread().getContextClassLoader().getResources( propertiesFileName );
|
|
42 |
9
| if( !urls.hasMoreElements() ) {
|
|
43 |
0
| throw getConfigurationException();
|
|
44 |
| } |
|
45 |
9
| for( ; urls.hasMoreElements(); ) {
|
|
46 |
9
| URL nextElement = (URL)urls.nextElement();
|
|
47 |
9
| if( nextElement == null ) {
|
|
48 |
0
| throw getConfigurationException();
|
|
49 |
| } |
|
50 |
9
| inputStream = (nextElement).openStream();
|
|
51 |
9
| if( inputStream != null ) {
|
|
52 |
9
| properties.load( inputStream );
|
|
53 |
9
| inputStream.close();
|
|
54 |
| } else { |
|
55 |
0
| throw new RDocConfigurationSystemException( "Could not read resource " + nextElement );
|
|
56 |
| } |
|
57 |
| } |
|
58 |
| } catch (IOException e) { |
|
59 |
0
| throw new NoSuchResourceException( propertiesFileName, e );
|
|
60 |
| } |
|
61 |
9
| Iterator iter = null;
|
|
62 |
9
| sourceRoots = new ResourceRoot[properties.size()];
|
|
63 |
9
| executionDir = basedirProvider.getBasedir();
|
|
64 |
9
| int i = 0;
|
|
65 |
9
| for( iter = properties.keySet().iterator(); iter.hasNext(); ) {
|
|
66 |
17
| String key = (String) iter.next();
|
|
67 |
17
| try {
|
|
68 |
17
| sourceRoots[i] = new SimpleSourceRoot( new URL( "file:///" + new File( executionDir, properties.getProperty( key ) ) ) );
|
|
69 |
| } catch (MalformedURLException e) { |
|
70 |
0
| e.printStackTrace();
|
|
71 |
| } |
|
72 |
17
| i++;
|
|
73 |
| } |
|
74 |
| } |
|
75 |
32
| return sourceRoots;
|
|
76 |
| } |
|
77 |
| |
|
78 |
0
| private RDocConfigurationSystemException getConfigurationException() {
|
|
79 |
0
| return new RDocConfigurationSystemException( "Could not find resources named " + propertiesFileName + " on classpath." );
|
|
80 |
| } |
|
81 |
| |
|
82 |
| private class SimpleSourceRoot implements ResourceRoot { |
|
83 |
| private URL url; |
|
84 |
| |
|
85 |
17
| public SimpleSourceRoot(URL url) {
|
|
86 |
17
| super();
|
|
87 |
17
| this.url = url;
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
105
| public URL getUrl() {
|
|
92 |
105
| return url;
|
|
93 |
| } |
|
94 |
| |
|
95 |
| } |
|
96 |
| } |