1 19 20 package org.netbeans.api.debugger.jpda; 21 22 import com.sun.jdi.Bootstrap; 23 import com.sun.jdi.VirtualMachine; 24 import com.sun.jdi.connect.AttachingConnector; 25 import com.sun.jdi.connect.Connector.Argument; 26 import com.sun.jdi.connect.IllegalConnectorArgumentsException; 27 import java.io.IOException ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 32 51 public final class AttachingDICookie extends AbstractDICookie { 52 53 56 public static final String ID = "netbeans-jpda-AttachingDICookie"; 57 58 private AttachingConnector attachingConnector; 59 private Map <String ,? extends Argument> args; 60 61 62 private AttachingDICookie ( 63 AttachingConnector attachingConnector, 64 Map <String ,? extends Argument> args 65 ) { 66 this.attachingConnector = attachingConnector; 67 this.args = args; 68 } 69 70 77 public static AttachingDICookie create ( 78 AttachingConnector attachingConnector, 79 Map <String ,? extends Argument> args 80 ) { 81 return new AttachingDICookie ( 82 attachingConnector, 83 args 84 ); 85 } 86 87 94 public static AttachingDICookie create ( 95 String hostName, 96 int portNumber 97 ) { 98 return new AttachingDICookie ( 99 findAttachingConnector ("socket"), 100 getArgs ( 101 findAttachingConnector ("socket"), 102 hostName, 103 portNumber 104 ) 105 ); 106 } 107 108 114 public static AttachingDICookie create ( 115 String name 116 ) { 117 return new AttachingDICookie ( 118 findAttachingConnector ("shmem"), 119 getArgs ( 120 findAttachingConnector ("shmem"), 121 name 122 ) 123 ); 124 } 125 126 131 public AttachingConnector getAttachingConnector () { 132 return attachingConnector; 133 } 134 135 140 public Map <String ,? extends Argument> getArgs () { 141 return args; 142 } 143 144 149 public int getPortNumber () { 150 Argument a = args.get ("port"); 151 if (a == null) return -1; 152 String pn = a.value (); 153 if (pn == null) return -1; 154 return Integer.parseInt (pn); 155 } 156 157 162 public String getHostName () { 163 Argument a = args.get ("hostname"); 164 if (a == null) return null; 165 return a.value (); 166 } 167 168 173 public String getSharedMemoryName () { 174 Argument a = args.get ("name"); 175 if (a == null) return null; 176 return a.value (); 177 } 178 179 184 public VirtualMachine getVirtualMachine () throws IOException , 185 IllegalConnectorArgumentsException { 186 return attachingConnector.attach (args); 187 } 188 189 190 192 private static Map <String ,? extends Argument> getArgs ( 193 AttachingConnector attachingConnector, 194 String hostName, 195 int portNumber 196 ) { 197 Map <String ,? extends Argument> args = attachingConnector.defaultArguments (); 198 args.get ("hostname").setValue (hostName); 199 args.get ("port").setValue ("" + portNumber); 200 return args; 201 } 202 203 private static Map <String ,? extends Argument> getArgs ( 204 AttachingConnector attachingConnector, 205 String name 206 ) { 207 Map <String ,? extends Argument> args = attachingConnector.defaultArguments (); 208 args.get ("name").setValue (name); 209 return args; 210 } 211 212 private static AttachingConnector findAttachingConnector (String s) { 213 Iterator <AttachingConnector> iter = Bootstrap.virtualMachineManager (). 214 attachingConnectors ().iterator (); 215 while (iter.hasNext ()) { 216 AttachingConnector ac = iter.next (); 217 if (ac.transport() != null && ac.transport ().name ().toLowerCase ().indexOf (s) > -1) 218 return ac; 219 } 220 return null; 221 } 222 } 223 | Popular Tags |