KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanukisoftware > wrapper > resources > ResourceManager


1 package org.tanukisoftware.wrapper.resources;
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  * Portions of the Software have been derived from source code
29  * developed by Silver Egg Technology under the following license:
30  *
31  * Copyright (c) 2001 Silver Egg Technology
32  *
33  * Permission is hereby granted, free of charge, to any person
34  * obtaining a copy of this software and associated documentation
35  * files (the "Software"), to deal in the Software without
36  * restriction, including without limitation the rights to use,
37  * copy, modify, merge, publish, distribute, sub-license, and/or
38  * sell copies of the Software, and to permit persons to whom the
39  * Software is furnished to do so, subject to the following
40  * conditions:
41  *
42  * The above copyright notice and this permission notice shall be
43  * included in all copies or substantial portions of the Software.
44  */

45
46 import java.util.Hashtable JavaDoc;
47 import java.util.MissingResourceException JavaDoc;
48 import java.util.ResourceBundle JavaDoc;
49 import java.text.MessageFormat JavaDoc;
50
51 /**
52  * Some helper functions for handling i18n issues. One instance of this class
53  * should be created for each resource.<P>
54  *
55  * The ResourceManager is created by a call to <CODE>getResourceManager()</CODE>.
56  * The (optional) parameter is the name of the desired resource, not including the
57  * <code>.properties</code> suffix.
58  *
59  * For example,<P>
60  * <CODE>
61  * ResourceManager res = getResourceBundle();
62  * </CODE>
63  * <P>to get the default resources, or<P>
64  * <CODE>
65  * ResourceManager res = getResourceBundle("sql");
66  * </CODE>
67  * <P>
68  * to load the resources in <code>sql.properties</code>.
69  *
70  * To use the ResourceManager make a call to any of the <CODE>format()</CODE>
71  * methods. If a string is not found in the bundle the key is returned and a
72  * message is logged to the debug channel for this class.
73  *
74  * @author Leif Mortenson <leif@tanukisoftware.com>
75  */

