1 4 package org.oddjob.values.types; 5 6 7 import java.io.BufferedReader ; 8 import java.io.ByteArrayInputStream ; 9 import java.io.ByteArrayOutputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.InputStreamReader ; 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.apache.log4j.Logger; 22 import org.oddjob.jobs.ExecJob; 23 import org.oddjob.util.OddjobConfigException; 24 25 68 public class EnvironmentType implements Map { 69 private static final Logger logger = Logger.getLogger(EnvironmentType.class); 70 71 private static Map procEnvironment; 72 73 private Map map; 74 75 81 private transient String command; 82 83 88 private transient String name; 89 90 96 public void setCommand(String command) { 97 this.command = command; 98 } 99 100 public void setName(String name) { 101 this.name = name; 102 } 103 104 public Object valueFor(Class required) { 105 load(); 106 if (name != null) { 107 return map.get(name); 108 } 109 return map; 110 } 111 112 118 private synchronized void load() { 119 if (map != null) { 120 return; 121 } 122 if (procEnvironment != null) { 123 map = procEnvironment; 124 return; 125 } 126 if (command == null) { 127 throw new OddjobConfigException("A command property must be set to get the environment."); 128 } 129 ByteArrayOutputStream out = new ByteArrayOutputStream (); 130 ExecJob exe = new ExecJob(); 131 exe.setCommand(command); 132 exe.setStdout(out); 133 logger.debug("Getting environment..."); 134 exe.run(); 135 try { 136 procEnvironment = process( 137 new ByteArrayInputStream (out.toString().getBytes())); 138 } catch (Exception exc) { 139 logger.warn("Failed parsing envionment [\n" + 140 out.toString() + "]", exc); 141 } 143 map = procEnvironment; 144 logger.debug("Loaded environment:\n" + MapType.propertiesFrom(map)); 145 } 146 147 155 Map process(InputStream is ) throws IOException { 156 Map env = new HashMap (); 157 BufferedReader in = 158 new BufferedReader (new InputStreamReader (is)); 159 160 String var = null; 161 String line, lineSep = System.getProperty("line.separator"); 162 163 List procEnvironment = new ArrayList (); 164 while ((line = in.readLine()) != null) { 165 if (line.indexOf('=') == -1) { 166 if (var == null) { 169 var = lineSep + line; 170 } else { 171 var += lineSep + line; 172 } 173 } else { 174 if (var != null) { 176 procEnvironment.add(var); 177 } 178 var = line; 179 } 180 } 181 if (var != null) { 183 procEnvironment.add(var); 184 } 185 186 for (Iterator it = procEnvironment.iterator(); it.hasNext(); ) { 187 String entry = (String ) it.next(); 188 int pos = entry.indexOf('='); 189 if (pos == -1) { 190 logger.warn("Ignoring: " + entry); 191 } else { 192 env.put(entry.substring(0, pos), 193 entry.substring(pos + 1)); 194 } 195 } 196 197 return env; 198 } 199 200 203 public void clear() { 204 throw new UnsupportedOperationException ("This map is read only!"); 205 } 206 207 210 public boolean containsKey(Object key) { 211 load(); 212 return map.containsKey(key); 213 } 214 215 218 public boolean containsValue(Object value) { 219 load(); 220 return map.containsValue(value); 221 } 222 223 226 public Set entrySet() { 227 load(); 228 return map.entrySet(); 229 } 230 231 234 public Object get(Object key) { 235 load(); 236 return map.get(key); 237 } 238 239 242 public boolean isEmpty() { 243 load(); 244 return map.isEmpty(); 245 } 246 247 250 public Set keySet() { 251 load(); 252 return map.keySet(); 253 } 254 255 258 public Object put(Object key, Object value) { 259 throw new UnsupportedOperationException ("This map is read only!"); 260 } 261 262 265 public void putAll(Map t) { 266 throw new UnsupportedOperationException ("This map is read only!"); 267 } 268 269 272 public Object remove(Object key) { 273 load(); 274 return map.remove(key); 275 } 276 277 280 public int size() { 281 load(); 282 return map.size(); 283 } 284 285 288 public Collection values() { 289 load(); 290 return map.values(); 291 } 292 293 } 294 | Popular Tags |