1
2
3
4
5 package org.apache.commons.jelly.eclipse.ui;
6
7 import java.io.File;
8 import java.text.MessageFormat;
9
10 import org.eclipse.core.resources.IFile;
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.resources.ResourcesPlugin;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.Path;
17 import org.eclipse.core.variables.VariablesPlugin;
18 import org.eclipse.debug.core.ILaunch;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
21 import org.eclipse.jdt.launching.IVMInstall;
22 import org.eclipse.jdt.launching.IVMRunner;
23 import org.eclipse.jdt.launching.VMRunnerConfiguration;
24
25 /***
26 * <p>Standard Eclipse Launch Configuration Delegate class, that's installed by
27 * the plugin.</p>
28 * <p>Since Jelly is a Java application, the launch process for Jelly is simply the
29 * launch of a Java application. Therefore the launch configuration for Jelly contains
30 * all attributes, a Java application has in the Eclipse workbench. The only additional
31 * attributes are the Jelly script file path and the output file path of the Jelly script.
32 * </p>
33 * @author Jürgen Mayrbäurl
34 * @since 0.9
35 * @see JellyLaunchConfigurationConstants
36 */
37 public class JellyLaunchConfigurationDelegate extends
38 AbstractJavaLaunchConfigurationDelegate
39 {
40
41
42
43
44
45 public void launch(ILaunchConfiguration configuration, String mode,
46 ILaunch launch, IProgressMonitor monitor) throws CoreException
47 {
48
49 IVMInstall vm = verifyVMInstall(configuration);
50 IVMRunner runner = vm.getVMRunner(mode);
51
52
53 VMRunnerConfiguration runConfig = new VMRunnerConfiguration(
54 JellyLauncherPlugin.getResourceString("jelly.launchMainClass"),
55 this.getClasspath(configuration));
56
57
58 String argsString = this.getProgramArguments(configuration);
59
60
61 String scriptFilePath = this.getScriptFile(configuration).getAbsolutePath();
62
63
64 String outConfig = null;
65 File outputFile = this.getOutputFile(configuration);
66 if(outputFile != null) outConfig = outputFile.getAbsolutePath();
67
68
69 if( (argsString != null) || (outConfig != null))
70 {
71 if(argsString == null)
72 {
73
74 runConfig.setProgramArguments(new String[] {
75 JellyLauncherPlugin.getResourceString("jelly.cmdLine.arg.script"),
76 "\"" + scriptFilePath + "\"",
77 JellyLauncherPlugin.getResourceString("jelly.cmdLine.arg.output"),
78 "\"" + outConfig + "\"" });
79 } else {
80
81 runConfig.setProgramArguments(new String[] {
82 JellyLauncherPlugin.getResourceString("jelly.cmdLine.arg.script"),
83 "\"" + scriptFilePath + "\"",
84 JellyLauncherPlugin.getResourceString("jelly.cmdLine.arg.output"),
85 "\"" + outConfig + "\"",
86 argsString });
87 }
88 } else {
89
90 runConfig.setProgramArguments(new String[] {scriptFilePath});
91 }
92
93
94 getClasspath(configuration);
95 String [] bootpath = getBootpath(configuration);
96 runConfig.setBootClassPath(bootpath);
97
98
99 runConfig.setWorkingDirectory(this.getWorkingDirectory(configuration).getAbsolutePath());
100
101
102 runner.run(runConfig, launch, monitor);
103 }
104
105
106
107 /***
108 * Gets and verifies the file path of the Jelly script file from the launch configuration
109 * @param configuration Eclipse launch configuration object
110 * @return Jelly script file
111 * @throws CoreException from Eclipse platform
112 * @see verifyScriptFile
113 */
114 public File getScriptFile(ILaunchConfiguration configuration) throws CoreException
115 {
116
117 return verifyScriptFile(configuration);
118 }
119
120 /***
121 * Gets and verifies the file path of the output file of the Jelly script from the launch configuration
122 * @param configuration Eclipse launch configuration object
123 * @return output file for the Jelly script
124 * @throws CoreException from Eclipse platform
125 * @see verifyOutputFile
126 */
127 public File getOutputFile(ILaunchConfiguration configuration) throws CoreException
128 {
129
130 return verifyOutputFile(configuration);
131 }
132
133 /***
134 * Reads the file path of the Jelly script from the launch configuration and
135 * verifies that the path exists and is the path of a file. If no Jelly script
136 * file is specified in the launch configuration or if the script file doesn't
137 * exist, aborts the launching process.
138 * @param configuration Eclipse launch configuration object
139 * @return Jelly script file
140 * @throws CoreException from Eclipse platform
141 */
142 protected File verifyScriptFile(ILaunchConfiguration configuration)
143 throws CoreException
144 {
145
146 IPath path = getScriptFilePath(configuration);
147
148
149
150 if (path == null) {
151 abort(MessageFormat.format(JellyLauncherPlugin.getResourceString("jelly.launch.messages.noscriptfile"), null),
152 null,JellyLaunchConfigurationConstants.ERR_SCRIPT_FILE_NOT_SPECIFIED);
153 } else {
154 if (path.isAbsolute()) {
155 File scriptfile = new File(path.toOSString());
156 if (scriptfile.isFile()) {
157 return scriptfile;
158 }
159
160
161
162
163 IResource res = ResourcesPlugin.getWorkspace().getRoot()
164 .findMember(path);
165 if (res instanceof IFile && res.exists()) {
166 return res.getLocation().toFile();
167 }
168 abort(MessageFormat.format(JellyLauncherPlugin.getResourceString("jelly.launch.messages.scriptFileDoesntExist"), new String[] { path.toString() }),
169 null,JellyLaunchConfigurationConstants.ERR_SCRIPT_FILE_DOES_NOT_EXIST);
170 } else {
171 IResource res = ResourcesPlugin.getWorkspace().getRoot()
172 .findMember(path);
173 if (res instanceof IFile && res.exists()) {
174 return res.getLocation().toFile();
175 }
176 abort(MessageFormat.format(JellyLauncherPlugin.getResourceString("jelly.launch.messages.scriptFileDoesntExist"), new String[] { path.toString() }),
177 null,JellyLaunchConfigurationConstants.ERR_SCRIPT_FILE_DOES_NOT_EXIST);
178 }
179 }
180 return null;
181 }
182
183 /***
184 * Reads the file path of the output file of the Jelly script from the launch configuration and
185 * verifies that the path exists and is the path of a file
186 * @param configuration Eclipse launch configuration object
187 * @return output file of the Jelly script
188 * @throws CoreException from Eclipse platform
189 */
190 protected File verifyOutputFile(ILaunchConfiguration configuration)
191 throws CoreException
192 {
193
194 IPath path = getOutputFilePath(configuration);
195
196
197 if (path == null) {
198 return null;
199 } else {
200 if (path.isAbsolute()) {
201 File outputfile = new File(path.toOSString());
202 if (outputfile.isFile()) {
203 return outputfile;
204 }
205
206
207
208
209 IResource res = ResourcesPlugin.getWorkspace().getRoot()
210 .findMember(path);
211 if (res instanceof IFile && res.exists()) {
212 return res.getLocation().toFile();
213 }
214 } else {
215 IResource res = ResourcesPlugin.getWorkspace().getRoot()
216 .findMember(path);
217 if (res instanceof IFile && res.exists()) {
218 return res.getLocation().toFile();
219 }
220 }
221 }
222 return null;
223 }
224
225 /***
226 * Returns the path of the Jelly script file in the Eclipse workspace
227 * @param configuration Eclipse launch configuration object
228 * @return Eclipse <code>IPath</code> path object for the Jelly script file in the workspace
229 * @throws CoreException from Eclipse platform
230 */
231 public IPath getScriptFilePath(ILaunchConfiguration configuration)
232 throws CoreException
233 {
234
235 String path = configuration.getAttribute(
236 JellyLaunchConfigurationConstants.ATTR_JELLYSCRIPT_PATH,
237 (String) null);
238
239
240 if (path != null) {
241 path = VariablesPlugin.getDefault().getStringVariableManager()
242 .performStringSubstitution(path);
243 return new Path(path);
244 }
245 return null;
246 }
247
248 /***
249 * Returns the path of the output file of the Jelly script in the Eclipse workspace
250 * @param configuration Eclipse launch configuration object
251 * @return Eclipse <code>IPath</code> path object for the output file of the Jelly script in the workspace
252 * @throws CoreException from Eclipse platform
253 */
254 public IPath getOutputFilePath(ILaunchConfiguration configuration)
255 throws CoreException
256 {
257
258 String path = configuration.getAttribute(
259 JellyLaunchConfigurationConstants.ATTR_OUTPUT_PATH,
260 (String) null);
261
262
263 if (path != null) {
264 path = VariablesPlugin.getDefault().getStringVariableManager()
265 .performStringSubstitution(path);
266 return new Path(path);
267 }
268 return null;
269 }
270 }