KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > debug > model > RemoteAntDebugBuildListener


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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
12 package org.eclipse.ant.internal.ui.debug.model;
13
14 import java.io.BufferedReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.net.Socket JavaDoc;
19 import java.net.UnknownHostException JavaDoc;
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     // sockets to communicate with the remote Ant debug build logger
35
private Socket JavaDoc fRequestSocket;
36     private PrintWriter JavaDoc fRequestWriter;
37     private BufferedReader JavaDoc fResponseReader;
38     
39     private int fRequestPort= -1;
40     private Thread JavaDoc fReaderThread;
41     
42     private AntDebugTarget fTarget;
43     
44     /**
45      * Reader thread that processes request responses from the remote Ant debug build logger
46      */

47     private class ReaderThread extends Thread JavaDoc {
48         public ReaderThread() {
49             super("Ant Request Response Reader Thread"); //$NON-NLS-1$
50
setDaemon(true);
51         }
52
53         public void run(){
54             try {
55                 String JavaDoc 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 JavaDoc ie) { //the other end has shutdown
64
RemoteAntDebugBuildListener.this.shutDown();
65             } catch (Exception JavaDoc e) {
66                 AntUIPlugin.log("Internal error processing remote response", e); //$NON-NLS-1$
67
RemoteAntDebugBuildListener.this.shutDown();
68             }
69         }
70     }
71     
72     public RemoteAntDebugBuildListener(ILaunch launch) {
73         super(launch);
74         //fDebug= true;
75
}
76     
77     protected void receiveMessage(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc exception= null;
129         for (int i= 1; i < 20; i++) {
130             try {
131                 fRequestSocket = new Socket JavaDoc("localhost", fRequestPort); //$NON-NLS-1$
132
fRequestWriter = new PrintWriter JavaDoc(fRequestSocket.getOutputStream(), true);
133                 fResponseReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(fRequestSocket.getInputStream()));
134                 
135                 fReaderThread= new ReaderThread();
136                 fReaderThread.start();
137                 return true;
138             } catch (UnknownHostException JavaDoc e) {
139                 exception= e;
140                 break;
141             } catch (IOException JavaDoc e) {
142                 exception= e;
143             }
144             try {
145                 Thread.sleep(500);
146             } catch(InterruptedException JavaDoc e) {
147             }
148         }
149         AntUIPlugin.log("Internal error attempting to connect to debug target", exception); //$NON-NLS-1$
150
return false;
151     }
152
153     /**
154      * Start listening to an Ant build. Start a server connection that
155      * the RemoteAntDebugBuildLogger can connect to.
156      *
157      * @param eventPort The port number to create the server connection on
158      * @param requestPort The port number to use for sending requests to the remote logger
159      */

160     public synchronized void startListening(int eventPort, int requestPort) {
161         super.startListening(eventPort);
162         fRequestPort= requestPort;
163     }
164     
165     /**
166      * Sends a request to the Ant build
167      *
168      * @param request debug command
169      */

170     protected void sendRequest(String JavaDoc 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                 // interrupt reader thread so that we don't block on close
191
// on a lock held by the BufferedReader
192
// see bug: 38955
193
fReaderThread.interrupt();
194             }
195             if (fResponseReader != null) {
196                 fResponseReader.close();
197                 fResponseReader= null;
198             }
199         } catch(IOException JavaDoc 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 JavaDoc e) {
211         }
212         super.shutDown();
213     }
214
215     /* (non-Javadoc)
216      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#resume()
217      */

218     public void resume() {
219         sendRequest(DebugMessageIds.RESUME);
220     }
221
222     /* (non-Javadoc)
223      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#suspend()
224      */

225     public void suspend() {
226         sendRequest(DebugMessageIds.SUSPEND);
227     }
228
229     /* (non-Javadoc)
230      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#stepInto()
231      */

232     public void stepInto() {
233         sendRequest(DebugMessageIds.STEP_INTO);
234     }
235
236     /* (non-Javadoc)
237      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#stepOver()
238      */

239     public void stepOver() {
240         sendRequest(DebugMessageIds.STEP_OVER);
241     }
242
243     /* (non-Javadoc)
244      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#handleBreakpoint(IBreakpoint, boolean)
245      */

246     public void handleBreakpoint(IBreakpoint breakpoint, boolean add) {
247         if (fTarget == null || !fTarget.supportsBreakpoint(breakpoint)) {
248             return;
249         }
250         StringBuffer JavaDoc message= new StringBuffer JavaDoc();
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     /* (non-Javadoc)
276      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#getProperties()
277      */

278     public void getProperties() {
279         sendRequest(DebugMessageIds.PROPERTIES);
280     }
281
282     /* (non-Javadoc)
283      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#getStackFrames()
284      */

285     public void getStackFrames() {
286         sendRequest(DebugMessageIds.STACK);
287     }
288
289     /* (non-Javadoc)
290      * @see org.eclipse.ant.internal.ui.debug.IAntDebugController#unescapeString(java.lang.StringBuffer)
291      */

292     public StringBuffer JavaDoc unescapeString(StringBuffer JavaDoc property) {
293         if (property.indexOf("\\r") == -1 && property.indexOf("\\n") == -1) { //$NON-NLS-1$ //$NON-NLS-2$
294
return property;
295         }
296         for (int i= 0; i < property.length(); i++) {
297             if ('\\' == property.charAt(i)) {
298                 String JavaDoc newString= ""; //$NON-NLS-1$
299
if ('r' == property.charAt(i + 1)) {
300                     if (i-1 > - 1 && '\\' == property.charAt(i-1)) {
301                         newString= "r"; //$NON-NLS-1$
302
} 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"; //$NON-NLS-1$
308
} 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