1 23 24 30 31 package com.sun.enterprise.util; 32 import java.util.ResourceBundle ; 33 import java.text.MessageFormat ; 34 35 61 public class LocalStringsImpl 62 { 63 67 public LocalStringsImpl() 68 { 69 setBundle(); 70 } 71 72 78 public LocalStringsImpl(Class clazz) 79 { 80 setBundle(clazz); 81 } 82 83 88 89 public String get(String indexString) 90 { 91 try 92 { 93 return getBundle().getString(indexString); 94 } 95 catch (Exception e) 96 { 97 return indexString; 99 } 100 } 101 102 109 public String get(String indexString, Object ... objects) 110 { 111 indexString = get(indexString); 112 113 try 114 { 115 MessageFormat mf = new MessageFormat (indexString); 116 return mf.format(objects); 117 } 118 catch(Exception e) 119 { 120 return indexString; 121 } 122 } 123 124 129 130 public String getString(String indexString, String defaultValue) 131 { 132 try 133 { 134 return getBundle().getString(indexString); 135 } 136 catch (Exception e) 137 { 138 return defaultValue; 140 } 141 } 142 148 149 public int getInt(String indexString, int defaultValue) 150 { 151 try 152 { 153 String s = getBundle().getString(indexString); 154 return Integer.parseInt(s); 155 } 156 catch (Exception e) 157 { 158 return defaultValue; 160 } 161 } 162 168 169 public boolean getBoolean(String indexString, boolean defaultValue) 170 { 171 try 172 { 173 return new Boolean (getBundle().getString(indexString)); 174 } 175 catch (Exception e) 176 { 177 return defaultValue; 179 } 180 } 181 182 184 private ResourceBundle getBundle() 185 { 186 return bundle; 187 } 188 189 191 private void setBundle() 192 { 193 199 try 200 { 201 StackTraceElement [] items = Thread.currentThread().getStackTrace(); 202 int lastMeOnStack = -1; 203 204 for(int i = 0; i < items.length; i++) 205 { 206 StackTraceElement item = items[i]; 207 if(item.getClassName().startsWith(thisPackage)) 208 lastMeOnStack = i; 209 } 210 211 String className = items[lastMeOnStack + 1].getClassName(); 212 setBundle(className); 213 } 214 catch(Exception e) 215 { 216 bundle = null; 217 } 218 } 219 220 222 private void setBundle(Class clazz) 223 { 224 setBundle(clazz.getName()); 225 } 226 227 229 private void setBundle(String className) 230 { 231 try 232 { 233 String props; 234 if(className.indexOf('.') < 0) 236 props = propsFilename; 237 else 238 props = className.substring(0, className.lastIndexOf('.')) + '.' + propsFilename; 239 240 bundle = ResourceBundle.getBundle(props); 241 } 242 catch(Exception e) 243 { 244 bundle = null; 245 } 246 } 247 248 250 private ResourceBundle bundle; 251 private static final String thisPackage = "com.sun.enterprise.util"; 252 private String propsFilename = "LocalStrings"; 253 } 254 | Popular Tags |