1 16 17 package org.apache.commons.configuration; 18 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.Reader ; 27 import java.io.UnsupportedEncodingException ; 28 import java.io.Writer ; 29 import java.net.URL ; 30 import java.util.Iterator ; 31 32 import org.apache.commons.configuration.reloading.InvariantReloadingStrategy; 33 import org.apache.commons.configuration.reloading.ReloadingStrategy; 34 35 76 public abstract class AbstractFileConfiguration extends BaseConfiguration implements FileConfiguration 77 { 78 protected String fileName; 79 80 protected String basePath; 81 82 protected boolean autoSave; 83 84 protected ReloadingStrategy strategy; 85 86 private Object reloadLock = new Object (); 87 88 private String encoding; 89 90 95 public AbstractFileConfiguration() 96 { 97 setReloadingStrategy(new InvariantReloadingStrategy()); 98 } 99 100 109 public AbstractFileConfiguration(String fileName) throws ConfigurationException 110 { 111 this(); 112 113 setPath(fileName); 115 116 load(); 118 } 119 120 127 public AbstractFileConfiguration(File file) throws ConfigurationException 128 { 129 this(); 130 131 setFile(file); 133 134 if (file.exists()) 136 { 137 load(); 138 } 139 } 140 141 148 public AbstractFileConfiguration(URL url) throws ConfigurationException 149 { 150 this(); 151 152 setURL(url); 154 155 load(); 157 } 158 159 164 public void load() throws ConfigurationException 165 { 166 load(getFileName()); 167 } 168 169 176 public void load(String fileName) throws ConfigurationException 177 { 178 try 179 { 180 URL url = ConfigurationUtils.locate(basePath, fileName); 181 if (url == null) 182 { 183 throw new ConfigurationException("Cannot locate configuration source " + fileName); 184 } 185 load(url); 186 } 187 catch (ConfigurationException e) 188 { 189 throw e; 190 } 191 catch (Exception e) 192 { 193 throw new ConfigurationException(e.getMessage(), e); 194 } 195 } 196 197 204 public void load(File file) throws ConfigurationException 205 { 206 try 207 { 208 load(file.toURL()); 209 } 210 catch (ConfigurationException e) 211 { 212 throw e; 213 } 214 catch (Exception e) 215 { 216 throw new ConfigurationException(e.getMessage(), e); 217 } 218 } 219 220 227 public void load(URL url) throws ConfigurationException 228 { 229 InputStream in = null; 230 231 try 232 { 233 in = url.openStream(); 234 load(in); 235 } 236 catch (ConfigurationException e) 237 { 238 throw e; 239 } 240 catch (Exception e) 241 { 242 throw new ConfigurationException(e.getMessage(), e); 243 } 244 finally 245 { 246 try 248 { 249 if (in != null) 250 { 251 in.close(); 252 } 253 } 254 catch (IOException e) 255 { 256 e.printStackTrace(); 257 } 258 } 259 } 260 261 269 public void load(InputStream in) throws ConfigurationException 270 { 271 load(in, getEncoding()); 272 } 273 274 283 public void load(InputStream in, String encoding) throws ConfigurationException 284 { 285 Reader reader = null; 286 287 if (encoding != null) 288 { 289 try 290 { 291 reader = new InputStreamReader (in, encoding); 292 } 293 catch (UnsupportedEncodingException e) 294 { 295 throw new ConfigurationException( 296 "The requested encoding is not supported, try the default encoding.", e); 297 } 298 } 299 300 if (reader == null) 301 { 302 reader = new InputStreamReader (in); 303 } 304 305 load(reader); 306 } 307 308 313 public void save() throws ConfigurationException 314 { 315 save(fileName); 316 strategy.init(); 317 } 318 319 327 public void save(String fileName) throws ConfigurationException 328 { 329 try 330 { 331 File file = ConfigurationUtils.getFile(basePath, fileName); 332 if (file == null) 333 { 334 throw new ConfigurationException("Invalid file name for save: " + fileName); 335 } 336 save(file); 337 } 338 catch (ConfigurationException e) 339 { 340 throw e; 341 } 342 catch (Exception e) 343 { 344 throw new ConfigurationException(e.getMessage(), e); 345 } 346 } 347 348 357 public void save(URL url) throws ConfigurationException 358 { 359 File file = ConfigurationUtils.fileFromURL(url); 360 if (file != null) 361 { 362 save(file); 363 } 364 else 365 { 366 throw new ConfigurationException("Could not save to URL " + url); 367 } 368 } 369 370 379 public void save(File file) throws ConfigurationException 380 { 381 OutputStream out = null; 382 383 try 384 { 385 createPath(file); 387 out = new FileOutputStream (file); 388 save(out); 389 } 390 catch (IOException e) 391 { 392 throw new ConfigurationException(e.getMessage(), e); 393 } 394 finally 395 { 396 try 398 { 399 if (out != null) 400 { 401 out.close(); 402 } 403 } 404 catch (IOException e) 405 { 406 e.printStackTrace(); 407 } 408 } 409 } 410 411 419 public void save(OutputStream out) throws ConfigurationException 420 { 421 save(out, getEncoding()); 422 } 423 424 432 public void save(OutputStream out, String encoding) throws ConfigurationException 433 { 434 Writer writer = null; 435 436 if (encoding != null) 437 { 438 try 439 { 440 writer = new OutputStreamWriter (out, encoding); 441 } 442 catch (UnsupportedEncodingException e) 443 { 444 throw new ConfigurationException( 445 "The requested encoding is not supported, try the default encoding.", e); 446 } 447 } 448 449 if (writer == null) 450 { 451 writer = new OutputStreamWriter (out); 452 } 453 454 save(writer); 455 } 456 457 460 public String getFileName() 461 { 462 return fileName; 463 } 464 465 472 public void setFileName(String fileName) 473 { 474 this.fileName = fileName; 475 } 476 477 480 public String getBasePath() 481 { 482 return basePath; 483 } 484 485 491 public void setBasePath(String basePath) 492 { 493 this.basePath = basePath; 494 } 495 496 503 public File getFile() 504 { 505 return ConfigurationUtils.getFile(getBasePath(), getFileName()); 506 } 507 508 515 public void setFile(File file) 516 { 517 setFileName(file.getName()); 518 setBasePath((file.getParentFile() != null) ? file.getParentFile().getAbsolutePath() : null); 519 } 520 521 528 public String getPath() 529 { 530 return getFile().getAbsolutePath(); 531 } 532 533 539 public void setPath(String path) 540 { 541 setFile(new File (path)); 542 } 543 544 549 public URL getURL() 550 { 551 return ConfigurationUtils.locate(getBasePath(), getFileName()); 552 } 553 554 562 public void setURL(URL url) 563 { 564 setBasePath(ConfigurationUtils.getBasePath(url)); 565 setFileName(ConfigurationUtils.getFileName(url)); 566 } 567 568 public void setAutoSave(boolean autoSave) 569 { 570 this.autoSave = autoSave; 571 } 572 573 public boolean isAutoSave() 574 { 575 return autoSave; 576 } 577 578 582 protected void possiblySave() 583 { 584 if (autoSave && fileName != null) 585 { 586 try 587 { 588 save(); 589 } 590 catch (ConfigurationException e) 591 { 592 throw new ConfigurationRuntimeException("Failed to auto-save", e); 593 } 594 } 595 } 596 597 protected void addPropertyDirect(String key, Object obj) 598 { 599 super.addPropertyDirect(key, obj); 600 possiblySave(); 601 } 602 603 public void clearProperty(String key) 604 { 605 super.clearProperty(key); 606 possiblySave(); 607 } 608 609 public ReloadingStrategy getReloadingStrategy() 610 { 611 return strategy; 612 } 613 614 public void setReloadingStrategy(ReloadingStrategy strategy) 615 { 616 this.strategy = strategy; 617 strategy.setConfiguration(this); 618 strategy.init(); 619 } 620 621 public void reload() 622 { 623 synchronized (reloadLock) 624 { 625 if (strategy.reloadingRequired()) 626 { 627 try 628 { 629 clear(); 630 load(); 631 632 strategy.reloadingPerformed(); 634 } 635 catch (Exception e) 636 { 637 e.printStackTrace(); 638 } 640 } 641 } 642 } 643 644 public Object getProperty(String key) 645 { 646 reload(); 647 return super.getProperty(key); 648 } 649 650 public boolean isEmpty() 651 { 652 reload(); 653 return super.isEmpty(); 654 } 655 656 public boolean containsKey(String key) 657 { 658 reload(); 659 return super.containsKey(key); 660 } 661 662 public Iterator getKeys() 663 { 664 reload(); 665 return super.getKeys(); 666 } 667 668 671 private void createPath(File file) 672 { 673 if (file != null) 674 { 675 if (!file.exists()) 677 { 678 File parent = file.getParentFile(); 679 if (parent != null && !parent.exists()) 680 { 681 parent.mkdirs(); 682 } 683 } 684 } 685 } 686 687 public String getEncoding() 688 { 689 return encoding; 690 } 691 692 public void setEncoding(String encoding) 693 { 694 this.encoding = encoding; 695 } 696 } | Popular Tags |