1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2.builder;
18
19 import java.io.File;
20 import java.net.URL;
21 import java.util.Map;
22
23 import org.apache.commons.configuration2.io.FileHandler;
24 import org.apache.commons.configuration2.io.FileLocationStrategy;
25 import org.apache.commons.configuration2.io.FileSystem;
26 import org.apache.commons.configuration2.io.URLConnectionOptions;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class FileBasedBuilderParametersImpl extends BasicBuilderParameters implements FileBasedBuilderProperties<FileBasedBuilderParametersImpl> {
47
48 private static final String PARAM_KEY = RESERVED_PARAMETER_PREFIX + "fileBased";
49
50
51 private static final String PROP_REFRESH_DELAY = "reloadingRefreshDelay";
52
53
54 private static final String PROP_DETECTOR_FACTORY = "reloadingDetectorFactory";
55
56
57
58
59 private FileHandler fileHandler;
60
61
62 private ReloadingDetectorFactory reloadingDetectorFactory;
63
64
65 private Long reloadingRefreshDelay;
66
67
68
69
70 public FileBasedBuilderParametersImpl() {
71 this(null);
72 }
73
74
75
76
77
78
79
80 public FileBasedBuilderParametersImpl(final FileHandler handler) {
81 fileHandler = handler != null ? handler : new FileHandler();
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95 public static FileBasedBuilderParametersImpl fromMap(final Map<String, ?> map) {
96 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl(FileHandler.fromMap(map));
97 if (map != null) {
98 params.setReloadingRefreshDelay((Long) map.get(PROP_REFRESH_DELAY));
99 params.setReloadingDetectorFactory((ReloadingDetectorFactory) map.get(PROP_DETECTOR_FACTORY));
100 }
101 return params;
102 }
103
104
105
106
107
108
109
110
111
112 public static FileBasedBuilderParametersImpl fromParameters(final Map<String, ?> params) {
113 return fromParameters(params, false);
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127 public static FileBasedBuilderParametersImpl fromParameters(final Map<String, ?> params, final boolean createIfMissing) {
128 if (params == null) {
129 throw new IllegalArgumentException("Parameters map must not be null!");
130 }
131
132 FileBasedBuilderParametersImpl instance = (FileBasedBuilderParametersImpl) params.get(PARAM_KEY);
133 if (instance == null && createIfMissing) {
134 instance = new FileBasedBuilderParametersImpl();
135 }
136 return instance;
137 }
138
139
140
141
142 @Override
143 public FileBasedBuilderParametersImpl clone() {
144 final FileBasedBuilderParametersImpl copy = (FileBasedBuilderParametersImpl) super.clone();
145 copy.fileHandler = new FileHandler(fileHandler.getContent(), fileHandler);
146 return copy;
147 }
148
149
150
151
152
153
154
155 public FileHandler getFileHandler() {
156 return fileHandler;
157 }
158
159
160
161
162
163
164 @Override
165 public Map<String, Object> getParameters() {
166 final Map<String, Object> params = super.getParameters();
167 params.put(PARAM_KEY, this);
168 return params;
169 }
170
171
172
173
174
175
176
177 public ReloadingDetectorFactory getReloadingDetectorFactory() {
178 return reloadingDetectorFactory;
179 }
180
181
182
183
184
185
186 public Long getReloadingRefreshDelay() {
187 return reloadingRefreshDelay;
188 }
189
190
191
192
193 @Override
194 public void inheritFrom(final Map<String, ?> source) {
195 super.inheritFrom(source);
196
197 final FileBasedBuilderParametersImpl srcParams = fromParameters(source);
198 if (srcParams != null) {
199 setFileSystem(srcParams.getFileHandler().getFileSystem());
200 setLocationStrategy(srcParams.getFileHandler().getLocationStrategy());
201 if (srcParams.getFileHandler().getEncoding() != null) {
202 setEncoding(srcParams.getFileHandler().getEncoding());
203 }
204 if (srcParams.getReloadingDetectorFactory() != null) {
205 setReloadingDetectorFactory(srcParams.getReloadingDetectorFactory());
206 }
207 if (srcParams.getReloadingRefreshDelay() != null) {
208 setReloadingRefreshDelay(srcParams.getReloadingRefreshDelay());
209 }
210 }
211 }
212
213 @Override
214 public FileBasedBuilderParametersImpl setBasePath(final String path) {
215 getFileHandler().setBasePath(path);
216 return this;
217 }
218
219 @Override
220 public FileBasedBuilderParametersImpl setEncoding(final String enc) {
221 getFileHandler().setEncoding(enc);
222 return this;
223 }
224
225 @Override
226 public FileBasedBuilderParametersImpl setFile(final File file) {
227 getFileHandler().setFile(file);
228 return this;
229 }
230
231 @Override
232 public FileBasedBuilderParametersImpl setFileName(final String name) {
233 getFileHandler().setFileName(name);
234 return this;
235 }
236
237 @Override
238 public FileBasedBuilderParametersImpl setFileSystem(final FileSystem fs) {
239 getFileHandler().setFileSystem(fs);
240 return this;
241 }
242
243 @Override
244 public FileBasedBuilderParametersImpl setLocationStrategy(final FileLocationStrategy strategy) {
245 getFileHandler().setLocationStrategy(strategy);
246 return this;
247 }
248
249 @Override
250 public FileBasedBuilderParametersImpl setPath(final String path) {
251 getFileHandler().setPath(path);
252 return this;
253 }
254
255 @Override
256 public FileBasedBuilderParametersImpl setReloadingDetectorFactory(final ReloadingDetectorFactory reloadingDetectorFactory) {
257 this.reloadingDetectorFactory = reloadingDetectorFactory;
258 return this;
259 }
260
261 @Override
262 public FileBasedBuilderParametersImpl setReloadingRefreshDelay(final Long reloadingRefreshDelay) {
263 this.reloadingRefreshDelay = reloadingRefreshDelay;
264 return this;
265 }
266
267 @Override
268 public FileBasedBuilderParametersImpl setURL(final URL url) {
269 getFileHandler().setURL(url);
270 return this;
271 }
272
273 @Override
274 public FileBasedBuilderParametersImpl setURL(final URL url, final URLConnectionOptions urlConnectionOptions) {
275 getFileHandler().setURL(url, urlConnectionOptions);
276 return this;
277 }
278 }