KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > PropertyWrapper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 // NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * Filename: ServerChannel.java
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42   
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/PropertyWrapper.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:03 $
47  */

48
49 package com.sun.enterprise.util;
50
51 import java.util.Properties JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.InputStream JavaDoc;
54 //Bug 4677074 begin
55
import java.util.logging.Logger JavaDoc;
56 import java.util.logging.Level JavaDoc;
57 import com.sun.logging.LogDomains;
58 //Bug 4677074 end
59

60 public class PropertyWrapper extends java.lang.Object JavaDoc implements java.io.Serializable JavaDoc
61 {
62 //Bug 4677074 begin
63
static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
64 //Bug 4677074 end
65
/** try a defauly property file if env name fails? */
66     protected boolean bTryDefaultFileOnEnvFailure = true;
67     /** maintains a flag to know if the properties were loaded successfully */
68     private boolean bLoaded = false;
69     /** debug flag */
70     protected boolean bDebug=false;
71     /** The name of the properties file. */
72     protected String JavaDoc properties_file_name = null;
73     /**
74     * The bucket to hold the properties.
75     * @see java.util.Properties
76     */

77     protected Properties JavaDoc props=null;
78
79     /**
80     * The constructor loads the properties from a file. The name of the file
81     * can be specified in two ways. If an environment property name exists,
82     * it would be the first to be given a shot. if the properties are not
83     * loaded and flag 'bTryDefaultFileOnEnvFailure' is true, a default filename
84     * in the propertiesFileName parameter will be given a shot.
85     * @param propertiesFileName The name of the file holding the properties
86     * @param envPropName Environment name for the property
87     * @param bTryDefaultFileOnEnvFailure try a defauly property file if env name fails?
88     */

89     public PropertyWrapper(String JavaDoc propertiesFileName, String JavaDoc envPropName, boolean bTryDefaultFileOnEnvFailure)
90     {
91         // First try to see if you can load properties vai the env variable
92
try
93         {
94             if(null!=envPropName)
95             {
96                 SecurityManager JavaDoc sm = System.getSecurityManager();
97                 if(null!=sm)
98                     sm.checkPropertyAccess(envPropName);
99                 String JavaDoc envPropFile = System.getProperty(envPropName);
100                 //if(bDebug) System.out.println("envname=" + envPropName + ",envPropName=" + envPropFile);
101
//Bug 4677074 begin
102
//if(com.sun.enterprise.util.logging.Debug.enabled) _logger.log(Level.FINE,"envname=" + envPropName + ",envPropName=" + envPropFile);
103
//Bug 4677074 end
104
if(null!=envPropFile)
105                 {
106                     properties_file_name = envPropFile;
107                     try
108                     {
109                         loadProperties();
110                     }catch(IOException JavaDoc ioe)
111                     {
112                         if(com.sun.enterprise.util.logging.Debug.enabled)
113 //Bug 4677074 System.err.println("PropertyWrapper::PropertyWrapper() > " + ioe);
114
//Bug 4677074 begin
115
_logger.log(Level.SEVERE,"enterprise_util.dbgcntl_ioexception",ioe);
116 //Bug 4677074 end
117
}
118                 }
119             }
120         } catch(SecurityException JavaDoc se)
121         {
122 //Bug 4677074 System.out.println("PropertyWrapper::PropertyWrapper() don not have security access to " + properties_file_name + " > " + se);
123
//Bug 4677074 begin
124
_logger.log(Level.SEVERE,"iplanet_util.security_exception",new Object JavaDoc[]{properties_file_name,se});
125 //Bug 4677074 end
126
}
127         // Try to load from a property file specified checking the 'bTryDefaultFileOnEnvFailure' flag
128
if( !bLoaded && bTryDefaultFileOnEnvFailure && (null!=propertiesFileName) )
129         {
130             try
131             {
132                 properties_file_name = propertiesFileName;
133                 loadProperties();
134             }catch(IOException JavaDoc ioe)
135             {
136                 if(com.sun.enterprise.util.logging.Debug.enabled)
137 //Bug 4677074 System.err.println("PropertyWrapper::PropertyWrapper() > " + ioe);
138
//Bug 4677074 begin
139
_logger.log(Level.SEVERE,"enterprise_util.dbgcntl_ioexception",ioe);
140 //Bug 4677074 end
141
}
142         }
143
144         if(bLoaded && bDebug)
145 //Bug 4677074 System.out.println("PropertyWrapper using " + properties_file_name);
146
//Bug 4677074 begin
147
_logger.log(Level.FINE,"PropertyWrapper using " + properties_file_name);
148 //Bug 4677074 end
149
else if (!bLoaded && bDebug)
150 //Bug 4677074 System.out.println("PropertyWrapper reports properties could not be loaded for env=" + envPropName + " or filename=" + propertiesFileName);
151
//Bug 4677074 begin
152
_logger.log(Level.FINE,"PropertyWrapper reports properties could not be loaded for env=" + envPropName + " or filename=" + propertiesFileName);
153 //Bug 4677074 end
154
}
155
156     /**
157     * The constructor loads the properties from a file. The name of the file
158     * can be specified in two ways. If an environment property name exists,
159     * it would be the first to be given a shot, if the properties are not
160     * loaded, a default filename in the propertiesFileName parameter will be
161     * given a shot.
162     * @param propertiesFileName The name of the file holding the properties
163     * @param envPropName Environment name for the property
164     */

165     public PropertyWrapper(String JavaDoc propertiesFileName, String JavaDoc envPropName)
166     {
167         this(propertiesFileName, envPropName, true);
168     }
169     
170     /**
171     * The constructor loads the properties from a file from the classpath.
172     * @param propertiesFileName The name of the file holding the properties
173     */

174     public PropertyWrapper(String JavaDoc propertiesFileName)
175     {
176         this(propertiesFileName, null, true);
177     }
178     
179     /**
180     * The constructor to initialize the object with a set of properties.
181     * @param props The property to init the object.
182     */

183     public PropertyWrapper(Properties JavaDoc props)
184     {
185         this.props=props;
186     }
187
188     /**
189     * Return the filename of the properties file.
190     * @return the name of the properties file for the instance.
191     */

192     public String JavaDoc getPropertiesFile()
193     {
194         return properties_file_name;
195     }
196
197     /**
198     * This method allows to load properties into the instance object, from the filename
199     * specified in the parameter of the constructor. Set flag bLoaded to true if the
200     * properties are loaded and are non empty.
201     * @exception BaseException when an error is encountered in reading the
202     * file listing the properties.
203     */

204     protected void loadProperties()
205         throws IOException JavaDoc
206     {
207         props = new Properties JavaDoc();
208
209         try
210         {
211             InputStream JavaDoc is = ClassLoader.getSystemResourceAsStream(properties_file_name);
212             props.load(is);
213             if(!props.isEmpty())
214                 bLoaded = true;
215         }catch(IOException JavaDoc e)
216         {
217             throw e;
218         }
219     }
220
221     /**
222     * This method returns the value for a particular name of a property.
223     * @param key The name stored in the properties file associated with a value.
224     * @return The value of the key in the properties, null in case the key is not found.
225     */

226     protected String JavaDoc getProperty(String JavaDoc key)
227     {
228         return getProperty(key, null);
229     }
230
231     /**
232     * This method returns the value for a particular name of a property with a default
233     * value supplied along.
234     * @param key The name stored in the properties file associated with a value.
235     * @param defaultVal The default value to be returned if the key is not found.
236     * @return The value of the key.
237     * @see #getProperty(java.lang.String)
238     */

239     protected String JavaDoc getProperty(String JavaDoc key, String JavaDoc defaultVal)
240     {
241         return props.getProperty(key, defaultVal);
242     }
243     
244     /**
245     * Read the string property and convert it to an int.
246     * @return int -1 on failure
247     */

248     protected int getIntProperty(String JavaDoc key)
249     {
250         String JavaDoc str = getProperty(key);
251         if(null==str)
252             return -1;
253         try
254         {
255             return Integer.parseInt(str);
256         }catch(NumberFormatException JavaDoc nfe)
257         {
258             return -1;
259         }
260     }
261     
262     /**
263     * Read the string property and convert it to an int.
264     * @return int -1 on conversion failure, default if no property found,
265     * converted value otherwise
266     */

267     protected int getIntProperty(String JavaDoc key, int defaultVal)
268     {
269         String JavaDoc str = getProperty(key);
270         if(null==str)
271             return defaultVal;
272         try
273         {
274             return Integer.parseInt(str);
275         }catch(NumberFormatException JavaDoc nfe)
276         {
277             return -1;
278         }
279     }
280     
281     /**
282     * Read the string property [true | false] and convert it into a boolean.
283     * @return boolean true or false, default if no property found
284     */

285     protected boolean getBooleanProperty(String JavaDoc key, boolean defaultVal)
286     {
287         String JavaDoc str = getProperty(key);
288         if(null==str)
289             return defaultVal;
290         if(str.toLowerCase().startsWith("true"))
291             return true;
292         if(str.toLowerCase().startsWith("false"))
293             return false;
294         return defaultVal;
295     }
296     
297     /**
298     * Read the string property and convert it to an long.
299     * @return long -1 on failure
300     */

301     protected long getLongProperty(String JavaDoc key)
302     {
303         String JavaDoc str = getProperty(key);
304         if (str == null)
305             return -1;
306         try {
307             return Long.parseLong(str);
308         } catch(NumberFormatException JavaDoc nfe) {
309             return -1;
310         }
311     }
312     
313     /**
314     * Read the string property and convert it to an long.
315     * @return long -1 on conversion failure, default if no property found,
316     * converted value otherwise
317     */

318     protected long getLongProperty(String JavaDoc key, long defaultVal)
319     {
320         String JavaDoc str = getProperty(key);
321         if (str == null)
322             return defaultVal;
323         try {
324             return Long.parseLong(str);
325         } catch(NumberFormatException JavaDoc nfe) {
326             return -1;
327         }
328     }
329     
330 }
331
Popular Tags