1 11 package org.eclipse.ant.internal.ui.antsupport.logger.debug; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.io.InputStreamReader ; 16 import java.io.PrintWriter ; 17 import java.net.ServerSocket ; 18 import java.net.Socket ; 19 import java.net.SocketTimeoutException ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.apache.tools.ant.BuildEvent; 26 import org.apache.tools.ant.Location; 27 import org.apache.tools.ant.Task; 28 import org.eclipse.ant.internal.ui.antsupport.logger.RemoteAntBuildLogger; 29 import org.eclipse.ant.internal.ui.antsupport.logger.util.AntDebugState; 30 import org.eclipse.ant.internal.ui.antsupport.logger.util.DebugMessageIds; 31 import org.eclipse.ant.internal.ui.antsupport.logger.util.IDebugBuildLogger; 32 33 38 public class RemoteAntDebugBuildLogger extends RemoteAntBuildLogger implements IDebugBuildLogger { 39 40 private ServerSocket fServerSocket; 41 private static final int fgServerSocketTimeout= 5000; 42 private Socket fRequestSocket; 43 44 private PrintWriter fRequestWriter; 45 46 private BufferedReader fRequestReader; 47 48 private boolean fBuildStartedSuspend= true; 49 50 private Task fStepOverTaskInterrupted; 51 52 private List fBreakpoints= null; 53 54 58 private int fRequestPort= -1; 59 private AntDebugState fDebugState; 60 61 64 private class ReaderThread extends Thread { 65 public ReaderThread() { 66 super("ReaderThread"); setDaemon(true); 68 } 69 70 public void run(){ 71 try { 72 String message= null; 73 while (fRequestReader != null) { 74 if ((message= fRequestReader.readLine()) != null) { 75 76 if (message.startsWith(DebugMessageIds.STEP_INTO)){ 77 synchronized(RemoteAntDebugBuildLogger.this) { 78 fDebugState.setStepIntoSuspend(true); 79 fDebugState.setStepIntoTask(fDebugState.getCurrentTask()); 80 RemoteAntDebugBuildLogger.this.notifyAll(); 81 } 82 } if (message.startsWith(DebugMessageIds.STEP_OVER)){ 83 synchronized(RemoteAntDebugBuildLogger.this) { 84 fDebugState.stepOver(); 85 } 86 } else if (message.startsWith(DebugMessageIds.SUSPEND)) { 87 synchronized(RemoteAntDebugBuildLogger.this) { 88 fDebugState.setStepIntoTask(null); 89 fDebugState.setStepOverTask(null); 90 fStepOverTaskInterrupted= null; 91 fDebugState.setClientSuspend(true); 92 } 93 } else if (message.startsWith(DebugMessageIds.RESUME)) { 94 synchronized(RemoteAntDebugBuildLogger.this) { 95 fDebugState.setStepIntoTask(null); 96 fDebugState.setStepOverTask(null); 97 fStepOverTaskInterrupted= null; 98 RemoteAntDebugBuildLogger.this.notifyAll(); 99 } 100 } else if (message.startsWith(DebugMessageIds.TERMINATE)) { 101 sendRequestResponse(DebugMessageIds.TERMINATED); 102 shutDown(); 103 } else if (message.startsWith(DebugMessageIds.STACK)) { 104 marshallStack(); 105 } else if (message.startsWith(DebugMessageIds.ADD_BREAKPOINT)) { 106 addBreakpoint(message); 107 } else if (message.startsWith(DebugMessageIds.REMOVE_BREAKPOINT)) { 108 removeBreakpoint(message); 109 } else if (message.startsWith(DebugMessageIds.PROPERTIES)) { 110 marshallProperties(); 111 } 112 } 113 } 114 } catch (Exception e) { 115 RemoteAntDebugBuildLogger.this.shutDown(); 116 } 117 } 118 } 119 120 private void requestConnect() { 121 if (fDebugMode) { 122 System.out.println("RemoteAntDebugBuildLogger: trying to connect" + fHost + ":" + fRequestPort); } 124 125 try{ 126 fServerSocket.setSoTimeout(fgServerSocketTimeout); 127 fRequestSocket= fServerSocket.accept(); 128 fRequestWriter= new PrintWriter (fRequestSocket.getOutputStream(), true); 129 fRequestReader = new BufferedReader (new InputStreamReader (fRequestSocket.getInputStream())); 130 131 ReaderThread readerThread= new ReaderThread(); 132 readerThread.setDaemon(true); 133 readerThread.start(); 134 return; 135 } catch(SocketTimeoutException e){ 136 } catch(IOException e) { 137 } 138 shutDown(); 139 } 140 141 144 protected void shutDown() { 145 if (fRequestWriter != null) { 146 fRequestWriter.close(); 147 fRequestWriter= null; 148 } 149 150 if (fRequestReader != null) { 151 try { 152 fRequestReader.close(); 153 } catch (IOException e) { 154 } 155 fRequestReader= null; 156 } 157 158 if (fRequestSocket != null) { 159 try { 160 fRequestSocket.close(); 161 } catch(IOException e) { 162 } 163 } 164 fRequestSocket= null; 165 166 super.shutDown(); 167 } 168 169 172 public synchronized void buildStarted(BuildEvent event) { 173 fDebugState= new AntDebugState(this); 174 super.buildStarted(event); 175 marshalMessage(-1, DebugMessageIds.BUILD_STARTED); 176 if (fRequestPort != -1) { 177 try { 178 fServerSocket= new ServerSocket (fRequestPort); 179 } catch (IOException ioe) { 180 shutDown(); 181 } 182 requestConnect(); 183 } else { 184 shutDown(); 185 } 186 fDebugState.buildStarted(); 187 fDebugState.setShouldSuspend(true); 188 waitIfSuspended(); 189 } 190 191 194 public void taskStarted(BuildEvent event) { 195 super.taskStarted(event); 196 fDebugState.taskStarted(event); 197 } 198 199 202 public synchronized void taskFinished(BuildEvent event) { 203 super.taskFinished(event); 204 fDebugState.taskFinished(); 205 } 206 207 210 public synchronized void waitIfSuspended() { 211 String detail= null; 212 boolean shouldSuspend= true; 213 RemoteAntBreakpoint breakpoint= breakpointAtLineNumber(fDebugState.getBreakpointLocation()); 214 if (breakpoint != null) { 215 detail= breakpoint.toMarshallString(); 216 fDebugState.setShouldSuspend(false); 217 if (fDebugState.getStepOverTask() != null) { 218 fStepOverTaskInterrupted= fDebugState.getStepOverTask(); 219 fDebugState.setStepOverTask(null); 220 } 221 } else if (fDebugState.getCurrentTask() != null) { 222 if (fDebugState.isStepIntoSuspend()) { 223 detail= DebugMessageIds.STEP; 224 fDebugState.setStepIntoSuspend(false); 225 } else if ((fDebugState.getLastTaskFinished() != null && fDebugState.getLastTaskFinished() == fDebugState.getStepOverTask()) || fDebugState.shouldSuspend()) { 226 detail= DebugMessageIds.STEP; 228 fDebugState.setStepOverTask(null); 229 fDebugState.setShouldSuspend(false); 230 } else if (fDebugState.getLastTaskFinished() != null && fDebugState.getLastTaskFinished() == fDebugState.getStepIntoTask()) { 231 detail= DebugMessageIds.STEP; 233 fDebugState.setStepIntoTask(null); 234 } else if (fDebugState.getLastTaskFinished() != null && fDebugState.getLastTaskFinished() == fStepOverTaskInterrupted) { 235 detail= DebugMessageIds.STEP; 237 fStepOverTaskInterrupted= null; 238 } else if (fDebugState.isClientSuspend()) { 239 detail= DebugMessageIds.CLIENT_REQUEST; 240 fDebugState.setClientSuspend(false); 241 } else { 242 shouldSuspend= false; 243 } 244 } else if (fDebugState.shouldSuspend() && fBuildStartedSuspend) { 245 fBuildStartedSuspend= false; 246 fDebugState.setShouldSuspend(false); 247 } else { 248 shouldSuspend= false; 249 } 250 251 if (shouldSuspend) { 252 if (detail != null) { 253 StringBuffer message= new StringBuffer (DebugMessageIds.SUSPENDED); 254 message.append(detail); 255 sendRequestResponse(message.toString()); 256 } 257 try { 258 wait(); 259 shouldSuspend= false; 260 } catch (InterruptedException e) { 261 } 262 } 263 } 264 265 private RemoteAntBreakpoint breakpointAtLineNumber(Location location) { 266 if (fBreakpoints == null || location == null || location == Location.UNKNOWN_LOCATION) { 267 return null; 268 } 269 String fileName= fDebugState.getFileName(location); 270 int lineNumber= fDebugState.getLineNumber(location); 271 for (int i = 0; i < fBreakpoints.size(); i++) { 272 RemoteAntBreakpoint breakpoint = (RemoteAntBreakpoint) fBreakpoints.get(i); 273 if (breakpoint.isAt(fileName, lineNumber)) { 274 return breakpoint; 275 } 276 } 277 return null; 278 } 279 280 private void sendRequestResponse(String message) { 281 if (fRequestWriter == null) { 282 return; 283 } 284 285 fRequestWriter.println(message); 286 } 287 288 protected void marshallStack() { 289 StringBuffer stackRepresentation= new StringBuffer (); 290 fDebugState.marshalStack(stackRepresentation); 291 sendRequestResponse(stackRepresentation.toString()); 292 } 293 294 protected void marshallProperties() { 295 StringBuffer propertiesRepresentation= new StringBuffer (); 296 fDebugState.marshallProperties(propertiesRepresentation, true); 297 sendRequestResponse(propertiesRepresentation.toString()); 298 } 299 300 protected void addBreakpoint(String breakpointRepresentation) { 301 if (fBreakpoints == null) { 302 fBreakpoints= new ArrayList (); 303 } 304 RemoteAntBreakpoint newBreakpoint= new RemoteAntBreakpoint(breakpointRepresentation); 305 if (!fBreakpoints.contains(newBreakpoint)) { 306 fBreakpoints.add(newBreakpoint); 307 } 308 } 309 310 protected void removeBreakpoint(String breakpointRepresentation) { 311 if (fBreakpoints == null) { 312 return; 313 } 314 RemoteAntBreakpoint equivalentBreakpoint= new RemoteAntBreakpoint(breakpointRepresentation); 315 for (Iterator iter = fBreakpoints.iterator(); iter.hasNext(); ) { 316 RemoteAntBreakpoint breakpoint = (RemoteAntBreakpoint) iter.next(); 317 if (breakpoint.equals(equivalentBreakpoint)) { 318 iter.remove(); 319 return; 320 } 321 } 322 } 323 324 327 public void targetStarted(BuildEvent event) { 328 fDebugState.targetStarted(event); 329 if (!fSentProcessId) { 330 establishConnection(); 331 } 332 waitIfSuspended(); 333 super.targetStarted(event); 334 } 335 336 339 public void targetFinished(BuildEvent event) { 340 super.targetFinished(event); 341 fDebugState.setTargetExecuting(null); 342 } 343 344 347 public void configure(Map userProperties) { 348 super.configure(userProperties); 349 String requestPortProperty= (String ) userProperties.remove("eclipse.connect.request_port"); if (requestPortProperty != null) { 351 fRequestPort= Integer.parseInt(requestPortProperty); 352 } 353 } 354 } 355 | Popular Tags |