View Javadoc

1   /*
2    * Created on 05.12.2004
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  	// INTERFACE IMPLEMENTATION
41  	
42  	/* (non-Javadoc)
43  	 * @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)
44  	 */
45  	public void launch(ILaunchConfiguration configuration, String mode,
46  			ILaunch launch, IProgressMonitor monitor) throws CoreException 
47  	{
48  		// Get and verfiy installed virtual machine
49  		IVMInstall vm = verifyVMInstall(configuration);
50  		IVMRunner runner = vm.getVMRunner(mode);
51  		
52  		// Setup the VM Runner configuration by getting the attributes from the configuration
53  		VMRunnerConfiguration runConfig = new VMRunnerConfiguration(
54  				JellyLauncherPlugin.getResourceString("jelly.launchMainClass"),
55  				this.getClasspath(configuration));
56  
57  		// Get the program arguments as string from the configuration
58  		String argsString = this.getProgramArguments(configuration);
59  		
60  		// Get the file path of the Jelly script file from the configuration
61  		String scriptFilePath = this.getScriptFile(configuration).getAbsolutePath();
62  
63  		// Get the optional file path for the output file of the Jelly script
64  		String outConfig = null;
65  		File outputFile = this.getOutputFile(configuration);
66  		if(outputFile != null) outConfig = outputFile.getAbsolutePath();
67  		
68  		// Setup the program arguments for Jelly, depending on the configuration setup
69  		if( (argsString != null) || (outConfig != null))
70  		{
71  			if(argsString == null)
72  			{
73  				// No extra program arguments specified
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  				// Extra program arguments must be appended
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  			// Simple call Jelly with the Script file path
90  			runConfig.setProgramArguments(new String[] {scriptFilePath});
91  		}
92  		
93  		// Setup the runtime classpathes
94  		getClasspath(configuration);
95  		String [] bootpath = getBootpath(configuration);
96  		runConfig.setBootClassPath(bootpath);
97  		
98  		// Get the working directory
99  		runConfig.setWorkingDirectory(this.getWorkingDirectory(configuration).getAbsolutePath());
100 		
101 		// And finally run it
102 		runner.run(runConfig, launch, monitor);
103 	}
104 	
105 	// IMPLEMENTATION
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 		// Get path from configuration and verify it
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 		// Get path from configuration and verify it
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 		// Get the Eclipse path of the Jelly script file
146 		IPath path = getScriptFilePath(configuration);
147 		
148 		// If we can't get the path to the Jelly script file, simply abort launching
149 		// with an appropriate error message
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 				// This may be a workspace relative path returned by a variable.
160 				// However variable paths start with a slash and thus are
161 				// thought to
162 				// be absolute
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 		// Get the file path of the output file of the launch configuration
194 		IPath path = getOutputFilePath(configuration);
195 		
196 		// Simply stop, if there's no output file (since it's optional)
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 				// This may be a workspace relative path returned by a variable.
206 				// However variable paths start with a slash and thus are
207 				// thought to
208 				// be absolute
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 		// Get the script file path from the launch configuration
235 		String path = configuration.getAttribute(
236 				JellyLaunchConfigurationConstants.ATTR_JELLYSCRIPT_PATH,
237 				(String) null);
238 		
239 		// If the file path string contains variables, they must be resolved
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 		// Get the output file path from the launch configuration
258 		String path = configuration.getAttribute(
259 				JellyLaunchConfigurationConstants.ATTR_OUTPUT_PATH,
260 				(String) null);
261 		
262 		// If the file path string contains variables, they must be resolved
263 		if (path != null) {
264 			path = VariablesPlugin.getDefault().getStringVariableManager()
265 					.performStringSubstitution(path);
266 			return new Path(path);
267 		}
268 		return null;
269 	}
270 }