KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > SystemProperties


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 /**
23  * @author pugh
24  */

25 public class SystemProperties {
26
27     public final static boolean ASSERTIONS_ENABLED;
28     static {
29         boolean tmp = false;
30         assert(tmp = true);
31         ASSERTIONS_ENABLED = tmp;
32     }
33     /**
34      * Get boolean property, returning false if a security manager prevents us
35      * from accessing system properties
36      * @return true if the property exists and is set to true
37      */

38     public static boolean getBoolean(String JavaDoc arg0) {
39         try {
40         return Boolean.getBoolean(arg0);
41         } catch (Exception JavaDoc e) {
42             return false;
43         }
44     }
45
46     public static boolean getBoolean(String JavaDoc name, boolean defaultValue) {
47         boolean result = defaultValue;
48         try {
49             String JavaDoc value = System.getProperty(name);
50             if (value == null) return defaultValue;
51             result = toBoolean(value);
52         } catch (IllegalArgumentException JavaDoc e) {
53         } catch (NullPointerException JavaDoc e) {
54         }
55         return result;
56     }
57     private static boolean toBoolean(String JavaDoc name) {
58         return ((name != null) && name.equalsIgnoreCase("true"));
59     }
60
61       
62     /**
63      * @param arg0 property name
64      * @param arg1 default value
65      * @return the int value (or arg1 if the property does not exist)
66      */

67     public static Integer JavaDoc getInteger(String JavaDoc arg0, int arg1) {
68         try {
69                 return Integer.getInteger(arg0, arg1);
70         } catch (Exception JavaDoc e) {
71             return arg1;
72         }
73     }
74
75     /**
76      * @param arg0 property name
77      * @return string value (or null if the property does not exist)
78      */

79     public static String JavaDoc getProperty(String JavaDoc arg0) {
80         try {
81         return System.getProperty(arg0);
82         } catch (Exception JavaDoc e) {
83             return null;
84         }
85     }
86
87     /**
88      * @param arg0 property name
89      * @param arg1 default value
90      * @return string value (or arg1 if the property does not exist)
91      */

92     public static String JavaDoc getProperty(String JavaDoc arg0, String JavaDoc arg1) {
93         try {
94         return System.getProperty(arg0, arg1);
95         } catch (Exception JavaDoc e) {
96             return arg1;
97         }
98     }
99
100 }
101
Popular Tags