KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > dbi > DbConfigManager


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbConfigManager.java,v 1.38 2006/11/15 23:00:02 cwl Exp $
7  */

8
9 package com.sleepycat.je.dbi;
10
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 import com.sleepycat.je.DatabaseException;
21 import com.sleepycat.je.DbInternal;
22 import com.sleepycat.je.EnvironmentConfig;
23 import com.sleepycat.je.config.BooleanConfigParam;
24 import com.sleepycat.je.config.ConfigParam;
25 import com.sleepycat.je.config.EnvironmentParams;
26 import com.sleepycat.je.config.IntConfigParam;
27 import com.sleepycat.je.config.LongConfigParam;
28
29 /**
30  * DbConfigManager holds the configuration parameters for an environment.
31  *
32  * In general, all configuration parameters are represented by a ConfigParam
33  * defined in com.sleepycat.je.config.EnvironmentParams and can be represented
34  * by a property described in the top level example.properties. Environment
35  * parameters have some interesting twists because there are some attributes
36  * that are scoped by handle, such as the commit durability (txnSync,
37  * txnNoSync, etc) parameters.
38  *
39  * DbConfigManager is instantiated first by the EnvironmentImpl, and is
40  * loaded with the base configuration parameters. If replication is enabled,
41  * additional properties are added when the Replicator is instantiated.
42  * In order to keep replication code out of the base code, replication
43  * parameters are loaded by way of the addConfigurations method.
44  */