76 public class ResourceManager
77 {
78     private static Hashtable JavaDoc m_resources = new Hashtable JavaDoc();
79     
80     /**
81      * Whenever the Default Locale of the JVM is changed, then any existing
82      * resource bundles will need to update their values. The
83      * _classRefreshCounter static variable gets updated whenever a refresh
84      * is done. The m_refreshCounter variable then is used to tell whether
85      * an individual ResourceManager is up to date or not.
86      */

87     private int m_refreshCounter;
88     private static int m_staticRefreshCounter = 0;
89     
90     /**
91      * The ResourceBundle for this locale.
92      */

93     private ResourceBundle JavaDoc m_bundle;
94     private String JavaDoc m_bundleName;
95     
96     /**
97      * Private Constructor.
98      */

99     private ResourceManager( String JavaDoc resourceName )
100     {
101         // Resolve the bundle name based on this class so that it will work correctly with obfuscators.
102
String JavaDoc className = this.getClass().getName();
103         // Strip off the class name at the end, keeping the ending '.'
104
int pos = className.lastIndexOf( '.' );
105         if ( pos > 0 )
106         {
107             m_bundleName = className.substring( 0, pos + 1 );
108         }
109         else
110         {
111             m_bundleName = "";
112         }
113         
114         m_bundleName += resourceName;
115         
116         // Now load the bundle for the current Locale
117
refreshBundle();
118     }
119     
120     private void refreshBundle()
121     {
122         try
123         {
124             m_bundle = ResourceBundle.getBundle( m_bundleName );
125         }
126         catch ( MissingResourceException JavaDoc e )
127         {
128             System.out.println( e );
129         }
130         
131         m_refreshCounter = m_staticRefreshCounter;
132     }
133     
134     /**
135      * Returns the default resource manager.
136      * An instance of the ResourceManager class is created the first
137      * time the method is called.
138      */

139     public static ResourceManager getResourceManager()
140     {
141         return getResourceManager( null );
142     }
143     
144     /**
145      * Returns the named resource manager.
146      * An instance of the ResourceManager class is created the first
147      * time the method is called.
148      *
149      * @param resourceName The name of the desired resource
150      */

151     public static synchronized ResourceManager getResourceManager( String JavaDoc resourceName )
152     {
153         if ( resourceName == null )
154         {
155             resourceName = "resource";
156         }
157         ResourceManager resource = (ResourceManager)m_resources.get( resourceName );
158         if ( resource == null )
159         {
160             resource = new ResourceManager( resourceName );
161             m_resources.put( resourceName, resource );
162         }
163         return resource;
164     }
165
166     /**
167      * Clears the resource manager's cache of bundles (this should be called
168      * if the default locale for the application changes).
169      */

170     public static synchronized void refresh()
171     {
172         m_resources = new Hashtable JavaDoc();
173         m_staticRefreshCounter++;
174     }
175     
176     private String JavaDoc getString( String JavaDoc key )
177     {
178         // Make sure that the ResourceManager is up to date in a thread safe way
179
synchronized(this)
180         {
181             if ( m_refreshCounter != m_staticRefreshCounter )
182             {
183                 refreshBundle();
184             }
185         }
186         
187         String JavaDoc msg;
188         if ( m_bundle == null )
189         {
190             msg = key;
191         }
192         else
193         {
194             try
195             {
196                 msg = m_bundle.getString( key );
197             }
198             catch ( MissingResourceException JavaDoc ex )
199             {
200                 msg = key;
201                 System.out.println( key + " is missing from resource bundle \"" + m_bundleName
202                     + "\"" );
203             }
204         }
205         
206         return msg;
207     }
208     
209     /**
210      * Returns a string that has been obtained from the resource manager
211      *
212      * @param key The string that is the key to the translated message
213      *
214      */

215     public String JavaDoc format( String JavaDoc key )
216     {
217         return getString( key );
218     }
219     
220     /**
221      * Returns a string that has been obtained from the resource manager then
222      * formatted using the passed parameters.
223      *
224      * @param pattern The string that is the key to the translated message
225      * @param o0 The param passed to format replaces {0}
226      *
227      */

228     public String JavaDoc format( String JavaDoc pattern, Object JavaDoc o0 )
229     {
230         return MessageFormat.format( getString( pattern ), new Object JavaDoc[] { o0 } );
231     }
232
233    /**
234      * Returns a string that has been obtained from the resource manager then
235      * formatted using the passed parameters.
236      *
237      * @param pattern The string that is the key to the translated message
238      * @param o0 The param passed to format replaces {0}
239      * @param o1 The param passed to format replaces {1}
240      *
241      */

242     public String JavaDoc format( String JavaDoc pattern, Object JavaDoc o0, Object JavaDoc o1 )
243     {
244         return MessageFormat.format( getString( pattern ), new Object JavaDoc[] { o0,o1 } );
245     }
246
247    /**
248      * Returns a string that has been obtained from the resource manager then
249      * formatted using the passed parameters.
250      *
251      * @param pattern The string that is the key to the translated message
252      * @param o0 The param passed to format replaces {0}
253      * @param o1 The param passed to format replaces {1}
254      * @param o2 The param passed to format replaces {2}
255      *
256      */

257     public String JavaDoc format( String JavaDoc pattern, Object JavaDoc o0, Object JavaDoc o1, Object JavaDoc o2 )
258     {
259         return MessageFormat.format( getString( pattern ), new Object JavaDoc[] { o0,o1,o2 } );
260     }
261
262    /**
263      * Returns a string that has been obtained from the resource manager then
264      * formatted using the passed parameters.
265      *
266      * @param pattern The string that is the key to the translated message
267      * @param o0 The param passed to format replaces {0}
268      * @param o1 The param passed to format replaces {1}
269      * @param o2 The param passed to format replaces {2}
270      * @param o3 The param passed to format replaces {3}
271      *
272      */

273     public String JavaDoc format( String JavaDoc pattern, Object JavaDoc o0, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3 )
274     {
275         return MessageFormat.format( getString( pattern ), new Object JavaDoc[] { o0,o1,o2,o3 } );
276     }
277
278     // add more if you need them...
279
}
280
281
Popular Tags