]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchDelegate.java
28f9671c14155da78bb1aabcfc0ca3ce1d49d8cf
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ui.launch / src / main / java / org / argeo / slc / ui / launch / script / SlcScriptLaunchDelegate.java
1 package org.argeo.slc.ui.launch.script;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.List;
6 import java.util.Properties;
7 import java.util.Vector;
8
9 import org.argeo.slc.ui.launch.DeployedSlcSystem;
10 import org.argeo.slc.ui.launch.EmbeddedSlcSystem;
11 import org.argeo.slc.ui.launch.SlcSystem;
12 import org.argeo.slc.ui.launch.SlcUiLaunchPlugin;
13 import org.argeo.slc.ui.launch.preferences.SlcPreferencePage;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.variables.IStringVariableManager;
24 import org.eclipse.core.variables.VariablesPlugin;
25 import org.eclipse.debug.core.DebugPlugin;
26 import org.eclipse.debug.core.ILaunch;
27 import org.eclipse.debug.core.ILaunchConfiguration;
28 import org.eclipse.debug.core.ILaunchManager;
29 import org.eclipse.jdt.core.IJavaProject;
30 import org.eclipse.jdt.core.IType;
31 import org.eclipse.jdt.core.JavaCore;
32 import org.eclipse.jdt.core.JavaModelException;
33 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
34 import org.eclipse.jdt.launching.IVMRunner;
35 import org.eclipse.jdt.launching.VMRunnerConfiguration;
36 import org.eclipse.jface.dialogs.ErrorDialog;
37 import org.eclipse.swt.widgets.Shell;
38
39 public class SlcScriptLaunchDelegate extends
40 AbstractJavaLaunchConfigurationDelegate {
41 public static final String ID = "org.argeo.slc.launch.slcScriptLaunchType";
42
43 public final static String ANT_MAIN = "org.apache.tools.ant.Main";
44 public final static String SLC_MAIN = "org.argeo.slc.cli.SlcMain";
45
46 public void launch(ILaunchConfiguration configuration, String mode,
47 ILaunch launch, IProgressMonitor monitor) throws CoreException {
48 if (!saveBeforeLaunch(configuration, mode, monitor))
49 return;
50
51 String scriptLocation = configuration.getAttribute(
52 SlcScriptUtils.ATTR_SCRIPT, "");
53 if (scriptLocation.equals(""))
54 abort("Script has to be provided", null, 1);
55
56 IStringVariableManager manager = VariablesPlugin.getDefault()
57 .getStringVariableManager();
58 scriptLocation = manager.performStringSubstitution(scriptLocation);
59 IPath path = new Path(scriptLocation);
60 IFile[] files = ResourcesPlugin.getWorkspace().getRoot()
61 .findFilesForLocation(path);
62
63 if (files.length == 0)
64 abort("Coulkd not find related file", null, 1);
65
66 IFile file = (IFile) files[0];
67 DebugPlugin
68 .logMessage("Launching " + file.getLocation().toFile(), null);
69
70 boolean pre093 = configuration.getAttribute(SlcScriptUtils.ATTR_PRE093,
71 false);
72
73 // Retrieve SLC Runtime
74 SlcSystem slcSystem = findSlcSystem(file, pre093);
75 if (slcSystem == null)
76 return;
77
78 IVMRunner vmRunner = slcSystem.getVmInstall().getVMRunner(mode);
79 final VMRunnerConfiguration vmConfig;
80 if (pre093) {
81 vmConfig = createPre093Config(slcSystem, file, mode);
82 } else {
83 vmConfig = createConfig(slcSystem, file, mode, configuration);
84 }
85 vmRunner.run(vmConfig, launch, monitor);
86 }
87
88 protected SlcSystem findSlcSystem(IFile file, boolean pre093)
89 throws CoreException {
90 SlcSystem slcSystem = null;
91
92 IProject project = file.getProject();
93 if (project.getNature("org.eclipse.jdt.core.javanature") != null) {
94 IJavaProject javaProject = JavaCore.create(project);
95 if (checkProjectForEmbedded(javaProject, pre093)) {
96 slcSystem = new EmbeddedSlcSystem(javaProject);
97 }
98 }
99
100 if (slcSystem == null) {
101 String slcRuntimePath = SlcUiLaunchPlugin.getDefault()
102 .getPreferenceStore().getString(
103 SlcPreferencePage.PREF_SLC_RUNTIME_LOCATION);
104 if (slcRuntimePath == null || slcRuntimePath.equals("")) {
105 showError("SLC Runtime path is not set. Set it in Windows > Preferences > SLC");
106 return null;
107 }
108
109 slcSystem = new DeployedSlcSystem(slcRuntimePath);
110 }
111
112 return slcSystem;
113 }
114
115 protected boolean checkProjectForEmbedded(IJavaProject project,
116 boolean pre093) {
117 try {
118 IType mainType = null;
119 if (pre093)
120 mainType = project.findType(ANT_MAIN);
121 else
122 mainType = project.findType(SLC_MAIN);
123
124 if (mainType == null)
125 return false;
126 else
127 return true;
128 } catch (JavaModelException e) {
129 e.printStackTrace();
130 return false;
131 }
132 }
133
134 // Regular SLC
135 protected VMRunnerConfiguration createConfig(SlcSystem deployedSlc,
136 IFile file, String mode, ILaunchConfiguration configuration)
137 throws CoreException {
138 VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(SLC_MAIN,
139 deployedSlc.getClasspath());
140 vmConfig.setVMArguments(getVmArguments(deployedSlc));
141 vmConfig.setWorkingDirectory(file.getLocation().toFile().getParent());
142 vmConfig.setProgramArguments(getProgramArguments(deployedSlc, file,
143 mode, configuration));
144 return vmConfig;
145 }
146
147 protected String[] getVmArguments(SlcSystem deployedSlc) {
148 List<String> list = new Vector<String>();
149 if (deployedSlc.getJavaLibraryPath() != null)
150 list.add("-Djava.library.path=" + deployedSlc.getJavaLibraryPath());
151 return list.toArray(new String[list.size()]);
152 }
153
154 protected String[] getProgramArguments(SlcSystem deployedSlc, IFile file,
155 String mode, ILaunchConfiguration configuration)
156 throws CoreException {
157 List<String> list = new Vector<String>();
158
159 list.add("--mode");
160 list.add("single");
161
162 // Script
163 list.add("--script");
164 list.add(file.getLocation().toFile().getAbsolutePath());
165
166 // Runtime
167 String runtime = configuration.getAttribute(
168 SlcScriptUtils.ATTR_RUNTIME, "");
169 if (!runtime.equals("")) {
170 list.add("--runtime");
171 list.add(runtime);
172 }
173
174 // Targets
175 String targets = configuration.getAttribute(
176 SlcScriptUtils.ATTR_TARGETS, "");
177 if (!targets.equals("")) {
178 list.add("--targets");
179 list.add(targets);
180 }
181
182 // Properties
183 Properties properties = new Properties();
184 StringReader reader = new StringReader(configuration.getAttribute(
185 SlcScriptUtils.ATTR_PROPERTIES, ""));
186 try {
187 properties.load(reader);
188 } catch (IOException e) {
189 throw new RuntimeException("Cannot read properties", e);
190 } finally {
191 if (reader != null)
192 reader.close();
193 }
194
195 for (Object key : properties.keySet()) {
196 list.add("-p");
197 StringBuffer buf = new StringBuffer("");
198 buf.append(key).append('=').append(properties.get(key));
199 list.add(buf.toString());
200 }
201
202 // Debug mode
203 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
204 list.add("--property");
205 list.add("log4j.logger.org.argeo.slc=DEBUG");
206 }
207 return list.toArray(new String[list.size()]);
208 }
209
210 // Pre SLC v0.9.3
211 protected VMRunnerConfiguration createPre093Config(SlcSystem deployedSlc,
212 IFile file, String mode) throws CoreException {
213 VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(ANT_MAIN,
214 deployedSlc.getClasspath());
215 vmConfig.setVMArguments(getPre093VmArguments(deployedSlc));
216 vmConfig.setWorkingDirectory(file.getLocation().toFile().getParent());
217 vmConfig.setProgramArguments(getPre093ProgramArguments(deployedSlc,
218 file, mode));
219 return vmConfig;
220 }
221
222 protected String[] getPre093VmArguments(SlcSystem deployedSlc) {
223 List<String> list = new Vector<String>();
224 // list.add("-Dant.home=" + deployedSlc.getAntHome());
225 if (deployedSlc.getJavaLibraryPath() != null)
226 list.add("-Djava.library.path=" + deployedSlc.getJavaLibraryPath());
227 return list.toArray(new String[list.size()]);
228 }
229
230 protected String[] getPre093ProgramArguments(SlcSystem deployedSlc,
231 IFile file, String mode) {
232 List<String> list = new Vector<String>();
233 list.add("-f");
234 list.add(file.getLocation().toFile().getAbsolutePath());
235 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
236 list.add("-d");
237 }
238 return list.toArray(new String[list.size()]);
239 }
240
241 // Utilities
242 private static void showError(String message) {
243 Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench()
244 .getActiveWorkbenchWindow().getShell();
245
246 IStatus status = new Status(IStatus.ERROR, SlcUiLaunchPlugin.ID,
247 message);
248 ErrorDialog.openError(shell, "Error", "Cannot launch SLC script",
249 status);
250 }
251
252 }