PropertyFileConfiguration.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * ------------------------------------
  28. * PropertyFileReportConfiguration.java
  29. * ------------------------------------
  30. * (C)opyright 2003, by Thomas Morgner and Contributors.
  31. *
  32. * Original Author: Thomas Morgner;
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. * $Id: PropertyFileConfiguration.java,v 1.10 2008/09/10 09:17:28 mungady Exp $
  36. *
  37. * Changes
  38. * -------
  39. * 14-Jan-2003 : Initial version
  40. */
  41. package org.jfree.base.config;
  42. import java.io.BufferedInputStream;
  43. import java.io.IOException;
  44. import java.io.InputStream;
  45. import java.util.Properties;
  46. import org.jfree.util.Log;
  47. import org.jfree.util.ObjectUtilities;
  48. /**
  49. * A report configuration that reads its values from an arbitary property file.
  50. *
  51. * @author Thomas Morgner
  52. */
  53. public class PropertyFileConfiguration extends HierarchicalConfiguration
  54. {
  55. /**
  56. * Default constructor.
  57. */
  58. public PropertyFileConfiguration() {
  59. // nothing required
  60. }
  61. /**
  62. * Load the properties in the given file.
  63. *
  64. * @param resourceName the file name.
  65. */
  66. public void load(final String resourceName)
  67. {
  68. load(resourceName, PropertyFileConfiguration.class);
  69. }
  70. /**
  71. * Loads the properties stored in the given file. This method does nothing if
  72. * the file does not exist or is unreadable. Appends the contents of the loaded
  73. * properties to the already stored contents.
  74. *
  75. * @param resourceName the file name of the stored properties.
  76. * @param resourceSource ?
  77. */
  78. public void load(final String resourceName, final Class resourceSource)
  79. {
  80. final InputStream in = ObjectUtilities.getResourceRelativeAsStream
  81. (resourceName, resourceSource);
  82. if (in != null)
  83. {
  84. try
  85. {
  86. load(in);
  87. }
  88. finally
  89. {
  90. try
  91. {
  92. in.close();
  93. }
  94. catch (IOException e)
  95. {
  96. // ignore
  97. }
  98. }
  99. }
  100. else
  101. {
  102. Log.debug ("Configuration file not found in the classpath: " + resourceName);
  103. }
  104. }
  105. /**
  106. * Loads the properties stored in the given file. This method does nothing if
  107. * the file does not exist or is unreadable. Appends the contents of the loaded
  108. * properties to the already stored contents.
  109. *
  110. * @param in the input stream used to read the properties.
  111. */
  112. public void load(final InputStream in)
  113. {
  114. if (in == null)
  115. {
  116. throw new NullPointerException();
  117. }
  118. try
  119. {
  120. final BufferedInputStream bin = new BufferedInputStream(in);
  121. final Properties p = new Properties();
  122. p.load(bin);
  123. this.getConfiguration().putAll(p);
  124. bin.close();
  125. }
  126. catch (IOException ioe)
  127. {
  128. Log.warn("Unable to read configuration", ioe);
  129. }
  130. }
  131. }