KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > LaunchListener


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  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.launcher;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
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 JavaDoc managedLaunches;
44     // maximum log file size
45
public static final long MAX_FILE_LENGTH = 1024 * 1024;
46     // different ways to open the error log
47
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 JavaDoc();
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     /**
62      * @see org.eclipse.debug.core.ILaunchesListener#launchesRemoved(org.eclipse.debug.core.ILaunch)
63      */

64     public void launchRemoved(ILaunch launch) {
65         update(launch, true);
66     }
67
68     /**
69      * @see org.eclipse.debug.core.ILaunchesListener#launchesAdded(org.eclipse.debug.core.ILaunch)
70      */

71     public void launchAdded(ILaunch launch) {
72     }
73
74     /**
75      * @see org.eclipse.debug.core.ILaunchesListener#launchesChanged(org.eclipse.debug.core.ILaunch)
76      */

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     /**
117      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent)
118      */

119     public void handleDebugEvents(DebugEvent[] events) {
120         for (int i = 0; i < events.length; i++) {
121             DebugEvent event = events[i];
122             Object JavaDoc 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             // launch failed because the associated workspace is in use
144
if (returnValue == 15) {
145                 Display.getDefault().asyncExec(new Runnable JavaDoc() {
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             // launch failed for reasons printed to the log.
155
if (returnValue == 13) {
156                 Display.getDefault().asyncExec(new Runnable JavaDoc() {
157                     public void run() {
158                         try {
159                             File JavaDoc log = getMostRecentLogFile(launch);
160                             if (log != null && log.exists()) {
161                                 MessageDialog dialog = new MessageDialog(
162                                     PDEPlugin.getActiveWorkbenchShell(),
163                                     PDEUIMessages.Launcher_error_title,
164                                     null, // accept the default window icon
165
PDEUIMessages.Launcher_error_code13,
166                                     MessageDialog.ERROR,
167                                     new String JavaDoc[] {
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"); //$NON-NLS-1$
176
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 JavaDoc 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 JavaDoc getMostRecentLogFile(ILaunch launch) throws CoreException {
203         ILaunchConfiguration configuration = launch.getLaunchConfiguration();
204         File JavaDoc latest = null;
205         String JavaDoc workspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration);
206         if (workspace.length() > 0) {
207             latest = new File JavaDoc(workspace, ".metadata/.log"); //$NON-NLS-1$
208
if (!latest.exists())
209                 latest = null;
210         }
211         File JavaDoc configDir = LaunchConfigurationHelper.getConfigurationLocation(configuration);
212         File JavaDoc[] 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")) { //$NON-NLS-1$
217
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