KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > resources > ITrackerResources


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.resources;
20
21 import java.rmi.*;
22 import java.util.*;
23 import javax.ejb.*;
24 import javax.rmi.*;
25
26 import cowsultants.itracker.ejb.client.exceptions.*;
27 import cowsultants.itracker.ejb.client.interfaces.*;
28 import cowsultants.itracker.ejb.client.models.*;
29 import cowsultants.itracker.ejb.client.util.*;
30
31 public class ITrackerResources {
32     public static final String JavaDoc RESOURCE_BUNDLE_NAME = "cowsultants.itracker.ejb.client.resources.ITracker";
33     public static final String JavaDoc DEFINED_LOCALES_KEY = "itracker.locales";
34     public static final String JavaDoc DEFAULT_LOCALE = "en_US";
35     public static final String JavaDoc BASE_LOCALE = "BASE";
36     public static final String JavaDoc NO_LOCALE = "ZZ_ZZ";
37
38     public static final String JavaDoc KEY_BASE_CUSTOMFIELD_TYPE = "itracker.web.generic.";
39     public static final String JavaDoc KEY_BASE_PROJECT_STATUS = "itracker.project.status.";
40     public static final String JavaDoc KEY_BASE_PERMISSION = "itracker.user.permission.";
41     public static final String JavaDoc KEY_BASE_RESOLUTION = "itracker.resolution.";
42     public static final String JavaDoc KEY_BASE_SEVERITY = "itracker.severity.";
43     public static final String JavaDoc KEY_BASE_STATUS = "itracker.status.";
44     public static final String JavaDoc KEY_BASE_USER_STATUS = "itracker.user.status.";
45
46     public static final String JavaDoc KEY_BASE_CUSTOMFIELD = "itracker.customfield.";
47     public static final String JavaDoc KEY_BASE_CUSTOMFIELD_OPTION = ".option.";
48     public static final String JavaDoc KEY_BASE_CUSTOMFIELD_LABEL = ".label";
49
50     private static String JavaDoc defaultLocale = null;
51     private static HashMap locales = new HashMap();
52     private static HashMap languages = new HashMap();
53     private static boolean initialized = false;
54     private static SystemConfigurationHome configHome = null;
55
56     private static Object JavaDoc bundleLock = new Object JavaDoc();
57
58     public ITrackerResources() {
59     }
60
61     public static void setConfigHome(SystemConfigurationHome value) {
62         configHome = value;
63     }
64
65     public HashMap getLanguages() {
66         return languages;
67     }
68
69     public static Locale getLocale() {
70         return getLocale(getDefaultLocale());
71     }
72
73     public static Locale getLocale(String JavaDoc localeString) {
74         if(localeString == null || localeString.trim().equals("")) {
75             return getLocale(getDefaultLocale());
76         }
77
78         Locale locale = (Locale) locales.get(localeString);
79         if(locale == null && localeString != null && ! localeString.trim().equals("")) {
80             try {
81                 Logger.logDebug("Creating new locale for '" + localeString + "'");
82                 if(localeString.length() == 5) {
83                     locale = new Locale(localeString.substring(0, 2), localeString.substring(3));
84                 } else if(localeString.length() == 2) {
85                     locale = new Locale(localeString, "");
86                 } else if(localeString.equals(BASE_LOCALE)) {
87                     locale = new Locale("", "");
88                 } else {
89                     Logger.logDebug("Invalid locale '" + localeString + "' specified. It must be either LN or LN_CN.");
90                     throw new Exception JavaDoc("Invalid locale string");
91                 }
92             } catch(Exception JavaDoc e) {
93                 if(! localeString.equals(getDefaultLocale())) {
94                     Logger.logDebug("Failed creating new locale for '" + localeString + "' attempting for default locale '" + getDefaultLocale() + "'");
95                     return getLocale(getDefaultLocale());
96                 } else {
97                     Logger.logDebug("Failed creating new default locale for '" + getDefaultLocale() + "' attempting for DEFAULT_LOCALE '" + DEFAULT_LOCALE + "'");
98                     return getLocale(DEFAULT_LOCALE);
99                 }
100             }
101             locales.put(localeString, locale);
102         }
103         return locale;
104     }
105
106     public static String JavaDoc getDefaultLocale() {
107         return (defaultLocale == null ? DEFAULT_LOCALE : defaultLocale);
108     }
109
110     public static void setDefaultLocale(String JavaDoc value) {
111         defaultLocale = value;
112     }
113
114     public static ResourceBundle getBundle() {
115         return getBundle(getDefaultLocale());
116     }
117
118     public static ResourceBundle getBundle(String JavaDoc locale) {
119         if(locale == null || locale.equals("")) {
120             locale = getDefaultLocale();
121         }
122
123         return getBundle(getLocale(locale));
124     }
125
126     public static ResourceBundle getBundle(Locale locale) {
127         if(locale == null) {
128             locale = getLocale(getDefaultLocale());
129         }
130         ResourceBundle bundle = (ResourceBundle) languages.get(locale);
131         if(bundle == null) {
132             if(configHome != null) {
133                 Logger.logDebug("Loading new resource bundle for locale " + locale + " from the database.");
134                 try {
135                     SystemConfiguration sc = configHome.create();
136                     LanguageModel[] languageItems = sc.getLanguage(locale);
137                     bundle = new ITrackerResourceBundle(locale, languageItems);
138                 } catch(CreateException ce) {
139                     Logger.logError("Unable to create new SystemConfiguration bean.", ce);
140                 }
141             } else {
142                 Logger.logDebug("Loading new resource bundle for locale " + locale + " from properties file.");
143                 bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_NAME, locale);
144             }
145             if(bundle != null) {
146                 putBundle(locale, bundle);
147             } else if(! locale.toString().equals(getDefaultLocale())) {
148                 bundle = getBundle(getLocale());
149             }
150         }
151
152         return bundle;
153     }
154
155     public static void putBundle(Locale locale, ResourceBundle bundle) {
156         if(locale != null && bundle != null) {
157             synchronized (bundleLock) {
158                 languages.put(locale, bundle);
159             }
160         }
161     }
162
163     /**
164       * Clears a single cached resource bundle. The next time the bundle is
165       * accessed, it will be reloaded and placed into the cache.
166       */

