1 11 12 package org.eclipse.ant.internal.ui.debug.model; 13 14 import java.io.BufferedReader ; 15 import java.io.IOException ; 16 import java.io.InputStreamReader ; 17 import java.io.PrintWriter ; 18 import java.net.Socket ; 19 import java.net.UnknownHostException ; 20 21 import org.eclipse.ant.internal.ui.AntUIPlugin; 22 import org.eclipse.ant.internal.ui.debug.IAntDebugController; 23 import org.eclipse.ant.internal.ui.launchConfigurations.RemoteAntBuildListener; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.debug.core.DebugEvent; 26 import org.eclipse.debug.core.DebugPlugin; 27 import org.eclipse.debug.core.ILaunch; 28 import org.eclipse.debug.core.model.IBreakpoint; 29 import org.eclipse.debug.core.model.ILineBreakpoint; 30 import org.eclipse.debug.core.model.IProcess; 31 32 public class RemoteAntDebugBuildListener extends RemoteAntBuildListener implements IAntDebugController { 33 34 private Socket fRequestSocket; 36 private PrintWriter fRequestWriter; 37 private BufferedReader fResponseReader; 38 39 private int fRequestPort= -1; 40 private Thread fReaderThread; 41 42 private AntDebugTarget fTarget; 43 44 47 private class ReaderThread extends Thread { 48 public ReaderThread() { 49 super("Ant Request Response Reader Thread"); setDaemon(true); 51 } 52 53 public void run(){ 54 try { 55 String message= null; 56 while (fResponseReader != null) { 57 synchronized (RemoteAntDebugBuildListener.this) { 58 if (fResponseReader != null && (message= fResponseReader.readLine()) != null) { 59 receiveMessage(message); 60 } 61 } 62 } 63 } catch (IOException ie) { RemoteAntDebugBuildListener.this.shutDown(); 65 } catch (Exception e) { 66 AntUIPlugin.log("Internal error processing remote response", e); RemoteAntDebugBuildListener.this.shutDown(); 68 } 69 } 70 } 71 72 public RemoteAntDebugBuildListener(ILaunch launch) { 73 super(launch); 74 } 76 77 protected void receiveMessage(String message) { 78 if (message.startsWith(DebugMessageIds.BUILD_STARTED)) { 79 buildStarted(); 80 } else if (message.startsWith(DebugMessageIds.SUSPENDED)){ 81 handleSuspendMessage(message); 82 } else if (message.startsWith(DebugMessageIds.TERMINATED)){ 83 fTarget.terminated(); 84 } else if (message.startsWith(DebugMessageIds.STACK)){ 85 AntThread thread= (AntThread) fTarget.getThreads()[0]; 86 thread.buildStack(message); 87 } else if (message.startsWith(DebugMessageIds.PROPERTIES)){ 88 AntThread thread= (AntThread) fTarget.getThreads()[0]; 89 thread.newProperties(message); 90 } else { 91 super.receiveMessage(message); 92 } 93 } 94 95 private void handleSuspendMessage(String message) { 96 if (message.endsWith(DebugMessageIds.CLIENT_REQUEST)) { 97 fTarget.suspended(DebugEvent.CLIENT_REQUEST); 98 } else if (message.endsWith(DebugMessageIds.STEP)) { 99 fTarget.suspended(DebugEvent.STEP_END); 100 } else if (message.indexOf(DebugMessageIds.BREAKPOINT) >= 0) { 101 fTarget.breakpointHit(message); 102 } 103 } 104 105 private void buildStarted() { 106 IProcess process= getProcess(); 107 while(process == null) { 108 try { 109 synchronized (this) { 110 wait(400); 111 } 112 process= getProcess(); 113 } catch (InterruptedException ie) { 114 } 115 } 116 fTarget= new AntDebugTarget(fLaunch, process, this); 117 fLaunch.addDebugTarget(fTarget); 118 119 if (!connectRequest()) { 120 RemoteAntDebugBuildListener.this.shutDown(); 121 return; 122 } 123 124 fTarget.buildStarted(); 125 } 126 127 private boolean connectRequest() { 128 Exception exception= null; 129 for (int i= 1; i < 20; i++) { 130 try { 131 fRequestSocket = new Socket ("localhost", fRequestPort); fRequestWriter = new PrintWriter (fRequestSocket.getOutputStream(), true); 133 fResponseReader = new BufferedReader (new InputStreamReader (fRequestSocket.getInputStream())); 134 135 fReaderThread= new ReaderThread(); 136 fReaderThread.start(); 137 return true; 138 } catch (UnknownHostException e) { 139 exception= e; 140 break; 141 } catch (IOException e) { 142 exception= e; 143 } 144 try { 145 Thread.sleep(500); 146 } catch(InterruptedException e) { 147 } 148 } 149 AntUIPlugin.log("Internal error attempting to connect to debug target", exception); return false; 151 } 152 153 160 public synchronized void startListening(int eventPort, int requestPort) { 161 super.startListening(eventPort); 162 fRequestPort= requestPort; 163 } 164 165 170 protected void sendRequest(String request) { 171 if (fRequestSocket == null) { 172 return; 173 } 174 synchronized (fRequestSocket) { 175 fRequestWriter.println(request); 176 } 177 } 178 179 protected synchronized void shutDown() { 180 if (fTarget != null) { 181 fTarget.terminated(); 182 fTarget= null; 183 } 184 fLaunch= null; 185 if (DebugPlugin.getDefault() != null) { 186 DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(this); 187 } 188 try { 189 if (fReaderThread != null) { 190 fReaderThread.interrupt(); 194 } 195 if (fResponseReader != null) { 196 fResponseReader.close(); 197 fResponseReader= null; 198 } 199 } catch(IOException e) { 200 } 201 if (fRequestWriter != null) { 202 fRequestWriter.close(); 203 fRequestWriter= null; 204 } 205 try{ 206 if(fRequestSocket != null) { 207 fRequestSocket.close(); 208 fRequestSocket= null; 209 } 210 } catch(IOException e) { 211 } 212 super.shutDown(); 213 } 214 215 218 public void resume() { 219 sendRequest(DebugMessageIds.RESUME); 220 } 221 222 225 public void suspend() { 226 sendRequest(DebugMessageIds.SUSPEND); 227 } 228 229 232 public void stepInto() { 233 sendRequest(DebugMessageIds.STEP_INTO); 234 } 235 236 239 public void stepOver() { 240 sendRequest(DebugMessageIds.STEP_OVER); 241 } 242 243 246 public void handleBreakpoint(IBreakpoint breakpoint, boolean add) { 247 if (fTarget == null || !fTarget.supportsBreakpoint(breakpoint)) { 248 return; 249 } 250 StringBuffer message= new StringBuffer (); 251 if (add) { 252 try { 253 if (!breakpoint.isEnabled()) { 254 return; 255 } 256 } catch (CoreException e) { 257 AntUIPlugin.log(e); 258 return; 259 } 260 message.append(DebugMessageIds.ADD_BREAKPOINT); 261 } else { 262 message.append(DebugMessageIds.REMOVE_BREAKPOINT); 263 } 264 message.append(DebugMessageIds.MESSAGE_DELIMITER); 265 message.append(breakpoint.getMarker().getResource().getLocation().toOSString()); 266 message.append(DebugMessageIds.MESSAGE_DELIMITER); 267 try { 268 message.append(((ILineBreakpoint)breakpoint).getLineNumber()); 269 sendRequest(message.toString()); 270 } catch (CoreException ce) { 271 AntUIPlugin.log(ce); 272 } 273 } 274 275 278 public void getProperties() { 279 sendRequest(DebugMessageIds.PROPERTIES); 280 } 281 282 285 public void getStackFrames() { 286 sendRequest(DebugMessageIds.STACK); 287 } 288 289 292 public StringBuffer unescapeString(StringBuffer property) { 293 if (property.indexOf("\\r") == -1 && property.indexOf("\\n") == -1) { return property; 295 } 296 for (int i= 0; i < property.length(); i++) { 297 if ('\\' == property.charAt(i)) { 298 String newString= ""; if ('r' == property.charAt(i + 1)) { 300 if (i-1 > - 1 && '\\' == property.charAt(i-1)) { 301 newString= "r"; } else { 303 newString+= '\r'; 304 } 305 } else if ('n' == property.charAt(i + 1)) { 306 if (i-1 > - 1 && '\\' == property.charAt(i-1)) { 307 newString= "n"; } else { 309 newString+= '\n'; 310 } 311 312 } 313 if (newString.length() > 0) { 314 property.replace(i, i + 2, newString); 315 } 316 } 317 } 318 319 return property; 320 } 321 } 322 | Popular Tags |