KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > i18n > StringManager


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  * @(#) StringManager.java
26  *
27  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  *
31  * This software is the confidential and proprietary information
32  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
33  * You shall not disclose such Confidential Information and shall
34  * use it only in accordance with the terms of the license
35  * agreement you entered into with iPlanet/Sun Microsystems.
36  */

37 package com.sun.enterprise.util.i18n;
38
39 import java.util.ResourceBundle JavaDoc;
40 import java.util.Locale JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.text.MessageFormat JavaDoc;
43
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46 import com.sun.logging.LogDomains;
47
48 /**
49  * Implementation of a local string manager. Provides access to i18n messages
50  * for classes that need them.
51  *
52  * <p> One StringManager per package can be created and accessed by the
53  * getManager method call. The ResourceBundle name is constructed from
54  * the given package name in the constructor plus the suffix of "LocalStrings".
55  * Thie means that localized information will be contained in a
56  * LocalStrings.properties file located in the package directory of the
57  * classpath.
58  *
59  * <xmp>
60  * Example:
61  *
62  * [LocalStrings.properties]
63  * test=At {1,time} on {1,date}, there was {2} on planet {0,number,integer}
64  *
65  *
66  * StringManager sm = StringManager.getManager(this);
67  * .....
68  *
69  *
70  * try {
71  * ....
72  * } catch (Exception e) {
73  * String localizedMsg = sm.getString("test",
74  * new Integer(7), new java.util.Date(System.currentTimeMillis()),
75  * "a disturbance in the Force");
76  *
77  * throw new MyException(localizedMsg, e);
78  * }
79  *
80  * Localized message:
81  * At 2:27:41 PM on Jul 8, 2002, there was a disturbance in the Force
82  * on planet 7
83  *
84  * </xmp>
85  *
86  * @author Nazrul Islam
87  * @since JDK1.4
88  */

89 public class StringManager extends StringManagerBase {
90
91     /** logger used for this class */
92     private static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
93
94     /** resource bundle to be used by this manager */
95     private ResourceBundle JavaDoc _resourceBundle = null;
96
97     /** name of the resource bundle property file name */
98     private static final String JavaDoc RES_BUNDLE_NM = ".LocalStrings";
99     
100
101     /** cache for all the local string managers (per pkg) */
102     private static Hashtable JavaDoc managers = new Hashtable JavaDoc();
103
104     /**
105      * Initializes the resource bundle.
106      *
107      * @param packageName name of the package
108      */

109     private StringManager(String JavaDoc packageName) {
110         super(packageName + RES_BUNDLE_NM);
111     }
112
113     /**
114      * Returns a local string manager for the given package name.
115      *
116      * @param packageName name of the package of the src
117      *
118      * @return a local string manager for the given package name
119      */

120     public synchronized static StringManager getManager(String JavaDoc packageName) {
121
122         StringManager mgr = (StringManager) managers.get(packageName);
123
124         if (mgr == null) {
125             mgr = new StringManager(packageName);
126             try {
127                 managers.put(packageName, mgr);
128             } catch (Exception JavaDoc e) {
129                 _logger.log(Level.SEVERE,"iplanet_util.error_while_caching",e);
130             }
131         }
132         return mgr;
133     }
134
135     /**
136      *
137      * Returns a local string manager for the given package name.
138      *
139      * @param callerClass the object making the call
140      *
141      * @return a local string manager for the given package name
142      */

143     public synchronized static StringManager getManager(Class JavaDoc callerClass) {
144
145         try {
146             Package JavaDoc pkg = callerClass.getPackage();
147             if (pkg != null) {
148                 String JavaDoc pkgName = pkg.getName();
149                 return getManager(pkgName);
150             } else {
151                 // class does not belong to any pkg
152
String JavaDoc pkgName = callerClass.getName();
153                 return getManager(pkgName);
154             }
155         } catch (Exception JavaDoc e) {
156             _logger.log(Level.SEVERE, "iplanet_util.error_in_getMgr", e);
157
158             // dummy string manager
159
return getManager("");
160         }
161     }
162
163
164     /**
165      * Unit test code.
166      *
167      * @param args arguments from command line
168      */

169     public static void main(String JavaDoc[] args) {
170
171         long b = System.currentTimeMillis();
172         try {
173             StringManager sm =
174                 StringManager.getManager("com.sun.enterprise.util.i18n");
175
176             String JavaDoc ls = sm.getString(
177                 "test",
178                 new Integer JavaDoc(7), new java.util.Date JavaDoc(System.currentTimeMillis()),
179                 "a disturbance in the Force");
180
181             System.out.println(ls);
182
183             System.out.println( sm.getString("bad") );
184
185         } catch (Exception JavaDoc e) {
186             System.out.println("---- Error ---- ");
187             e.printStackTrace();
188         } finally {
189             long a = System.currentTimeMillis();
190             System.out.println("Time: " + (a-b) );
191         }
192     }
193 }
194
Popular Tags