1 3 package jodd.util; 4 5 import java.io.IOException ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import com.sun.jdi.Bootstrap; 11 import com.sun.jdi.VirtualMachine; 12 import com.sun.jdi.connect.Connector; 13 import com.sun.jdi.connect.IllegalConnectorArgumentsException; 14 import com.sun.jdi.connect.LaunchingConnector; 15 import com.sun.jdi.connect.VMStartException; 16 17 18 21 public class VmUtil { 22 23 28 public static LaunchingConnector findLaunchingConnector() { 29 List connectors = Bootstrap.virtualMachineManager().allConnectors(); 30 Iterator it = connectors.iterator(); 31 while (it.hasNext()) { 32 Connector connector = (Connector) it.next(); 33 if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) { 34 return (LaunchingConnector) connector; 35 } 36 } 37 return null; 38 } 39 40 51 public static Map connectorArguments(LaunchingConnector connector, String mainArgs, String vmOptions, boolean watchFields) { 52 if (connector == null) { 53 return null; 54 } 55 Map arguments = connector.defaultArguments(); 56 57 Connector.Argument mainArg = (Connector.Argument) arguments.get("main"); 59 if ((mainArgs != null) && (mainArgs.trim().length() != 0)) { 60 if (mainArg == null) { 61 return null; 62 } 63 mainArg.setValue(mainArgs); 64 } 65 66 if (watchFields == true) { 68 if (vmOptions == null) { 69 vmOptions = ""; 70 } 71 vmOptions += " -classic"; } 73 74 Connector.Argument optionArg = (Connector.Argument) arguments.get("options"); 76 if ((vmOptions != null) && (vmOptions.trim().length() != 0)) { 77 if (optionArg == null) { 78 return null; 79 } 80 optionArg.setValue(vmOptions); 81 } 82 83 return arguments; 84 } 85 86 87 88 96 public static VirtualMachine launchTarget(String mainArgs, String vmOptions, boolean wf) { 97 LaunchingConnector connector = findLaunchingConnector(); 98 if (connector == null) { 99 throw new Error ("No connector found."); 100 } 101 Map arguments = connectorArguments(connector, mainArgs, vmOptions, wf); 102 if (arguments == null) { 103 throw new Error ("Bad connector."); 104 } 105 try { 106 return connector.launch(arguments); 107 } catch (IOException ioex) { 108 throw new Error ("Unable to launch target VM: " + ioex); 109 } catch (IllegalConnectorArgumentsException icaex) { 110 throw new Error ("Internal error: " + icaex); 111 } catch (VMStartException vmsex) { 112 throw new Error ("Target VM failed to initialize: " + vmsex.getMessage()); 113 } 114 } 115 116 } 117 | Popular Tags |