KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > base > config > PropertyFileConfiguration


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.6 2005/10/18 13:14:12 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 14-Jan-2003 : Initial version
40  */

41 package org.jfree.base.config;
42
43 import java.io.BufferedInputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.util.Properties JavaDoc;
47
48 import org.jfree.util.Log;
49 import org.jfree.util.ObjectUtilities;
50
51 /**
52  * A report configuration that reads its values from an arbitary property file.
53  *
54  * @author Thomas Morgner
55  */

56 public class PropertyFileConfiguration extends HierarchicalConfiguration
57 {
58   /**
59    * Loads the properties stored in the given file. This method does nothing if
60    * the file does not exist or is unreadable. Appends the contents of the loaded
61    * properties to the already stored contents.
62    *
63    * @param resourceName the file name of the stored properties.
64    */

65   public void load(final String JavaDoc resourceName)
66   {
67     final InputStream JavaDoc in = ObjectUtilities.getResourceRelativeAsStream
68             (resourceName, PropertyFileConfiguration.class);
69     if (in != null)
70     {
71       load(in);
72     }
73     else
74     {
75       Log.debug ("Configuration file not found in the classpath: " + resourceName);
76     }
77
78   }
79
80   /**
81    * Loads the properties stored in the given file. This method does nothing if
82    * the file does not exist or is unreadable. Appends the contents of the loaded
83    * properties to the already stored contents.
84    *
85    * @param in the input stream used to read the properties.
86    */

87   public void load(final InputStream JavaDoc in)
88   {
89     if (in == null)
90     {
91       throw new NullPointerException JavaDoc();
92     }
93
94     try
95     {
96       final BufferedInputStream JavaDoc bin = new BufferedInputStream JavaDoc(in);
97       final Properties JavaDoc p = new Properties JavaDoc();
98       p.load(bin);
99       this.getConfiguration().putAll(p);
100       bin.close();
101     }
102     catch (IOException JavaDoc ioe)
103     {
104       Log.warn("Unable to read configuration", ioe);
105     }
106
107   }
108
109   /**
110    * Default constructor.
111    */

112   public PropertyFileConfiguration() {
113       // nothing required
114
}
115 }
116
Popular Tags