KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > EnvironmentMutableConfig


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: EnvironmentMutableConfig.java,v 1.28 2006/11/07 04:37:20 linda Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import java.util.Enumeration JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 import com.sleepycat.je.config.ConfigParam;
16 import com.sleepycat.je.config.EnvironmentParams;
17 import com.sleepycat.je.dbi.DbConfigManager;
18 import com.sleepycat.je.dbi.EnvironmentImpl;
19
20 /**
21  * Javadoc for this public class is generated
22  * via the doc templates in the doc_src directory.
23  */

24 public class EnvironmentMutableConfig implements Cloneable JavaDoc {
25
26     /*
27      * Change copyHandlePropsTo and Environment.copyToHandleConfig
28      * when adding fields here.
29      */

30     private boolean txnNoSync = false;
31     private boolean txnWriteNoSync = false;
32
33     /*
34      * Cache size is a category of property that is calculated within the
35      * environment. It is only supplied when returning the cache size to the
36      * application and never used internally; internal code directly checks
37      * with the MemoryBudget class;
38      */

39     protected long cacheSize;
40
41     /**
42      * Note that in the implementation we choose not to extend Properties
43      * in order to keep the configuration type safe.
44      */

45     protected Properties JavaDoc props;
46
47     /**
48      * For unit testing, to prevent loading of je.properties.
49      */

50     private boolean loadPropertyFile = true;
51
52     /**
53      * Internal boolean that says whether or not to validate params. Setting
54      * it to false means that parameter value validatation won't be performed
55      * during setVal() calls. Only should be set to false by unit tests using
56      * DbInternal.
57      */

58     boolean validateParams = true;
59
60     /**
61      * Javadoc for this public method is generated via
62      * the doc templates in the doc_src directory.
63      */

64     public EnvironmentMutableConfig() {
65         props = new Properties JavaDoc();
66     }
67
68     /**
69      * Used by EnvironmentConfig to construct from properties.
70      */

71     EnvironmentMutableConfig(Properties JavaDoc properties)
72         throws IllegalArgumentException JavaDoc {
73
74         DbConfigManager.validateProperties(properties,
75                                            false, // forReplication
76
this.getClass().getName());
77         /* For safety, copy the passed in properties. */
78         props = new Properties JavaDoc();
79         props.putAll(properties);
80     }
81
82     /**
83      * Javadoc for this public method is generated via
84      * the doc templates in the doc_src directory.
85      */

86     public void setTxnNoSync(boolean noSync) {
87         txnNoSync = noSync;
88     }
89
90     /**
91      * Javadoc for this public method is generated via
92      * the doc templates in the doc_src directory.
93      */

94     public boolean getTxnNoSync() {
95         return txnNoSync;
96     }
97
98     /**
99      * Javadoc for this public method is generated via
100      * the doc templates in the doc_src directory.
101      */

102     public void setTxnWriteNoSync(boolean writeNoSync) {
103         txnWriteNoSync = writeNoSync;
104     }
105
106     /**
107      * Javadoc for this public method is generated via
108      * the doc templates in the doc_src directory.
109      */

110     public boolean getTxnWriteNoSync() {
111         return txnWriteNoSync;
112     }
113
114     /**
115      * Javadoc for this public method is generated via
116      * the doc templates in the doc_src directory.
117      */

118     public void setCacheSize(long totalBytes)
119         throws IllegalArgumentException JavaDoc {
120
121         DbConfigManager.setVal(props, EnvironmentParams.MAX_MEMORY,
122                                Long.toString(totalBytes), validateParams);
123     }
124
125     /**
126      * Javadoc for this public method is generated via
127      * the doc templates in the doc_src directory.
128      */

129     public long getCacheSize() {
130
131         /*
132          * CacheSize is filled in from the EnvironmentImpl by way of
133          * copyHandleProps.
134          */

135         return cacheSize;
136     }
137
138     /**
139      * Javadoc for this public method is generated via
140      * the doc templates in the doc_src directory.
141      */

142     public void setCachePercent(int percent)
143         throws IllegalArgumentException JavaDoc {
144
145         DbConfigManager.setVal(props, EnvironmentParams.MAX_MEMORY_PERCENT,
146                                Integer.toString(percent), validateParams);
147     }
148
149     /**
150      * Javadoc for this public method is generated via
151      * the doc templates in the doc_src directory.
152      */

153     public int getCachePercent() {
154
155         String JavaDoc val =
156             DbConfigManager.getVal(props,
157                                    EnvironmentParams.MAX_MEMORY_PERCENT);
158         try {
159             return Integer.parseInt(val);
160         } catch (NumberFormatException JavaDoc e) {
161             throw new IllegalArgumentException JavaDoc
162         ("Cache percent is not a valid integer: " + e.getMessage());
163         }
164     }
165
166     /**
167      * Javadoc for this public method is generated via
168      * the doc templates in the doc_src directory.
169      */

170     public void setConfigParam(String JavaDoc paramName, String JavaDoc value)
171         throws IllegalArgumentException JavaDoc {
172         
173         DbConfigManager.setConfigParam(props,
174                                        paramName,
175                                        value,
176                                        true, /* require mutability. */
177                                        validateParams,
178                                        false /* forReplication */);
179     }
180
181     /**
182      * Javadoc for this public method is generated via
183      * the doc templates in the doc_src directory.
184      */

185     public String JavaDoc getConfigParam(String JavaDoc paramName)
186         throws IllegalArgumentException JavaDoc {
187         
188        return DbConfigManager.getConfigParam(props,
189                                              paramName);
190     }
191
192     /*
193      * Helpers
194      */

195     void setValidateParams(boolean validateParams) {
196     this.validateParams = validateParams;
197     }
198
199     /**
200      * Check that the immutable values in the environment config used to open
201      * an environment match those in the config object saved by the underlying
202      * shared EnvironmentImpl.
203      */

204     void checkImmutablePropsForEquality(EnvironmentMutableConfig passedConfig)
205         throws IllegalArgumentException JavaDoc {
206
207         Properties JavaDoc passedProps = passedConfig.props;
208         Iterator JavaDoc iter = EnvironmentParams.SUPPORTED_PARAMS.keySet().iterator();
209         while (iter.hasNext()) {
210             String JavaDoc paramName = (String JavaDoc) iter.next();
211             ConfigParam param = (ConfigParam)
212                 EnvironmentParams.SUPPORTED_PARAMS.get(paramName);
213             assert param != null;
214             if (!param.isMutable()) {
215                 String JavaDoc paramVal = props.getProperty(paramName);
216                 String JavaDoc useParamVal = passedProps.getProperty(paramName);
217                 if ((paramVal != null) ? (!paramVal.equals(useParamVal))
218                                        : (useParamVal != null)) {
219                     throw new IllegalArgumentException JavaDoc
220                         (paramName + " is set to " +
221                          useParamVal +
222                          " in the config parameter" +
223                          " which is incompatible" +
224                          " with the value of " +
225                          paramVal + " in the" +
226                          " underlying environment");
227                 }
228             }
229         }
230     }
231
232     /**
233      * Overrides Object.clone() to clone all properties, used by this class and
234      * EnvironmentConfig.
235      */

236     protected Object JavaDoc clone()
237         throws CloneNotSupportedException JavaDoc {
238
239         EnvironmentMutableConfig copy =
240             (EnvironmentMutableConfig) super.clone();
241         copy.props = (Properties JavaDoc) props.clone();
242         return copy;
243     }
244
245     /**
246      * Used by Environment to create a copy of the application
247      * supplied configuration. Done this way to provide non-public cloning.
248      */

249     EnvironmentMutableConfig cloneMutableConfig() {
250         try {
251             EnvironmentMutableConfig copy = (EnvironmentMutableConfig) clone();
252             /* Remove all immutable properties. */
253             copy.clearImmutableProps();
254             return copy;
255         } catch (CloneNotSupportedException JavaDoc willNeverOccur) {
256             return null;
257         }
258     }
259
260     /**
261      * Copies the per-handle properties of this object to the given config
262      * object.
263      */

264     void copyHandlePropsTo(EnvironmentMutableConfig other) {
265         other.txnNoSync = txnNoSync;
266         other.txnWriteNoSync = txnWriteNoSync;
267     }
268
269     /**
270      * Copies all mutable props to the given config object.
271      */

272     void copyMutablePropsTo(EnvironmentMutableConfig toConfig) {
273
274         Properties JavaDoc toProps = toConfig.props;
275         Enumeration JavaDoc propNames = props.propertyNames();
276         while (propNames.hasMoreElements()) {
277             String JavaDoc paramName = (String JavaDoc) propNames.nextElement();
278             ConfigParam param = (ConfigParam)
279                 EnvironmentParams.SUPPORTED_PARAMS.get(paramName);
280             assert param != null;
281             if (param.isMutable()) {
282                 String JavaDoc newVal = props.getProperty(paramName);
283                 toProps.setProperty(paramName, newVal);
284             }
285         }
286     }
287
288     /**
289      * Fill in the properties calculated by the environment to the given
290      * config object.
291      */

292     void fillInEnvironmentGeneratedProps(EnvironmentImpl envImpl) {
293         cacheSize = envImpl.getMemoryBudget().getMaxMemory();
294     }
295
296     /**
297      * Removes all immutable props.
298      */

299     private void clearImmutableProps() {
300         Enumeration JavaDoc propNames = props.propertyNames();
301         while (propNames.hasMoreElements()) {
302             String JavaDoc paramName = (String JavaDoc) propNames.nextElement();
303             ConfigParam param = (ConfigParam)
304                 EnvironmentParams.SUPPORTED_PARAMS.get(paramName);
305             assert param != null;
306             if (!param.isMutable()) {
307                 props.remove(paramName);
308             }
309         }
310     }
311
312     Properties JavaDoc getProps() {
313         return props;
314     }
315
316     /**
317      * For unit testing, to prevent loading of je.properties.
318      */

319     void setLoadPropertyFile(boolean loadPropertyFile) {
320         this.loadPropertyFile = loadPropertyFile;
321     }
322
323     /**
324      * For unit testing, to prevent loading of je.properties.
325      */

326     boolean getLoadPropertyFile() {
327         return loadPropertyFile;
328     }
329
330     /**
331      * Testing support
332      */

333     int getNumExplicitlySetParams() {
334         return props.size();
335     }
336
337     public String JavaDoc toString() {
338         return props.toString();
339     }
340 }
341
Popular Tags