45 public class DbConfigManager {
46
47     /*
48      * The name of the JE properties file, to be found in the environment
49      * directory.
50      */

51     private static final String JavaDoc PROPFILE_NAME = "je.properties";
52
53     /*
54      * All properties in effect for this JE instance, both environment
55      * and replicator scoped, are stored in this Properties field.
56      */

57     private Properties JavaDoc props;
58
59     /*
60      * Save a reference to the environment config to access debug properties
61      * that are fields in EnvironmentConfig, must be set before the
62      * environment is created, and are not represented as JE properties.
63      */

64     private EnvironmentConfig environmentConfig;
65
66     public DbConfigManager(EnvironmentConfig config)
67     throws DbConfigException {
68
69         environmentConfig = config;
70     if (config == null) {
71         props = new Properties JavaDoc();
72     } else {
73         props = DbInternal.getProps(config);
74     }
75     }
76
77     /**
78      * Add all configuration properties in the specified property bag
79      * to this environment's configuration. Used to add replication
80      * specific configurations from ReplicatorConfig without referring
81      * to replication classes.
82      */

83     public void addConfigurations(Properties JavaDoc additionalProps) {
84         props.putAll(additionalProps);
85     }
86
87     public EnvironmentConfig getEnvironmentConfig() {
88         return environmentConfig;
89     }
90
91     /*
92      * Parameter Access
93      */

94
95     /**
96      * Get this parameter from the environment wide configuration settings.
97      * @param configParam
98      *
99      * @return default for param if param wasn't explicitly set
100      */

101     public synchronized String JavaDoc get(ConfigParam configParam)
102     throws IllegalArgumentException JavaDoc {
103
104         return getConfigParam(props, configParam.getName());
105     }
106
107     /**
108      * Get this parameter from the environment wide configuration settings.
109      * @param configParam
110      *
111      * @return default for param if param wasn't explicitly set
112      */

113     public synchronized String JavaDoc get(String JavaDoc configParamName)
114     throws IllegalArgumentException JavaDoc {
115
116         return getConfigParam(props, configParamName);
117     }
118
119     /**
120      * Get this parameter from the environment wide configuration settings.
121      *
122      * @param configParam
123      *
124      * @return default for param if it wasn't explicitly set.
125      */

126     public boolean getBoolean(BooleanConfigParam configParam)
127         throws DatabaseException {
128
129         /* See if it's specified. */
130         String JavaDoc val = get(configParam);
131         return Boolean.valueOf(val).booleanValue();
132     }
133
134     /**
135      * Get this parameter from the environment wide configuration settings.
136      *
137      * @param configParam
138      * @return default for param if it wasn't explicitly set.
139      */

140     public int getInt(IntConfigParam configParam)
141     throws DatabaseException {
142
143         // See if it's specified
144
String JavaDoc val = get(configParam);
145         int intValue = 0;
146         if (val != null) {
147             try {
148                 intValue = Integer.parseInt(val);
149             } catch (NumberFormatException JavaDoc e) {
150                 /*
151          * This should never happen if we put error checking into
152                  * the loading of config values.
153          */

154                 assert false: e.getMessage();
155             }
156         }
157         return intValue;
158     }
159
160     /**
161      * Get this parameter from the environment wide configuration settings.
162      *
163      * @param configParam
164      * @return default for param if it wasn't explicitly set
165      */

166     public long getLong(LongConfigParam configParam)
167     throws DatabaseException {
168
169         /* See if it's specified. */
170         String JavaDoc val = get(configParam);
171         long longValue = 0;
172         if (val != null) {
173             try {
174                 longValue = Long.parseLong(val);
175             } catch (NumberFormatException JavaDoc e) {
176                 /*
177          * This should never happen if we put error checking
178          * into the loading of config values.
179          */

180                 assert false : e.getMessage();
181             }
182         }
183         return longValue;
184     }
185
186     /*
187      * Helper methods used by EnvironmentConfig and ReplicatorConfig.
188      */

189
190     /**
191      * Validate a collection of configurations at Environment and Replicator
192      * startup time. Check for valid configuration names and values.
193      */

194     public static void validateProperties(Properties JavaDoc props,
195                                           boolean forReplication,
196                                           String JavaDoc configClassName)
197         throws IllegalArgumentException JavaDoc {
198
199         /* Check that the properties have valid names and values */
200         Enumeration JavaDoc propNames = props.propertyNames();
201         while (propNames.hasMoreElements()) {
202             String JavaDoc name = (String JavaDoc) propNames.nextElement();
203             /* Is this a valid property name? */
204             ConfigParam param =
205                 (ConfigParam) EnvironmentParams.SUPPORTED_PARAMS.get(name);
206             if (param == null) {
207                 throw new IllegalArgumentException JavaDoc
208             (name + " is not a valid BDBJE environment configuration");
209             }
210
211             if (forReplication) {
212                 if (!param.isForReplication()) {
213                     throw new IllegalArgumentException JavaDoc
214                         (name +
215                          " is not a replication environment configuration" +
216                          " and cannot be used in " + configClassName);
217                 }
218             } else {
219                 if (param.isForReplication()) {
220                     throw new IllegalArgumentException JavaDoc
221                         (name +
222                          " is a replication environment configuration" +
223                          " and cannot be used in " + configClassName);
224                 }
225             }
226
227             /* Is this a valid property value? */
228             param.validateValue(props.getProperty(name));
229         }
230     }
231
232     /**
233      * Apply the configurations specified in the je.properties file to override
234      * the programatically set configuration values held in the property bag.
235      */

236     public static void applyFileConfig(File JavaDoc envHome,
237                                        Properties JavaDoc props,
238                                        boolean forReplication,
239                                        String JavaDoc errorClassName)
240         throws IllegalArgumentException JavaDoc {
241
242         File JavaDoc paramFile = null;
243         try {
244         Properties JavaDoc fileProps = new Properties JavaDoc();
245         if (envHome != null) {
246         if (envHome.isFile()) {
247             paramFile = envHome;
248         } else {
249             paramFile = new File JavaDoc(envHome, PROPFILE_NAME);
250         }
251         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(paramFile);
252         fileProps.load(fis);
253         fis.close();
254         }
255
256             /* Validate the existing file. */
257             validateProperties(fileProps,
258                                forReplication,
259                                errorClassName);
260                 
261             /* Add them to the configuration object. */
262             Iterator JavaDoc iter = fileProps.entrySet().iterator();
263             while (iter.hasNext()) {
264                 Map.Entry JavaDoc propPair = (Map.Entry JavaDoc) iter.next();
265                 String JavaDoc name = (String JavaDoc) propPair.getKey();
266                 String JavaDoc value = (String JavaDoc) propPair.getValue();
267                 setConfigParam(props,
268                                name,
269                                value,
270                                false, /* don't need mutability, we're
271                                          initializing */

272                                false, /* value already validated when set in
273                                           config object */

274                                forReplication);
275             }
276         } catch (FileNotFoundException JavaDoc e) {
277             
278             /*
279              * Klockwork - ok
280              * Eat the exception, okay if the file doesn't exist.
281              */

282         } catch (IOException JavaDoc e) {
283             IllegalArgumentException JavaDoc e2 = new IllegalArgumentException JavaDoc
284                 ("An error occurred when reading " + paramFile);
285             e2.initCause(e);
286             throw e2;
287         }
288     }
289
290     /**
291      * Helper method for environment and replicator configuration classes.
292      * Set a configuration parameter. Check that the name is valid.
293      * If specified, also check that the value is valid.Value checking
294      * may be disabled for unit testing.
295      *
296      * @param props Property bag held within the configuration object.
297      */

298     public static void setConfigParam(Properties JavaDoc props,
299                                       String JavaDoc paramName,
300                                       String JavaDoc value,
301                                       boolean requireMutability,
302                                       boolean validateValue,
303                                       boolean forReplication)
304         throws IllegalArgumentException JavaDoc {
305         
306         /* Is this a valid property name? */
307         ConfigParam param =
308             (ConfigParam) EnvironmentParams.SUPPORTED_PARAMS.get(paramName);
309         if (param == null) {
310             throw new IllegalArgumentException JavaDoc
311         (paramName +
312          " is not a valid BDBJE environment configuration");
313         }
314
315         if (forReplication) {
316             if (!param.isForReplication()) {
317                 throw new IllegalArgumentException JavaDoc
318                     (paramName + " is not a BDBJE replication configuration.");
319             }
320         } else {
321             if (param.isForReplication()) {
322                 throw new IllegalArgumentException JavaDoc
323                     (paramName + " is only available for BDBJE replication.");
324             }
325         }
326
327         /* Is this a mutable property? */
328         if (requireMutability && !param.isMutable()) {
329             throw new IllegalArgumentException JavaDoc
330         (paramName +
331          " is not a mutable BDBJE environment configuration");
332         }
333
334         setVal(props, param, value, validateValue);
335     }
336
337     /**
338      * Helper method for environment and replicator configuration classes.
339      * Get the configuration value for the specified parameter, checking
340      * that the parameter name is valid.
341      * @param props Property bag held within the configuration object.
342      */

343     public static String JavaDoc getConfigParam(Properties JavaDoc props, String JavaDoc paramName)
344         throws IllegalArgumentException JavaDoc {
345         
346         /* Is this a valid property name? */
347         ConfigParam param =
348             (ConfigParam) EnvironmentParams.SUPPORTED_PARAMS.get(paramName);
349         if (param == null) {
350             throw new IllegalArgumentException JavaDoc
351         (paramName +
352          " is not a valid BDBJE environment configuration");
353         }
354
355         return DbConfigManager.getVal(props, param);
356     }
357
358     /**
359      * Helper method for environment and replicator configuration classes.
360      * Gets either the value stored in this configuration or the
361      * default value for this param.
362      */

363     public static String JavaDoc getVal(Properties JavaDoc props, ConfigParam param) {
364         String JavaDoc val = props.getProperty(param.getName());
365         if (val == null) {
366             val = param.getDefault();
367         }
368         return val;
369     }
370
371     /**
372      * Helper method for environment and replicator configuration classes.
373      * Set and validate the value for the specified parameter.
374      */

375     public static void setVal(Properties JavaDoc props,
376                               ConfigParam param,
377                               String JavaDoc val,
378                               boolean validateValue)
379         throws IllegalArgumentException JavaDoc {
380
381     if (validateValue) {
382         param.validateValue(val);
383     }
384         props.setProperty(param.getName(), val);
385     }
386
387 }
388
Popular Tags