KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > SafeProperties


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: SafeProperties.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.util;
46
47 import java.util.Properties JavaDoc;
48
49 import org.openejb.OpenEJBException;
50
51 public class SafeProperties{
52
53     private String JavaDoc systemLocation;
54     private Properties JavaDoc props;
55
56     /**
57      * Creates a new SafeProperties usign the Properties object passed in.
58      *
59      * @throws OpenEJBExcption if the properties object passed in is null.
60      */

61     public SafeProperties(Properties JavaDoc props, String JavaDoc systemLocation) throws OpenEJBException{
62         if (props == null) OpenEJBErrorHandler.propertiesObjectIsNull(systemLocation);
63         this.props = props;
64         this.systemLocation = systemLocation;
65     }
66
67     /**
68      * Returns the value of the key.
69      *
70      * @throws OpenEJBException if the property does not exist or is null.
71      */

72     public String JavaDoc getProperty(String JavaDoc key) throws OpenEJBException{
73         String JavaDoc value = props.getProperty(key);
74         if (value == null)OpenEJBErrorHandler.propertyNotFound(key, systemLocation+ " properties object");
75         return value;
76     }
77     /**
78      * Returns the value of the key.
79      *
80      * @throws OpenEJBException if the property does not exist or is null.
81      */

82     public String JavaDoc getProperty(String JavaDoc key, String JavaDoc defaultValue) throws OpenEJBException{
83         String JavaDoc value = props.getProperty(key);
84         if (value == null)
85             return defaultValue;
86         else
87             return value;
88     }
89
90     /**
91      * Returns the value of the key as an int.
92      *
93      * @throws OpenEJBException if the property does not exist, is null, or cannot be converted to an int.
94      */

95     public int getPropertyAsInt(String JavaDoc key) throws OpenEJBException{
96         int integer = 0;
97         String JavaDoc value = getProperty(key);
98         try{
99             integer = Integer.parseInt(value);
100         }
101         catch(NumberFormatException JavaDoc nfe){
102             OpenEJBErrorHandler.propertyValueIsIllegal(key, value);
103         }
104         return integer;
105     }
106     /**
107      * Returns the value of the key as an int.
108      *
109      * @throws OpenEJBException if the property does not exist, is null, or cannot be converted to an int.
110      */

111     public int getPropertyAsInt(String JavaDoc key, int defaultValue) throws OpenEJBException{
112         int integer = defaultValue;
113         String JavaDoc value = getProperty(key,String.valueOf(defaultValue));
114         try{
115             integer = Integer.parseInt(value);
116         }
117         catch(NumberFormatException JavaDoc nfe){
118             OpenEJBErrorHandler.propertyValueIsIllegal(key, value);
119         }
120         return integer;
121     }
122     /**
123      * Returns the value of the key as an Integer.
124      *
125      * @throws OpenEJBException if the property does not exist, is null, or cannot be converted to an Integer.
126      */

127     public Integer JavaDoc getPropertyAsInteger(String JavaDoc key, Integer JavaDoc defaultValue) throws OpenEJBException{
128         Integer JavaDoc integer = null;
129         String JavaDoc value = getProperty(key,defaultValue.toString());
130         try{
131             integer = new Integer JavaDoc(value);
132         }
133         catch(NumberFormatException JavaDoc nfe){
134             OpenEJBErrorHandler.propertyValueIsIllegal(key, value);
135         }
136         return integer;
137     }
138     /**
139      * Returns the value of the key as an Integer.
140      *
141      * @throws OpenEJBException if the property does not exist, is null, or cannot be converted to an Integer.
142      */

143     public Integer JavaDoc getPropertyAsInteger(String JavaDoc key) throws OpenEJBException{
144         Integer JavaDoc integer = null;
145         String JavaDoc value = getProperty(key);
146         try{
147             integer = new Integer JavaDoc(value);
148         }
149         catch(NumberFormatException JavaDoc nfe){
150             OpenEJBErrorHandler.propertyValueIsIllegal(key, value);
151         }
152         return integer;
153     }
154     /**
155      * Returns the value of the key as an boolean.
156      *
157      */

158     public boolean getPropertyAsBoolean(String JavaDoc key) throws OpenEJBException{
159         Integer JavaDoc integer = null;
160         String JavaDoc value = getProperty(key);
161         return new Boolean JavaDoc(value).booleanValue();
162     }
163     /**
164      * Returns the value of the key as an boolean.
165      *
166      */

167     public Boolean JavaDoc getPropertyAsBoolean(String JavaDoc key, Boolean JavaDoc defaultValue) throws OpenEJBException{
168         Integer JavaDoc integer = null;
169         String JavaDoc value = getProperty(key, defaultValue.toString());
170         return new Boolean JavaDoc(value);
171     }
172
173 }
Popular Tags