1 package org.sapia.magnet.domain.system; 2 3 import java.io.File ; 6 import java.util.ArrayList ; 7 import java.util.Collection ; 8 import java.util.List ; 9 import java.util.Iterator ; 10 import java.util.TreeMap ; 11 12 import org.apache.log4j.Logger; 15 16 import org.sapia.console.CmdLine; 19 import org.sapia.magnet.Log; 20 import org.sapia.magnet.domain.DefaultLaunchHandler; 21 import org.sapia.magnet.domain.Magnet; 22 import org.sapia.magnet.domain.Param; 23 import org.sapia.magnet.domain.Profile; 24 import org.sapia.magnet.render.MagnetContext; 25 import org.sapia.magnet.render.RenderingException; 26 27 28 39 public class SystemLauncher extends DefaultLaunchHandler { 40 41 45 46 private static final Logger _theLogger = Logger.getLogger(SystemLauncher.class); 47 48 52 53 private String _theCommand; 54 55 56 private String _theWorkingDirectory; 57 58 62 65 public SystemLauncher() { 66 _theWorkingDirectory = System.getProperty("user.dir"); 67 } 68 69 73 78 public String getCommand() { 79 return _theCommand; 80 } 81 82 87 public String getWorkingDirectory() { 88 return _theWorkingDirectory; 89 } 90 91 95 100 public void setCommand(String aCommand) { 101 _theCommand = aCommand; 102 } 103 104 109 public void setWorkingDirectory(String aWorkingDirectory) { 110 _theWorkingDirectory = aWorkingDirectory; 111 } 112 113 117 125 protected Environment findEnvironment(String anId, Collection someMagnets) { 126 if (anId == null) { 128 throw new IllegalArgumentException ("The environment identifer passed in is null"); 129 } else if (someMagnets == null) { 130 throw new IllegalArgumentException ("The collection of magnet passed in is null"); 131 } 132 133 Environment aResult = null; 134 for (Iterator it = someMagnets.iterator(); aResult == null && it.hasNext(); ) { 135 Magnet aParentMagnet = (Magnet) it.next(); 136 aResult = findEnvironment(anId, aParentMagnet); 137 } 138 139 return aResult; 140 } 141 142 150 protected Environment findEnvironment(String anId, Magnet aMagnet) { 151 if (anId == null) { 153 throw new IllegalArgumentException ("The environment identifer passed in is null"); 154 } else if (aMagnet == null) { 155 throw new IllegalArgumentException ("The magnet passed in is null"); 156 } 157 158 boolean isFound = false; 160 Environment aResult = null; 161 162 for (Iterator it = aMagnet.getObjectsFor("Environment").iterator(); !isFound && it.hasNext(); ) { 163 Environment anEnvironment = (Environment) it.next(); 164 if (anId.equals(anEnvironment.getId())) { 165 isFound = true; 166 aResult = anEnvironment; 167 } 168 } 169 170 if (aResult == null && aMagnet.getParents().size() != 0) { 171 return findEnvironment(anId, aMagnet.getParents()); 172 } else { 173 return aResult; 174 } 175 } 176 177 184 protected List extractVariables(Environment anEnvironment) { 185 if (anEnvironment == null) { 187 throw new IllegalArgumentException ("The environment passed in is null"); 188 } 189 190 ArrayList someVariables = new ArrayList (); 191 ArrayList someIdentifiers = new ArrayList (); 192 extractVariablesIter(anEnvironment, someVariables, someIdentifiers); 193 194 return someVariables; 195 } 196 197 200 private void extractVariablesIter(Environment anEnvironment, List someVariables, List someIdentifiers) { 201 if (someIdentifiers.contains(anEnvironment.getId())) { 203 throw new IllegalStateException ("Circular referenced environment objects detected"); 204 } else { 205 someIdentifiers.add(anEnvironment.getId()); 206 } 207 208 if (anEnvironment.getParent() != null) { 209 Environment aParent = findEnvironment(anEnvironment.getParent(), getMagnet()); 210 if (aParent == null) { 211 String aMessage = "Unable to find the parent environment: " + anEnvironment.getParent(); 212 _theLogger.error(aMessage); 213 throw new RuntimeException (aMessage); 214 } else { 215 extractVariablesIter(aParent, someVariables, someIdentifiers); 216 } 217 } 218 219 for (Iterator it = anEnvironment.getVariables().iterator(); it.hasNext(); ) { 220 Variable aVariable = (Variable) it.next(); 221 someVariables.add(aVariable); 222 } 223 } 224 225 230 protected String [] parseCommandString() { 231 CmdLine aCommandLine = CmdLine.parse(_theCommand); 232 return aCommandLine.toArray(); 233 } 234 235 239 245 public void render(MagnetContext aContext) throws RenderingException { 246 super.render(aContext); 248 249 Profile aProfile = findProfile(aContext.getProfile()); 251 252 if (aProfile != null) { 253 aProfile.render(aContext); 255 MagnetContext aSubContext = new MagnetContext(aContext); 256 for (Iterator it = aProfile.getParameters().getParams().iterator(); it.hasNext(); ) { 257 Param aParam = (Param) it.next(); 258 aSubContext.addParameter(aParam, false); 259 } 260 261 _theCommand = resolveValue(aSubContext, _theCommand); 263 _theWorkingDirectory = resolveValue(aSubContext, _theWorkingDirectory); 264 } else { 265 if (aContext.getProfile() == null || aContext.getProfile().length() == 0) { 267 Log.warn("Unable to find a default profile in this system launcher", this); 268 } else { 269 Log.warn("Unable to find the profile " + aContext.getProfile() + " in this system launcher", this); 270 } 271 } 272 } 273 274 279 public void execute(String aProfileName) { 280 try { 281 Profile aProfile = findProfile(aProfileName); 283 if (aProfile == null) { 284 if (aProfileName == null || aProfileName.length() == 0) { 285 Log.warn("Skipping this system launcher --> no default profile defined", this); 286 } else { 287 Log.warn("Skipping this system launcher --> no profile found for the name " + aProfileName, this); 288 } 289 return; 290 } 291 292 Log.info("Executing profile " + aProfile.getName(), this); 293 294 String [] someCommands = parseCommandString(); 296 297 TreeMap someEnvironmentVariables = new TreeMap (); 299 300 for (Iterator it = aProfile.getObjectsFor("environment").iterator(); it.hasNext(); ) { 301 Environment anEnvironment = (Environment) it.next(); 302 303 for (Iterator someVariables = extractVariables(anEnvironment).iterator(); someVariables.hasNext(); ) { 304 Variable aVariable = (Variable) someVariables.next(); 305 StringBuffer aBuffer = new StringBuffer (); 306 aBuffer.append(aVariable.getName()).append("=").append(aVariable.getValue()); 307 someEnvironmentVariables.put(aVariable.getName(), aBuffer.toString()); 308 } 309 } 310 311 File aDirectory = new File (_theWorkingDirectory); 313 if (aDirectory == null || !aDirectory.isDirectory()) { 314 String aMessage = "The working directory '" + _theWorkingDirectory + "' is invalid"; 315 Log.error(aMessage, this); 316 throw new RuntimeException (aMessage); 317 } 318 319 StringBuffer aBuffer = new StringBuffer (); 321 aBuffer.append("Set the working directory to '").append(_theWorkingDirectory).append("'"); 322 Log.info(aBuffer.toString(), this); 323 324 aBuffer.setLength(0); 325 aBuffer.append("Running the command '").append(_theCommand).append("'"); 326 Log.info(aBuffer.toString(), this); 327 328 if (someEnvironmentVariables.size() > 0) { 329 Log.info("Environment variables of the system process:", this); 330 for (Iterator it = someEnvironmentVariables.values().iterator(); it.hasNext(); ) { 331 aBuffer.setLength(0); 332 aBuffer.append("\t").append(it.next()); 333 Log.info(aBuffer.toString(), this); 334 } 335 } 336 337 ProcessTask aTask = new ProcessTask(someCommands, 339 (String []) someEnvironmentVariables.values().toArray(new String [0]), aDirectory); 340 Thread aThread = new Thread (aTask); 341 aThread.setName(getName() + "-" + aProfile.getName()); 342 aThread.start(); 343 344 } catch (RuntimeException re) { 345 String aMessage = "System error spawning this process"; 346 _theLogger.error(aMessage, re); 347 } 348 } 349 350 355 public String toString() { 356 StringBuffer aBuffer = new StringBuffer (super.toString()); 357 aBuffer.append("[command=").append(_theCommand). 358 append(" workingDirectory=").append(_theWorkingDirectory). 359 append("]"); 360 361 return aBuffer.toString(); 362 } 363 } 364 | Popular Tags |