1 23 24 27 28 29 package com.sun.enterprise.cli.framework; 30 31 import java.util.Iterator ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 38 39 public class CommandEnvironment implements ICommandEnvironment 40 { 41 private static CommandEnvironment commandEnvironment; 43 44 private static final String ENVIRONMENT_PREFIX = "AS_ADMIN_"; 47 48 49 private HashMap environments; 50 51 54 protected CommandEnvironment() 55 { 56 environments = (HashMap )getSystemEnvironment(); 57 } 58 59 60 64 private Map getSystemEnvironmentNative() 65 { 66 HashMap hashMap = new HashMap (); 67 try 68 { 69 CLIDescriptorsReader cdr = CLIDescriptorsReader.getInstance(); 70 String envPrefix = cdr.getEnvironmentPrefix(); 71 envPrefix = envPrefix==null?ENVIRONMENT_PREFIX:envPrefix; 73 74 final String [] strArray = new CliUtil().getEnv(envPrefix); 76 77 for (int ii=0; ii<strArray.length; ii++) 78 { 79 final int index = strArray[ii].indexOf("="); 80 final String sOptionName = strArray[ii].substring(envPrefix.length(), 83 index).toLowerCase(); 84 hashMap.put( sOptionName.replace('_', '-'), 85 strArray[ii].substring(index+1)); 86 } 87 } 88 catch (java.lang.UnsatisfiedLinkError e) 89 { 90 CLILogger.getInstance().printDebugMessage(e.getLocalizedMessage()); 91 CLILogger.getInstance().printWarning(getLocalizedString("UnableToReadEnv")); 93 } 94 return hashMap; 95 } 96 97 98 102 private Map getSystemEnvironment() 103 { 104 final CLIDescriptorsReader cdr = CLIDescriptorsReader.getInstance(); 106 String sPrefix = cdr.getEnvironmentPrefix(); 107 sPrefix = sPrefix==null?ENVIRONMENT_PREFIX:sPrefix; 109 110 final Map <String , String > mEnv = System.getenv(); 111 HashMap <String , String > hmEnv = new HashMap <String , String >(); 112 final Iterator iterEnv = mEnv.keySet().iterator(); 113 114 while( iterEnv.hasNext() ) 115 { 116 final String sEnvKey = ( String )iterEnv.next(); 117 if (sEnvKey.startsWith(sPrefix)) { 118 final String sOptionName = sEnvKey.substring(sPrefix.length()).toLowerCase(); 120 hmEnv.put(sOptionName.replace('_','-'), (String )mEnv.get(sEnvKey)); 123 } 124 } 125 return hmEnv; 126 } 127 128 129 130 133 public static CommandEnvironment getInstance() 134 { 135 if (commandEnvironment == null) 136 { 137 commandEnvironment = new CommandEnvironment(); 138 } 139 return commandEnvironment; 140 } 141 142 143 148 public void setEnvironment(String name, String value) 149 { 150 if ( environments.containsKey(name) ) 151 { 152 environments.remove( name ); 153 } 154 environments.put(name, value ); 155 } 156 157 158 163 public Object removeEnvironment( String name ) 164 { 165 return environments.remove(name); 166 } 167 168 171 public Object getEnvironmentValue(String key) 172 { 173 return environments.get(key); 174 } 175 176 179 public HashMap getEnvironments() 180 { 181 return environments; 182 } 183 184 185 190 public String toString() 191 { 192 String description = ""; 193 final Iterator environIter = environments.keySet().iterator(); 194 while( environIter.hasNext() ) 195 { 196 String environKey = ( String )environIter.next(); 197 description += " " + environKey + " = " + 198 (String )environments.get(environKey); 199 } 200 return description; 201 } 202 203 204 207 public int getNumEnvironments() 208 { 209 return environments.size(); 210 } 211 212 217 private String getLocalizedString(String key) 218 { 219 try 220 { 221 final LocalStringsManager lsm = 222 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 223 return lsm.getString(key); 224 } 225 catch (CommandValidationException cve) 226 { 227 return LocalStringsManager.DEFAULT_STRING_VALUE; 228 } 229 } 230 231 } 232 233 | Popular Tags |