1 23 24 28 29 33 34 37 38 package com.sun.enterprise.cli.framework; 39 40 import java.util.ResourceBundle ; 41 import java.net.URL ; 42 import java.text.MessageFormat ; 43 import java.util.HashMap ; 44 45 49 public class GlobalsManager 50 { 51 private static GlobalsManager sGlobalsMgr = null; 52 private ICommandEnvironment mGlobalCommandEnvironment = null; 53 54 private static HashMap resourceBundles = new HashMap (); 55 56 private static String COMMANDS_BASE_PACKAGE = null; 57 private final static String FRAMEWORK_BASE_PACKAGE = GlobalsManager.class.getPackage().getName(); 58 private static String FRAMEWORK_PROPERTY_FILE_NAME = "LocalStrings"; 59 private static String COMMANDS_PROPERTY_FILE_NAME = "LocalStrings"; 60 61 64 65 protected GlobalsManager() { 66 this(new CommandEnvironment()); 67 } 68 69 protected GlobalsManager(ICommandEnvironment env){ 70 mGlobalCommandEnvironment = env; 71 resourceBundles = new HashMap (); 72 } 73 74 78 public static void setInstance( GlobalsManager globalsMgr ) 79 { 80 if ( sGlobalsMgr != globalsMgr ) 81 { 82 sGlobalsMgr = globalsMgr; 83 } 84 } 85 86 90 public static GlobalsManager getInstance() 91 { 92 if( sGlobalsMgr == null ) 93 { 94 sGlobalsMgr = new GlobalsManager(); 95 } 96 return sGlobalsMgr; 97 } 98 99 102 public ICommandEnvironment getGlobalsEnv() 103 { 104 return mGlobalCommandEnvironment; 105 } 106 107 112 public void setGlobalsEnv( ICommandEnvironment env ) 113 { 114 mGlobalCommandEnvironment = env; 115 } 116 117 121 public String getOption( String optionName ) 122 { 123 String optionValue = (String )mGlobalCommandEnvironment.getEnvironmentValue(optionName ); 124 return optionValue; 125 } 126 127 131 public void setOption( String name, String value ) 132 { 133 mGlobalCommandEnvironment.setEnvironment(name, value); 134 } 135 136 139 public void removeOption( String optionName) 140 { 141 mGlobalCommandEnvironment.removeEnvironment(optionName); 142 } 143 144 147 public static String getString(String key) throws CommandException 148 { 149 return getString(key, COMMANDS_BASE_PACKAGE, 150 COMMANDS_PROPERTY_FILE_NAME); 151 } 152 153 157 public static String getString(String key, Object [] toInsert) throws CommandException 158 { 159 return getString(key, toInsert, COMMANDS_BASE_PACKAGE, 160 COMMANDS_PROPERTY_FILE_NAME); 161 } 162 163 166 public static String getFrameworkString(String key) throws CommandException 167 { 168 return getString(key, FRAMEWORK_BASE_PACKAGE, 169 FRAMEWORK_PROPERTY_FILE_NAME); 170 } 171 172 176 public static String getFrameworkString(String key, Object [] toInsert) 177 throws CommandException 178 { 179 return getString(key, toInsert, FRAMEWORK_BASE_PACKAGE, 180 FRAMEWORK_PROPERTY_FILE_NAME); 181 } 182 183 186 private static String getString(String key, String basePkg, String propertyFile) 187 throws CommandException 188 { 189 return getResourceBundle(basePkg, propertyFile).getString(key); 190 } 191 192 195 private static String getString(String key, Object [] toInsert, 196 String basePkg, String propertyFile) 197 throws CommandException 198 { 199 String fmtStr = null; 200 try 201 { 202 ResourceBundle resBundle = getResourceBundle(basePkg, propertyFile); 203 MessageFormat msgFormat = 204 new MessageFormat (resBundle.getString(key)); 205 fmtStr = msgFormat.format(toInsert); 206 } 207 catch(Exception e) 208 { 209 throw new CommandException(e.getLocalizedMessage()); 210 } 211 return fmtStr; 212 } 213 214 private static ResourceBundle getResourceBundle(String packageName, 215 String propertyFile) 216 throws CommandException 217 { 218 ResourceBundle resBundle = (ResourceBundle ) resourceBundles.get(packageName); 219 if (resBundle == null) 220 { 221 try 222 { 223 resBundle = ResourceBundle.getBundle( 224 packageName + "." + propertyFile); 225 resourceBundles.put(packageName, resBundle); 226 } 227 catch(java.util.MissingResourceException mre) 228 { 229 throw new CommandException(mre.getLocalizedMessage()); 230 } 231 } 232 return resBundle; 233 } 234 235 238 public static void setBasePackage(String basePkg) 239 { 240 COMMANDS_BASE_PACKAGE = basePkg; 241 } 242 243 246 public static void setPropertyFile(String fileName) 247 { 248 COMMANDS_PROPERTY_FILE_NAME = fileName; 249 } 250 251 public static void main(String [] args) 252 { 253 try 254 { 255 GlobalsManager globalsMgr = GlobalsManager.getInstance(); 256 System.out.println(globalsMgr.getFrameworkString("junk", new Object []{"junk", "prashanth"})); 257 globalsMgr.setBasePackage("com.sun.enterprise.cli.commands"); 258 System.out.println(globalsMgr.getString("junk", new Object []{"junk", "prashanth"})); 259 } 260 catch(CommandException ce) 261 { 262 ce.printStackTrace(); 263 } 264 } 265 266 } 267 268 | Popular Tags |