Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / config / ConfigProperties.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18                 
19 package peersim.config;
20
21 import java.util.Properties;
22 import java.io.*;
23
24 /**
25 * Class for handling configuration files. Extends the functionality
26 * of Properties by handling files, system resources and command lines.
27 */
28 public class ConfigProperties extends Properties {
29
30
31 // =========== Public Constructors ===================================
32 // ===================================================================
33
34
35 /**
36 * Calls super constructor.
37 */
38 public ConfigProperties() { super(); }
39
40 // -------------------------------------------------------------------
41
42 /**
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.
51 * <p>
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.
56 * <p>
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.
60 *
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.
64
65 */
66 public  ConfigProperties( String[] pars, String resource ) {
67         
68         try
69         {
70                 if( resource != null )
71                 {
72                         loadSystemResource(resource);
73                         System.err.println("ConfigProperties: System resource "
74                         +resource+" loaded.");
75                 }
76         }
77         catch( Exception e )
78         {
79                 System.err.println("ConfigProperties: " + e );
80         }
81         
82         if( pars == null || pars.length == 0 ) return;
83         
84         for (int i=0; i < pars.length; i++)
85         {
86                 try
87                 {
88                         load( pars[i] );
89                         System.err.println(
90                                 "ConfigProperties: File "+pars[i]+" loaded.");
91                         pars[i] = "";
92                 }
93                 catch( IOException e )
94                 {
95                         try
96                         {
97                                 loadPropertyString( pars[i] );
98                                 System.err.println("ConfigProperties: Property '" +
99                                         pars[i] + "' set.");
100                         }
101                         catch( Exception e2 )
102                         {
103                                 System.err.println("ConfigProperties: " + e2 );
104                         }
105                 }
106                 catch( Exception e )
107                 {
108                         System.err.println("ConfigProperties: " + e );
109                 }
110         }
111 }
112
113 // -------------------------------------------------------------------
114
115 /**
116 * Constructs a ConfigProperty object by loading a file by calling
117 * {@link #load}.
118 * @param fileName The name of the configuration file.
119 */
120 public ConfigProperties( String fileName ) throws IOException {
121
122         load( fileName );
123 }
124
125 // -------------------------------------------------------------------
126
127 /**
128 * Calls super constructor.
129 */
130 public  ConfigProperties( Properties props ) {
131
132         super( props );
133 }
134
135 // -------------------------------------------------------------------
136
137 /**
138 * Calls {@link #ConfigProperties(String[],String)} with resource set to null.
139 */
140 public  ConfigProperties( String[] pars ) {
141
142         this( pars, null );
143 }
144
145
146 // =========== Public methods ========================================
147 // ===================================================================
148
149
150 /**
151 * Loads given file. Calls <code>Properties.load</code> with a file
152 * input stream to the given file.
153 */
154 public void load( String fileName ) throws IOException {
155
156         FileInputStream fis = new FileInputStream( fileName );
157         load( fis );
158         fis.close();
159 }
160
161 // -------------------------------------------------------------------
162
163 /**
164 * Adds the properties from the given property file. Searches in the class path
165 * for the file with the given name.
166 */
167 public void loadSystemResource( String n ) throws IOException {
168         
169         ClassLoader cl = getClass().getClassLoader();
170         load( cl.getResourceAsStream( n ) );
171 }
172
173 // -------------------------------------------------------------------
174
175 /**
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.
182 */
183 public void loadPropertyString( String prop ) throws IOException {
184
185         StringBuffer sb = new StringBuffer();
186         sb.append( prop ).append( "\n" );
187         load( new ByteArrayInputStream(sb.toString().getBytes()) );
188 }
189 }
190