167     public static void clearBundle(Locale locale) {
168         if(locale != null) {
169             synchronized (bundleLock) {
170                 languages.remove(locale);
171             }
172         }
173     }
174
175     /**
176       * Clears all cached resource bundles. The next time a bundle is accessed,
177       * it will be reloaded and placed into the cache.
178       */

179     public static void clearBundles() {
180         synchronized (bundleLock) {
181             languages.clear();
182         }
183     }
184
185     /**
186       * Clears a single key from all cached resource bundles. The key is then marked
187       * that it is dirty and should be reloaded on hte next access.
188       */

189     public static void clearKeyFromBundles(String JavaDoc key, boolean markDirty) {
190         if(key != null) {
191             synchronized (bundleLock) {
192                 for(Iterator iter = languages.values().iterator(); iter.hasNext(); ) {
193                     ((ITrackerResourceBundle) iter.next()).removeValue(key, markDirty);
194                 }
195             }
196         }
197     }
198
199     public static String JavaDoc getString(String JavaDoc key) {
200         return getString(key, getLocale(defaultLocale));
201     }
202
203     public static String JavaDoc getString(String JavaDoc key, String JavaDoc locale) {
204         if(key == null) {
205             return "";
206         }
207
208         if(locale == null || locale.equals("")) {
209             locale = getDefaultLocale();
210         }
211
212         return getString(key, getLocale(locale));
213     }
214
215     public static String JavaDoc getString(String JavaDoc key, Locale locale) {
216         if(key == null) {
217             return "";
218         }
219
220         if(locale == null) {
221             locale = getLocale(getDefaultLocale());
222         }
223
224         try {
225             try {
226                 return getBundle(locale).getString(key);
227             } catch(ITrackerDirtyResourceException idre) {
228                 if(configHome != null) {
229                     Logger.logDebug("Loading new key to replace dirty key " + key + " for resource bundle for locale " + locale);
230                     try {
231                         SystemConfiguration sc = configHome.create();
232                         LanguageModel languageItem = sc.getLanguageItemByKey(key, locale);
233                         ((ITrackerResourceBundle) getBundle(locale)).updateValue(languageItem);
234                     } catch(Exception JavaDoc e) {
235                         ((ITrackerResourceBundle) getBundle(locale)).removeValue(key, false);
236                     }
237                 } else {
238                     ((ITrackerResourceBundle) getBundle(locale)).removeValue(key, false);
239                 }
240             }
241             return getBundle(locale).getString(key);
242         } catch(NullPointerException JavaDoc npe) {
243             Logger.logError("Unable to get any resources. The requested locale was " + locale);
244             return "MISSING BUNDLE: " + locale;
245         } catch(MissingResourceException mre) {
246             Logger.logInfo("MissingResourceException caught while retrieving translation key '" + key + "' for locale " + locale);
247             return "MISSING KEY: " + key;
248         }
249     }
250
251     public static String JavaDoc getString(String JavaDoc key, Object JavaDoc[] options) {
252         return getString(key, getLocale(getDefaultLocale()), options);
253     }
254
255     public static String JavaDoc getString(String JavaDoc key, String JavaDoc locale, Object JavaDoc[] options) {
256         return getString(key, getLocale(locale), options);
257     }
258
259     public static String JavaDoc getString(String JavaDoc key, Locale locale, Object JavaDoc[] options) {
260         String JavaDoc message = getString(key, locale);
261         return MessageFormat.format(message, options, locale);
262     }
263
264     public static String JavaDoc getString(String JavaDoc key, String JavaDoc locale, String JavaDoc option) {
265         String JavaDoc message = getString(key, locale);
266         return MessageFormat.format(message, new Object JavaDoc[] {option}, getLocale(locale));
267     }
268
269     public static String JavaDoc getString(String JavaDoc key, Locale locale, String JavaDoc option) {
270         String JavaDoc message = getString(key, locale);
271         return MessageFormat.format(message, new Object JavaDoc[] {option}, locale);
272     }
273
274     public static String JavaDoc getCheckForKey(String JavaDoc key) throws MissingResourceException {
275         return getCheckForKey(key, getLocale());
276     }
277
278     public static String JavaDoc getCheckForKey(String JavaDoc key, Locale locale) throws MissingResourceException {
279         try {
280             return getBundle(locale).getString(key);
281         } catch(ITrackerDirtyResourceException idre) {
282             return getString(key, locale);
283         } catch(NullPointerException JavaDoc npe) {
284             Logger.logError("Unable to get ResourceBundle for locale " + locale);
285             throw new MissingResourceException("MISSING LOCALE: " + locale, "ITrackerResources", key);
286         }
287     }
288
289     public static boolean isLongString(String JavaDoc key) {
290         String JavaDoc value = getString(key);
291         if(value.length() > 80 || value.indexOf('\n') > 0) {
292             return true;
293         }
294         return false;
295     }
296
297     public static String JavaDoc escapeUnicodeString(String JavaDoc str, boolean escapeAll) {
298         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
299         for(int i = 0; i < str.length(); i++) {
300             char ch = str.charAt(i);
301             if (! escapeAll && ((ch >= 0x0020) && (ch <= 0x007e))) {
302                 sb.append(ch);
303             } else {
304                 sb.append('\\').append('u');
305                 sb.append(encodeHex((ch >> 12) & 0xF));
306                 sb.append(encodeHex((ch >> 8) & 0xF));
307                 sb.append(encodeHex((ch >> 4) & 0xF));
308                 sb.append(encodeHex( ch & 0xF));
309             }
310         }
311         return sb.toString();
312     }
313
314     public static String JavaDoc unescapeUnicodeString(String JavaDoc str) {
315         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
316
317         for(int i = 0; i < str.length(); ) {
318             char ch = str.charAt(i++);
319             if(ch == '\\') {
320                 if(str.charAt(i++) == 'u') {
321                     int value = 0;
322                     for(int j = 0; j < 4; j++) {
323                         value = (value<<4) + decodeHex(str.charAt(i++));
324                     }
325                     sb.append((char) value);
326                 } else {
327                     sb.append("\\" + str.charAt(i));
328                 }
329             } else {
330                 sb.append(ch);
331             }
332         }
333         return sb.toString();
334     }
335
336     public static final String JavaDoc HEXCHARS = "0123456789ABCDEF";
337     public static char encodeHex(int value) {
338         return HEXCHARS.charAt(value & 0xf);
339     }
340
341     public static int decodeHex(char ch) {
342         int value = -1;
343
344         if(ch >= '0' && ch <= '9') {
345            value = ch - '0';
346         } else if(ch >= 'a' && ch <= 'f') {
347            value = ch - 'a' + 10;
348         } else if(ch >= 'A' && ch <= 'F') {
349            value = ch - 'A' + 10;
350         }
351
352         return value;
353     }
354
355 }
356
Popular Tags