KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > model > JDIDebugElement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.debug.core.model;
12  
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.debug.core.DebugEvent;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.model.DebugElement;
19 import org.eclipse.debug.core.model.IDebugElement;
20 import org.eclipse.debug.core.model.IDebugTarget;
21 import org.eclipse.debug.core.model.IDisconnect;
22 import org.eclipse.debug.core.model.IStepFilters;
23 import org.eclipse.debug.core.model.ITerminate;
24 import org.eclipse.jdi.TimeoutException;
25 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
26 import org.eclipse.jdt.debug.core.JDIDebugModel;
27 import org.eclipse.jdt.internal.debug.core.EventDispatcher;
28 import org.eclipse.jdt.internal.debug.core.IJDIEventListener;
29 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
30
31 import com.sun.jdi.VMDisconnectedException;
32 import com.sun.jdi.VirtualMachine;
33 import com.sun.jdi.request.EventRequest;
34 import com.sun.jdi.request.EventRequestManager;
35
36 public abstract class JDIDebugElement extends DebugElement implements IDisconnect {
37     
38     /**
39      * Creates a JDI debug element associated with the
40      * specified debug target.
41      *
42      * @param target The associated debug target
43      */

44     public JDIDebugElement(JDIDebugTarget target) {
45         super(target);
46     }
47
48     /**
49      * Convenience method to log errors
50      */

51     protected void logError(Exception JavaDoc e) {
52         if (!((JDIDebugTarget)getDebugTarget()).isAvailable()) {
53             // Don't log VMDisconnectedExceptions that occur
54
// when the VM is unavailable.
55
if (e instanceof VMDisconnectedException ||
56                 (e instanceof CoreException && ((CoreException)e).getStatus().getException() instanceof VMDisconnectedException)) {
57                 return;
58             }
59         }
60         JDIDebugPlugin.log(e);
61     }
62     
63     /**
64      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
65      */

66     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
67         if (adapter == IDebugElement.class) {
68             return this;
69         }
70         if (adapter == IStepFilters.class) {
71             return getDebugTarget();
72         }
73         if (adapter == IDebugTarget.class) {
74             return getDebugTarget();
75         }
76         if (adapter == ITerminate.class) {
77             return getDebugTarget();
78         }
79         if (adapter == IJavaDebugTarget.class) {
80             return getJavaDebugTarget();
81         }
82         return super.getAdapter(adapter);
83     }
84     
85     /**
86      * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
87      */

88     public String JavaDoc getModelIdentifier() {
89         return JDIDebugModel.getPluginIdentifier();
90     }
91
92     /**
93      * Queues a debug event with the event dispatcher to be fired
94      * as an event set when all event processing is complete.
95      *
96      * @param event the event to queue
97      */

98     public void queueEvent(DebugEvent event) {
99         EventDispatcher dispatcher = ((JDIDebugTarget)getDebugTarget()).getEventDispatcher();
100         if (dispatcher != null) {
101             dispatcher.queue(event);
102         }
103     }
104
105     /**
106      * Fires a debug event marking the SUSPEND of this element with
107      * the associated detail.
108      *
109      * @param detail The int detail of the event
110      * @see org.eclipse.debug.core.DebugEvent
111      */

112     public void fireSuspendEvent(int detail) {
113         getJavaDebugTarget().incrementSuspendCount(detail);
114         super.fireSuspendEvent(detail);
115     }
116     
117     /**
118      * Queues a debug event marking the SUSPEND of this element with
119      * the associated detail.
120      *
121      * @param detail The int detail of the event
122      * @see org.eclipse.debug.core.DebugEvent
123      */

124     public void queueSuspendEvent(int detail) {
125         getJavaDebugTarget().incrementSuspendCount(detail);
126         queueEvent(new DebugEvent(this, DebugEvent.SUSPEND, detail));
127     }
128     
129     /**
130      * Throws a new debug exception with a status code of <code>REQUEST_FAILED</code>.
131      *
132      * @param message Failure message
133      * @param e Exception that has occurred (<code>can be null</code>)
134      * @throws DebugException The exception with a status code of <code>REQUEST_FAILED</code>
135      */

