KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > antsupport > logger > debug > RemoteAntDebugBuildLogger


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.antsupport.logger.debug;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStreamReader JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.net.ServerSocket JavaDoc;
18 import java.net.Socket JavaDoc;
19 import java.net.SocketTimeoutException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
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 /**
34  * Parts adapted from org.eclipse.jdt.internal.junit.runner.RemoteTestRunner
35  * A build logger that reports via a socket connection.
36  * See DebugMessageIds and MessageIds for more information about the protocol.
37  */

38 public class RemoteAntDebugBuildLogger extends RemoteAntBuildLogger implements IDebugBuildLogger {
39     
40     private ServerSocket JavaDoc fServerSocket;
41     private static final int fgServerSocketTimeout= 5000;
42     private Socket JavaDoc fRequestSocket;
43     
44     private PrintWriter JavaDoc fRequestWriter;
45     
46     private BufferedReader JavaDoc fRequestReader;
47     
48     private boolean fBuildStartedSuspend= true;
49     
50     private Task fStepOverTaskInterrupted;
51     
52     private List JavaDoc fBreakpoints= null;
53     
54     /**
55      * Request port to connect to.
56      * Used for debug connections
57      */

58     private int fRequestPort= -1;
59     private AntDebugState fDebugState;
60
61     /**
62      * Reader thread that processes requests from the debug client.
63      */

64     private class ReaderThread extends Thread JavaDoc {
65         public ReaderThread() {
66             super("ReaderThread"); //$NON-NLS-1$
67
setDaemon(true);
68         }
69
70         public void run(){
71             try {
72                 String JavaDoc 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 JavaDoc 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); //$NON-NLS-1$ //$NON-NLS-2$
123
}
124         
125         try{
126             fServerSocket.setSoTimeout(fgServerSocketTimeout);
127             fRequestSocket= fServerSocket.accept();
128             fRequestWriter= new PrintWriter JavaDoc(fRequestSocket.getOutputStream(), true);
129             fRequestReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(fRequestSocket.getInputStream()));
130             
131             ReaderThread readerThread= new ReaderThread();
132             readerThread.setDaemon(true);
133             readerThread.start();
134             return;
135         } catch(SocketTimeoutException JavaDoc e){
136         } catch(IOException JavaDoc e) {
137         }
138         shutDown();
139     }
140     
141     /* (non-Javadoc)
142      * @see org.eclipse.ant.internal.ui.antsupport.logger.RemoteAntBuildLogger#shutDown()
143      */

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 JavaDoc e) {
154             }
155             fRequestReader= null;
156         }
157         
158         if (fRequestSocket != null) {
159             try {
160                 fRequestSocket.close();
161             } catch(IOException JavaDoc e) {
162             }
163         }
164         fRequestSocket= null;
165         
166         super.shutDown();
167     }
168     
169     /* (non-Javadoc)
170      * @see org.apache.tools.ant.BuildListener#buildStarted(org.apache.tools.ant.BuildEvent)
171      */

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 JavaDoc(fRequestPort);
179             } catch (IOException JavaDoc ioe) {
180                 shutDown();
181             }
182             requestConnect();
183         } else {
184             shutDown();
185         }
186         fDebugState.buildStarted();
187         fDebugState.setShouldSuspend(true);
188         waitIfSuspended();
189     }
190
191     /* (non-Javadoc)
192      * @see org.apache.tools.ant.BuildListener#taskStarted(org.apache.tools.ant.BuildEvent)
193      */

194     public void taskStarted(BuildEvent event) {
195         super.taskStarted(event);
196         fDebugState.taskStarted(event);
197     }
198     
199     /* (non-Javadoc)
200      * @see org.apache.tools.ant.BuildListener#taskFinished(org.apache.tools.ant.BuildEvent)
201      */

