1 11 package org.eclipse.debug.internal.core; 12 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.File ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.UnsupportedEncodingException ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.transform.TransformerException ; 27 28 import org.eclipse.core.resources.IContainer; 29 import org.eclipse.core.resources.IFile; 30 import org.eclipse.core.resources.IResource; 31 import org.eclipse.core.resources.IWorkspaceRunnable; 32 import org.eclipse.core.resources.ResourcesPlugin; 33 import org.eclipse.core.runtime.CoreException; 34 import org.eclipse.core.runtime.IPath; 35 import org.eclipse.core.runtime.IProgressMonitor; 36 import org.eclipse.core.runtime.IStatus; 37 import org.eclipse.core.runtime.NullProgressMonitor; 38 import org.eclipse.core.runtime.Status; 39 import org.eclipse.core.runtime.SubProgressMonitor; 40 import org.eclipse.debug.core.DebugException; 41 import org.eclipse.debug.core.DebugPlugin; 42 import org.eclipse.debug.core.ILaunchConfiguration; 43 import org.eclipse.debug.core.ILaunchConfigurationType; 44 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 45 46 import com.ibm.icu.text.MessageFormat; 47 48 51 public class LaunchConfigurationWorkingCopy extends LaunchConfiguration implements ILaunchConfigurationWorkingCopy { 52 53 57 private LaunchConfiguration fOriginal; 58 59 63 private LaunchConfigurationWorkingCopy fParent = null; 64 65 68 private LaunchConfigurationInfo fInfo; 69 70 74 private boolean fDirty = false; 75 76 79 private String fName; 80 81 84 private boolean fRenamed = false; 85 86 89 private boolean fSuppressChange = true; 90 91 95 private IContainer fContainer; 96 97 106 protected LaunchConfigurationWorkingCopy(LaunchConfiguration original) throws CoreException { 107 super(original.getLocation()); 108 setName(original.getName()); 109 copyFrom(original); 110 setOriginal(original); 111 fSuppressChange = false; 112 } 113 114 122 protected LaunchConfigurationWorkingCopy(LaunchConfigurationWorkingCopy parent) throws CoreException { 123 super(parent.getLocation()); 124 setName(parent.getName()); 125 copyFrom(parent); 126 setOriginal((LaunchConfiguration) parent.getOriginal()); 127 fParent = parent; 128 fSuppressChange = false; 129 } 130 131 142 protected LaunchConfigurationWorkingCopy(LaunchConfiguration original, String name) throws CoreException { 143 super(original.getLocation()); 144 copyFrom(original); 145 setName(name); 146 fSuppressChange = false; 147 } 148 149 158 protected LaunchConfigurationWorkingCopy(IContainer container, String name, ILaunchConfigurationType type) { 159 super((IPath)null); 160 setName(name); 161 setInfo(new LaunchConfigurationInfo()); 162 getInfo().setType(type); 163 setContainer(container); 164 fSuppressChange = false; 165 } 166 167 170 public boolean isDirty() { 171 return fDirty; 172 } 173 174 177 public synchronized ILaunchConfiguration doSave() throws CoreException { 178 return doSave(new NullProgressMonitor()); 179 } 180 181 190 public synchronized ILaunchConfiguration doSave(IProgressMonitor monitor) throws CoreException { 191 if (getParent() != null) { 192 LaunchConfigurationWorkingCopy wc = (LaunchConfigurationWorkingCopy) getParent(); 194 if(isMoved()) { 195 wc.rename(getName()); 196 wc.setContainer(getContainer()); 197 } 198 wc.setAttributes(getInfo().getAttributes()); 199 return wc; 200 } 201 else { 202 boolean useRunnable= true; 203 if (isLocal()) { 204 if (isMoved()) { 205 useRunnable= !isNew() && !getOriginal().isLocal(); 208 } else { 209 useRunnable= false; 210 } 211 } 212 if (useRunnable) { 213 IWorkspaceRunnable wr = new IWorkspaceRunnable() { 214 public void run(IProgressMonitor pm) throws CoreException { 215 doSave0(pm); 216 } 217 }; 218 ResourcesPlugin.getWorkspace().run(wr, null, 0, monitor); 219 } else { 220 doSave0(monitor); 222 } 223 getLaunchManager().setMovedFromTo(null, null); 224 } 225 return new LaunchConfiguration(getLocation()); 226 } 227 228 232 private void doSave0(IProgressMonitor monitor) throws CoreException { 233 boolean moved = (!isNew() && isMoved()); 235 if (moved) { 236 ILaunchConfiguration to = new LaunchConfiguration(getLocation()); 237 ILaunchConfiguration from = getOriginal(); 238 getLaunchManager().setMovedFromTo(from, to); 239 } 240 if (moved) { 243 getOriginal().delete(); 244 } 245 if(monitor == null) { 247 monitor = new NullProgressMonitor(); 248 } 249 monitor.beginTask(MessageFormat.format(DebugCoreMessages.LaunchConfigurationWorkingCopy_0, new String [] {getName()}), 2); 250 writeNewFile(monitor); 251 monitor.done(); 252 fDirty = false; 253 } 254 255 260 protected void writeNewFile(IProgressMonitor monitor) throws CoreException { 261 String xml = null; 262 Exception e= null; 263 try { 264 xml = getInfo().getAsXML(); 265 } catch (IOException ioe) { 266 e= ioe; 267 } catch (ParserConfigurationException pce) { 268 e= pce; 269 } catch (TransformerException te) { 270 e= te; 271 } 272 if (e != null) { 273 throw new DebugException( 274 new Status( 275 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 276 DebugException.REQUEST_FAILED, MessageFormat.format(DebugCoreMessages.LaunchConfigurationWorkingCopy__0__occurred_generating_launch_configuration_XML__1, new String []{e.toString()}), null 277 ) 278 ); 279 } 280 281 if (isLocal()) { 282 try { 284 boolean added = false; 285 monitor.subTask(DebugCoreMessages.LaunchConfigurationWorkingCopy_1); 286 File file = getLocation().toFile(); 287 File dir = getLocation().removeLastSegments(1).toFile(); 288 dir.mkdirs(); 289 if (!file.exists()) { 290 added = true; 291 file.createNewFile(); 292 monitor.worked(1); 293 } 294 FileOutputStream stream = new FileOutputStream (file); 295 stream.write(xml.getBytes("UTF8")); stream.close(); 297 298 if (added) { 299 getLaunchManager().launchConfigurationAdded(new LaunchConfiguration(getLocation())); 300 } else { 301 getLaunchManager().launchConfigurationChanged(new LaunchConfiguration(getLocation())); 302 } 303 monitor.worked(1); 305 } catch (IOException ie) { 306 monitor.setCanceled(true); 307 throw new DebugException( 308 new Status( 309 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 310 DebugException.REQUEST_FAILED, MessageFormat.format(DebugCoreMessages.LaunchConfigurationWorkingCopy__0__occurred_generating_launch_configuration_XML__1, new String []{ie.toString()}), null 311 ) 312 ); 313 } 314 } else { 315 IFile file = getFile(); 317 if (file == null) { 318 monitor.setCanceled(true); 319 throw new DebugException( 320 new Status( 321 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 322 DebugException.REQUEST_FAILED, DebugCoreMessages.LaunchConfigurationWorkingCopy_5, null 323 )); 324 } 325 IContainer dir = file.getParent(); 326 if (!dir.exists()) { 327 monitor.setCanceled(true); 328 throw new DebugException( 329 new Status( 330 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 331 DebugException.REQUEST_FAILED, DebugCoreMessages.LaunchConfigurationWorkingCopy_Specified_container_for_launch_configuration_does_not_exist_2, null 332 ) 333 ); 334 } 335 ByteArrayInputStream stream = null; 336 try { 337 stream = new ByteArrayInputStream (xml.getBytes("UTF8")); } catch (UnsupportedEncodingException ue) { 339 monitor.setCanceled(true); 340 throw new DebugException( 341 new Status( 342 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 343 DebugException.REQUEST_FAILED, DebugCoreMessages.LaunchConfigurationWorkingCopy_5, ue 344 )); 345 } 346 SubProgressMonitor spm = null; 347 if (!file.exists()) { 348 spm = new SubProgressMonitor(monitor, 1); 350 spm.setTaskName(MessageFormat.format(DebugCoreMessages.LaunchConfigurationWorkingCopy_2, new String [] {getName()})); 351 file.create(stream, false, spm); 352 } else { 353 if (file.isReadOnly()) { 355 IStatus status = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] {file}, null); 356 if (!status.isOK()) { 357 monitor.setCanceled(true); 358 throw new CoreException(status); 359 } 360 } 361 spm = new SubProgressMonitor(monitor, 1); 363 spm.setTaskName(MessageFormat.format(DebugCoreMessages.LaunchConfigurationWorkingCopy_3, new String [] {getName()})); 364 file.setContents(stream, true, false, spm); 365 } 366 } 367 if(monitor.isCanceled()) { 368 return; 369 } 370 } 371 372 375 public void setAttribute(String attributeName, int value) { 376 getInfo().setAttribute(attributeName, new Integer (value)); 377 setDirty(); 378 } 379 380 383 public void setAttribute(String attributeName, String value) { 384 getInfo().setAttribute(attributeName, value); 385 setDirty(); 386 } 387 388 391 public void setAttribute(String attributeName, boolean value) { 392 getInfo().setAttribute(attributeName, Boolean.valueOf(value)); 393 setDirty(); 394 } 395 396 399 public void setAttribute(String attributeName, List value) { 400 getInfo().setAttribute(attributeName, value); 401 setDirty(); 402 } 403 404 407 public void setAttribute(String attributeName, Map value) { 408 getInfo().setAttribute(attributeName, value); 409 setDirty(); 410 } 411 412 415 public ILaunchConfiguration getOriginal() { 416 ILaunchConfiguration config = fOriginal; 417 ILaunchConfigurationWorkingCopy parent = fParent; 418 while(parent != null) { 419 config = parent.getOriginal(); 420 parent = parent.getParent(); 421 } 422 return config; 423 } 424 425 428 public ILaunchConfigurationWorkingCopy getParent() { 429 return fParent; 430 } 431 432 444 private void copyFrom(LaunchConfiguration original) throws CoreException { 445 LaunchConfigurationInfo info = original.getInfo(); 446 setInfo(info.getCopy()); 447 setContainer(original.getContainer()); 448 fDirty = false; 449 } 450 451 458 private void setOriginal(LaunchConfiguration original) { 459 fOriginal = original; 460 } 461 462 468 protected void setInfo(LaunchConfigurationInfo info) { 469 fInfo = info; 470 } 471 472 475 public boolean isWorkingCopy() { 476 return true; 477 } 478 479 485 protected LaunchConfigurationInfo getInfo() { 486 return fInfo; 487 } 488 489 494 private void setDirty() { 495 fDirty = true; 496 if (!suppressChangeNotification()) { 497 getLaunchManager().getConfigurationNotifier().notify(this, LaunchManager.CHANGED); 498 } 499 } 500 501 504 public void setModes(Set modes) { 505 getInfo().setAttribute(ATTR_LAUNCH_MODES, (modes.size() > 0 ? modes : null)); 506 setDirty(); 507 } 508 509 512 public void addModes(Set modes) { 513 try { 514 Set opts = getModes(); 515 if(opts.addAll(modes)) { 516 getInfo().setAttribute(ATTR_LAUNCH_MODES, opts); 517 setDirty(); 518 } 519 } 520 catch (CoreException e) { 521 DebugPlugin.log(e); 522 } 523 } 524 525 528 public void removeModes(Set options) { 529 try { 530 Set opts = getModes(); 531 if(opts.removeAll(options)) { 532 getInfo().setAttribute(ATTR_LAUNCH_MODES, (opts.size() < 1 ? null : opts)); 533 setDirty(); 534 } 535 } 536 catch (CoreException e) { 537 DebugPlugin.log(e); 538 } 539 } 540 541 544 public void rename(String name) { 545 if (!getName().equals(name)) { 546 setName(name); 547 fRenamed = isNew() || !(getOriginal().getName().equals(name)); 548 } 549 } 550 551 556 private void setName(String name) { 557 fName = name; 558 setDirty(); 559 } 560 561 564 public String getName() { 565 return fName; 566 } 567 568 571 public boolean isLocal() { 572 return getContainer() == null; 573 } 574 575 581 public IPath getLocation() { 582 if (isMoved()) { 583 IPath path = null; 584 if (isLocal()) { 585 path = LaunchManager.LOCAL_LAUNCH_CONFIGURATION_CONTAINER_PATH; 586 } else { 587 path = getContainer().getLocation(); 588 } 589 path = path.append(getName() + "." + LAUNCH_CONFIGURATION_FILE_EXTENSION); return path; 591 } 592 return getOriginal().getLocation(); 593 } 594 595 602 protected boolean isNew() { 603 return getOriginal() == null; 604 } 605 606 613 protected boolean isMoved() { 614 if (isNew() || fRenamed) { 615 return true; 616 } 617 IContainer newContainer = getContainer(); 618 IContainer originalContainer = ((LaunchConfiguration)getOriginal()).getContainer(); 619 if (newContainer == originalContainer) { 620 return false; 621 } 622 if (newContainer == null) { 623 return !originalContainer.equals(newContainer); 624 } 625 return !newContainer.equals(originalContainer); 626 } 627 628 633 public String getMemento() { 634 return null; 635 } 636 637 641 protected boolean suppressChangeNotification() { 642 return fSuppressChange; 643 } 644 645 648 public void setContainer(IContainer container) { 649 if (container == fContainer) { 650 return; 651 } 652 if (container != null) { 653 if (container.equals(fContainer)) { 654 return; 655 } 656 } else { 657 if (fContainer.equals(container)) { 658 return; 659 } 660 } 661 fContainer = container; 662 setDirty(); 663 } 664 665 674 protected IContainer getContainer() { 675 return fContainer; 676 } 677 678 681 public void setAttributes(Map attributes) { 682 getInfo().setAttributes(attributes); 683 setDirty(); 684 } 685 686 689 public void setMappedResources(IResource[] resources) { 690 ArrayList paths = null; 691 ArrayList types = null; 692 if(resources != null && resources.length > 0) { 693 paths = new ArrayList (resources.length); 694 types = new ArrayList (resources.length); 695 for (int i = 0; i < resources.length; i++) { 696 IResource resource = resources[i]; 697 if(resource != null) { 698 paths.add(resource.getFullPath().toPortableString()); 699 types.add(new Integer (resource.getType()).toString()); 700 } 701 } 702 } 703 setAttribute(LaunchConfiguration.ATTR_MAPPED_RESOURCE_PATHS, paths); 704 setAttribute(LaunchConfiguration.ATTR_MAPPED_RESOURCE_TYPES, types); 705 } 706 707 710 public void setPreferredLaunchDelegate(Set modes, String delegateId) { 711 if(modes != null) { 712 try { 713 Map delegates = getAttribute(LaunchConfiguration.ATTR_PREFERRED_LAUNCHERS, (Map )null); 714 Map map = new HashMap (); 716 if(delegates != null) { 717 map.putAll(delegates); 718 } 719 if(delegateId == null) { 720 map.remove(modes.toString()); 721 } 722 else { 723 map.put(modes.toString(), delegateId); 724 } 725 setAttribute(LaunchConfiguration.ATTR_PREFERRED_LAUNCHERS, map); 726 } 727 catch (CoreException ce) {DebugPlugin.log(ce);} 728 } 729 } 730 731 735 public ILaunchConfigurationWorkingCopy getWorkingCopy() throws CoreException { 736 return new LaunchConfigurationWorkingCopy(this); 737 } 738 } 739 740 | Popular Tags |