KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > utilint > PropUtil


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: PropUtil.java,v 1.22 2006/10/30 21:14:29 bostic Exp $
7  */

8
9 package com.sleepycat.je.utilint;
10
11 import java.util.Enumeration JavaDoc;
12 import java.util.Properties JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import com.sleepycat.je.DatabaseException;
16
17 /**
18  * Convenience methods for handling JE properties.
19  */

20 public class PropUtil {
21
22     /**
23      * @return true if the property is set to "true".
24      */

25     public static boolean getBoolean(Properties JavaDoc props, String JavaDoc propName) {
26         String JavaDoc value = props.getProperty(propName);
27         if ((value != null) && (value.equalsIgnoreCase("true"))) {
28             return true;
29         } else {
30             return false;
31         }
32     }
33
34     /**
35      * Validate properties in the property bag. If null was passed, return an
36      * empty property object, else return the original property object.
37      *
38      * @throws DatabaseException if the property bag contains
39      * a property not specified in the set of allowed properties.
40      */

41     public static Properties JavaDoc validateProps(Properties JavaDoc props,
42                                            Set JavaDoc allowedProps,
43                                            String JavaDoc apiMethod)
44         throws DatabaseException {
45
46         if (props == null) {
47             return new Properties JavaDoc();
48         } else {
49             if (props.size() > 0) {
50                 Enumeration JavaDoc e = props.propertyNames();
51                 while (e.hasMoreElements()) {
52                     String JavaDoc propName = (String JavaDoc) e.nextElement();
53                     validateProp(propName, allowedProps, apiMethod);
54                 }
55             }
56             return props;
57         }
58     }
59
60     /**
61      * @throws DatabaseException if the property is not valid.
62      */

63     public static void validateProp(String JavaDoc propName,
64                                     Set JavaDoc allowedProps,
65                                     String JavaDoc apiMethod)
66         throws DatabaseException {
67
68         if (!allowedProps.contains(propName)) {
69             throw new DatabaseException
70         (propName + " is not a valid property for " + apiMethod);
71         }
72     }
73
74     /**
75      * Convert microseconds to milliseconds, ensuring that any microsecond
76      * value greater than zero converts to at least one millisecond to avoid a
77      * zero millisecond result since Object.wait(0) waits forever.
78      */

79     public static long microsToMillis(long micros) {
80         return (micros + 999) / 1000;
81     }
82 }
83
84
Popular Tags