KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > SOMLocalStringsManager


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 package com.sun.enterprise.admin.util;
24
25 import java.util.ResourceBundle JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29
30
31 /**
32  * Implementation of a SOM local string manager as SOM-specific wrapper around LocalStringsManager;
33  * Provides access to i18n messages for classes that need them.
34  * This particular implementation presents the following resources organization:
35  *<ul>
36  * <li>1. Resource files share locations with sources (same directories);</li>
37  * <li>2. Base directory is "com.sun.enterprise.admin";</li>
38  * <li>3. Search ONLY in first resource bundle, which found on the way up to basePackage( No additional hiearchical search of value if it is not found in that file)</li>
39  *</ul>
40  */

41
42 public class SOMLocalStringsManager extends LocalStringsManager
43 {
44     private static final String JavaDoc DEFAULT_PROPERTY_FILE_NAME = "bundle";
45     private static final String JavaDoc DEFAULT_PACKAGE_NAME = "com.sun.enterprise.admin";
46     //private static final String NO_DEFAULT = "No local strings defined";
47

48     /** cache for all the local string managers (per pkg) */
49     private static Hashtable JavaDoc managers = new Hashtable JavaDoc();
50
51     public SOMLocalStringsManager(String JavaDoc packageName)
52     {
53         super(packageName, DEFAULT_PROPERTY_FILE_NAME);
54         setFixedResourceBundle(packageName);
55     }
56
57    /**
58      * Returns a local string manager for the given package name.
59      *
60      * @param packageName name of the package of the src
61      *
62      * @return a local string manager for the given package name
63      */

64     public synchronized static SOMLocalStringsManager getManager(String JavaDoc packageName) {
65
66         SOMLocalStringsManager mgr = (SOMLocalStringsManager) managers.get(packageName);
67
68         if (mgr == null) {
69             mgr = new SOMLocalStringsManager(packageName);
70             try {
71                 managers.put(packageName, mgr);
72             } catch (Exception JavaDoc e) {
73                 // do nothing
74
}
75         }
76         return mgr;
77     }
78
79     /**
80      *
81      * Returns a local string manager for the given package name.
82      *
83      * @param callerClass the object making the call
84      *
85      * @return a local string manager for the given package name
86      */

87     public synchronized static SOMLocalStringsManager getManager(Class JavaDoc callerClass) {
88     
89         try {
90             int dotIdx = callerClass.getName().lastIndexOf(".");
91             if (dotIdx != -1) {
92                 String JavaDoc pkgName = callerClass.getName().substring(0, dotIdx);
93                 return getManager(pkgName);
94             } else {
95                 // class does not belong to any pkg
96
String JavaDoc pkgName = callerClass.getName();
97                 return getManager(pkgName);
98             }
99         } catch (Exception JavaDoc e) {
100             // Return default manager, and not null, so as to avoid
101
// null pointer exception in the calling method.
102
return getManager(DEFAULT_PACKAGE_NAME);
103         }
104     }
105     
106     /**
107      * Get a localized string.
108      * @param key The name of the resource to fetch
109      * @return The localized string
110      */

111     public String JavaDoc getString( String JavaDoc key)
112     {
113         return super.getString(key, key); // key itself will act as default value,
114
//instead of some string like "No local strings found"
115
}
116
117     /**
118      * Get a localized string.
119      * @param key The name of the resource to fetch
120      * @param args The default return value if not found
121      * @return The localized string
122      */

123     public String JavaDoc getString( String JavaDoc key, String JavaDoc defaultValue)
124     {
125         return super.getString(key, defaultValue);
126     }
127
128     /**
129      * Get a local string and format the arguments accordingly.
130      * @param key The key to the local format string
131      * @param arguments The set of arguments to provide to the formatter
132      * @return A formatted localized string
133      */

134     public String JavaDoc getString( String JavaDoc key, Object JavaDoc[] args)
135     {
136         return super.getString(key, key, args);
137     }
138
139     /**
140      * Get a local string and format the arguments accordingly.
141      * @param key The key to the local format string
142      * @param defaultFormat The default format if not found in the resources
143      * @param arguments The set of arguments to provide to the formatter
144      * @return A formatted localized string
145      */

146     public String JavaDoc getString( String JavaDoc key, String JavaDoc defaultFormat, Object JavaDoc[] args)
147     {
148         return super.getString(key, defaultFormat, args);
149     }
150
151     /**
152      * Convenience method - getString() overriding for fixed number of formatting arguments.
153      * @param key The key to the local format string
154      * @param defaultFormat The default format if not found in the resources
155      * @param arg1 The first argument to provide to the formatter
156      * @return A formatted localized string
157      */

158     public String JavaDoc getString( String JavaDoc key, String JavaDoc defaultFormat, Object JavaDoc arg1)
159     {
160         return getString(key, defaultFormat, (new Object JavaDoc[]{arg1}));
161     }
162
163     /**
164      * Convenience method - getString() overriding for fixed number of formatting arguments.
165      * @param key The key to the local format string
166      * @param defaultFormat The default format if not found in the resources
167      * @param arg1 The first argument to provide to the formatter
168      * @param arg1 The second argument to provide to the formatter
169      * @return A formatted localized string
170      */

171     public String JavaDoc getString( String JavaDoc key, String JavaDoc defaultFormat, Object JavaDoc arg1, Object JavaDoc arg2)
172     {
173         return getString(key, defaultFormat, (new Object JavaDoc[]{arg1, arg2}));
174     }
175
176     /**
177      * Convenience method - getString() overriding for fixed number of formatting arguments.
178      * @param key The key to the local format string
179      * @param defaultFormat The default format if not found in the resources
180      * @param arg1 The first argument to provide to the formatter
181      * @param arg2 The second argument to provide to the formatter
182      * @param arg3 The third argument to provide to the formatter
183      * @return A formatted localized string
184      */

185     public String JavaDoc getString( String JavaDoc key, String JavaDoc defaultFormat, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3)
186     {
187         return getString(key, defaultFormat, (new Object JavaDoc[]{arg1, arg2, arg3}));
188     }
189 }
Popular Tags