2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.config;
21 import java.util.Properties;
25 * Class for handling configuration files. Extends the functionality
26 * of Properties by handling files, system resources and command lines.
28 public class ConfigProperties extends Properties {
31 // =========== Public Constructors ===================================
32 // ===================================================================
36 * Calls super constructor.
38 public ConfigProperties() { super(); }
40 // -------------------------------------------------------------------
43 * Constructs a ConfigProperty object from a parameter list.
44 * The algorithm is as follows: first <code>resource</code> is used to attempt
45 * loading default values from the given system resource.
46 * Then all Strings in <code>pars</code> are processed in the order they
47 * appear in the array. For <code>pars[i]</code>, first a property file
48 * with the name <code>pars[i]</code> is attempted to be loaded. If the file
49 * does not exist or loading produces any other IOException, <code>pars[i]</code>
50 * is interpreted as a property definition, and it is set.
52 * A little inconvenience is that if <code>pars[i]</code> is supposed to be
53 * a command line argument, but it is a valid filename at the same time by
54 * accident, the algorithm will process it as a file instead of a command line
55 * argument. The caller must take care of that.
57 * No exceptions are thrown, instead error messages are written to the
58 * standard error. Users who want a finer control should use
59 * the public methods of this class.
61 * @param pars The (probably command line) parameter list.
62 * @param resource The name of the system resource that contains the
63 * defaults. null if there isn't any.
66 public ConfigProperties( String[] pars, String resource ) {
70 if( resource != null )
72 loadSystemResource(resource);
73 System.err.println("ConfigProperties: System resource "
74 +resource+" loaded.");
79 System.err.println("ConfigProperties: " + e );
82 if( pars == null || pars.length == 0 ) return;
84 for (int i=0; i < pars.length; i++)
90 "ConfigProperties: File "+pars[i]+" loaded.");
93 catch( IOException e )
97 loadPropertyString( pars[i] );
98 System.err.println("ConfigProperties: Property '" +
101 catch( Exception e2 )
103 System.err.println("ConfigProperties: " + e2 );
108 System.err.println("ConfigProperties: " + e );
113 // -------------------------------------------------------------------
116 * Constructs a ConfigProperty object by loading a file by calling
118 * @param fileName The name of the configuration file.
120 public ConfigProperties( String fileName ) throws IOException {
125 // -------------------------------------------------------------------
128 * Calls super constructor.
130 public ConfigProperties( Properties props ) {
135 // -------------------------------------------------------------------
138 * Calls {@link #ConfigProperties(String[],String)} with resource set to null.
140 public ConfigProperties( String[] pars ) {
146 // =========== Public methods ========================================
147 // ===================================================================
151 * Loads given file. Calls <code>Properties.load</code> with a file
152 * input stream to the given file.
154 public void load( String fileName ) throws IOException {
156 FileInputStream fis = new FileInputStream( fileName );
161 // -------------------------------------------------------------------
164 * Adds the properties from the given property file. Searches in the class path
165 * for the file with the given name.
167 public void loadSystemResource( String n ) throws IOException {
169 ClassLoader cl = getClass().getClassLoader();
170 load( cl.getResourceAsStream( n ) );
173 // -------------------------------------------------------------------
176 * Appends a property defined in the given string.
177 * The string is considered as a property file line.
178 * It is converted to a byte array according to the
179 * default character encoding and then loaded by the
180 * <code>Properties.load</code> method. This means that the ISO-8859-1
181 * (or compatible) encoding is assumed.
183 public void loadPropertyString( String prop ) throws IOException {
185 StringBuffer sb = new StringBuffer();
186 sb.append( prop ).append( "\n" );
187 load( new ByteArrayInputStream(sb.toString().getBytes()) );