202     public synchronized void taskFinished(BuildEvent event) {
203         super.taskFinished(event);
204         fDebugState.taskFinished();
205     }
206     
207     /* (non-Javadoc)
208      * @see org.eclipse.ant.internal.ui.antsupport.logger.util.IDebugBuildLogger#waitIfSuspended()
209      */

210     public synchronized void waitIfSuspended() {
211         String JavaDoc 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                 //suspend as a step over has finished
227
detail= DebugMessageIds.STEP;
228                 fDebugState.setStepOverTask(null);
229                 fDebugState.setShouldSuspend(false);
230             } else if (fDebugState.getLastTaskFinished() != null && fDebugState.getLastTaskFinished() == fDebugState.getStepIntoTask()) {
231                 //suspend as a task that was stepped into has finally completed
232
detail= DebugMessageIds.STEP;
233                  fDebugState.setStepIntoTask(null);
234             } else if (fDebugState.getLastTaskFinished() != null && fDebugState.getLastTaskFinished() == fStepOverTaskInterrupted) {
235                 //suspend as a task that was stepped over but hit a breakpoint has finally completed
236
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 JavaDoc message= new StringBuffer JavaDoc(DebugMessageIds.SUSPENDED);
254                 message.append(detail);
255                 sendRequestResponse(message.toString());
256             }
257              try {
258                  wait();
259                  shouldSuspend= false;
260              } catch (InterruptedException JavaDoc 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 JavaDoc 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 JavaDoc message) {
281         if (fRequestWriter == null) {
282             return;
283         }
284         
285         fRequestWriter.println(message);
286     }
287     
288     protected void marshallStack() {
289         StringBuffer JavaDoc stackRepresentation= new StringBuffer JavaDoc();
290         fDebugState.marshalStack(stackRepresentation);
291         sendRequestResponse(stackRepresentation.toString());
292     }
293     
294     protected void marshallProperties() {
295         StringBuffer JavaDoc propertiesRepresentation= new StringBuffer JavaDoc();
296         fDebugState.marshallProperties(propertiesRepresentation, true);
297         sendRequestResponse(propertiesRepresentation.toString());
298     }
299     
300     protected void addBreakpoint(String JavaDoc breakpointRepresentation) {
301         if (fBreakpoints == null) {
302             fBreakpoints= new ArrayList JavaDoc();
303         }
304         RemoteAntBreakpoint newBreakpoint= new RemoteAntBreakpoint(breakpointRepresentation);
305         if (!fBreakpoints.contains(newBreakpoint)) {
306             fBreakpoints.add(newBreakpoint);
307         }
308     }
309     
310     protected void removeBreakpoint(String JavaDoc breakpointRepresentation) {
311         if (fBreakpoints == null) {
312             return;
313         }
314         RemoteAntBreakpoint equivalentBreakpoint= new RemoteAntBreakpoint(breakpointRepresentation);
315         for (Iterator JavaDoc 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     /* (non-Javadoc)
325      * @see org.apache.tools.ant.BuildListener#targetStarted(org.apache.tools.ant.BuildEvent)
326      */

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     /* (non-Javadoc)
337      * @see org.apache.tools.ant.BuildListener#targetFinished(org.apache.tools.ant.BuildEvent)
338      */

339     public void targetFinished(BuildEvent event) {
340         super.targetFinished(event);
341         fDebugState.setTargetExecuting(null);
342     }
343     
344     /* (non-Javadoc)
345      * @see org.eclipse.ant.internal.ui.antsupport.logger.RemoteAntBuildLogger#configure(java.util.Map)
346      */

347     public void configure(Map JavaDoc userProperties) {
348        super.configure(userProperties);
349        String JavaDoc requestPortProperty= (String JavaDoc) userProperties.remove("eclipse.connect.request_port"); //$NON-NLS-1$
350
if (requestPortProperty != null) {
351             fRequestPort= Integer.parseInt(requestPortProperty);
352         }
353     }
354 }
355
Popular Tags