001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs;
021:
022: /**
023: * @author pugh
024: */
025: public class SystemProperties {
026:
027: public final static boolean ASSERTIONS_ENABLED;
028: static {
029: boolean tmp = false;
030: assert (tmp = true);
031: ASSERTIONS_ENABLED = tmp;
032: }
033:
034: /**
035: * Get boolean property, returning false if a security manager prevents us
036: * from accessing system properties
037: * @return true if the property exists and is set to true
038: */
039: public static boolean getBoolean(String arg0) {
040: try {
041: return Boolean.getBoolean(arg0);
042: } catch (Exception e) {
043: return false;
044: }
045: }
046:
047: public static boolean getBoolean(String name, boolean defaultValue) {
048: boolean result = defaultValue;
049: try {
050: String value = System.getProperty(name);
051: if (value == null)
052: return defaultValue;
053: result = toBoolean(value);
054: } catch (IllegalArgumentException e) {
055: } catch (NullPointerException e) {
056: }
057: return result;
058: }
059:
060: private static boolean toBoolean(String name) {
061: return ((name != null) && name.equalsIgnoreCase("true"));
062: }
063:
064: /**
065: * @param arg0 property name
066: * @param arg1 default value
067: * @return the int value (or arg1 if the property does not exist)
068: */
069: public static Integer getInteger(String arg0, int arg1) {
070: try {
071: return Integer.getInteger(arg0, arg1);
072: } catch (Exception e) {
073: return arg1;
074: }
075: }
076:
077: /**
078: * @param arg0 property name
079: * @return string value (or null if the property does not exist)
080: */
081: public static String getProperty(String arg0) {
082: try {
083: return System.getProperty(arg0);
084: } catch (Exception e) {
085: return null;
086: }
087: }
088:
089: /**
090: * @param arg0 property name
091: * @param arg1 default value
092: * @return string value (or arg1 if the property does not exist)
093: */
094: public static String getProperty(String arg0, String arg1) {
095: try {
096: return System.getProperty(arg0, arg1);
097: } catch (Exception e) {
098: return arg1;
099: }
100: }
101:
102: }
|