001: package org.tanukisoftware.wrapper;
002:
003: /*
004: * Copyright (c) 1999, 2006 Tanuki Software Inc.
005: *
006: * Permission is hereby granted, free of charge, to any person
007: * obtaining a copy of the Java Service Wrapper and associated
008: * documentation files (the "Software"), to deal in the Software
009: * without restriction, including without limitation the rights
010: * to use, copy, modify, merge, publish, distribute, sub-license,
011: * and/or sell copies of the Software, and to permit persons to
012: * whom the Software is furnished to do so, subject to the
013: * following conditions:
014: *
015: * The above copyright notice and this permission notice shall be
016: * included in all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
020: * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
021: * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
022: * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
023: * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
024: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
025: * OTHER DEALINGS IN THE SOFTWARE.
026: */
027:
028: /**
029: * A collection of utility methods which make it easy to work with System
030: * Properties without littering code with error handling.
031: *
032: * @author Leif Mortenson <leif@tanukisoftware.com>
033: */
034: public final class WrapperSystemPropertyUtil {
035: /*---------------------------------------------------------------
036: * Static Methods
037: *-------------------------------------------------------------*/
038: /**
039: * Resolves a boolean property.
040: *
041: * @param name The name of the property to lookup.
042: * @param defaultValue The value to return if it is not set or is invalid.
043: *
044: * @return The requested property value.
045: */
046: public static boolean getBooleanProperty(String name,
047: boolean defaultValue) {
048: String val = System.getProperty(name);
049: if (val != null) {
050: if (val.equalsIgnoreCase("TRUE")) {
051: return true;
052: }
053: }
054: return false;
055: }
056:
057: /**
058: * Resolves an integer property.
059: *
060: * @param name The name of the property to lookup.
061: * @param defaultValue The value to return if it is not set or is invalid.
062: *
063: * @return The requested property value.
064: */
065: public static int getIntProperty(String name, int defaultValue) {
066: String val = System.getProperty(name);
067: if (val != null) {
068: try {
069: return Integer.parseInt(val);
070: } catch (NumberFormatException e) {
071: return defaultValue;
072: }
073: } else {
074: return defaultValue;
075: }
076: }
077:
078: /**
079: * Resolves a long property.
080: *
081: * @param name The name of the property to lookup.
082: * @param defaultValue The value to return if it is not set or is invalid.
083: *
084: * @return The requested property value.
085: */
086: public static long getLongProperty(String name, long defaultValue) {
087: String val = System.getProperty(name);
088: if (val != null) {
089: try {
090: return Long.parseLong(val);
091: } catch (NumberFormatException e) {
092: return defaultValue;
093: }
094: } else {
095: return defaultValue;
096: }
097: }
098:
099: /*---------------------------------------------------------------
100: * Constructors
101: *-------------------------------------------------------------*/
102: /**
103: * Not instantiable.
104: */
105: private WrapperSystemPropertyUtil() {
106: }
107: }
|