|
@@ -10,7 +10,7 @@ |
10 |
10 |
|
{ |
11 |
11 |
|
public static final String CURRENT_DIR = "."; |
12 |
12 |
|
|
13 |
|
- |
private String mRelativeConfigFilePath; |
|
13 |
+ |
protected final String mRelativeConfigFilePath; |
14 |
14 |
|
private ConfigAccessorFactory mConfigAccessorFactory; |
15 |
15 |
|
private List<String> mAdditionalSearchPaths = new ArrayList<String>(); |
16 |
16 |
|
|
|
@@ -24,7 +24,7 @@ |
24 |
24 |
|
public ConfigAccessorLocator( String pRelativeConfigFilePath, ConfigAccessorFactory pConfigAccessorFactory, String... pAdditionalSearchPaths ) |
25 |
25 |
|
{ |
26 |
26 |
|
this( pRelativeConfigFilePath, pConfigAccessorFactory ); |
27 |
|
- |
addSearchPaths( pAdditionalSearchPaths ); |
|
27 |
+ |
addSearchPaths(pAdditionalSearchPaths); |
28 |
28 |
|
} |
29 |
29 |
|
|
30 |
30 |
|
@SuppressWarnings({"UnusedDeclaration"}) |
|
@@ -34,6 +34,25 @@ |
34 |
34 |
|
addSearchPaths( pAdditionalSearchPaths ); |
35 |
35 |
|
} |
36 |
36 |
|
|
|
37 |
+ |
public ConfigAccessorLocator( String pRelativeConfigFilePath ) |
|
38 |
+ |
{ |
|
39 |
+ |
this( pRelativeConfigFilePath, ConfigAccessorFactoryProperties.INSTANCE ); |
|
40 |
+ |
} |
|
41 |
+ |
|
|
42 |
+ |
@SuppressWarnings({"UnusedDeclaration"}) |
|
43 |
+ |
public ConfigAccessorLocator( String pRelativeConfigFilePath, String... pAdditionalSearchPaths ) |
|
44 |
+ |
{ |
|
45 |
+ |
this( pRelativeConfigFilePath ); |
|
46 |
+ |
addSearchPaths(pAdditionalSearchPaths); |
|
47 |
+ |
} |
|
48 |
+ |
|
|
49 |
+ |
@SuppressWarnings({"UnusedDeclaration"}) |
|
50 |
+ |
public ConfigAccessorLocator( String pRelativeConfigFilePath, List<String> pAdditionalSearchPaths ) |
|
51 |
+ |
{ |
|
52 |
+ |
this( pRelativeConfigFilePath ); |
|
53 |
+ |
addSearchPaths( pAdditionalSearchPaths ); |
|
54 |
+ |
} |
|
55 |
+ |
|
37 |
56 |
|
private void addSearchPath( String pSearchPath ) |
38 |
57 |
|
{ |
39 |
58 |
|
if ( pSearchPath != null ) |
|
@@ -76,34 +95,22 @@ |
76 |
95 |
|
public ConfigDataAccessor createConfigDataAccessor() |
77 |
96 |
|
{ |
78 |
97 |
|
ConfigDataAccessor found = searchForConfigFileInFileSystem(); |
79 |
|
- |
if ( found != null ) |
|
98 |
+ |
if ( found == null ) |
80 |
99 |
|
{ |
81 |
|
- |
return found; |
82 |
|
- |
} |
83 |
|
- |
InputStream is = getClass().getResourceAsStream( mRelativeConfigFilePath ); |
84 |
|
- |
if ( is != null ) |
85 |
|
- |
{ |
86 |
|
- |
try |
|
100 |
+ |
if ( null == (found = loadConfigFileAsResource()) ) |
87 |
101 |
|
{ |
88 |
|
- |
ConfigDataAccessor configAccessor = mConfigAccessorFactory.create( null, // |
89 |
|
- |
mRelativeConfigFilePath, // |
90 |
|
- |
is ); |
91 |
|
- |
System.out.println( "Loaded configuration as 'resource': " + mRelativeConfigFilePath ); |
92 |
|
- |
return configAccessor; |
93 |
|
- |
} |
94 |
|
- |
catch ( IOException e ) |
95 |
|
- |
{ |
96 |
|
- |
throw newErrorMessage( "load as 'resource': " + mRelativeConfigFilePath, e ); |
97 |
|
- |
} |
98 |
|
- |
finally |
99 |
|
- |
{ |
100 |
|
- |
Utils.dispose( is ); |
|
102 |
+ |
throw unableToLocate(); |
101 |
103 |
|
} |
102 |
104 |
|
} |
103 |
|
- |
throw newErrorMessage( "locate", null ); |
|
105 |
+ |
return found; |
104 |
106 |
|
} |
105 |
107 |
|
|
106 |
|
- |
private ConfigDataAccessor searchForConfigFileInFileSystem() |
|
108 |
+ |
protected RuntimeException unableToLocate() |
|
109 |
+ |
{ |
|
110 |
+ |
return newErrorMessage( "locate", null ); |
|
111 |
+ |
} |
|
112 |
+ |
|
|
113 |
+ |
protected ConfigDataAccessor searchForConfigFileInFileSystem() |
107 |
114 |
|
{ |
108 |
115 |
|
ConfigDataAccessor found = searchForConfigFileAtSystemPropertyUserHomeDir(); |
109 |
116 |
|
if ( found != null ) |
|
@@ -130,7 +137,7 @@ |
130 |
137 |
|
return searchForConfigFileAtSystemProperty( "user.dir" ); |
131 |
138 |
|
} |
132 |
139 |
|
|
133 |
|
- |
private RuntimeException newErrorMessage( String pWhy, Throwable pCause ) |
|
140 |
+ |
protected RuntimeException newErrorMessage( String pWhy, Throwable pCause ) |
134 |
141 |
|
{ |
135 |
142 |
|
return new IllegalStateException( "Unable to " + pWhy + " '" + mRelativeConfigFilePath + "'," + " looked in the following Hierarchies: " + mCheckedForFileAt, pCause ); |
136 |
143 |
|
} |
|
@@ -165,16 +172,21 @@ |
165 |
172 |
|
} |
166 |
173 |
|
catch ( IOException e ) |
167 |
174 |
|
{ |
168 |
|
- |
return null; // Now going to fail, but if it does, then ignore |
|
175 |
+ |
return null; // Not going to fail, but if it does, then ignore |
169 |
176 |
|
} |
170 |
177 |
|
} |
171 |
178 |
|
String fileRelativeFrom = locateFile( new File( pParentPath ), mRelativeConfigFilePath ); |
172 |
|
- |
if ( fileRelativeFrom == null ) |
173 |
|
- |
{ |
174 |
|
- |
return null; |
175 |
|
- |
} |
|
179 |
+ |
return (fileRelativeFrom == null) ? null : loadConfigAccessorFrom(new File(fileRelativeFrom, mRelativeConfigFilePath)); |
|
180 |
+ |
} |
176 |
181 |
|
|
177 |
|
- |
File fileAt = new File( fileRelativeFrom, mRelativeConfigFilePath ); |
|
182 |
+ |
protected ConfigDataAccessor loadConfigFileAsResource() |
|
183 |
+ |
{ |
|
184 |
+ |
InputStream is = getClass().getResourceAsStream( mRelativeConfigFilePath ); |
|
185 |
+ |
return ( is == null ) ? null : loadConfigAccessorFrom(is, null, "'resource': " + mRelativeConfigFilePath ); |
|
186 |
+ |
} |
|
187 |
+ |
|
|
188 |
+ |
protected ConfigDataAccessor loadConfigAccessorFrom(File fileAt) { |
|
189 |
+ |
String zFrom = "file: " + fileAt.getAbsolutePath(); |
178 |
190 |
|
InputStream is; |
179 |
191 |
|
try |
180 |
192 |
|
{ |
|
@@ -182,21 +194,25 @@ |
182 |
194 |
|
} |
183 |
195 |
|
catch ( IOException e ) |
184 |
196 |
|
{ |
185 |
|
- |
throw newErrorMessage( "open (" + fileAt.getAbsolutePath() + ")", e ); |
|
197 |
+ |
throw newErrorMessage( "open (" + zFrom + ")", e ); |
186 |
198 |
|
} |
|
199 |
+ |
return loadConfigAccessorFrom(is, fileAt.getParent(), zFrom); |
|
200 |
+ |
} |
|
201 |
+ |
|
|
202 |
+ |
private ConfigDataAccessor loadConfigAccessorFrom(InputStream pIS, String pFromDirectory, String pFrom ) { |
187 |
203 |
|
try |
188 |
204 |
|
{ |
189 |
|
- |
ConfigDataAccessor configAccessor = mConfigAccessorFactory.create( fileRelativeFrom, mRelativeConfigFilePath, is ); |
190 |
|
- |
System.out.println( "Loaded configuration from: " + fileAt.getAbsolutePath() ); |
|
205 |
+ |
ConfigDataAccessor configAccessor = mConfigAccessorFactory.create( pFromDirectory, mRelativeConfigFilePath, pIS ); |
|
206 |
+ |
System.out.println( "Loaded configuration from " + pFrom ); |
191 |
207 |
|
return configAccessor; |
192 |
208 |
|
} |
193 |
209 |
|
catch ( IOException e ) |
194 |
210 |
|
{ |
195 |
|
- |
throw newErrorMessage( "load (" + fileAt.getAbsolutePath() + ")", e ); |
|
211 |
+ |
throw newErrorMessage( "load (" + pFrom + ")", e ); |
196 |
212 |
|
} |
197 |
213 |
|
finally |
198 |
214 |
|
{ |
199 |
|
- |
Utils.dispose( is ); |
|
215 |
+ |
Utils.dispose(pIS); |
200 |
216 |
|
} |
201 |
217 |
|
} |
202 |
218 |
|
|