KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
25  * LocalStringsImpl.java
26  *
27  * Created on December 3, 2005, 5:26 PM
28  *
29  */

30
31 package com.sun.enterprise.util;
32 import java.util.ResourceBundle JavaDoc;
33 import java.text.MessageFormat JavaDoc;
34
35 /**
36  * This class makes getting localized strings super-simple. This is the companion
37  * class to Strings. Use this class when performance may be an issue. I.e. Strings
38  * is all-static and creates a ResourceBundle on every call. This class is instantiated
39  * once and can be used over and over from the same package.
40  * <p>Specifics:
41  * <ul>
42  * <li>Your calling code should have a file named LocalStrings.properties in its
43  * package directory.
44  * <li>If your localized string has no arguments call get(String) to get the localized
45  * String value.
46  * <li>If you have a parameterized string, call get(String, Object...)
47  * </ul>
48  * <p>Note: <b>You can not get an Exception out of calling this code!</b> If the String
49  * or the properties file does not exist, it will return the String that you gave
50  * in the first place as the argument.
51  * <p>Example:
52  * <ul>
53  * <li> LocalStringsImpl sh = new LocalStringsImpl();
54  * <li>String s = sh.get("xyz");
55  * <li>String s = sh.get("xyz", new Date(), 500, "something", 2.00003);
56  * <li>String s = sh.get("xyz", "something", "foo", "whatever");
57  * </ul>
58  *
59  * @author bnevins
60  */

61 public class LocalStringsImpl
62 {
63     /**
64      * Create a LocalStringsImpl instance.
65      * Automatically discover the caller's LocalStrings.properties file
66      */

67     public LocalStringsImpl()
68     {
69         setBundle();
70     }
71     
72     /**
73      * Create a LocalStringsImpl instance.
74      * use the proffered class object to find LocalStrings.properties.
75      * This is the constructor to use if you are concerned about getting
76      * the fastest performance.
77      */

78     public LocalStringsImpl(Class JavaDoc clazz)
79     {
80         setBundle(clazz);
81     }
82     
83     /**
84      * Get a String from the caller's package's LocalStrings.properties
85      * @param indexString The string index into the localized string file
86      * @return the String from LocalStrings or the supplied String if it doesn't exist
87      */

88     
89     public String JavaDoc get(String JavaDoc indexString)
90     {
91         try
92         {
93             return getBundle().getString(indexString);
94         }
95         catch (Exception JavaDoc e)
96         {
97             // it is not an error to have no key...
98
return indexString;
99         }
100     }
101     
102     /**
103      * Get and format a String from the caller's package's LocalStrings.properties
104      * @param indexString The string index into the localized string file
105      * @param objects The arguments to give to MessageFormat
106      * @return the String from LocalStrings or the supplied String if it doesn't exist --
107      * using the array of supplied Object arguments
108      */

109     public String JavaDoc get(String JavaDoc indexString, Object JavaDoc... objects)
110     {
111         indexString = get(indexString);
112         
113         try
114         {
115             MessageFormat JavaDoc mf = new MessageFormat JavaDoc(indexString);
116             return mf.format(objects);
117         }
118         catch(Exception JavaDoc e)
119         {
120             return indexString;
121         }
122     }
123     
124     /**
125      * Get a String from the caller's package's LocalStrings.properties
126      * @param indexString The string index into the localized string file
127      * @return the String from LocalStrings or the supplied default value if it doesn't exist
128      */

129     
130     public String JavaDoc getString(String JavaDoc indexString, String JavaDoc defaultValue)
131     {
132         try
133         {
134             return getBundle().getString(indexString);
135         }
136         catch (Exception JavaDoc e)
137         {
138             // it is not an error to have no key...
139
return defaultValue;
140         }
141     }
142     /**
143      * Get an integer from the caller's package's LocalStrings.properties
144      * @param indexString The string index into the localized string file
145      * @return the integer value from LocalStrings or the supplied default if
146      * it doesn't exist or is bad.
147      */

148     
149     public int getInt(String JavaDoc indexString, int defaultValue)
150     {
151         try
152         {
153             String JavaDoc s = getBundle().getString(indexString);
154             return Integer.parseInt(s);
155         }
156         catch (Exception JavaDoc e)
157         {
158             // it is not an error to have no key...
159
return defaultValue;
160         }
161     }
162     /**
163      * Get a boolean from the caller's package's LocalStrings.properties
164      * @param indexString The string index into the localized string file
165      * @return the integer value from LocalStrings or the supplied default if
166      * it doesn't exist or is bad.
167      */

168     
169     public boolean getBoolean(String JavaDoc indexString, boolean defaultValue)
170     {
171         try
172         {
173             return new Boolean JavaDoc(getBundle().getString(indexString));
174         }
175         catch (Exception JavaDoc e)
176         {
177             // it is not an error to have no key...
178
return defaultValue;
179         }
180     }
181     
182     ///////////////////////////////////////////////////////////////////////////
183

184     private ResourceBundle JavaDoc getBundle()
185     {
186         return bundle;
187     }
188     
189     ///////////////////////////////////////////////////////////////////////////
190

191     private void setBundle()
192     {
193         // go through the stack, top to bottom. The item that is below the LAST
194
// method that is in the util framework is where the logfile is.
195
// note that there may be more than one method from util in the stack
196
// because they may be calling us indirectly from LoggerHelper. Also
197
// note that this class won't work from any class in the util hierarchy itself.
198

199         try
200         {
201             StackTraceElement JavaDoc[] items = Thread.currentThread().getStackTrace();
202             int lastMeOnStack = -1;
203             
204             for(int i = 0; i < items.length; i++)
205             {
206                 StackTraceElement JavaDoc item = items[i];
207                 if(item.getClassName().startsWith(thisPackage))
208                     lastMeOnStack = i;
209             }
210             
211             String JavaDoc className = items[lastMeOnStack + 1].getClassName();
212             setBundle(className);
213         }
214         catch(Exception JavaDoc e)
215         {
216             bundle = null;
217         }
218     }
219
220     ///////////////////////////////////////////////////////////////////////////
221

222     private void setBundle(Class JavaDoc clazz)
223     {
224         setBundle(clazz.getName());
225     }
226     
227     ///////////////////////////////////////////////////////////////////////////
228

229     private void setBundle(String JavaDoc className)
230     {
231         try
232         {
233             String JavaDoc props;
234             // handle the default package...
235
if(className.indexOf('.') < 0)
236                 props = propsFilename;
237             else
238                 props = className.substring(0, className.lastIndexOf('.')) + '.' + propsFilename;
239             
240             bundle = ResourceBundle.getBundle(props);
241         }
242         catch(Exception JavaDoc e)
243         {
244             bundle = null;
245         }
246     }
247
248     ///////////////////////////////////////////////////////////////////////////
249

250     private ResourceBundle JavaDoc bundle;
251     private static final String JavaDoc thisPackage = "com.sun.enterprise.util";
252     private String JavaDoc propsFilename = "LocalStrings";
253 }
254
Popular Tags