KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > property > PropertyUtil


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.property.PropertyUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.property;
23
24 import org.apache.derby.iapi.reference.Property;
25 import org.apache.derby.iapi.reference.SQLState;
26 import org.apache.derby.iapi.reference.Attribute;
27 import org.apache.derby.iapi.reference.EngineType;
28 import org.apache.derby.iapi.services.monitor.Monitor;
29 import org.apache.derby.iapi.services.monitor.ModuleFactory;
30 import org.apache.derby.iapi.error.StandardException;
31 import org.apache.derby.iapi.util.StringUtil;
32
33 import java.util.Properties JavaDoc;
34 import java.io.Serializable JavaDoc;
35 import java.util.Dictionary JavaDoc;
36
37 /**
38     There are 5 property objects within a JBMS system.
39
40     1) JVM - JVM set - those in System.getProperties
41     2) APP - Application set - derby.properties file
42     3) SRV - Persistent Service set - Those stored in service.properties
43     4) TRAN - Persistent Transactional set - Those stored via the AccessManager interface
44     5) BOOT - Set by a boot method (rare)
45
46     This class has a set of static methods to find a property using a consistent search order
47     from the above set.
48     <BR>
49     getSystem*() methods use the search order.
50     <OL>
51     <LI> JVM
52     <LI> APP
53     </OL>
54     <BR>
55     getService* methods use the search order
56     <OL>
57     <LI> JVM
58     <LI> TRAN
59     <LI> SRV
60     <LI> APP
61     </OL>
62
63 */