136     public void requestFailed(String JavaDoc message, Exception JavaDoc e) throws DebugException {
137         requestFailed(message, e, DebugException.REQUEST_FAILED);
138     }
139     
140     /**
141      * Throws a new debug exception with a status code of <code>TARGET_REQUEST_FAILED</code>
142      * with the given underlying exception. If the underlying exception is not a JDI
143      * exception, the original exception is thrown.
144      *
145      * @param message Failure message
146      * @param e underlying exception that has occurred
147      * @throws DebugException The exception with a status code of <code>TARGET_REQUEST_FAILED</code>
148      */

149     public void targetRequestFailed(String JavaDoc message, RuntimeException JavaDoc e) throws DebugException {
150         if (e == null || e.getClass().getName().startsWith("com.sun.jdi") || e instanceof TimeoutException) { //$NON-NLS-1$
151
requestFailed(message, e, DebugException.TARGET_REQUEST_FAILED);
152         } else {
153             throw e;
154         }
155     }
156
157     /**
158      * Throws a new debug exception with the given status code.
159      *
160      * @param message Failure message
161      * @param e Exception that has occurred (<code>can be null</code>)
162      * @param code status code
163      * @throws DebugException a new exception with given status code
164      */

165     public void requestFailed(String JavaDoc message, Throwable JavaDoc e, int code) throws DebugException {
166         throwDebugException(message, code, e);
167     }
168         
169     /**
170      * Throws a new debug exception with a status code of <code>TARGET_REQUEST_FAILED</code>.
171      *
172      * @param message Failure message
173      * @param e Throwable that has occurred
174      * @throws DebugException The exception with a status code of <code>TARGET_REQUEST_FAILED</code>
175      */

176     public void targetRequestFailed(String JavaDoc message, Throwable JavaDoc e) throws DebugException {
177         throwDebugException(message, DebugException.TARGET_REQUEST_FAILED, e);
178     }
179     
180     /**
181      * Throws a new debug exception with a status code of <code>TARGET_REQUEST_FAILED</code>
182      * with the given underlying exception. The underlying exception is an exception thrown
183      * by a JDI request.
184      *
185      * @param message Failure message
186      * @param e throwable exception that has occurred
187      * @throws DebugException the exception with a status code of <code>TARGET_REQUEST_FAILED</code>
188      */

189     public void jdiRequestFailed(String JavaDoc message, Throwable JavaDoc e) throws DebugException {
190         throwDebugException(message, DebugException.TARGET_REQUEST_FAILED, e);
191     }
192     
193     /**
194      * Throws a new debug exception with a status code of <code>NOT_SUPPORTED</code>.
195      *
196      * @param message Failure message
197      * @throws DebugException The exception with a status code of <code>NOT_SUPPORTED</code>.
198      */

199     public void notSupported(String JavaDoc message) throws DebugException {
200         throwDebugException(message, DebugException.NOT_SUPPORTED, null);
201     }
202     
203     /**
204      * Throws a debug exception with the given message, error code, and underlying
205      * exception.
206      */

207     protected void throwDebugException(String JavaDoc message, int code, Throwable JavaDoc exception) throws DebugException {
208         if(exception instanceof VMDisconnectedException) {
209             disconnected();
210         }
211         throw new DebugException(new Status(IStatus.ERROR, JDIDebugModel.getPluginIdentifier(),
212             code, message, exception));
213     }
214     
215     /**
216      * Logs the given exception if it is a JDI exception, otherwise throws the
217      * runtime exception.
218      *
219      * @param e The internal runtime exception
220      */

221     public void internalError(RuntimeException JavaDoc e) {
222         if (e.getClass().getName().startsWith("com.sun.jdi") || e instanceof TimeoutException) { //$NON-NLS-1$
223
logError(e);
224         } else {
225             throw e;
226         }
227     }
228     
229     /**
230      * Logs a debug exception with the given message,
231      * with a status code of <code>INTERNAL_ERROR</code>.
232      *
233      * @param message The internal error message
234      */

235     protected void internalError(String JavaDoc message) {
236         logError(new DebugException(new Status(IStatus.ERROR, JDIDebugModel.getPluginIdentifier(),
237             DebugException.INTERNAL_ERROR, message, null)));
238     }
239
240     /**
241      * Returns the common "<unknown>" message.
242      *
243      * @return the unknown String
244      */

245     protected String JavaDoc getUnknownMessage() {
246         return JDIDebugModelMessages.JDIDebugElement_unknown;
247     }
248         
249     /**
250      * Returns this elements debug target as its implementation
251      * class.
252      *
253      * @return Java debug target
254      */

