1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.daemon.support;
19
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.Properties;
25 import java.text.ParseException;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public final class DaemonConfiguration
49 {
50
51
52
53 protected final static String DEFAULT_CONFIG = "daemon.properties";
54
55
56
57 protected final static String PREFIX = "daemon.";
58 private final static String BTOKEN = "${";
59 private final static String ETOKEN = "}";
60
61 private final Properties configurationProperties;
62 private final Properties systemProperties;
63
64
65
66
67 static final String[] EMPTY_STRING_ARRAY = {};
68
69
70
71
72 public DaemonConfiguration()
73 {
74 configurationProperties = new Properties();
75 systemProperties = System.getProperties();
76 }
77
78
79
80
81
82
83
84 public boolean load(String fileName)
85 {
86 if (fileName == null) {
87 fileName = DEFAULT_CONFIG;
88 }
89
90 try (InputStream inputStream = new FileInputStream(fileName)) {
91 configurationProperties.clear();
92 configurationProperties.load(inputStream);
93 return true;
94 } catch (final IOException ex) {
95
96 return false;
97 }
98 }
99
100 private String expandProperty(final String propValue)
101 throws ParseException
102 {
103 final StringBuilder expanded;
104 int btoken;
105 int ctoken = 0;
106
107 if (propValue == null) {
108 return null;
109 }
110 expanded = new StringBuilder();
111 btoken = propValue.indexOf(BTOKEN);
112 while (btoken != -1) {
113 if (btoken > 0 && propValue.charAt(btoken - 1) == BTOKEN.charAt(0)) {
114
115 expanded.append(propValue.substring(ctoken, btoken));
116 ctoken = btoken + 1;
117 btoken = propValue.indexOf(BTOKEN, btoken + BTOKEN.length());
118 continue;
119 }
120 final int etoken = propValue.indexOf(ETOKEN, btoken);
121 if (etoken == -1) {
122
123 throw new ParseException("Error while looking for teminating '" +
124 ETOKEN + "'", btoken);
125 }
126 final String variable = propValue.substring(btoken + BTOKEN.length(), etoken);
127 String sysvalue = systemProperties.getProperty(variable);
128 if (sysvalue == null) {
129
130
131 sysvalue = System.getenv(variable);
132 }
133 if (sysvalue != null) {
134 final String strtoken = propValue.substring(ctoken, btoken);
135 expanded.append(strtoken);
136 expanded.append(sysvalue);
137 ctoken = etoken + ETOKEN.length();
138 }
139 btoken = propValue.indexOf(BTOKEN, etoken + ETOKEN.length());
140 }
141
142 expanded.append(propValue.substring(ctoken));
143 return expanded.toString();
144 }
145
146
147
148
149
150
151
152
153 public String getProperty(final String name)
154 throws ParseException
155 {
156 if (name == null) {
157 return null;
158 }
159 return expandProperty(configurationProperties.getProperty(PREFIX + name));
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 public String[] getPropertyArray(final String name)
178 throws ParseException
179 {
180 final ArrayList<String> list = new ArrayList<>();
181 String args;
182
183
184
185 while ((args = getProperty(name + "[" + list.size() + "]")) != null) {
186 list.add(args);
187 }
188 return list.toArray(EMPTY_STRING_ARRAY);
189 }
190 }
191