| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.commons.configuration; |
| 19 | |
|
| 20 | |
import java.io.Reader; |
| 21 | |
import java.io.Writer; |
| 22 | |
import java.io.File; |
| 23 | |
import java.io.InputStream; |
| 24 | |
import java.io.OutputStream; |
| 25 | |
import java.net.URL; |
| 26 | |
import java.util.Iterator; |
| 27 | |
|
| 28 | |
import org.apache.commons.configuration.event.ConfigurationEvent; |
| 29 | |
import org.apache.commons.configuration.event.ConfigurationListener; |
| 30 | |
import org.apache.commons.configuration.reloading.ReloadingStrategy; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public abstract class AbstractHierarchicalFileConfiguration |
| 45 | |
extends HierarchicalConfiguration |
| 46 | |
implements FileConfiguration, ConfigurationListener |
| 47 | |
{ |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
private FileConfigurationDelegate delegate; |
| 52 | |
|
| 53 | |
protected AbstractHierarchicalFileConfiguration() |
| 54 | 412 | { |
| 55 | 412 | delegate = createDelegate(); |
| 56 | 412 | initDelegate(delegate); |
| 57 | 412 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public AbstractHierarchicalFileConfiguration(String fileName) throws ConfigurationException |
| 66 | |
{ |
| 67 | 1 | this(); |
| 68 | |
|
| 69 | 1 | delegate.setFileName(fileName); |
| 70 | |
|
| 71 | |
|
| 72 | 1 | load(); |
| 73 | 1 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public AbstractHierarchicalFileConfiguration(File file) throws ConfigurationException |
| 82 | |
{ |
| 83 | 83 | this(); |
| 84 | |
|
| 85 | 83 | setFile(file); |
| 86 | |
|
| 87 | |
|
| 88 | 83 | if (file.exists()) |
| 89 | |
{ |
| 90 | 82 | load(); |
| 91 | |
} |
| 92 | 83 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public AbstractHierarchicalFileConfiguration(URL url) throws ConfigurationException |
| 101 | |
{ |
| 102 | 1 | this(); |
| 103 | |
|
| 104 | 1 | setURL(url); |
| 105 | |
|
| 106 | |
|
| 107 | 1 | load(); |
| 108 | 1 | } |
| 109 | |
|
| 110 | |
protected void addPropertyDirect(String key, Object obj) |
| 111 | |
{ |
| 112 | 66 | super.addPropertyDirect(key, obj); |
| 113 | 66 | delegate.possiblySave(); |
| 114 | 66 | } |
| 115 | |
|
| 116 | |
public void clearProperty(String key) |
| 117 | |
{ |
| 118 | 198 | super.clearProperty(key); |
| 119 | 198 | delegate.possiblySave(); |
| 120 | 198 | } |
| 121 | |
|
| 122 | |
public void clearTree(String key) |
| 123 | |
{ |
| 124 | 1 | super.clearTree(key); |
| 125 | 1 | delegate.possiblySave(); |
| 126 | 1 | } |
| 127 | |
|
| 128 | |
public void setProperty(String key, Object value) |
| 129 | |
{ |
| 130 | 20 | super.setProperty(key, value); |
| 131 | 20 | delegate.possiblySave(); |
| 132 | 20 | } |
| 133 | |
|
| 134 | |
public void load() throws ConfigurationException |
| 135 | |
{ |
| 136 | 240 | delegate.load(); |
| 137 | 231 | } |
| 138 | |
|
| 139 | |
public void load(String fileName) throws ConfigurationException |
| 140 | |
{ |
| 141 | 2 | delegate.load(fileName); |
| 142 | 2 | } |
| 143 | |
|
| 144 | |
public void load(File file) throws ConfigurationException |
| 145 | |
{ |
| 146 | 9 | delegate.load(file); |
| 147 | 7 | } |
| 148 | |
|
| 149 | |
public void load(URL url) throws ConfigurationException |
| 150 | |
{ |
| 151 | 3 | delegate.load(url); |
| 152 | 3 | } |
| 153 | |
|
| 154 | |
public void load(InputStream in) throws ConfigurationException |
| 155 | |
{ |
| 156 | 0 | delegate.load(in); |
| 157 | 0 | } |
| 158 | |
|
| 159 | |
public void load(InputStream in, String encoding) throws ConfigurationException |
| 160 | |
{ |
| 161 | 1 | delegate.load(in, encoding); |
| 162 | 1 | } |
| 163 | |
|
| 164 | |
public void save() throws ConfigurationException |
| 165 | |
{ |
| 166 | 2 | delegate.save(); |
| 167 | 2 | } |
| 168 | |
|
| 169 | |
public void save(String fileName) throws ConfigurationException |
| 170 | |
{ |
| 171 | 3 | delegate.save(fileName); |
| 172 | 3 | } |
| 173 | |
|
| 174 | |
public void save(File file) throws ConfigurationException |
| 175 | |
{ |
| 176 | 7 | delegate.save(file); |
| 177 | 6 | } |
| 178 | |
|
| 179 | |
public void save(URL url) throws ConfigurationException |
| 180 | |
{ |
| 181 | 1 | delegate.save(url); |
| 182 | 1 | } |
| 183 | |
|
| 184 | |
public void save(OutputStream out) throws ConfigurationException |
| 185 | |
{ |
| 186 | 1 | delegate.save(out); |
| 187 | 1 | } |
| 188 | |
|
| 189 | |
public void save(OutputStream out, String encoding) throws ConfigurationException |
| 190 | |
{ |
| 191 | 1 | delegate.save(out, encoding); |
| 192 | 1 | } |
| 193 | |
|
| 194 | |
public String getFileName() |
| 195 | |
{ |
| 196 | 28 | return delegate.getFileName(); |
| 197 | |
} |
| 198 | |
|
| 199 | |
public void setFileName(String fileName) |
| 200 | |
{ |
| 201 | 104 | delegate.setFileName(fileName); |
| 202 | 104 | } |
| 203 | |
|
| 204 | |
public String getBasePath() |
| 205 | |
{ |
| 206 | 51 | return delegate.getBasePath(); |
| 207 | |
} |
| 208 | |
|
| 209 | |
public void setBasePath(String basePath) |
| 210 | |
{ |
| 211 | 48 | delegate.setBasePath(basePath); |
| 212 | 48 | } |
| 213 | |
|
| 214 | |
public File getFile() |
| 215 | |
{ |
| 216 | 4 | return delegate.getFile(); |
| 217 | |
} |
| 218 | |
|
| 219 | |
public void setFile(File file) |
| 220 | |
{ |
| 221 | 144 | delegate.setFile(file); |
| 222 | 144 | } |
| 223 | |
|
| 224 | |
public URL getURL() |
| 225 | |
{ |
| 226 | 1 | return delegate.getURL(); |
| 227 | |
} |
| 228 | |
|
| 229 | |
public void setURL(URL url) |
| 230 | |
{ |
| 231 | 7 | delegate.setURL(url); |
| 232 | 7 | } |
| 233 | |
|
| 234 | |
public void setAutoSave(boolean autoSave) |
| 235 | |
{ |
| 236 | 1 | delegate.setAutoSave(autoSave); |
| 237 | 1 | } |
| 238 | |
|
| 239 | |
public boolean isAutoSave() |
| 240 | |
{ |
| 241 | 2 | return delegate.isAutoSave(); |
| 242 | |
} |
| 243 | |
|
| 244 | |
public ReloadingStrategy getReloadingStrategy() |
| 245 | |
{ |
| 246 | 3 | return delegate.getReloadingStrategy(); |
| 247 | |
} |
| 248 | |
|
| 249 | |
public void setReloadingStrategy(ReloadingStrategy strategy) |
| 250 | |
{ |
| 251 | 5 | delegate.setReloadingStrategy(strategy); |
| 252 | 5 | } |
| 253 | |
|
| 254 | |
public void reload() |
| 255 | |
{ |
| 256 | 1259 | setDetailEvents(false); |
| 257 | |
try |
| 258 | |
{ |
| 259 | 1259 | delegate.reload(); |
| 260 | 1259 | } |
| 261 | |
finally |
| 262 | |
{ |
| 263 | 0 | setDetailEvents(true); |
| 264 | |
} |
| 265 | 1259 | } |
| 266 | |
|
| 267 | |
public String getEncoding() |
| 268 | |
{ |
| 269 | 24 | return delegate.getEncoding(); |
| 270 | |
} |
| 271 | |
|
| 272 | |
public void setEncoding(String encoding) |
| 273 | |
{ |
| 274 | 3 | delegate.setEncoding(encoding); |
| 275 | 3 | } |
| 276 | |
|
| 277 | |
public boolean containsKey(String key) |
| 278 | |
{ |
| 279 | 349 | reload(); |
| 280 | 349 | return super.containsKey(key); |
| 281 | |
} |
| 282 | |
|
| 283 | |
public Iterator getKeys(String prefix) |
| 284 | |
{ |
| 285 | 2 | reload(); |
| 286 | 2 | return super.getKeys(prefix); |
| 287 | |
} |
| 288 | |
|
| 289 | |
public Object getProperty(String key) |
| 290 | |
{ |
| 291 | 899 | reload(); |
| 292 | 899 | return super.getProperty(key); |
| 293 | |
} |
| 294 | |
|
| 295 | |
public boolean isEmpty() |
| 296 | |
{ |
| 297 | 9 | reload(); |
| 298 | 9 | return super.isEmpty(); |
| 299 | |
} |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
protected FileConfigurationDelegate createDelegate() |
| 311 | |
{ |
| 312 | 163 | return new FileConfigurationDelegate(); |
| 313 | |
} |
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
private void initDelegate(FileConfigurationDelegate del) |
| 321 | |
{ |
| 322 | 412 | del.addConfigurationListener(this); |
| 323 | 412 | } |
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
public void configurationChanged(ConfigurationEvent event) |
| 333 | |
{ |
| 334 | |
|
| 335 | 8 | setDetailEvents(true); |
| 336 | |
try |
| 337 | |
{ |
| 338 | 8 | fireEvent(event.getType(), event.getPropertyName(), event |
| 339 | |
.getPropertyValue(), event.isBeforeUpdate()); |
| 340 | 8 | } |
| 341 | |
finally |
| 342 | |
{ |
| 343 | 0 | setDetailEvents(false); |
| 344 | |
} |
| 345 | 8 | } |
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
protected FileConfigurationDelegate getDelegate() |
| 353 | |
{ |
| 354 | 227 | return delegate; |
| 355 | |
} |
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
protected void setDelegate(FileConfigurationDelegate delegate) |
| 362 | |
{ |
| 363 | 1 | this.delegate = delegate; |
| 364 | 1 | } |
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | 413 | protected class FileConfigurationDelegate extends AbstractFileConfiguration |
| 372 | |
{ |
| 373 | |
public void load(Reader in) throws ConfigurationException |
| 374 | |
{ |
| 375 | 27 | AbstractHierarchicalFileConfiguration.this.load(in); |
| 376 | 27 | } |
| 377 | |
|
| 378 | |
public void save(Writer out) throws ConfigurationException |
| 379 | |
{ |
| 380 | 18 | AbstractHierarchicalFileConfiguration.this.save(out); |
| 381 | 17 | } |
| 382 | |
|
| 383 | |
public void clear() |
| 384 | |
{ |
| 385 | 4 | AbstractHierarchicalFileConfiguration.this.clear(); |
| 386 | 4 | } |
| 387 | |
} |
| 388 | |
} |