1 12 package org.eclipse.debug.core; 13 14 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 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 41 42 public class Launch extends PlatformObject implements ILaunch, IDisconnect, ILaunchListener, ILaunchConfigurationListener, IDebugEventSetListener { 43 44 50 private List fTargets= new ArrayList (); 51 52 55 private ILaunchConfiguration fConfiguration= null; 56 57 61 private List fProcesses= new ArrayList (); 62 63 67 private ISourceLocator fLocator= null; 68 69 72 private String fMode; 73 74 77 private HashMap fAttributes; 78 79 84 private boolean fSuppressChange = true; 85 86 95 public Launch(ILaunchConfiguration launchConfiguration, String 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 107 private void addEventListener() { 108 DebugPlugin.getDefault().addDebugEventListener(this); 109 } 110 111 114 private void removeEventListener() { 115 DebugPlugin.getDefault().removeDebugEventListener(this); 116 } 117 118 121 public boolean canTerminate() { 122 List 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 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 142 public Object [] getChildren() { 143 ArrayList children = new ArrayList (getDebugTargets0()); 144 children.addAll(getProcesses0()); 145 return children.toArray(); 146 } 147 148 151 public IDebugTarget getDebugTarget() { 152 if (!getDebugTargets0().isEmpty()) { 153 return (IDebugTarget)getDebugTargets0().get(0); 154 } 155 return null; 156 } 157 158 161 public IProcess[] getProcesses() { 162 return (IProcess[])getProcesses0().toArray(new IProcess[getProcesses0().size()]); 163 } 164 165 171 protected List getProcesses0() { 172 return fProcesses; 173 } 174 175 178 public ISourceLocator getSourceLocator() { 179 return fLocator; 180 } 181 182 185 public void setSourceLocator(ISourceLocator sourceLocator) { 186 fLocator = sourceLocator; 187 } 188 189 192 public boolean isTerminated() { 193 if (getProcesses0().isEmpty() && getDebugTargets0().isEmpty()) { 194 return false; 195 } 196 197 Iterator 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 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 219 public void terminate() throws DebugException { 220 MultiStatus status= 221 new MultiStatus(DebugPlugin.getUniqueIdentifier(), DebugException.REQUEST_FAILED, DebugCoreMessages.Launch_terminate_failed, null); 222 223 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 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 271 public String getLaunchMode() { 272 return fMode; 273 } 274 275 278 public ILaunchConfiguration getLaunchConfiguration() { 279 return fConfiguration; 280 } 281 282 285 public void setAttribute(String key, String value) { 286 if (fAttributes == null) { 287 fAttributes = new HashMap (5); 288 } 289 fAttributes.put(key, value); 290 } 291 292 295 public String getAttribute(String key) { 296 if (fAttributes == null) { 297 return null; 298 } 299 return (String )fAttributes.get(key); 300 } 301 302 305 public IDebugTarget[] getDebugTargets() { 306 return (IDebugTarget[])fTargets.toArray(new IDebugTarget[fTargets.size()]); 307 } 308 309 315 protected List getDebugTargets0() { 316 return fTargets; 317 } 318 319 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 335 public void removeDebugTarget(IDebugTarget target) { 336 if (target != null) { 337 if (getDebugTargets0().remove(target)) { 338 fireChanged(); 339 } 340 } 341 } 342 343 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 359 public void removeProcess(IProcess process) { 360 if (process != null) { 361 if (getProcesses0().remove(process)) { 362 fireChanged(); 363 } 364 } 365 } 366 367 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 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 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 409 public boolean hasChildren() { 410 return getProcesses0().size() > 0 || (getDebugTargets0().size() > 0); 411 } 412 413 419 public boolean canDisconnect() { 420 List 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 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 441 public void disconnect() throws DebugException { 442 List 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 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 467 public boolean isDisconnected() { 468 List 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 targets = getDebugTargets0(); 478 for (int i = 0; i < targets.size(); i++) { 479 if ( !((IDebugTarget)targets.get(i)).isDisconnected() ) { 480 return false; 481 } 482 } 483 return hasChildren(); 485 } 486 487 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 503 protected ILaunchManager getLaunchManager() { 504 return DebugPlugin.getDefault().getLaunchManager(); 505 } 506 507 510 public void launchAdded(ILaunch launch) { 511 } 512 513 516 public void launchChanged(ILaunch launch) { 517 } 518 519 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 537 public void launchConfigurationChanged(ILaunchConfiguration configuration) {} 538 539 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 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 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 581 public Object getAdapter(Class adapter) { 582 if (adapter.equals(ILaunch.class)) { 583 return this; 584 } 585 if(adapter.equals(ILaunchConfiguration.class)) { 587 return getLaunchConfiguration(); 588 } 589 return super.getAdapter(adapter); 590 } 591 592 593 594 } 595 | Popular Tags |