KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanukisoftware > wrapper > WrapperSystemPropertyUtil


1 package org.tanukisoftware.wrapper;
2
3 /*
4  * Copyright (c) 1999, 2006 Tanuki Software Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of the Java Service Wrapper and associated
8  * documentation files (the "Software"), to deal in the Software
9  * without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sub-license,
11  * and/or sell copies of the Software, and to permit persons to
12  * whom the Software is furnished to do so, subject to the
13  * following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */

27
28 /**
29  * A collection of utility methods which make it easy to work with System
30  * Properties without littering code with error handling.
31  *
32  * @author Leif Mortenson <leif@tanukisoftware.com>
33  */

34 public final class WrapperSystemPropertyUtil
35 {
36     /*---------------------------------------------------------------
37      * Static Methods
38      *-------------------------------------------------------------*/

39     /**
40      * Resolves a boolean property.
41      *
42      * @param name The name of the property to lookup.
43      * @param defaultValue The value to return if it is not set or is invalid.
44      *
45      * @return The requested property value.
46      */

47     public static boolean getBooleanProperty( String JavaDoc name, boolean defaultValue )
48     {
49         String JavaDoc val = System.getProperty( name );
50         if ( val != null )
51         {
52             if ( val.equalsIgnoreCase( "TRUE" ) )
53             {
54                 return true;
55             }
56         }
57         return false;
58     }
59     
60     /**
61      * Resolves an integer property.
62      *
63      * @param name The name of the property to lookup.
64      * @param defaultValue The value to return if it is not set or is invalid.
65      *
66      * @return The requested property value.
67      */

68     public static int getIntProperty( String JavaDoc name, int defaultValue )
69     {
70         String JavaDoc val = System.getProperty( name );
71         if ( val != null )
72         {
73             try
74             {
75                 return Integer.parseInt( val );
76             }
77             catch ( NumberFormatException JavaDoc e )
78             {
79                 return defaultValue;
80             }
81         }
82         else
83         {
84             return defaultValue;
85         }
86     }
87     
88     /**
89      * Resolves a long property.
90      *
91      * @param name The name of the property to lookup.
92      * @param defaultValue The value to return if it is not set or is invalid.
93      *
94      * @return The requested property value.
95      */

96     public static long getLongProperty( String JavaDoc name, long defaultValue )
97     {
98         String JavaDoc val = System.getProperty( name );
99         if ( val != null )
100         {
101             try
102             {
103                 return Long.parseLong( val );
104             }
105             catch ( NumberFormatException JavaDoc e )
106             {
107                 return defaultValue;
108             }
109         }
110         else
111         {
112             return defaultValue;
113         }
114     }
115     
116     /*---------------------------------------------------------------
117      * Constructors
118      *-------------------------------------------------------------*/

119     /**
120      * Not instantiable.
121      */

122     private WrapperSystemPropertyUtil()
123     {
124     }
125 }
126
127
Popular Tags