01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2002,2008 Oracle. All rights reserved.
05: *
06: * $Id: PropUtil.java,v 1.22.2.3 2008/01/07 15:14:18 cwl Exp $
07: */
08:
09: package com.sleepycat.je.utilint;
10:
11: import java.util.Enumeration;
12: import java.util.Properties;
13: import java.util.Set;
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 props, String propName) {
26: String 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 validateProps(Properties props,
42: Set allowedProps, String apiMethod)
43: throws DatabaseException {
44:
45: if (props == null) {
46: return new Properties();
47: } else {
48: if (props.size() > 0) {
49: Enumeration e = props.propertyNames();
50: while (e.hasMoreElements()) {
51: String propName = (String) e.nextElement();
52: validateProp(propName, allowedProps, apiMethod);
53: }
54: }
55: return props;
56: }
57: }
58:
59: /**
60: * @throws DatabaseException if the property is not valid.
61: */
62: public static void validateProp(String propName, Set allowedProps,
63: String apiMethod) throws DatabaseException {
64:
65: if (!allowedProps.contains(propName)) {
66: throw new DatabaseException(propName
67: + " is not a valid property for " + apiMethod);
68: }
69: }
70:
71: /**
72: * Convert microseconds to milliseconds, ensuring that any microsecond
73: * value greater than zero converts to at least one millisecond to avoid a
74: * zero millisecond result since Object.wait(0) waits forever.
75: */
76: public static long microsToMillis(long micros) {
77: return (micros + 999) / 1000;
78: }
79: }
|