1 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 RESOURCE_BUNDLE_NAME = "cowsultants.itracker.ejb.client.resources.ITracker"; 33 public static final String DEFINED_LOCALES_KEY = "itracker.locales"; 34 public static final String DEFAULT_LOCALE = "en_US"; 35 public static final String BASE_LOCALE = "BASE"; 36 public static final String NO_LOCALE = "ZZ_ZZ"; 37 38 public static final String KEY_BASE_CUSTOMFIELD_TYPE = "itracker.web.generic."; 39 public static final String KEY_BASE_PROJECT_STATUS = "itracker.project.status."; 40 public static final String KEY_BASE_PERMISSION = "itracker.user.permission."; 41 public static final String KEY_BASE_RESOLUTION = "itracker.resolution."; 42 public static final String KEY_BASE_SEVERITY = "itracker.severity."; 43 public static final String KEY_BASE_STATUS = "itracker.status."; 44 public static final String KEY_BASE_USER_STATUS = "itracker.user.status."; 45 46 public static final String KEY_BASE_CUSTOMFIELD = "itracker.customfield."; 47 public static final String KEY_BASE_CUSTOMFIELD_OPTION = ".option."; 48 public static final String KEY_BASE_CUSTOMFIELD_LABEL = ".label"; 49 50 private static String 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 bundleLock = new Object (); 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 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 ("Invalid locale string"); 91 } 92 } catch(Exception 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 getDefaultLocale() { 107 return (defaultLocale == null ? DEFAULT_LOCALE : defaultLocale); 108 } 109 110 public static void setDefaultLocale(String value) { 111 defaultLocale = value; 112 } 113 114 public static ResourceBundle getBundle() { 115 return getBundle(getDefaultLocale()); 116 } 117 118 public static ResourceBundle getBundle(String 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 167 public static void clearBundle(Locale locale) { 168 if(locale != null) { 169 synchronized (bundleLock) { 170 languages.remove(locale); 171 } 172 } 173 } 174 175 179 public static void clearBundles() { 180 synchronized (bundleLock) { 181 languages.clear(); 182 } 183 } 184 185 189 public static void clearKeyFromBundles(String 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 getString(String key) { 200 return getString(key, getLocale(defaultLocale)); 201 } 202 203 public static String getString(String key, String 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 getString(String 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 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 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 getString(String key, Object [] options) { 252 return getString(key, getLocale(getDefaultLocale()), options); 253 } 254 255 public static String getString(String key, String locale, Object [] options) { 256 return getString(key, getLocale(locale), options); 257 } 258 259 public static String getString(String key, Locale locale, Object [] options) { 260 String message = getString(key, locale); 261 return MessageFormat.format(message, options, locale); 262 } 263 264 public static String getString(String key, String locale, String option) { 265 String message = getString(key, locale); 266 return MessageFormat.format(message, new Object [] {option}, getLocale(locale)); 267 } 268 269 public static String getString(String key, Locale locale, String option) { 270 String message = getString(key, locale); 271 return MessageFormat.format(message, new Object [] {option}, locale); 272 } 273 274 public static String getCheckForKey(String key) throws MissingResourceException { 275 return getCheckForKey(key, getLocale()); 276 } 277 278 public static String getCheckForKey(String 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 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 key) { 290 String 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 escapeUnicodeString(String str, boolean escapeAll) { 298 StringBuffer sb = new StringBuffer (); 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 unescapeUnicodeString(String str) { 315 StringBuffer sb = new StringBuffer (); 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 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 |