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.IllegalConnectorArgumentsException; 25 import com.sun.jdi.connect.ListeningConnector; 26 import com.sun.jdi.connect.Connector.Argument; 27 28 import java.io.IOException ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 32 51 public final class ListeningDICookie extends AbstractDICookie { 52 53 56 public static final String ID = "netbeans-jpda-ListeningDICookie"; 57 58 private ListeningConnector listeningConnector; 59 private Map <String , ? extends Argument> args; 60 61 private ListeningDICookie ( 62 ListeningConnector listeningConnector, 63 Map <String , ? extends Argument> args 64 ) { 65 this.listeningConnector = listeningConnector; 66 this.args = args; 67 } 68 69 76 public static ListeningDICookie create ( 77 ListeningConnector listeningConnector, 78 Map <String , ? extends Argument> args 79 ) { 80 return new ListeningDICookie ( 81 listeningConnector, 82 args 83 ); 84 } 85 86 92 public static ListeningDICookie create ( 93 int portNumber 94 ) { 95 return new ListeningDICookie ( 96 findListeningConnector ("socket"), 97 getArgs ( 98 findListeningConnector ("socket"), 99 portNumber 100 ) 101 ); 102 } 103 104 110 public static ListeningDICookie create ( 111 String name 112 ) { 113 return new ListeningDICookie ( 114 findListeningConnector ("socket"), 115 getArgs ( 116 findListeningConnector ("socket"), 117 name 118 ) 119 ); 120 } 121 122 private static ListeningConnector findListeningConnector (String s) { 123 Iterator iter = Bootstrap.virtualMachineManager (). 124 listeningConnectors ().iterator (); 125 while (iter.hasNext ()) { 126 ListeningConnector ac = (ListeningConnector) iter.next (); 127 if (ac.transport() != null && ac.transport ().name ().toLowerCase ().indexOf (s) > -1) 128 return ac; 129 } 130 return null; 131 } 132 133 private static Map <String , ? extends Argument> getArgs ( 134 ListeningConnector listeningConnector, 135 int portNumber 136 ) { 137 Map <String , ? extends Argument> args = listeningConnector.defaultArguments (); 138 args.get ("port").setValue ("" + portNumber); 139 return args; 140 } 141 142 private static Map <String , ? extends Argument> getArgs ( 143 ListeningConnector listeningConnector, 144 String name 145 ) { 146 Map <String , ? extends Argument> args = listeningConnector.defaultArguments (); 147 args.get ("name").setValue (name); 148 return args; 149 } 150 151 156 public ListeningConnector getListeningConnector () { 157 return listeningConnector; 158 } 159 160 165 public Map <String , ? extends Argument> getArgs () { 166 return args; 167 } 168 169 174 public int getPortNumber () { 175 Argument a = (Argument) args.get ("port"); 176 if (a == null) return -1; 177 String pn = a.value (); 178 if (pn == null) return -1; 179 try { 180 return Integer.parseInt (pn); 181 } catch (NumberFormatException e) { 182 return -1; 183 } 184 } 185 186 191 public String getSharedMemoryName () { 192 Argument a = (Argument) args.get ("name"); 193 if (a == null) return null; 194 return a.value (); 195 } 196 197 202 public VirtualMachine getVirtualMachine () throws IOException , 203 IllegalConnectorArgumentsException { 204 try { 205 try { 206 listeningConnector.startListening(args); 207 } catch (Exception e) { 208 } 210 return listeningConnector.accept (args); 211 } finally { 212 try { 213 listeningConnector.stopListening(args); 214 } catch (Exception e) { 215 } 217 } 218 } 219 } 220 | Popular Tags |