64 public class PropertyUtil {
65
66     // List of properties that are stored in the service.properties file
67
public static final String JavaDoc[] servicePropertyList = {
68         EngineType.PROPERTY,
69         Property.NO_AUTO_BOOT,
70         Property.STORAGE_TEMP_DIRECTORY,
71         Attribute.CRYPTO_PROVIDER,
72         Attribute.CRYPTO_ALGORITHM,
73         Attribute.RESTORE_FROM,
74         Attribute.LOG_DEVICE,
75         Property.LOG_ARCHIVE_MODE
76     };
77
78     /**
79         Property is set in JVM set
80     */

81     public static final int SET_IN_JVM = 0;
82     /**
83         Property is set in DATABASE set
84     */

85     public static final int SET_IN_DATABASE = 1;
86     /**
87         Property is set in APPLICATION (derby.properties) set
88     */

89     public static final int SET_IN_APPLICATION = 2;
90
91     /**
92         Property is not set.
93     */

94     public static final int NOT_SET = -1;
95
96
97     static int whereSet(String JavaDoc key, Dictionary JavaDoc set) {
98
99         boolean dbOnly = isDBOnly(set);
100
101         if (!dbOnly) {
102             if (Monitor.getMonitor().getJVMProperty(key) != null) {
103                 return SET_IN_JVM;
104             }
105         }
106         
107         if ((set != null) && (set.get(key) != null))
108                 return SET_IN_DATABASE;
109
110         if (!dbOnly) {
111             if (PropertyUtil.getSystemProperty(key) != null)
112                 return SET_IN_APPLICATION;
113         }
114
115         return NOT_SET;
116     }
117
118     public static boolean isDBOnly(Dictionary JavaDoc set) {
119
120         if (set == null)
121             return false;
122
123         String JavaDoc value = (String JavaDoc) set.get(Property.DATABASE_PROPERTIES_ONLY);
124
125         boolean dbOnly = Boolean.valueOf(
126                     (value != null ? value.trim() : value)).booleanValue();
127
128         return dbOnly;
129     }
130
131     public static boolean isDBOnly(Properties JavaDoc set) {
132
133         if (set == null)
134             return false;
135
136         String JavaDoc value = set.getProperty(Property.DATABASE_PROPERTIES_ONLY);
137
138         boolean dbOnly = Boolean.valueOf(
139                     (value != null ? value.trim() : value)).booleanValue();
140
141         return dbOnly;
142     }
143     
144     /**
145         Find a system wide property.
146
147         @return the value of the property or null if it does not exist.
148         @see #getSystemProperty(String,String)
149     */

150     public static String JavaDoc getSystemProperty(String JavaDoc key) {
151         return PropertyUtil.getSystemProperty(key, (String JavaDoc) null);
152     }
153
154     /**
155         Find a system wide property with a default. Search order is
156
157         <OL>
158         <LI> JVM property
159         <LI> derby.properties
160         </OL>
161
162         <P>
163         This method can be used by a system that is not running Cloudscape,
164         just to maintain the same lookup logic and security manager concerns
165         for finding derby.properties and reading system properties.
166
167         @return the value of the property or defaultValue if it does not exist.
168     */

169     public static String JavaDoc getSystemProperty(String JavaDoc key, String JavaDoc defaultValue) {
170
171         ModuleFactory monitor = Monitor.getMonitorLite();
172
173         String JavaDoc value = monitor.getJVMProperty(key);
174
175         if (value == null) {
176
177             Properties JavaDoc applicationProperties =
178                 monitor.getApplicationProperties();
179
180             if (applicationProperties != null)
181                 value = applicationProperties.getProperty(key);
182         }
183         return value == null ? defaultValue : value;
184     }
185
186
187     /**
188         Get a property from the passed in set. The passed in set is
189         either:
190         
191           <UL>
192           <LI> The properties object passed into ModuleControl.boot()
193           after the database has been booted. This set will be a DoubleProperties
194           object with the per-database transaction set as the read set
195           and the service.properties as the write set.
196           <LI>
197           The Dictionary set returned/passed in by a method of BasicService.Properties.
198           </UL>
199         <BR>
200         This method uses the same search order as the getService() calls.
201
202     */

203     public static String JavaDoc getPropertyFromSet(Properties JavaDoc set, String JavaDoc key) {
204     
205         boolean dbOnly = set != null ? isDBOnly(set) : false;
206
207         return PropertyUtil.getPropertyFromSet(dbOnly, set, key);
208     }
209
210     public static Serializable JavaDoc getPropertyFromSet(Dictionary JavaDoc set, String JavaDoc key) {
211     
212         boolean dbOnly = set != null ? isDBOnly(set) : false;
213
214         return PropertyUtil.getPropertyFromSet(dbOnly, set, key);
215     }
216
217     public static Serializable JavaDoc getPropertyFromSet(boolean dbOnly, Dictionary JavaDoc set, String JavaDoc key) {
218
219         if (set != null) {
220
221             Serializable JavaDoc value;
222
223             if (!dbOnly) {
224                 value = Monitor.getMonitor().getJVMProperty(key);
225                 if (value != null)
226                     return value;
227             }
228         
229             value = (Serializable JavaDoc) set.get(key);
230             if (value != null)
231                 return value;
232
233             if (dbOnly)
234                 return null;
235         }
236
237         return PropertyUtil.getSystemProperty(key);
238     }
239
240     public static String JavaDoc getPropertyFromSet(boolean dbOnly, Properties JavaDoc set, String JavaDoc key) {
241
242         if (set != null) {
243
244             String JavaDoc value;
245
246             if (!dbOnly) {
247                 value = Monitor.getMonitor().getJVMProperty(key);
248                 if (value != null)
249                     return value;
250             }
251         
252             value = set.getProperty(key);
253             if (value != null)
254                 return value;
255
256             if (dbOnly)
257                 return null;
258         }
259
260         return PropertyUtil.getSystemProperty(key);
261     }
262
263     /**
264         Get a property only looking in the Persistent Transactional (database) set.
265
266         @exception StandardException Standard Cloudscape error handling.
267     */

268     public static String JavaDoc getDatabaseProperty(PersistentSet set, String JavaDoc key)
269         throws StandardException {
270
271         if (set == null)
272             return null;
273
274         Object JavaDoc obj = set.getProperty(key);
275         if (obj == null) { return null; }
276         return obj.toString();
277     }
278
279     /**
280         Find a service wide property with a default. Search order is
281
282         The service is the persistent service associated with the
283         current context stack.
284
285         @return the value of the property or defaultValue if it does not exist.
286
287         @exception StandardException Standard Cloudscape error handling.
288     */

289     public static String JavaDoc getServiceProperty(PersistentSet set, String JavaDoc key, String JavaDoc defaultValue)
290         throws StandardException {
291
292
293         String JavaDoc value =
294             PropertyUtil.getDatabaseProperty(
295                 set, Property.DATABASE_PROPERTIES_ONLY);
296
297         boolean dbOnly =
298             Boolean.valueOf(
299                 (value != null ? value.trim() : value)).booleanValue();
300
301         if (!dbOnly) {
302             value = Monitor.getMonitor().getJVMProperty(key);
303             if (value != null)
304                 return value;
305         }
306
307         value = PropertyUtil.getDatabaseProperty(set, key);
308         if (value != null)
309             return value;
310
311         if (dbOnly) {
312             return defaultValue;
313         }
314
315         return PropertyUtil.getSystemProperty(key, defaultValue);
316     }
317
318
319     /**
320         Find a service wide property.
321
322         The service is the persistent service associated with the
323         current context stack.
324
325         @return the value of the property or null if it does not exist.
326
327             @exception StandardException Standard Cloudscape error handling.
328     */

329     public static String JavaDoc getServiceProperty(PersistentSet set, String JavaDoc key)
330         throws StandardException {
331         return PropertyUtil.getServiceProperty(set, key, (String JavaDoc) null);
332     }
333
334     /**
335         Get a system wide property as a boolean.
336
337         @return true of the property is set to 'true, TRUE', false otherwise
338     */

339     public static boolean getSystemBoolean(String JavaDoc key) {
340
341         String JavaDoc value = PropertyUtil.getSystemProperty(key);
342
343         return(
344             Boolean.valueOf(
345                 (value != null ? value.trim() : value)).booleanValue());
346     }
347
348     /**
349         Get a service wide property as a boolean.
350
351         @return true of the property is set to 'true, TRUE', false otherwise
352
353         @exception StandardException Standard Cloudscape error handling.
354     */

355     public static boolean getServiceBoolean(PersistentSet set, String JavaDoc key, boolean defValue)
356         throws StandardException {
357
358         String JavaDoc value = PropertyUtil.getServiceProperty(set, key);
359
360         return booleanProperty(key, value, defValue);
361     }
362
363     /**s
364         Get a system wide property as a int.
365
366         @return value of the property if set subject to min and max, defaultValue if
367         it is not set or set to a non-integer value.
368     */

369     public static int getSystemInt(String JavaDoc key, int min, int max, int defaultValue) {
370         return PropertyUtil.handleInt(PropertyUtil.getSystemProperty(key), min, max, defaultValue);
371     }
372
373     /**
374         Get a service wide property as a int.
375
376         @return value of the property if set subject to min and max, defaultValue if
377         it is not set or set to a non-integer value.
378
379         @exception StandardException Standard Cloudscape error handling.
380
381     */

382     public static int getServiceInt(PersistentSet set, String JavaDoc key, int min, int max, int defaultValue)
383         throws StandardException {
384         //return PropertyUtil.intPropertyValue(key, PropertyUtil.getServiceProperty(set, key), min, max, defaultValue);
385
return PropertyUtil.handleInt(PropertyUtil.getServiceProperty(set, key), min, max, defaultValue);
386     }
387
388     /**
389         Get a service wide property as a int. The passed in Properties
390         set overrides any system, applcation or per-database properties.
391
392         @return value of the property if set subject to min and max, defaultValue if
393         it is not set or set to a non-integer value.
394
395         @exception StandardException Standard Cloudscape error handling.
396
397     */

398     public static int getServiceInt(PersistentSet set, Properties JavaDoc props, String JavaDoc key, int min, int max, int defaultValue)
399         throws StandardException {
400
401         String JavaDoc value = null;
402
403         if (props != null)
404             value = props.getProperty(key);
405
406         if (value == null)
407             value = PropertyUtil.getServiceProperty(set, key);
408
409         return PropertyUtil.handleInt(value, min, max, defaultValue);
410     }
411
412     /**
413         Get a system wide property as a int.
414
415         @return value of the property if, defaultValue if
416         it is not set or set to a non-integer value.
417     */

418     public static int getSystemInt(String JavaDoc key, int defaultValue) {
419         return PropertyUtil.getSystemInt(key, 0, Integer.MAX_VALUE, defaultValue);
420     }
421
422     /**
423         Parse an string as an int based property value.
424     */

425     public static int handleInt(String JavaDoc value, int min, int max, int defaultValue) {
426
427         if (value == null)
428             return defaultValue;
429
430         try {
431             int intValue = Integer.parseInt(value);
432             if ((intValue >= min) && (intValue <= max))
433                 return intValue;
434         }
435         catch (NumberFormatException JavaDoc nfe)
436         {
437             // just leave the default.
438
}
439         return defaultValue;
440     }
441
442     /**
443       Parse and validate and return a boolean property value. If the value is invalid
444       raise an exception.
445
446       <P>
447       The following are valid property values.
448       <UL>
449       <LI> null - returns defaultValue
450       <LI> "true" - returns true (in any case without the quotes)
451       <LI> "false" - return true (in any case without the quotes)
452       </UL>
453       @exception StandardException Oops
454       */

455     public static boolean booleanProperty(String JavaDoc p, Serializable JavaDoc v, boolean defaultValue)
456          throws StandardException
457     {
458         if (v==null)
459             return defaultValue;
460
461         String JavaDoc vS = ((String JavaDoc) v).trim();
462         if (StringUtil.SQLToLowerCase(vS).equals("true"))
463             return true;
464         if (StringUtil.SQLToLowerCase(vS).equals("false"))
465             return false;
466
467         throw StandardException.newException(SQLState.PROPERTY_INVALID_VALUE, p,vS);
468     }
469
470     /**
471       Parse, validate and return an integer property value. If the value is invalid
472       raise an exception. If the value passed in is null return a default value.
473
474       @exception StandardException Oops
475       */

476     public static int intPropertyValue(String JavaDoc p, Serializable JavaDoc v,
477                                        int minValue, int maxValue, int defaultValue)
478          throws StandardException
479     {
480         if (v==null)
481             return defaultValue;
482
483         String JavaDoc vs = ((String JavaDoc)v).trim();
484         try {
485             int result = Integer.parseInt(vs);
486             if (result < minValue || result > maxValue)
487                 throw StandardException.newException(SQLState.PROPERTY_INVALID_VALUE, p,vs);
488             return result;
489         }
490         catch (NumberFormatException JavaDoc nfe) {
491             throw StandardException.newException(SQLState.PROPERTY_INVALID_VALUE, p,vs);
492         }
493     }
494
495     /**
496       Return true iff the key is the name of a database property that is
497       stored in services.properties.
498       */

499     public static boolean isServiceProperty(String JavaDoc key)
500     {
501         for (int i = 0; i < PropertyUtil.servicePropertyList.length; i++)
502             if (key.equals(PropertyUtil.servicePropertyList[i])) return true;
503         return false;
504     }
505 }
506
507
Popular Tags