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