Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/derby/iapi/services/property/PropertyUtil.java


1   /*
2   
3      Derby - Class org.apache.derby.iapi.services.property.PropertyUtil
4   
5      Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable.
6   
7      Licensed under the Apache License, Version 2.0 (the "License");
8      you may not use this file except in compliance with the License.
9      You may obtain a copy of the License at
10  
11        http://www.apache.org/licenses/LICENSE-2.0
12  
13     Unless required by applicable law or agreed to in writing, software
14     distributed under the License is distributed on an "AS IS" BASIS,
15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16     See the License for the specific language governing permissions and
17     limitations under the License.
18  
19   */
20  
21  package org.apache.derby.iapi.services.property;
22  
23  import org.apache.derby.iapi.reference.Property;
24  import org.apache.derby.iapi.reference.SQLState;
25  import org.apache.derby.iapi.reference.Attribute;
26  import org.apache.derby.iapi.reference.EngineType;
27  import org.apache.derby.iapi.services.monitor.Monitor;
28  import org.apache.derby.iapi.services.monitor.ModuleFactory;
29  import org.apache.derby.iapi.error.StandardException;
30  import org.apache.derby.iapi.util.StringUtil;
31  
32  import java.util.Properties;
33  import java.io.Serializable;
34  import java.util.Dictionary;
35  
36  /**
37    There are 5 property objects within a JBMS system.
38  
39    1) JVM - JVM set - those in System.getProperties
40    2) APP - Application set - derby.properties file
41    3) SRV - Persistent Service set - Those stored in service.properties
42    4) TRAN - Persistent Transactional set - Those stored via the AccessManager interface
43    5) BOOT - Set by a boot method (rare)
44  
45    This class has a set of static methods to find a property using a consistent search order
46    from the above set.
47    <BR>
48    getSystem*() methods use the search order.
49    <OL>
50    <LI> JVM
51    <LI> APP
52    </OL>
53    <BR>
54    getService* methods use the search order
55    <OL>
56    <LI> JVM
57    <LI> TRAN
58    <LI> SRV
59    <LI> APP
60    </OL>
61  
62  */
63  public class PropertyUtil {
64  
65    // List of properties that are stored in the service.properties file
66    public static final String[] servicePropertyList = {
67      EngineType.PROPERTY,
68      Property.NO_AUTO_BOOT,
69      Property.STORAGE_TEMP_DIRECTORY,
70          Attribute.CRYPTO_PROVIDER,
71          Attribute.CRYPTO_ALGORITHM,
72      Attribute.RESTORE_FROM,
73      Attribute.LOG_DEVICE,
74      Property.LOG_ARCHIVE_MODE
75    };
76  
77    /**
78      Property is set in JVM set
79    */
80    public static final int SET_IN_JVM = 0;  
81    /**
82      Property is set in DATABASE set
83    */
84    public static final int SET_IN_DATABASE = 1;
85    /**
86      Property is set in APPLICATION (derby.properties) set
87    */
88    public static final int SET_IN_APPLICATION = 2;
89  
90    /**
91      Property is not set.
92    */
93    public static final int NOT_SET = -1;
94  
95  
96    static int whereSet(String key, Dictionary set) {
97  
98      boolean dbOnly = isDBOnly(set);
99  
100     if (!dbOnly) {
101       if (Monitor.getMonitor().getJVMProperty(key) != null) {
102         return SET_IN_JVM;
103       }
104     }
105     
106     if ((set != null) && (set.get(key) != null))
107         return SET_IN_DATABASE;
108 
109     if (!dbOnly) {
110       if (PropertyUtil.getSystemProperty(key) != null)
111         return SET_IN_APPLICATION;
112     }
113 
114     return NOT_SET;
115   }
116 
117   public static boolean isDBOnly(Dictionary set) {
118 
119     if (set == null)
120       return false;
121 
122     String value = (String) set.get(Property.DATABASE_PROPERTIES_ONLY);
123 
124     boolean dbOnly = Boolean.valueOf(
125                     (value != null ? value.trim() : value)).booleanValue();
126 
127     return dbOnly;
128   }
129 
130   public static boolean isDBOnly(Properties set) {
131 
132     if (set == null)
133       return false;
134 
135     String value = set.getProperty(Property.DATABASE_PROPERTIES_ONLY);
136 
137     boolean dbOnly = Boolean.valueOf(
138                     (value != null ? value.trim() : value)).booleanValue();
139 
140     return dbOnly;
141   }
142   
143   /**
144     Find a system wide property. Search order is
145 
146     @return the value of the property or null if it does not exist.
147   */
148   public static String getSystemProperty(String key) {
149     return PropertyUtil.getSystemProperty(key, (String) null);
150   }
151 
152   /**
153     Find a system wide property with a default. Search order is
154 
155     <OL>
156     <LI> JVM property
157     <LI> derby.properties
158     </OL>
159 
160     <P>
161     This method can be used by a system that is not running Cloudscape,
162     just to maintain the same lookup logic and security manager concerns
163     for finding derby.properties and reading system properties.
164 
165     @return the value of the property or defaultValue if it does not exist.
166   */
167   public static String getSystemProperty(String key, String defaultValue) {
168 
169     ModuleFactory monitor = Monitor.getMonitorLite();
170 
171     String value = monitor.getJVMProperty(key);
172 
173     if (value == null) {
174 
175       Properties applicationProperties =
176         monitor.getApplicationProperties();
177 
178       if (applicationProperties != null)
179         value = applicationProperties.getProperty(key);
180     }
181     return value == null ? defaultValue : value;
182   }
183 
184 
185   /**
186     Get a property from the passed in set. The passed in set is
187     either:
188     
189       <UL>
190       <LI> The properties object passed into ModuleControl.boot()
191       after the database has been booted. This set will be a DoubleProperties
192       object with the per-database transaction set as the read set
193       and the service.properties as the write set.
194       <LI>
195       The Dictionary set returned/passed in by a method of BasicService.Properties.
196       </UL>
197     <BR>
198     This method uses the same search order as the getService() calls.
199 
200   */
201   public static String getPropertyFromSet(Properties set, String key) {
202   
203     boolean dbOnly = set != null ? isDBOnly(set) : false;
204 
205     return PropertyUtil.getPropertyFromSet(dbOnly, set, key);
206   }
207 
208   public static Serializable getPropertyFromSet(Dictionary set, String key) {
209   
210     boolean dbOnly = set != null ? isDBOnly(set) : false;
211 
212     return PropertyUtil.getPropertyFromSet(dbOnly, set, key);
213   }
214 
215   public static Serializable getPropertyFromSet(boolean dbOnly, Dictionary set, String key) {
216 
217     if (set != null) {
218 
219       Serializable value;
220 
221       if (!dbOnly) {
222         value = Monitor.getMonitor().getJVMProperty(key);
223         if (value != null)
224           return value;
225       }
226     
227       value = (Serializable) set.get(key);
228       if (value != null)
229         return value;
230 
231       if (dbOnly)
232         return null;
233     }
234 
235     return PropertyUtil.getSystemProperty(key);
236   }
237 
238   public static String getPropertyFromSet(boolean dbOnly, Properties set, String key) {
239 
240     if (set != null) {
241 
242       String value;
243 
244       if (!dbOnly) {
245         value = Monitor.getMonitor().getJVMProperty(key);
246         if (value != null)
247           return value;
248       }
249     
250       value = set.getProperty(key);
251       if (value != null)
252         return value;
253 
254       if (dbOnly)
255         return null;
256     }
257 
258     return PropertyUtil.getSystemProperty(key);
259   }
260 
261   /**
262     Get a property only looking in the Persistent Transactional (database) set.
263 
264     @exception StandardException Standard Cloudscape error handling. 
265   */
266   public static String getDatabaseProperty(PersistentSet set, String key) 
267     throws StandardException {
268 
269     if (set == null)
270       return null;
271 
272     Object obj = set.getProperty(key);
273      if (obj == null) { return null; }
274      return obj.toString();
275   }
276 
277   /**
278     Find a service wide property with a default. Search order is
279 
280     The service is the persistent service associated with the
281     current context stack.
282 
283     @return the value of the property or defaultValue if it does not exist.
284 
285     @exception StandardException Standard Cloudscape error handling. 
286   */
287   public static String getServiceProperty(PersistentSet set, String key, String defaultValue) 
288     throws StandardException {
289 
290 
291     String value =
292       PropertyUtil.getDatabaseProperty(
293                 set, Property.DATABASE_PROPERTIES_ONLY);
294 
295     boolean dbOnly = 
296             Boolean.valueOf(
297                 (value != null ? value.trim() : value)).booleanValue();
298 
299     if (!dbOnly) {
300       value = Monitor.getMonitor().getJVMProperty(key);
301       if (value != null)
302         return value;
303     }
304 
305     value = PropertyUtil.getDatabaseProperty(set, key);
306     if (value != null)
307       return value;
308 
309     if (dbOnly) {
310       return defaultValue;
311     }
312 
313     return PropertyUtil.getSystemProperty(key, defaultValue);
314   }
315 
316 
317   /**
318     Find a service wide property. 
319 
320     The service is the persistent service associated with the
321     current context stack.
322 
323     @return the value of the property or null if it does not exist.
324 
325       @exception StandardException Standard Cloudscape error handling. 
326   */
327   public static String getServiceProperty(PersistentSet set, String key)
328     throws StandardException {
329     return PropertyUtil.getServiceProperty(set, key, (String) null);
330   }
331 
332   /**
333     Get a system wide property as a boolean.
334 
335     @return true of the property is set to 'true, TRUE', false otherwise
336   */
337   public static boolean getSystemBoolean(String key) {
338 
339         String value = PropertyUtil.getSystemProperty(key);
340 
341     return( 
342             Boolean.valueOf(
343                 (value != null ? value.trim() : value)).booleanValue());
344   }
345 
346   /**
347     Get a service wide property as a boolean.
348 
349     @return true of the property is set to 'true, TRUE', false otherwise
350 
351     @exception StandardException Standard Cloudscape error handling. 
352   */
353   public static boolean getServiceBoolean(PersistentSet set, String key, boolean defValue) 
354     throws StandardException {
355 
356         String value = PropertyUtil.getServiceProperty(set, key);
357 
358     return booleanProperty(key, value, defValue);
359   }
360 
361   /**s
362     Get a system wide property as a int.
363 
364     @return value of the property if set subject to min and max, defaultValue if
365     it is not set or set to a non-integer value.
366   */
367   public static int getSystemInt(String key, int min, int max, int defaultValue) {
368     return PropertyUtil.handleInt(PropertyUtil.getSystemProperty(key), min, max, defaultValue);
369   }
370 
371   /**
372     Get a service wide property as a int.
373 
374     @return value of the property if set subject to min and max, defaultValue if
375     it is not set or set to a non-integer value.
376 
377     @exception StandardException Standard Cloudscape error handling. 
378 
379   */
380   public static int getServiceInt(PersistentSet set, String key, int min, int max, int defaultValue)
381     throws StandardException {
382     //return PropertyUtil.intPropertyValue(key, PropertyUtil.getServiceProperty(set, key), min, max, defaultValue);
383     return PropertyUtil.handleInt(PropertyUtil.getServiceProperty(set, key), min, max, defaultValue);
384   }
385 
386   /**
387     Get a service wide property as a int. The passed in Properties
388     set overrides any system, applcation or per-database properties.
389 
390     @return value of the property if set subject to min and max, defaultValue if
391     it is not set or set to a non-integer value.
392 
393     @exception StandardException Standard Cloudscape error handling. 
394 
395   */
396   public static int getServiceInt(PersistentSet set, Properties props, String key, int min, int max, int defaultValue)
397     throws StandardException {
398 
399     String value = null;
400 
401     if (props != null)
402       value = props.getProperty(key);
403 
404     if (value == null)
405       value = PropertyUtil.getServiceProperty(set, key);
406 
407     return PropertyUtil.handleInt(value, min, max, defaultValue);
408   }
409 
410   /**
411     Get a system wide property as a int.
412 
413     @return value of the property if, defaultValue if
414     it is not set or set to a non-integer value.
415   */
416   public static int getSystemInt(String key, int defaultValue) {
417     return PropertyUtil.getSystemInt(key, 0, Integer.MAX_VALUE, defaultValue);
418   }
419 
420   /**
421     Parse an string as an int based property value.
422   */
423   public static int handleInt(String value, int min, int max, int defaultValue) {
424 
425     if (value == null)
426       return defaultValue;
427 
428     try {
429       int intValue = Integer.parseInt(value);
430       if ((intValue >= min) && (intValue <= max))
431         return intValue;
432     }
433     catch (NumberFormatException nfe)
434     {
435       // just leave the default.
436     }
437     return defaultValue;
438   }
439 
440   /**
441     Parse and validate and return a boolean property value. If the value is invalid
442     raise an exception.
443 
444     <P>
445     The following are valid property values.
446     <UL>
447     <LI> null - returns defaultValue
448     <LI> "true" - returns true (in any case without the quotes)
449     <LI> "false" - return true (in any case without the quotes)
450     </UL>
451     @exception StandardException Oops
452     */
453   public static boolean booleanProperty(String p, Serializable v, boolean defaultValue)
454      throws StandardException
455   {
456     if (v==null)
457       return defaultValue;
458 
459     String vS = ((String) v).trim();
460     if (StringUtil.SQLToLowerCase(vS).equals("true"))
461       return true;
462     if (StringUtil.SQLToLowerCase(vS).equals("false"))
463       return false;
464 
465     throw StandardException.newException(SQLState.PROPERTY_INVALID_VALUE, p,vS);
466   }
467 
468   /**
469     Parse, validate and return an integer property value. If the value is invalid
470     raise an exception. If the value passed in is null return a default value.
471 
472     @exception StandardException Oops
473     */
474   public static int intPropertyValue(String p, Serializable v,
475                      int minValue, int maxValue, int defaultValue)
476      throws StandardException
477   {
478     if (v==null)
479       return defaultValue;
480 
481     String vs = ((String)v).trim();
482     try {
483       int result = Integer.parseInt(vs);
484       if (result < minValue || result > maxValue)
485         throw StandardException.newException(SQLState.PROPERTY_INVALID_VALUE, p,vs);
486       return result;
487     }
488     catch (NumberFormatException nfe) {
489       throw StandardException.newException(SQLState.PROPERTY_INVALID_VALUE, p,vs);
490     }
491   }
492 
493   /**
494     Return true iff the key is the name of a database property that is 
495     stored in services.properties.
496     */ 
497   public static boolean isServiceProperty(String key)
498   {
499     for (int i = 0; i < PropertyUtil.servicePropertyList.length; i++) 
500       if (key.equals(PropertyUtil.servicePropertyList[i])) return true;
501     return false;
502   }
503 }
504