1 23 24 37 package com.sun.enterprise.util.i18n; 38 39 import java.util.ResourceBundle ; 40 import java.util.Locale ; 41 import java.util.Hashtable ; 42 import java.text.MessageFormat ; 43 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 import com.sun.logging.LogDomains; 47 48 85 public class StringManagerBase { 86 87 88 private static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 89 90 91 private ResourceBundle _resourceBundle = null; 92 93 94 private static final String NO_DEFAULT = "No local string defined"; 95 96 97 private static Hashtable managers = new Hashtable (); 98 99 104 protected StringManagerBase(String resourceBundleName) { 105 try { 106 _resourceBundle = ResourceBundle.getBundle(resourceBundleName); 107 } catch (Exception e) { 108 _logger.log(Level.SEVERE, "iplanet_util.no_resource_bundle", e); 109 } 110 } 111 112 119 public synchronized static StringManagerBase getStringManager(String resourceBundleName) { 120 StringManagerBase mgr = (StringManagerBase) managers.get(resourceBundleName); 121 if (mgr == null) { 122 mgr = new StringManagerBase(resourceBundleName); 123 try { 124 managers.put(resourceBundleName, mgr); 125 } catch (Exception e) { 126 _logger.log(Level.SEVERE,"iplanet_util.error_while_caching",e); 127 } 128 } 129 return mgr; 130 } 131 132 139 public String getString(String key) { 140 return getStringWithDefault(key, NO_DEFAULT); 141 } 142 143 152 public String getStringWithDefault(String key, String defaultValue) { 153 154 String value = null; 155 156 try { 157 value = this._resourceBundle.getString(key); 158 } catch (Exception e) { 159 _logger.log(Level.FINE,"No local string for: " + key, e); 160 } 161 162 if (value != null) { 163 return value; 164 } else { 165 return defaultValue; 166 } 167 } 168 169 180 public String getStringWithDefault(String key, String defaultFormat, 181 Object arguments[]) { 182 183 MessageFormat f = 184 new MessageFormat ( getStringWithDefault(key, defaultFormat) ); 185 186 for (int i=0; i<arguments.length; i++) { 187 188 if ( arguments[i] == null ) { 189 190 arguments[i] = "null"; 191 192 } else if ( !(arguments[i] instanceof String ) && 193 !(arguments[i] instanceof Number ) && 194 !(arguments[i] instanceof java.util.Date )) { 195 196 arguments[i] = arguments[i].toString(); 197 } 198 } 199 200 String fmtStr = null; 201 try { 202 fmtStr = f.format(arguments); 203 } catch (Exception e) { 204 _logger.log(Level.WARNING, "iplanet_util.error_while_formating", e); 205 206 fmtStr = defaultFormat; 208 } 209 return fmtStr; 210 } 211 212 221 public String getString(String key, Object arg1) { 222 223 return getStringWithDefault(key, NO_DEFAULT, new Object [] {arg1}); 224 } 225 226 236 public String getString(String key, Object arg1, Object arg2) { 237 238 return getStringWithDefault(key, NO_DEFAULT, new Object [] {arg1, arg2}); 239 } 240 241 252 public String getString(String key, Object arg1, Object arg2, 253 Object arg3) { 254 255 return getStringWithDefault(key, NO_DEFAULT, 256 new Object [] {arg1, arg2, arg3}); 257 } 258 259 271 public String getString(String key, Object arg1, Object arg2, 272 Object arg3, Object arg4) { 273 274 return getStringWithDefault(key, NO_DEFAULT, 275 new Object [] {arg1, arg2, arg3, arg4}); 276 } 277 278 287 public String getString(String key, Object [] args) { 288 289 return getStringWithDefault(key, NO_DEFAULT, args); 290 } 291 292 293 298 public static void main(String [] args) { 299 300 long b = System.currentTimeMillis(); 301 try { 302 StringManagerBase sm = 303 StringManagerBase.getStringManager("com.sun.enterprise.util.i18n.LocalStrings"); 304 305 String ls = sm.getString( 306 "test", 307 new Integer (7), new java.util.Date (System.currentTimeMillis()), 308 "a disturbance in the Force"); 309 310 System.out.println(ls); 311 312 System.out.println( sm.getString("bad") ); 313 314 } catch (Exception e) { 315 System.out.println("---- Error ---- "); 316 e.printStackTrace(); 317 } finally { 318 long a = System.currentTimeMillis(); 319 System.out.println("Time: " + (a-b) ); 320 } 321 } 322 } 323 | Popular Tags |