1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.utilities;
39
40 import static java.lang.String.format;
41 import static org.apache.commons.io.FilenameUtils.removeExtension;
42
43 import java.io.File;
44 import java.io.IOException;
45 import java.util.Arrays;
46 import java.util.Iterator;
47 import java.util.Vector;
48 import java.util.logging.Level;
49 import java.util.logging.Logger;
50 import org.apache.commons.configuration2.CompositeConfiguration;
51 import org.apache.commons.configuration2.PropertiesConfiguration;
52 import org.apache.commons.configuration2.SystemConfiguration;
53 import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
54 import org.apache.commons.configuration2.builder.fluent.Parameters;
55 import org.apache.commons.configuration2.ex.ConfigurationException;
56
57
58
59
60
61
62
63 public class Keyword {
64
65 private static final Logger logger = Logger.getLogger(Keyword.class.getName());
66 private final String keyword;
67 private final Vector<String> data;
68
69
70
71
72
73
74 public Keyword(String k) {
75 keyword = k;
76 data = new Vector<>();
77 }
78
79
80
81
82
83
84
85 public Keyword(String k, String entry) {
86 this(k);
87 data.add(entry);
88 }
89
90
91
92
93
94
95
96 public Keyword(String k, String[] entry) {
97 this(k);
98 data.addAll(Arrays.asList(entry));
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public static CompositeConfiguration loadProperties(File file) {
120
121
122 CompositeConfiguration properties = new CompositeConfiguration();
123
124
125
126
127
128
129 PropertiesConfiguration systemConfiguration = new PropertiesConfiguration();
130 systemConfiguration.append(new SystemConfiguration());
131 systemConfiguration.setHeader("JVM system properties (i.e. command line -Dkey=value pairs).");
132 properties.addConfiguration(systemConfiguration);
133
134
135
136 if (properties.containsKey("key")) {
137 String keyString = properties.getString("key");
138 File keyFile = new File(keyString);
139 if (keyFile.exists() && keyFile.canRead()) {
140 try {
141 FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
142 new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
143 .configure(
144 new Parameters()
145 .properties()
146 .setFile(keyFile)
147 .setThrowExceptionOnMissing(true)
148 .setIncludesAllowed(false));
149 PropertiesConfiguration propertyConfiguration = builder.getConfiguration();
150 propertyConfiguration.setHeader(
151 "Structure properties from (" + keyString + ").");
152 properties.addConfiguration(propertyConfiguration);
153 properties.addProperty("propertyFile", keyFile.getCanonicalPath());
154 } catch (ConfigurationException | IOException e) {
155 logger.log(Level.INFO, " Error loading {0}.", keyFile);
156 }
157 } else {
158 logger.severe(
159 format(" Keyword file supplied at the command line does not exist: %s.", keyString));
160 }
161 } else if (file != null) {
162 String structureBasename = removeExtension(file.getAbsolutePath());
163 String propertyFilename =
164 (new File(structureBasename + ".properties").exists())
165 ? structureBasename + ".properties"
166 : (new File(structureBasename + ".prop").exists())
167 ? structureBasename + ".prop"
168 : (new File(structureBasename + ".key").exists())
169 ? structureBasename + ".key"
170 : null;
171 if (propertyFilename != null) {
172 File structurePropFile = new File(propertyFilename);
173 if (structurePropFile.canRead()) {
174 try {
175 FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
176 new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
177 .configure(
178 new Parameters()
179 .properties()
180 .setFile(structurePropFile)
181 .setThrowExceptionOnMissing(true)
182 .setIncludesAllowed(false));
183 PropertiesConfiguration propertyConfiguration = builder.getConfiguration();
184 propertyConfiguration.setHeader(
185 "Structure properties from (" + propertyFilename + ").");
186 properties.addConfiguration(propertyConfiguration);
187 properties.addProperty("propertyFile", structurePropFile.getCanonicalPath());
188 } catch (ConfigurationException | IOException e) {
189 logger.log(Level.INFO, " Error loading {0}.", structureBasename);
190 }
191 }
192 }
193 }
194
195
196 String filename = System.getProperty("user.home") + File.separator + ".ffx/ffx.properties";
197 File userPropFile = new File(filename);
198 if (userPropFile.exists() && userPropFile.canRead()) {
199 try {
200 FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
201 new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
202 .configure(
203 new Parameters()
204 .properties()
205 .setFile(userPropFile)
206 .setThrowExceptionOnMissing(true)
207
208 .setIncludesAllowed(false));
209 PropertiesConfiguration ffxConfiguration = builder.getConfiguration();
210 ffxConfiguration.setHeader("FFX user property file (" + filename + ").");
211 properties.addConfiguration(ffxConfiguration);
212 } catch (ConfigurationException e) {
213 logger.log(Level.INFO, " Error loading {0}.", filename);
214 }
215 }
216
217
218 filename = System.getenv("FFX_PROPERTIES");
219 if (filename != null) {
220 File systemPropFile = new File(filename);
221 if (systemPropFile.exists() && systemPropFile.canRead()) {
222 try {
223 FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
224 new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
225 .configure(
226 new Parameters()
227 .properties()
228 .setFile(systemPropFile)
229 .setThrowExceptionOnMissing(true)
230
231 .setIncludesAllowed(false));
232 PropertiesConfiguration envConfiguration = builder.getConfiguration();
233 envConfiguration.setHeader("Environment variable FFX_PROPERTIES (" + filename + ").");
234 properties.addConfiguration(envConfiguration);
235 } catch (ConfigurationException e) {
236 logger.log(Level.INFO, " Error loading {0}.", filename);
237 }
238 }
239 }
240
241
242 if (logger.isLoggable(Level.FINE)) {
243 Iterator<String> i = properties.getKeys();
244 StringBuilder sb = new StringBuilder();
245 sb.append(format("\n %-30s %s\n", "Property", "Value"));
246 while (i.hasNext()) {
247 String s = i.next();
248 sb.append(
249 format(" %-30s %s\n", s, Arrays.toString(properties.getList(s).toArray())));
250 }
251 logger.fine(sb.toString());
252 }
253
254 return properties;
255 }
256
257
258
259
260
261
262 public void append(String entry) {
263 data.add(entry);
264 }
265
266
267
268
269
270
271 public void append(String[] entry) {
272 data.addAll(Arrays.asList(entry));
273 }
274
275
276
277
278 public void clear() {
279 data.clear();
280 }
281
282
283
284
285
286
287 public Vector<String> getEntries() {
288 return data;
289 }
290
291
292
293
294
295
296
297 public String getEntry(int i) {
298 return data.get(i);
299 }
300
301
302
303
304
305
306 public String getKeyword() {
307 return keyword;
308 }
309
310
311
312
313 public void print() {
314 logger.info(this.toString());
315 }
316
317
318 @Override
319 public String toString() {
320 StringBuilder sb = new StringBuilder(keyword + " ");
321 for (String s : data) {
322 sb.append(s);
323 }
324 return sb.toString();
325 }
326 }