1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.grinder.common;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.InputStream;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.io.PrintWriter;
32 import java.util.Enumeration;
33 import java.util.Properties;
34
35
36
37
38
39
40
41
42
43
44 public class GrinderProperties extends Properties {
45 private static final long serialVersionUID = 1;
46
47
48
49
50 public static final String SCRIPT = "grinder.script";
51
52
53
54
55 public static final String LOG_DIRECTORY = "grinder.logDirectory";
56
57
58
59
60 public static final String CONSOLE_HOST = "grinder.consoleHost";
61
62
63
64
65 public static final String CONSOLE_PORT = "grinder.consolePort";
66
67
68
69
70 public static final File DEFAULT_PROPERTIES = new File("grinder.properties");
71
72
73
74
75 public static final File DEFAULT_SCRIPT = new File("grinder.py");
76
77 private transient PrintWriter m_errorWriter =
78 new PrintWriter(System.err, true);
79
80
81 private File m_file;
82
83
84
85
86 public GrinderProperties() {
87 m_file = null;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101 public GrinderProperties(File file) throws PersistenceException {
102 setAssociatedFile(file);
103
104 if (m_file.exists()) {
105 InputStream propertiesInputStream = null;
106 try {
107 propertiesInputStream = new FileInputStream(m_file);
108 load(propertiesInputStream);
109 }
110 catch (IOException e) {
111 UncheckedInterruptedException.ioException(e);
112 throw new PersistenceException(
113 "Error loading properties file '" + m_file.getPath() + '\'', e);
114 }
115 finally {
116 Closer.close(propertiesInputStream);
117 }
118 }
119
120 final Enumeration<?> systemProperties =
121 System.getProperties().propertyNames();
122
123 while (systemProperties.hasMoreElements()) {
124 final String name = (String)systemProperties.nextElement();
125
126 if (name.startsWith("grinder.")) {
127 put(name, System.getProperty(name));
128 }
129 }
130 }
131
132
133
134
135
136
137 public final File getAssociatedFile() {
138 return m_file;
139 }
140
141
142
143
144
145
146
147
148 public final void setAssociatedFile(File file) {
149 m_file = file;
150 }
151
152
153
154
155
156
157
158 public final void save() throws PersistenceException {
159
160 if (m_file == null) {
161 throw new PersistenceException("No associated file");
162 }
163
164 OutputStream outputStream = null;
165
166 try {
167 outputStream = new FileOutputStream(m_file);
168 store(outputStream, generateFileHeader());
169 }
170 catch (IOException e) {
171 UncheckedInterruptedException.ioException(e);
172 throw new PersistenceException(
173 "Error writing properties file '" + m_file.getPath() + '\'', e);
174 }
175 finally {
176 Closer.close(outputStream);
177 }
178 }
179
180
181
182
183
184
185
186
187 public final void saveSingleProperty(String name)
188 throws PersistenceException {
189
190 if (m_file == null) {
191 throw new PersistenceException("No associated file");
192 }
193
194 try {
195 final Properties properties = new Properties();
196
197 InputStream inputStream = null;
198
199 try {
200 inputStream = new FileInputStream(m_file);
201 properties.load(inputStream);
202 }
203 catch (IOException e) {
204
205 UncheckedInterruptedException.ioException(e);
206 }
207 finally {
208 Closer.close(inputStream);
209 }
210
211 OutputStream outputStream = null;
212
213 try {
214 outputStream = new FileOutputStream(m_file);
215
216 final String value = getProperty(name);
217
218 if (value != null) {
219 properties.setProperty(name, value);
220 }
221 else {
222 properties.remove(name);
223 }
224
225 properties.store(outputStream, generateFileHeader());
226 }
227 finally {
228 Closer.close(outputStream);
229 }
230 }
231 catch (IOException e) {
232 UncheckedInterruptedException.ioException(e);
233 throw new PersistenceException(
234 "Error writing properties file '" + m_file.getPath() + '\'', e);
235 }
236 }
237
238 private String generateFileHeader() {
239 return "Properties file for The Grinder";
240 }
241
242
243
244
245
246
247 public final void setErrorWriter(PrintWriter writer) {
248 m_errorWriter = writer;
249 }
250
251
252
253
254
255
256
257
258 public final synchronized GrinderProperties
259 getPropertySubset(String prefix) {
260 final GrinderProperties result = new GrinderProperties();
261
262 final Enumeration<?> propertyNames = propertyNames();
263
264 while (propertyNames.hasMoreElements()) {
265 final String name = (String)propertyNames.nextElement();
266
267 if (name.startsWith(prefix)) {
268 result.setProperty(name.substring(prefix.length()),
269 getProperty(name));
270 }
271 }
272
273 return result;
274 }
275
276
277
278
279
280
281
282
283
284
285 public final int getInt(String propertyName, int defaultValue) {
286 final String s = getProperty(propertyName);
287
288 if (s != null) {
289 try {
290 return Integer.parseInt(s.trim());
291 }
292 catch (NumberFormatException e) {
293 m_errorWriter.println("Warning, property '" + propertyName +
294 "' does not specify an integer value");
295 }
296 }
297
298 return defaultValue;
299 }
300
301
302
303
304
305
306
307 public final void setInt(String propertyName, int value) {
308 setProperty(propertyName, Integer.toString(value));
309 }
310
311
312
313
314
315
316
317
318
319
320
321 public final long getLong(String propertyName, long defaultValue) {
322 final String s = getProperty(propertyName);
323
324 if (s != null) {
325 try {
326 return Long.parseLong(s.trim());
327 }
328 catch (NumberFormatException e) {
329 m_errorWriter.println("Warning, property '" + propertyName +
330 "' does not specify an integer value");
331 }
332 }
333
334 return defaultValue;
335 }
336
337
338
339
340
341
342
343 public final void setLong(String propertyName, long value) {
344 setProperty(propertyName, Long.toString(value));
345 }
346
347
348
349
350
351
352
353
354
355
356 public final short getShort(String propertyName, short defaultValue) {
357 final String s = getProperty(propertyName);
358
359 if (s != null) {
360 try {
361 return Short.parseShort(s.trim());
362 }
363 catch (NumberFormatException e) {
364 m_errorWriter.println("Warning, property '" + propertyName +
365 "' does not specify a short value");
366 }
367 }
368
369 return defaultValue;
370 }
371
372
373
374
375
376
377
378 public final void setShort(String propertyName, short value) {
379 setProperty(propertyName, Short.toString(value));
380 }
381
382
383
384
385
386
387
388
389
390
391 public final double getDouble(String propertyName, double defaultValue) {
392 final String s = getProperty(propertyName);
393
394 if (s != null) {
395 try {
396 return Double.parseDouble(s.trim());
397 }
398 catch (NumberFormatException e) {
399 m_errorWriter.println("Warning, property '" + propertyName +
400 "' does not specify a double value");
401 }
402 }
403
404 return defaultValue;
405 }
406
407
408
409
410
411
412
413 public final void setDouble(String propertyName, double value) {
414 setProperty(propertyName, Double.toString(value));
415 }
416
417
418
419
420
421
422
423
424
425
426 public final boolean getBoolean(String propertyName, boolean defaultValue) {
427 final String s = getProperty(propertyName);
428
429 if (s != null) {
430 return Boolean.valueOf(s).booleanValue();
431 }
432
433 return defaultValue;
434 }
435
436
437
438
439
440
441
442 public final void setBoolean(String propertyName, boolean value) {
443 setProperty(propertyName, String.valueOf(value));
444 }
445
446
447
448
449
450
451
452
453
454
455
456
457 public final File getFile(String propertyName, File defaultValue) {
458 final String s = getProperty(propertyName);
459
460 if (s != null) {
461 return new File(s);
462 }
463
464 return defaultValue;
465 }
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481 public final File resolveRelativeFile(File file) {
482
483 if (m_file != null && file != null && !file.isAbsolute()) {
484 return new File(m_file.getParentFile(), file.getPath());
485 }
486
487 return file;
488 }
489
490
491
492
493
494
495
496 public final void setFile(String propertyName, File value) {
497 setProperty(propertyName, value.getPath());
498 }
499
500
501
502
503 public static final class PersistenceException extends GrinderException {
504 private PersistenceException(String message) {
505 super(message);
506 }
507
508 private PersistenceException(String message, Throwable t) {
509 super(message, t);
510 }
511 }
512
513
514
515
516 private void readObject(java.io.ObjectInputStream in)
517 throws IOException, ClassNotFoundException {
518 in.defaultReadObject();
519 setErrorWriter(new PrintWriter(System.err, true));
520 }
521 }