255     protected JDIDebugTarget getJavaDebugTarget() {
256         return (JDIDebugTarget)getDebugTarget();
257     }
258
259     /**
260      * Returns the target VM associated with this element, or <code>null</code>
261      * if none.
262      *
263      * @return target VM or <code>null</code> if none
264      */

265     protected VirtualMachine getVM() {
266         return ((JDIDebugTarget)getDebugTarget()).getVM();
267     }
268     
269     /**
270      * Returns the underlying VM's event request manager, or <code>null</code>
271      * if none (disconnected/terminated)
272      *
273      * @return event request manager or <code>null</code>
274      */

275     public EventRequestManager getEventRequestManager() {
276         VirtualMachine vm = getVM();
277         if (vm == null) {
278             return null;
279         }
280         return vm.eventRequestManager();
281     }
282     
283     /**
284      * Adds the given listener to this target's event dispatcher's
285      * table of listeners for the specified event request. The listener
286      * will be notified each time the event occurs.
287      *
288      * @param listener the listener to register
289      * @param request the event request
290      */

291     public void addJDIEventListener(IJDIEventListener listener, EventRequest request) {
292         EventDispatcher dispatcher = ((JDIDebugTarget)getDebugTarget()).getEventDispatcher();
293         if (dispatcher != null) {
294             dispatcher.addJDIEventListener(listener, request);
295         }
296     }
297     
298     /**
299      * Removes the given listener from this target's event dispatcher's
300      * table of listeners for the specifed event request. The listener
301      * will no longer be notified when the event occurs. Listeners
302      * are responsible for deleting the event request if desired.
303      *
304      * @param listener the listener to remove
305      * @param request the event request
306      */

307     public void removeJDIEventListener(IJDIEventListener listener, EventRequest request) {
308         EventDispatcher dispatcher = ((JDIDebugTarget)getDebugTarget()).getEventDispatcher();
309         if (dispatcher != null) {
310             dispatcher.removeJDIEventListener(listener, request);
311         }
312     }
313     
314     /**
315      * The VM has disconnected. Notify the target.
316      */

317     protected void disconnected() {
318         if (getDebugTarget() != null) {
319             getJavaDebugTarget().disconnected();
320         }
321     }
322     
323     /**
324      * @see IJavaDebugTarget#setRequestTimeout(int)
325      */

326     public void setRequestTimeout(int timeout) {
327         if (supportsRequestTimeout()) {
328             VirtualMachine vm = getVM();
329             if (vm != null) {
330                 ((org.eclipse.jdi.VirtualMachine)vm).setRequestTimeout(timeout);
331             }
332         }
333     }
334     
335     /**
336      * @see IJavaDebugTarget#getRequestTimeout()
337      */

338     public int getRequestTimeout() {
339         if (supportsRequestTimeout()) {
340             VirtualMachine vm = getVM();
341             if (vm != null) {
342                 return ((org.eclipse.jdi.VirtualMachine)vm).getRequestTimeout();
343             }
344         }
345         return -1;
346     }
347     /**
348      * @see IJavaDebugTarget#supportsRequestTimeout()
349      */

350     public boolean supportsRequestTimeout() {
351         return getJavaDebugTarget().isAvailable() && getVM() instanceof org.eclipse.jdi.VirtualMachine;
352     }
353     
354     /**
355      * @see org.eclipse.debug.core.model.IDisconnect#canDisconnect()
356      */

357     public boolean canDisconnect() {
358         return getDebugTarget().canDisconnect();
359     }
360
361     /**
362      * @see org.eclipse.debug.core.model.IDisconnect#disconnect()
363      */

364     public void disconnect() throws DebugException {
365         getDebugTarget().disconnect();
366     }
367
368     /**
369      * @see org.eclipse.debug.core.model.IDisconnect#isDisconnected()
370      */

371     public boolean isDisconnected() {
372         return getDebugTarget().isDisconnected();
373     }
374
375     /**
376      * @see org.eclipse.debug.core.model.IStepFilters#isStepFiltersEnabled()
377      */

378     public boolean isStepFiltersEnabled() {
379         return getJavaDebugTarget().isStepFiltersEnabled();
380     }
381 }
382
Popular Tags