KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > program > launchConfigurations > ProgramLaunchDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * Keith Seitz (keiths@redhat.com) - environment variables contribution (Bug 27243)
11  *******************************************************************************/

12 package org.eclipse.ui.externaltools.internal.program.launchConfigurations;
13
14
15 import java.io.File JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.ILaunch;
26 import org.eclipse.debug.core.ILaunchConfiguration;
27 import org.eclipse.debug.core.ILaunchConfigurationType;
28 import org.eclipse.debug.core.ILaunchManager;
29 import org.eclipse.debug.core.model.IProcess;
30 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
31 import org.eclipse.debug.ui.CommonTab;
32 import org.eclipse.debug.ui.RefreshTab;
33 import org.eclipse.jface.dialogs.MessageDialog;
34 import org.eclipse.osgi.util.NLS;
35 import org.eclipse.ui.IWindowListener;
36 import org.eclipse.ui.IWorkbenchWindow;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsUtil;
39 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
40
41 /**
42  * Launch delegate for a program.
43  */

44 public class ProgramLaunchDelegate extends LaunchConfigurationDelegate {
45
46     private static IWindowListener fWindowListener;
47
48     /**
49      * A window listener that warns the user about any running programs when
50      * the workbench closes. Programs are killed when the VM exits.
51      */

52     private class ProgramLaunchWindowListener implements IWindowListener {
53         public void windowActivated(IWorkbenchWindow window) {
54         }
55         public void windowDeactivated(IWorkbenchWindow window) {
56         }
57         public void windowClosed(IWorkbenchWindow window) {
58             IWorkbenchWindow windows[]= PlatformUI.getWorkbench().getWorkbenchWindows();
59             if (windows.length > 1) {
60                 // There are more windows still open.
61
return;
62             }
63             ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager();
64             ILaunchConfigurationType programType= manager.getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE);
65             if (programType == null) {
66                 return;
67             }
68             ILaunch launches[]= manager.getLaunches();
69             ILaunchConfigurationType configType;
70             ILaunchConfiguration config;
71             for (int i = 0; i < launches.length; i++) {
72                 try {
73                     config= launches[i].getLaunchConfiguration();
74                     if (config == null) {
75                         continue;
76                     }
77                     configType= config.getType();
78                 } catch (CoreException e) {
79                     continue;
80                 }
81                 if (configType.equals(programType)) {
82                     if (!launches[i].isTerminated()) {
83                         MessageDialog.openWarning(window.getShell(), ExternalToolsProgramMessages.ProgramLaunchDelegate_Workbench_Closing_1, ExternalToolsProgramMessages.ProgramLaunchDelegate_The_workbench_is_exiting);
84                         break;
85                     }
86                 }
87             }
88         }
89         public void windowOpened(IWorkbenchWindow window) {
90         }
91     }
92
93     /**
94      * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
95      */

96     public void launch(ILaunchConfiguration configuration, String JavaDoc mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
97         
98         if (monitor.isCanceled()) {
99             return;
100         }
101         
102         // resolve location
103
IPath location = ExternalToolsUtil.getLocation(configuration);
104         
105         if (monitor.isCanceled()) {
106             return;
107         }
108         
109         // resolve working directory
110
IPath workingDirectory = ExternalToolsUtil.getWorkingDirectory(configuration);
111         
112         if (monitor.isCanceled()) {
113             return;
114         }
115         
116         // resolve arguments
117
String JavaDoc[] arguments = ExternalToolsUtil.getArguments(configuration);
118         
119         if (monitor.isCanceled()) {
120             return;
121         }
122         
123         int cmdLineLength = 1;
124         if (arguments != null) {
125             cmdLineLength += arguments.length;
126         }
127         String JavaDoc[] cmdLine = new String JavaDoc[cmdLineLength];
128         cmdLine[0] = location.toOSString();
129         if (arguments != null) {
130             System.arraycopy(arguments, 0, cmdLine, 1, arguments.length);
131         }
132         
133         File JavaDoc workingDir = null;
134         if (workingDirectory != null) {
135             workingDir = workingDirectory.toFile();
136         }
137         
138         if (monitor.isCanceled()) {
139             return;
140         }
141         
142         String JavaDoc[] envp = DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
143         
144         if (monitor.isCanceled()) {
145             return;
146         }
147         
148         if (fWindowListener == null) {
149             fWindowListener= new ProgramLaunchWindowListener();
150             PlatformUI.getWorkbench().addWindowListener(fWindowListener);
151         }
152         Process JavaDoc p = DebugPlugin.exec(cmdLine, workingDir, envp);
153         IProcess process = null;
154         
155         // add process type to process attributes
156
Map JavaDoc processAttributes = new HashMap JavaDoc();
157         String JavaDoc programName = location.lastSegment();
158         String JavaDoc extension = location.getFileExtension();
159         if (extension != null) {
160             programName = programName.substring(0, programName.length() - (extension.length() + 1));
161         }
162         programName = programName.toLowerCase();
163         processAttributes.put(IProcess.ATTR_PROCESS_TYPE, programName);
164         
165         if (p != null) {
166             monitor.beginTask(NLS.bind(ExternalToolsProgramMessages.ProgramLaunchDelegate_3, new String JavaDoc[] {configuration.getName()}), IProgressMonitor.UNKNOWN);
167             process = DebugPlugin.newProcess(launch, p, location.toOSString(), processAttributes);
168             if (process == null) {
169                 p.destroy();
170                 throw new CoreException(new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, IExternalToolConstants.ERR_INTERNAL_ERROR, ExternalToolsProgramMessages.ProgramLaunchDelegate_4, null));
171             }
172             
173         }
174         process.setAttribute(IProcess.ATTR_CMDLINE, generateCommandLine(cmdLine));
175         
176         if (CommonTab.isLaunchInBackground(configuration)) {
177             // refresh resources after process finishes
178
if (RefreshTab.getRefreshScope(configuration) != null) {
179                 BackgroundResourceRefresher refresher = new BackgroundResourceRefresher(configuration, process);
180                 refresher.startBackgroundRefresh();
181             }
182         } else {
183             // wait for process to exit
184
while (!process.isTerminated()) {
185                 try {
186                     if (monitor.isCanceled()) {
187                         process.terminate();
188                         break;
189                     }
190                     Thread.sleep(50);
191                 } catch (InterruptedException JavaDoc e) {
192                 }
193             }
194             
195             // refresh resources
196
RefreshTab.refreshResources(configuration, monitor);
197         }
198     }
199     
200     private String JavaDoc generateCommandLine(String JavaDoc[] commandLine) {
201         if (commandLine.length < 1)
202             return ""; //$NON-NLS-1$
203
StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
204         for (int i= 0; i < commandLine.length; i++) {
205             buf.append(' ');
206             char[] characters= commandLine[i].toCharArray();
207             StringBuffer JavaDoc command= new StringBuffer JavaDoc();
208             boolean containsSpace= false;
209             for (int j = 0; j < characters.length; j++) {
210                 char character= characters[j];
211                 if (character == '\"') {
212                     command.append('\\');
213                 } else if (character == ' ') {
214                     containsSpace = true;
215                 }
216                 command.append(character);
217             }
218             if (containsSpace) {
219                 buf.append('\"');
220                 buf.append(command);
221                 buf.append('\"');
222             } else {
223                 buf.append(command);
224             }
225         }
226         return buf.toString();
227     }
228     
229 }
230
Popular Tags