1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.filesystem.EFS; 17 import org.eclipse.core.filesystem.IFileStore; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.NullProgressMonitor; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.debug.core.DebugEvent; 22 import org.eclipse.debug.core.DebugException; 23 import org.eclipse.debug.core.DebugPlugin; 24 import org.eclipse.debug.core.IDebugEventSetListener; 25 import org.eclipse.debug.core.ILaunch; 26 import org.eclipse.debug.core.ILaunchConfiguration; 27 import org.eclipse.debug.core.ILaunchListener; 28 import org.eclipse.debug.core.ILaunchManager; 29 import org.eclipse.debug.core.model.IProcess; 30 import org.eclipse.jface.dialogs.IDialogConstants; 31 import org.eclipse.jface.dialogs.MessageDialog; 32 import org.eclipse.pde.internal.runtime.logview.LogView; 33 import org.eclipse.pde.internal.ui.PDEPlugin; 34 import org.eclipse.pde.internal.ui.PDEUIMessages; 35 import org.eclipse.swt.widgets.Display; 36 import org.eclipse.ui.IWorkbenchPage; 37 import org.eclipse.ui.IWorkbenchWindow; 38 import org.eclipse.ui.PartInitException; 39 import org.eclipse.ui.ide.IDE; 40 41 42 public class LaunchListener implements ILaunchListener, IDebugEventSetListener { 43 private ArrayList managedLaunches; 44 public static final long MAX_FILE_LENGTH = 1024 * 1024; 46 public static final int OPEN_IN_ERROR_LOG_VIEW = 0; 48 public static final int OPEN_IN_SYSTEM_EDITOR = 1; 49 50 public LaunchListener() { 51 managedLaunches = new ArrayList (); 52 } 53 54 public void manage(ILaunch launch) { 55 if (managedLaunches.size() == 0) 56 hookListener(true); 57 if (!managedLaunches.contains(launch)) 58 managedLaunches.add(launch); 59 } 60 61 64 public void launchRemoved(ILaunch launch) { 65 update(launch, true); 66 } 67 68 71 public void launchAdded(ILaunch launch) { 72 } 73 74 77 public void launchChanged(ILaunch launch) { 78 } 79 80 private void update(ILaunch launch, boolean remove) { 81 if (managedLaunches.contains(launch)) { 82 if (remove || launch.isTerminated()) { 83 managedLaunches.remove(launch); 84 if (managedLaunches.size() == 0) { 85 hookListener(false); 86 } 87 } 88 } 89 } 90 91 private void hookListener(boolean add) { 92 DebugPlugin debugPlugin = DebugPlugin.getDefault(); 93 ILaunchManager launchManager = debugPlugin.getLaunchManager(); 94 if (add) { 95 launchManager.addLaunchListener(this); 96 debugPlugin.addDebugEventListener(this); 97 } else { 98 launchManager.removeLaunchListener(this); 99 debugPlugin.removeDebugEventListener(this); 100 } 101 } 102 103 private void doRestart(ILaunch launch) { 104 ILaunchConfiguration config = launch.getLaunchConfiguration(); 105 try { 106 config.launch(launch.getLaunchMode(), new NullProgressMonitor()); 107 } catch (CoreException e) { 108 PDEPlugin.logException(e); 109 } 110 } 111 112 public void shutdown() { 113 hookListener(false); 114 } 115 116 119 public void handleDebugEvents(DebugEvent[] events) { 120 for (int i = 0; i < events.length; i++) { 121 DebugEvent event = events[i]; 122 Object source = event.getSource(); 123 if (source instanceof IProcess && event.getKind() == DebugEvent.TERMINATE) { 124 IProcess process = (IProcess) source; 125 ILaunch launch = process.getLaunch(); 126 if (launch != null) { 127 try { 128 launchTerminated(launch, process.getExitValue()); 129 } catch (DebugException e) { 130 } 131 } 132 } 133 } 134 } 135 136 private void launchTerminated(final ILaunch launch, int returnValue) { 137 if (managedLaunches.contains(launch)) { 138 update(launch, true); 139 if (returnValue == 23) { 140 doRestart(launch); 141 return; 142 } 143 if (returnValue == 15) { 145 Display.getDefault().asyncExec(new Runnable () { 146 public void run() { 147 MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), 148 PDEUIMessages.Launcher_error_title, 149 PDEUIMessages.Launcher_error_code15); 150 } 151 }); 152 return; 153 } 154 if (returnValue == 13) { 156 Display.getDefault().asyncExec(new Runnable () { 157 public void run() { 158 try { 159 File log = getMostRecentLogFile(launch); 160 if (log != null && log.exists()) { 161 MessageDialog dialog = new MessageDialog( 162 PDEPlugin.getActiveWorkbenchShell(), 163 PDEUIMessages.Launcher_error_title, 164 null, PDEUIMessages.Launcher_error_code13, 166 MessageDialog.ERROR, 167 new String [] { 168 PDEUIMessages.Launcher_error_displayInLogView, 169 PDEUIMessages.Launcher_error_displayInSystemEditor, 170 IDialogConstants.NO_LABEL}, 171 OPEN_IN_ERROR_LOG_VIEW); 172 int dialog_value = dialog.open(); 173 if (dialog_value == OPEN_IN_ERROR_LOG_VIEW) { 174 LogView errlog = (LogView)PDEPlugin.getActivePage() 175 .showView("org.eclipse.pde.runtime.LogView"); errlog.handleImportPath(log.getAbsolutePath()); 177 errlog.sortByDateDescending(); 178 } else if (dialog_value == OPEN_IN_SYSTEM_EDITOR) { 179 openInEditor(log); 180 } 181 } 182 } catch (CoreException e) { 183 } 184 } 185 }); 186 } 187 } 188 } 189 190 private void openInEditor(File log) { 191 IFileStore fileStore= EFS.getLocalFileSystem().getStore(new Path(log.getAbsolutePath())); 192 if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) { 193 IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow(); 194 IWorkbenchPage page = ww.getActivePage(); 195 try { 196 IDE.openEditorOnFileStore(page, fileStore); 197 } catch (PartInitException e) { 198 } 199 } 200 } 201 202 private File getMostRecentLogFile(ILaunch launch) throws CoreException { 203 ILaunchConfiguration configuration = launch.getLaunchConfiguration(); 204 File latest = null; 205 String workspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration); 206 if (workspace.length() > 0) { 207 latest = new File (workspace, ".metadata/.log"); if (!latest.exists()) 209 latest = null; 210 } 211 File configDir = LaunchConfigurationHelper.getConfigurationLocation(configuration); 212 File [] children = configDir.listFiles(); 213 if (children != null) { 214 for (int i = 0; i < children.length; i++) { 215 if (!children[i].isDirectory() 216 && children[i].getName().endsWith(".log")) { if (latest == null 218 || latest.lastModified() < children[i].lastModified()) 219 latest = children[i]; 220 } 221 } 222 } 223 return latest; 224 } 225 226 } 227 | Popular Tags |