1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package ubic.basecode.util; |
16 | |
|
17 | |
import java.io.File; |
18 | |
import java.io.IOException; |
19 | |
import java.net.URISyntaxException; |
20 | |
import java.net.URL; |
21 | |
|
22 | |
import org.apache.commons.configuration2.PropertiesConfiguration; |
23 | |
import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl; |
24 | |
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; |
25 | |
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler; |
26 | |
import org.apache.commons.configuration2.convert.ListDelimiterHandler; |
27 | |
import org.apache.commons.configuration2.ex.ConfigurationException; |
28 | |
import org.apache.commons.configuration2.io.FileHandler; |
29 | |
import org.apache.commons.configuration2.io.FileLocator; |
30 | |
import org.apache.commons.configuration2.io.FileLocatorUtils; |
31 | |
import org.apache.commons.io.FileUtils; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class ConfigUtils { |
40 | |
|
41 | 1 | private static final ListDelimiterHandler LIST_DELIMITER_HANDLER = new DefaultListDelimiterHandler( ',' ); |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public static FileBasedConfigurationBuilder<PropertiesConfiguration> getConfigBuilder( File file ) |
48 | |
throws ConfigurationException { |
49 | 6 | if ( !file.exists() ) { |
50 | |
try { |
51 | 3 | FileTools.touch( file ); |
52 | 0 | } catch ( IOException e ) { |
53 | 0 | throw new ConfigurationException( "Couldn't create the file: " + e.getMessage() ); |
54 | 3 | } |
55 | |
} |
56 | 6 | FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>( |
57 | |
PropertiesConfiguration.class ); |
58 | 6 | builder.configure( new FileBasedBuilderParametersImpl().setFile( file ).setListDelimiterHandler( LIST_DELIMITER_HANDLER ) ); |
59 | 6 | return builder; |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public static FileBasedConfigurationBuilder<PropertiesConfiguration> getConfigBuilder( String name ) |
67 | |
throws ConfigurationException { |
68 | 2 | File f = locateConfig( name ); |
69 | 2 | return getConfigBuilder( f ); |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public static FileBasedConfigurationBuilder<PropertiesConfiguration> getConfigBuilder( URL url ) |
78 | |
throws ConfigurationException { |
79 | |
File file; |
80 | |
try { |
81 | 2 | file = new File( url.toURI() ); |
82 | 2 | return getConfigBuilder( file ); |
83 | 0 | } catch ( URISyntaxException e ) { |
84 | 0 | throw new ConfigurationException( "Couldn't map url to a uri: " + url ); |
85 | |
} |
86 | |
|
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public static PropertiesConfiguration loadClasspathConfig( String name ) throws ConfigurationException { |
96 | 0 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
97 | |
|
98 | 0 | URL url = loader.getResource( name ); |
99 | 0 | if ( url == null ) { |
100 | 0 | throw new ConfigurationException( "Couldn't locate: " + name ); |
101 | |
} |
102 | |
|
103 | 0 | PropertiesConfiguration pc = createConfiguration(); |
104 | 0 | FileHandler handler = new FileHandler( pc ); |
105 | 0 | handler.setURL( url ); |
106 | 0 | handler.load(); |
107 | 0 | return pc; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public static PropertiesConfiguration loadConfig( File file ) throws ConfigurationException { |
116 | 7 | if ( !file.exists() ) { |
117 | |
try { |
118 | 4 | FileTools.touch( file ); |
119 | 0 | } catch ( IOException e ) { |
120 | 0 | throw new ConfigurationException( "Couldn't create the file: " + e.getMessage() ); |
121 | 4 | } |
122 | |
} |
123 | 7 | PropertiesConfiguration pc = createConfiguration(); |
124 | 7 | FileHandler handler = new FileHandler( pc ); |
125 | 7 | handler.setFile( file ); |
126 | 7 | handler.load(); |
127 | 7 | return pc; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public static PropertiesConfiguration loadConfig( String name ) throws ConfigurationException { |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 3 | File f = locateConfig( name ); |
141 | |
|
142 | 3 | if ( f == null ) { |
143 | |
|
144 | |
|
145 | |
|
146 | 0 | return loadClasspathConfig( name ); |
147 | |
} |
148 | |
|
149 | 3 | return loadConfig( f ); |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public static PropertiesConfiguration loadConfig( URL url ) throws ConfigurationException { |
158 | |
|
159 | |
try { |
160 | 2 | File file = new File( url.toURI() ); |
161 | 2 | return loadConfig( file ); |
162 | 0 | } catch ( Exception e ) { |
163 | 0 | throw new ConfigurationException( "Couldn't map url to a uri: " + url ); |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
private static File locateConfig( String name ) throws ConfigurationException { |
173 | |
File f; |
174 | 5 | FileLocator fl = FileLocatorUtils.fileLocator().fileName( name ).create(); |
175 | 5 | URL location = FileLocatorUtils.locate( fl ); |
176 | 5 | if ( location == null ) { |
177 | 3 | f = new File( name ); |
178 | 3 | if ( f.isAbsolute() ) { |
179 | 2 | return f; |
180 | |
} |
181 | 1 | return new File( FileUtils.getUserDirectory(), name ); |
182 | |
} |
183 | |
try { |
184 | 2 | return new File( location.toURI() ); |
185 | 0 | } catch ( Exception e ) { |
186 | 0 | throw new ConfigurationException( "Couldn't map url to a uri: " + name ); |
187 | |
} |
188 | |
} |
189 | |
|
190 | |
public static URL locate( String name ) { |
191 | 0 | FileLocator fl = FileLocatorUtils.fileLocator().fileName( name ).create(); |
192 | 0 | return FileLocatorUtils.locate( fl ); |
193 | |
} |
194 | |
|
195 | |
private static PropertiesConfiguration createConfiguration() { |
196 | 7 | PropertiesConfiguration pc = new PropertiesConfiguration(); |
197 | 7 | pc.setListDelimiterHandler( LIST_DELIMITER_HANDLER ); |
198 | 7 | return pc; |
199 | |
} |
200 | |
} |