KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > core > Launch


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  * Pawel Piech - Bug 82003: The IDisconnect implementation by Launch module is too restrictive.
11  *******************************************************************************/

12 package org.eclipse.debug.core;
13
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.core.runtime.PlatformObject;
23 import org.eclipse.debug.core.model.IDebugTarget;
24 import org.eclipse.debug.core.model.IDisconnect;
25 import org.eclipse.debug.core.model.IProcess;
26 import org.eclipse.debug.core.model.ISourceLocator;
27 import org.eclipse.debug.internal.core.DebugCoreMessages;
28 import org.eclipse.debug.internal.core.LaunchManager;
29
30 /**
31  * A launch is the result of launching a debug session
32  * and/or one or more system processes. This class provides
33  * a public implementation of <code>ILaunch</code> for client
34  * use.
35  * <p>
36  * Clients may instantiate this class. Clients may subclass this class.
37  * </p>
38  * @see ILaunch
39  * @see ILaunchManager
40  */

41
42 public class Launch extends PlatformObject implements ILaunch, IDisconnect, ILaunchListener, ILaunchConfigurationListener, IDebugEventSetListener {
43     
44     /**
45      * The debug targets associated with this
46      * launch (the primary target is the first one
47      * in this collection), or empty if
48      * there are no debug targets.
49      */

50     private List JavaDoc fTargets= new ArrayList JavaDoc();
51
52     /**
53      * The configuration that was launched, or null.
54      */

55     private ILaunchConfiguration fConfiguration= null;
56
57     /**
58      * The system processes associated with
59      * this launch, or empty if none.
60      */

61     private List JavaDoc fProcesses= new ArrayList JavaDoc();
62
63     /**
64      * The source locator to use in the debug session
65      * or <code>null</code> if not supported.
66      */

67     private ISourceLocator fLocator= null;
68
69     /**
70      * The mode this launch was launched in.
71      */

72     private String JavaDoc fMode;
73     
74     /**
75      * Table of client defined attributes
76      */

77     private HashMap JavaDoc fAttributes;
78     
79     /**
80      * Flag indicating that change notification should
81      * be suppressed. <code>true</code> until this
82      * launch has been initialized.
83      */

84     private boolean fSuppressChange = true;
85         
86     /**
87      * Constructs a launch with the specified attributes.
88      *
89      * @param launchConfiguration the configuration that was launched
90      * @param mode the mode of this launch - run or debug (constants
91      * defined by <code>ILaunchManager</code>)
92      * @param locator the source locator to use for this debug session, or
93      * <code>null</code> if not supported
94      */

95     public Launch(ILaunchConfiguration launchConfiguration, String JavaDoc mode, ISourceLocator locator) {
96         fConfiguration = launchConfiguration;
97         setSourceLocator(locator);
98         fMode = mode;
99         fSuppressChange = false;
100         getLaunchManager().addLaunchListener(this);
101         getLaunchManager().addLaunchConfigurationListener(this);
102     }
103     
104     /**
105      * Registers debug event listener.
106      */

107     private void addEventListener() {
108         DebugPlugin.getDefault().addDebugEventListener(this);
109     }
110     
111     /**
112      * Removes debug event listener.
113      */

114     private void removeEventListener() {
115         DebugPlugin.getDefault().removeDebugEventListener(this);
116     }
117     
118     /**
119      * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
120      */

121     public boolean canTerminate() {
122         List JavaDoc processes = getProcesses0();
123         for (int i = 0; i < processes.size(); i++) {
124             IProcess process = (IProcess)processes.get(i);
125             if (process.canTerminate()) {
126                 return true;
127             }
128         }
129         List JavaDoc targets = getDebugTargets0();
130         for (int i = 0; i < targets.size(); i++) {
131             IDebugTarget target = (IDebugTarget)targets.get(i);
132             if (target.canTerminate() || target.canDisconnect()) {
133                 return true;
134             }
135         }
136         return false;
137     }
138
139     /**
140      * @see ILaunch#getChildren()
141      */

142     public Object JavaDoc[] getChildren() {
143         ArrayList JavaDoc children = new ArrayList JavaDoc(getDebugTargets0());
144         children.addAll(getProcesses0());
145         return children.toArray();
146     }
147
148     /**
149      * @see ILaunch#getDebugTarget()
150      */

151     public IDebugTarget getDebugTarget() {
152         if (!getDebugTargets0().isEmpty()) {
153             return (IDebugTarget)getDebugTargets0().get(0);
154         }
155         return null;
156     }
157         
158     /**
159      * @see ILaunch#getProcesses()
160      */

161     public IProcess[] getProcesses() {
162         return (IProcess[])getProcesses0().toArray(new IProcess[getProcesses0().size()]);
163     }
164     
165     /**
166      * Returns the processes associated with this
167      * launch, in its internal form - a list.
168      *
169      * @return list of processes
170      */

171     protected List JavaDoc getProcesses0() {
172         return fProcesses;
173     }
174     
175     /**
176      * @see ILaunch#getSourceLocator()
177      */

178     public ISourceLocator getSourceLocator() {
179         return fLocator;
180     }
181     
182     /**
183      * @see ILaunch#setSourceLocator(ISourceLocator)
184      */

185     public void setSourceLocator(ISourceLocator sourceLocator) {
186         fLocator = sourceLocator;
187     }
188
189     /**
190      * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
191      */

192     public boolean isTerminated() {
193         if (getProcesses0().isEmpty() && getDebugTargets0().isEmpty()) {
194             return false;
195         }
196
197         Iterator JavaDoc processes = getProcesses0().iterator();
198         while (processes.hasNext()) {
199             IProcess process = (IProcess)processes.next();
200             if (!process.isTerminated()) {
201                 return false;
202             }
203         }
204         
205         Iterator JavaDoc targets = getDebugTargets0().iterator();
206         while (targets.hasNext()) {
207             IDebugTarget target = (IDebugTarget)targets.next();
208             if (!(target.isTerminated() || target.isDisconnected())) {
209                 return false;
210             }
211         }
212         
213         return true;
214     }
215
216     /**
217      * @see org.eclipse.debug.core.model.ITerminate#terminate()
218      */

219     public void terminate() throws DebugException {
220         MultiStatus status=
221             new MultiStatus(DebugPlugin.getUniqueIdentifier(), DebugException.REQUEST_FAILED, DebugCoreMessages.Launch_terminate_failed, null);
222         
223         // terminate the system processes
224
IProcess[] processes = getProcesses();
225         for (int i = 0; i < processes.length; i++) {
226             IProcess process = processes[i];
227             if (process.canTerminate()) {
228                 try {
229                     process.terminate();
230                 } catch (DebugException e) {
231                     status.merge(e.getStatus());
232                 }
233             }
234         }
235         
236         // terminate or disconnect debug target if it is still alive
237
IDebugTarget[] targets = getDebugTargets();
238         for (int i = 0; i < targets.length; i++) {
239             IDebugTarget target= targets[i];
240             if (target != null) {
241                 if (target.canTerminate()) {
242                     try {
243                         target.terminate();
244                     } catch (DebugException e) {
245                         status.merge(e.getStatus());
246                     }
247                 } else {
248                     if (target.canDisconnect()) {
249                         try {
250                             target.disconnect();
251                         } catch (DebugException de) {
252                             status.merge(de.getStatus());
253                         }
254                     }
255                 }
256             }
257         }
258         if (status.isOK()) {
259             return;
260         }
261         IStatus[] children= status.getChildren();
262         if (children.length == 1) {
263             throw new DebugException(children[0]);
264         }
265         throw new DebugException(status);
266     }
267
268     /**
269      * @see ILaunch#getLaunchMode()
270      */

271     public String JavaDoc getLaunchMode() {
272         return fMode;
273     }
274     
275     /**
276      * @see ILaunch#getLaunchConfiguration()
277      */

278     public ILaunchConfiguration getLaunchConfiguration() {
279         return fConfiguration;
280     }
281
282     /**
283      * @see ILaunch#setAttribute(String, String)
284      */

285     public void setAttribute(String JavaDoc key, String JavaDoc value) {
286         if (fAttributes == null) {
287             fAttributes = new HashMap JavaDoc(5);
288         }
289         fAttributes.put(key, value);
290     }
291
292     /**
293      * @see ILaunch#getAttribute(String)
294      */

295     public String JavaDoc getAttribute(String JavaDoc key) {
296         if (fAttributes == null) {
297             return null;
298         }
299         return (String JavaDoc)fAttributes.get(key);
300     }
301
302     /**
303      * @see ILaunch#getDebugTargets()
304      */

305     public IDebugTarget[] getDebugTargets() {
306         return (IDebugTarget[])fTargets.toArray(new IDebugTarget[fTargets.size()]);
307     }
308     
309     /**
310      * Returns the debug targets associated with this
311      * launch, in its internal form - a list
312      *
313      * @return list of debug targets
314      */

315     protected List JavaDoc getDebugTargets0() {
316         return fTargets;
317     }
318
319     /**
320      * @see ILaunch#addDebugTarget(IDebugTarget)
321      */

322     public void addDebugTarget(IDebugTarget target) {
323         if (target != null) {
324             if (!getDebugTargets0().contains(target)) {
325                 addEventListener();
326                 getDebugTargets0().add(target);
327                 fireChanged();
328             }
329         }
330     }
331     
332     /**
333      * @see ILaunch#removeDebugTarget(IDebugTarget)
334      */

335     public void removeDebugTarget(IDebugTarget target) {
336         if (target != null) {
337             if (getDebugTargets0().remove(target)) {
338                 fireChanged();
339             }
340         }
341     }
342     
343     /**
344      * @see ILaunch#addProcess(IProcess)
345      */

346     public void addProcess(IProcess process) {
347         if (process != null) {
348             if (!getProcesses0().contains(process)) {
349                 addEventListener();
350                 getProcesses0().add(process);
351                 fireChanged();
352             }
353         }
354     }
355     
356     /**
357      * @see ILaunch#removeProcess(IProcess)
358      */

359     public void removeProcess(IProcess process) {
360         if (process != null) {
361             if (getProcesses0().remove(process)) {
362                 fireChanged();
363             }
364         }
365     }
366     
367     /**
368      * Adds the given processes to this launch.
369      *
370      * @param processes processes to add
371      */

372     protected void addProcesses(IProcess[] processes) {
373         if (processes != null) {
374             for (int i = 0; i < processes.length; i++) {
375                 addProcess(processes[i]);
376                 fireChanged();
377             }
378         }
379     }
380     
381     /**
382      * Notifies listeners that this launch has changed.
383      * Has no effect of this launch has not yet been
384      * properly created/initialized.
385      */

386     protected void fireChanged() {
387         if (!fSuppressChange) {
388             ((LaunchManager)getLaunchManager()).fireUpdate(this, LaunchManager.CHANGED);
389             ((LaunchManager)getLaunchManager()).fireUpdate(new ILaunch[] {this}, LaunchManager.CHANGED);
390         }
391     }
392
393     /**
394      * Notifies listeners that this launch has terminated.
395      * Has no effect of this launch has not yet been
396      * properly created/initialized.
397      */

398     protected void fireTerminate() {
399         if (!fSuppressChange) {
400             ((LaunchManager)getLaunchManager()).fireUpdate(this, LaunchManager.TERMINATE);
401             ((LaunchManager)getLaunchManager()).fireUpdate(new ILaunch[] {this}, LaunchManager.TERMINATE);
402         }
403         removeEventListener();
404     }
405     
406     /**
407      * @see ILaunch#hasChildren()
408      */

409     public boolean hasChildren() {
410         return getProcesses0().size() > 0 || (getDebugTargets0().size() > 0);
411     }
412     
413     /**
414      * Returns whether any processes or targets can be disconnected.
415      * Ones that are already terminated or disconnected are ignored.
416      *
417      * @see org.eclipse.debug.core.model.IDisconnect#canDisconnect()
418      */

419     public boolean canDisconnect() {
420         List JavaDoc processes = getProcesses0();
421         for (int i = 0; i < processes.size(); i++) {
422             if (processes.get(i) instanceof IDisconnect) {
423                 IDisconnect process = (IDisconnect)processes.get(i);
424                 if (process.canDisconnect()) {
425                     return true;
426                 }
427             }
428         }
429         List JavaDoc targets = getDebugTargets0();
430         for (int i = 0; i < targets.size(); i++) {
431             if ( ((IDebugTarget)targets.get(i)).canDisconnect() ) {
432                 return true;
433             }
434         }
435         return false;
436     }
437
438     /**
439      * @see org.eclipse.debug.core.model.IDisconnect#disconnect()
440      */

441     public void disconnect() throws DebugException {
442         List JavaDoc processes = getProcesses0();
443         for (int i = 0; i < processes.size(); i++) {
444             if (processes.get(i) instanceof IDisconnect) {
445                 IDisconnect disconnect = (IDisconnect)processes.get(i);
446                 if (disconnect.canDisconnect()) {
447                     disconnect.disconnect();
448                 }
449             }
450         }
451         List JavaDoc targets = getDebugTargets0();
452         for (int i = 0; i < targets.size(); i++) {
453             IDebugTarget debugTarget = (IDebugTarget)targets.get(i);
454             if (debugTarget.canDisconnect()) {
455                 debugTarget.disconnect();
456             }
457         }
458     }
459
460     /**
461      * Returns whether all of the contained targets and processes are
462      * disconnected. Processes that don't support disconnecting are not
463      * counted.
464      *
465      * @see org.eclipse.debug.core.model.IDisconnect#isDisconnected()
466      */

467     public boolean isDisconnected() {
468         List JavaDoc processes = getProcesses0();
469         for (int i = 0; i < processes.size(); i++) {
470             if (processes.get(i) instanceof IDisconnect) {
471                 IDisconnect process = (IDisconnect)processes.get(i);
472                 if (!process.isDisconnected()) {
473                     return false;
474                 }
475             }
476         }
477         List JavaDoc targets = getDebugTargets0();
478         for (int i = 0; i < targets.size(); i++) {
479             if ( !((IDebugTarget)targets.get(i)).isDisconnected() ) {
480                 return false;
481             }
482         }
483         // only return true if there are processes or targets that are disconnected
484
return hasChildren();
485     }
486
487     /* (non-Javadoc)
488      * @see org.eclipse.debug.core.ILaunchListener#launchRemoved(org.eclipse.debug.core.ILaunch)
489      */

490     public void launchRemoved(ILaunch launch) {
491         if (this.equals(launch)) {
492             removeEventListener();
493             getLaunchManager().removeLaunchListener(this);
494             getLaunchManager().removeLaunchConfigurationListener(this);
495         }
496     }
497
498     /**
499      * Returns the launch manager.
500      *
501      * @return the launch manager.
502      */

503     protected ILaunchManager getLaunchManager() {
504         return DebugPlugin.getDefault().getLaunchManager();
505     }
506
507     /* (non-Javadoc)
508      * @see org.eclipse.debug.core.ILaunchListener#launchAdded(org.eclipse.debug.core.ILaunch)
509      */

510     public void launchAdded(ILaunch launch) {
511     }
512
513     /* (non-Javadoc)
514      * @see org.eclipse.debug.core.ILaunchListener#launchChanged(org.eclipse.debug.core.ILaunch)
515      */

516     public void launchChanged(ILaunch launch) {
517     }
518
519     /* (non-Javadoc)
520      *
521      * If the launch configuration this launch is associated with is
522      * moved, update the underlying handle to the new location.
523      *
524      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationAdded(org.eclipse.debug.core.ILaunchConfiguration)
525      */

526     public void launchConfigurationAdded(ILaunchConfiguration configuration) {
527         ILaunchConfiguration from = getLaunchManager().getMovedFrom(configuration);
528         if (from != null && from.equals(getLaunchConfiguration())) {
529             fConfiguration = configuration;
530             fireChanged();
531         }
532     }
533
534     /* (non-Javadoc)
535      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationChanged(org.eclipse.debug.core.ILaunchConfiguration)
536      */

537     public void launchConfigurationChanged(ILaunchConfiguration configuration) {}
538
539     /* (non-Javadoc)
540      *
541      * Update the launch configuration associated with this launch if the
542      * underlying configuration is deleted.
543      *
544      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationRemoved(org.eclipse.debug.core.ILaunchConfiguration)
545      */

546     public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
547         if (configuration.equals(getLaunchConfiguration())) {
548             if (getLaunchManager().getMovedTo(configuration) == null) {
549                 fConfiguration = null;
550                 fireChanged();
551             }
552         }
553     }
554
555     /* (non-Javadoc)
556      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
557      */

558     public void handleDebugEvents(DebugEvent[] events) {
559         for (int i = 0; i < events.length; i++) {
560             DebugEvent event = events[i];
561             if (event.getKind() == DebugEvent.TERMINATE) {
562                 Object JavaDoc object = event.getSource();
563                 ILaunch launch = null;
564                 if (object instanceof IProcess) {
565                     launch = ((IProcess)object).getLaunch();
566                 } else if (object instanceof IDebugTarget) {
567                     launch = ((IDebugTarget)object).getLaunch();
568                 }
569                 if (this.equals(launch)) {
570                     if (isTerminated()) {
571                         fireTerminate();
572                     }
573                 }
574             }
575         }
576     }
577
578     /* (non-Javadoc)
579      * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
580      */

581     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
582         if (adapter.equals(ILaunch.class)) {
583             return this;
584         }
585         //CONTEXTLAUNCHING
586
if(adapter.equals(ILaunchConfiguration.class)) {
587             return getLaunchConfiguration();
588         }
589         return super.getAdapter(adapter);
590     }
591     
592     
593
594 }
595
Popular Tags