1 11 package org.eclipse.jdt.internal.debug.ui.snippeteditor; 12 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.net.URLEncoder ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.StringTokenizer ; 23 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.resources.IWorkspaceRoot; 27 import org.eclipse.core.resources.ResourcesPlugin; 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.FileLocator; 30 import org.eclipse.core.runtime.IPath; 31 import org.eclipse.core.runtime.Path; 32 import org.eclipse.core.runtime.QualifiedName; 33 import org.eclipse.debug.core.DebugEvent; 34 import org.eclipse.debug.core.DebugPlugin; 35 import org.eclipse.debug.core.IDebugEventSetListener; 36 import org.eclipse.debug.core.ILaunch; 37 import org.eclipse.debug.core.ILaunchConfiguration; 38 import org.eclipse.debug.core.ILaunchConfigurationType; 39 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 40 import org.eclipse.debug.core.ILaunchManager; 41 import org.eclipse.debug.core.model.IBreakpoint; 42 import org.eclipse.debug.core.model.IDebugTarget; 43 import org.eclipse.debug.ui.IDebugUIConstants; 44 import org.eclipse.jdt.core.IJavaProject; 45 import org.eclipse.jdt.core.JavaCore; 46 import org.eclipse.jdt.debug.core.IJavaLineBreakpoint; 47 import org.eclipse.jdt.debug.core.JDIDebugModel; 48 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 49 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 50 import org.eclipse.jdt.internal.launching.JavaMigrationDelegate; 51 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 52 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 53 import org.eclipse.jdt.launching.IVMInstall; 54 import org.eclipse.jdt.launching.JavaRuntime; 55 import org.eclipse.jface.dialogs.MessageDialog; 56 57 import com.ibm.icu.text.MessageFormat; 58 59 62 63 public class ScrapbookLauncher implements IDebugEventSetListener { 64 65 public static final String SCRAPBOOK_LAUNCH = IJavaDebugUIConstants.PLUGIN_ID + ".scrapbook_launch"; 67 public static final String SCRAPBOOK_FILE_PATH = IJavaDebugUIConstants.PLUGIN_ID + ".scrapbook_file_path"; 69 74 public static final QualifiedName SNIPPET_EDITOR_LAUNCH_CONFIG_HANDLE_MEMENTO = new QualifiedName(IJavaDebugUIConstants.PLUGIN_ID, "snippet_editor_launch_config"); 76 private IJavaLineBreakpoint fMagicBreakpoint; 77 78 private HashMap fScrapbookToVMs = new HashMap (10); 79 private HashMap fVMsToBreakpoints = new HashMap (10); 80 private HashMap fVMsToScrapbooks = new HashMap (10); 81 82 private static ScrapbookLauncher fgDefault = null; 83 84 private ScrapbookLauncher() { 85 } 87 88 public static ScrapbookLauncher getDefault() { 89 if (fgDefault == null) { 90 fgDefault = new ScrapbookLauncher(); 91 } 92 return fgDefault; 93 } 94 95 102 protected ILaunch launch(IFile page) { 103 104 cleanupLaunchConfigurations(); 106 107 if (!page.getFileExtension().equals("jpage")) { showNoPageDialog(); 109 return null; 110 } 111 112 IDebugTarget vm = getDebugTarget(page); 113 if (vm != null) { 114 return vm.getLaunch(); 116 } 117 118 IJavaProject javaProject= JavaCore.create(page.getProject()); 119 120 URL jarURL = null; 121 try { 122 jarURL = JDIDebugUIPlugin.getDefault().getBundle().getEntry("snippetsupport.jar"); jarURL = FileLocator.toFileURL(jarURL); 124 } catch (MalformedURLException e) { 125 JDIDebugUIPlugin.errorDialog(SnippetMessages.getString("ScrapbookLauncher.Unable_to_launch_scrapbook_VM_6"), e); return null; 127 } catch (IOException e) { 128 JDIDebugUIPlugin.errorDialog(SnippetMessages.getString("ScrapbookLauncher.Unable_to_launch_scrapbook_VM_6"), e); return null; 130 } 131 132 List cp = new ArrayList (3); 133 IRuntimeClasspathEntry supportEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(jarURL.getFile())); 134 cp.add(supportEntry); 135 try { 137 IRuntimeClasspathEntry[] entries = JavaRuntime.computeUnresolvedRuntimeClasspath(javaProject); 138 for (int i = 0; i < entries.length; i++) { 139 if (entries[i].getClasspathProperty() != IRuntimeClasspathEntry.USER_CLASSES) { 140 cp.add(entries[i]); 141 } 142 } 143 IRuntimeClasspathEntry[] classPath = (IRuntimeClasspathEntry[])cp.toArray(new IRuntimeClasspathEntry[cp.size()]); 144 145 return doLaunch(javaProject, page, classPath); 146 } catch (CoreException e) { 147 JDIDebugUIPlugin.statusDialog(SnippetMessages.getString("ScrapbookLauncher.Unable_to_launch_scrapbook_VM_6"), e.getStatus()); } 149 return null; 150 } 151 152 private ILaunch doLaunch(IJavaProject p, IFile page, IRuntimeClasspathEntry[] classPath) { 153 try { 154 if (fVMsToScrapbooks.isEmpty()) { 155 DebugPlugin.getDefault().addDebugEventListener(this); 157 } 158 ILaunchConfiguration config = null; 159 ILaunchConfigurationWorkingCopy wc = null; 160 try { 161 config = getLaunchConfigurationTemplate(page); 162 if (config != null) { 163 wc = config.getWorkingCopy(); 164 } 165 } catch (CoreException e) { 166 config = null; 167 JDIDebugUIPlugin.statusDialog(SnippetMessages.getString("ScrapbookLauncher.Unable_to_retrieve_settings"), e.getStatus()); } 169 170 if (config == null) { 171 config = createLaunchConfigurationTemplate(page); 172 wc = config.getWorkingCopy(); 173 } 174 175 IPath outputLocation = p.getProject().getWorkingLocation(JDIDebugUIPlugin.getUniqueIdentifier()); 176 File f = outputLocation.toFile(); 177 URL u = null; 178 try { 179 u = getEncodedURL(f); 180 } catch (MalformedURLException e) { 181 JDIDebugUIPlugin.errorDialog(SnippetMessages.getString("ScrapbookLauncher.Unable_to_launch_scrapbook_VM_6"),e); return null; 183 } 184 String [] defaultClasspath = JavaRuntime.computeDefaultRuntimeClassPath(p); 185 String [] urls = new String [defaultClasspath.length + 1]; 186 urls[0] = u.toExternalForm(); 187 for (int i = 0; i < defaultClasspath.length; i++) { 188 f = new File (defaultClasspath[i]); 189 try { 190 urls[i + 1] = getEncodedURL(f).toExternalForm(); 191 } catch (MalformedURLException e) { 192 JDIDebugUIPlugin.errorDialog(SnippetMessages.getString("ScrapbookLauncher.Unable_to_launch_scrapbook_VM_6"), e); return null; 194 } 195 } 196 197 List classpathList= new ArrayList (classPath.length); 199 for (int i = 0; i < classPath.length; i++) { 200 classpathList.add(classPath[i].getMemento()); 201 } 202 203 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false); 204 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpathList); 205 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, p.getElementName()); 206 if (wc.getAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, (String )null) == null) { 207 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, "org.eclipse.jdt.debug.ui.scrapbookSourcepathProvider"); } 209 210 StringBuffer urlsString = new StringBuffer (); 211 for (int i = 0; i < urls.length; i++) { 212 urlsString.append(' '); 213 urlsString.append(urls[i]); 214 } 215 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, urlsString.toString()); 216 wc.setAttribute(SCRAPBOOK_LAUNCH, SCRAPBOOK_LAUNCH); 217 218 config = wc.doSave(); 219 220 ILaunch launch = config.launch(ILaunchManager.DEBUG_MODE, null); 221 if (launch != null) { 222 IDebugTarget dt = launch.getDebugTarget(); 223 IBreakpoint magicBreakpoint = createMagicBreakpoint("org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookMain"); fScrapbookToVMs.put(page, dt); 225 fVMsToScrapbooks.put(dt, page); 226 fVMsToBreakpoints.put(dt, magicBreakpoint); 227 dt.breakpointAdded(magicBreakpoint); 228 launch.setAttribute(SCRAPBOOK_LAUNCH, SCRAPBOOK_LAUNCH); 229 return launch; 230 } 231 } catch (CoreException e) { 232 JDIDebugUIPlugin.statusDialog(SnippetMessages.getString("ScrapbookLauncher.Unable_to_launch_scrapbook_VM_6"), e.getStatus()); } 234 return null; 235 } 236 237 240 IBreakpoint createMagicBreakpoint(String typeName) throws CoreException{ 241 fMagicBreakpoint= JDIDebugModel.createLineBreakpoint(ResourcesPlugin.getWorkspace().getRoot(), typeName, 59, -1, -1, 0, false, null); 243 fMagicBreakpoint.setPersisted(false); 244 return fMagicBreakpoint; 245 } 246 247 250 public void handleDebugEvents(DebugEvent[] events) { 251 for (int i = 0; i < events.length; i++) { 252 DebugEvent event = events[i]; 253 if (event.getSource() instanceof IDebugTarget && event.getKind() == DebugEvent.TERMINATE) { 254 cleanup((IDebugTarget)event.getSource()); 255 } 256 } 257 } 258 259 266 public IDebugTarget getDebugTarget(IFile page) { 267 return (IDebugTarget)fScrapbookToVMs.get(page); 268 } 269 270 279 public IBreakpoint getMagicBreakpoint(IDebugTarget target) { 280 return (IBreakpoint)fVMsToBreakpoints.get(target); 281 } 282 283 protected void showNoPageDialog() { 284 String title= SnippetMessages.getString("ScrapbookLauncher.error.title"); String msg= SnippetMessages.getString("ScrapbookLauncher.error.pagenotfound"); MessageDialog.openError(JDIDebugUIPlugin.getActiveWorkbenchShell(),title, msg); 287 } 288 289 protected void cleanup(IDebugTarget target) { 290 Object page = fVMsToScrapbooks.get(target); 291 if (page != null) { 292 fVMsToScrapbooks.remove(target); 293 fScrapbookToVMs.remove(page); 294 fVMsToBreakpoints.remove(target); 295 ILaunch launch = target.getLaunch(); 296 if (launch != null) { 297 getLaunchManager().removeLaunch(launch); 298 } 299 if (fVMsToScrapbooks.isEmpty()) { 300 DebugPlugin.getDefault().removeDebugEventListener(this); 302 } 303 } 304 } 305 306 protected URL getEncodedURL(File file) throws MalformedURLException { 307 String urlDelimiter= "/"; String unencoded= file.toURL().toExternalForm(); 311 StringBuffer encoded= new StringBuffer (); 312 StringTokenizer tokenizer= new StringTokenizer (unencoded, urlDelimiter); 313 314 encoded.append(tokenizer.nextToken()); encoded.append(urlDelimiter); 316 encoded.append(tokenizer.nextToken()); 318 while (tokenizer.hasMoreElements()) { 319 encoded.append(urlDelimiter); 320 String token= tokenizer.nextToken(); 321 encoded.append(URLEncoder.encode(token)); 322 } 323 if (file.isDirectory()) { 324 encoded.append(urlDelimiter); 325 } 326 return new URL (encoded.toString()); 327 } 328 329 334 public static ILaunchConfiguration getLaunchConfigurationTemplate(IFile file) throws CoreException { 335 String memento = getLaunchConfigMemento(file); 336 if (memento != null) { 337 return getLaunchManager().getLaunchConfiguration(memento); 338 } 339 return null; 340 } 341 342 345 public static ILaunchConfiguration createLaunchConfigurationTemplate(IFile page) throws CoreException { 346 ILaunchConfigurationType lcType = getLaunchManager().getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION); 347 String name = MessageFormat.format(SnippetMessages.getString("ScrapbookLauncher.17"), new String []{page.getName()}); ILaunchConfigurationWorkingCopy wc = lcType.newInstance(null, name); 349 wc.setAttribute(IDebugUIConstants.ATTR_PRIVATE, true); 350 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookMain"); wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, page.getProject().getName()); 352 wc.setAttribute(SCRAPBOOK_LAUNCH, SCRAPBOOK_LAUNCH); 353 wc.setAttribute(SCRAPBOOK_FILE_PATH, page.getFullPath().toString()); 354 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, "org.eclipse.jdt.debug.ui.scrapbookSourcepathProvider"); JavaMigrationDelegate.updateResourceMapping(wc); 356 ILaunchConfiguration config = wc.doSave(); 357 setLaunchConfigMemento(page, config.getMemento()); 358 return config; 359 } 360 361 365 private static String getLaunchConfigMemento(IFile file) { 366 try { 367 return file.getPersistentProperty(SNIPPET_EDITOR_LAUNCH_CONFIG_HANDLE_MEMENTO); 368 } catch (CoreException e) { 369 JDIDebugUIPlugin.log(e); 370 } 371 return null; 372 } 373 374 378 protected static void setLaunchConfigMemento(IFile file, String memento) { 379 try { 380 file.setPersistentProperty(SNIPPET_EDITOR_LAUNCH_CONFIG_HANDLE_MEMENTO, memento); 381 } catch (CoreException e) { 382 JDIDebugUIPlugin.log(e); 383 } 384 } 385 386 389 protected static ILaunchManager getLaunchManager() { 390 return DebugPlugin.getDefault().getLaunchManager(); 391 } 392 393 399 public static String getWorkingDirectoryAttribute(IFile file) throws CoreException { 400 ILaunchConfiguration config = getLaunchConfigurationTemplate(file); 401 if (config != null) { 402 return config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String )null); 403 } 404 return null; 405 } 406 407 413 public static String getVMArgsAttribute(IFile file) throws CoreException { 414 ILaunchConfiguration config = getLaunchConfigurationTemplate(file); 415 if (config != null) { 416 return config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, (String )null); 417 } 418 return null; 419 } 420 421 426 public static IVMInstall getVMInstall(IFile file) throws CoreException { 427 ILaunchConfiguration config = getLaunchConfigurationTemplate(file); 428 if (config == null) { 429 IJavaProject pro = JavaCore.create(file.getProject()); 430 return JavaRuntime.getVMInstall(pro); 431 } 432 return JavaRuntime.computeVMInstall(config); 433 } 434 435 441 public void cleanupLaunchConfigurations() { 442 try { 443 ILaunchConfigurationType lcType = getLaunchManager().getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION); 444 ILaunchConfiguration[] configs = getLaunchManager().getLaunchConfigurations(lcType); 445 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 446 for (int i = 0; i < configs.length; i++) { 447 String path = configs[i].getAttribute(SCRAPBOOK_FILE_PATH, (String )null); 448 if (path != null) { 449 IPath pagePath = new Path(path); 450 IResource res = root.findMember(pagePath); 451 if (res == null) { 452 configs[i].delete(); 454 } 455 } 456 } 457 } catch (CoreException e) { 458 JDIDebugUIPlugin.log(e); 460 } 461 } 462 } 463 | Popular Tags |