1 package org
.argeo
.slc
.ide
.ui
.launch
.script
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.IOException
;
6 import java
.util
.Properties
;
7 import java
.util
.Vector
;
9 import org
.argeo
.slc
.ide
.ui
.DeployedSlcSystem
;
10 import org
.argeo
.slc
.ide
.ui
.EmbeddedSlcSystem
;
11 import org
.argeo
.slc
.ide
.ui
.SlcSystem
;
12 import org
.argeo
.slc
.ide
.ui
.SlcIdeUiPlugin
;
13 import org
.argeo
.slc
.ide
.ui
.launch
.preferences
.SlcLaunchPreferencePage
;
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
;
39 public class SlcScriptLaunchDelegate
extends
40 AbstractJavaLaunchConfigurationDelegate
{
41 public static final String ID
= "org.argeo.slc.launch.slcScriptLaunchType";
43 public final static String ANT_MAIN
= "org.apache.tools.ant.Main";
44 public final static String SLC_MAIN
= "org.argeo.slc.cli.SlcMain";
46 @SuppressWarnings("deprecation")
47 public void launch(ILaunchConfiguration configuration
, String mode
,
48 ILaunch launch
, IProgressMonitor monitor
) throws CoreException
{
49 if (!saveBeforeLaunch(configuration
, mode
, monitor
))
52 String scriptLocation
= configuration
.getAttribute(
53 SlcScriptUtils
.ATTR_SCRIPT
, "");
54 if (scriptLocation
.equals(""))
55 abort("Script has to be provided", null, 1);
57 IStringVariableManager manager
= VariablesPlugin
.getDefault()
58 .getStringVariableManager();
59 scriptLocation
= manager
.performStringSubstitution(scriptLocation
);
60 IPath path
= new Path(scriptLocation
);
61 IFile
[] files
= ResourcesPlugin
.getWorkspace().getRoot()
62 .findFilesForLocation(path
);
64 if (files
.length
== 0)
65 abort("Coulkd not find related file", null, 1);
67 IFile file
= (IFile
) files
[0];
69 .logMessage("Launching " + file
.getLocation().toFile(), null);
71 boolean pre093
= configuration
.getAttribute(SlcScriptUtils
.ATTR_PRE093
,
74 // Retrieve SLC Runtime
75 SlcSystem slcSystem
= findSlcSystem(file
, pre093
);
76 if (slcSystem
== null)
79 IVMRunner vmRunner
= slcSystem
.getVmInstall().getVMRunner(mode
);
80 final VMRunnerConfiguration vmConfig
;
82 vmConfig
= createPre093Config(slcSystem
, file
, mode
);
84 vmConfig
= createConfig(slcSystem
, file
, mode
, configuration
);
86 vmRunner
.run(vmConfig
, launch
, monitor
);
89 protected SlcSystem
findSlcSystem(IFile file
, boolean pre093
)
90 throws CoreException
{
91 SlcSystem slcSystem
= null;
93 IProject project
= file
.getProject();
94 if (project
.getNature("org.eclipse.jdt.core.javanature") != null) {
95 IJavaProject javaProject
= JavaCore
.create(project
);
96 if (checkProjectForEmbedded(javaProject
, pre093
)) {
97 slcSystem
= new EmbeddedSlcSystem(javaProject
);
101 if (slcSystem
== null) {
102 String slcRuntimePath
= SlcIdeUiPlugin
.getDefault()
103 .getPreferenceStore().getString(
104 SlcLaunchPreferencePage
.PREF_SLC_RUNTIME_LOCATION
);
105 if (slcRuntimePath
== null || slcRuntimePath
.equals("")) {
106 showError("SLC Runtime path is not set. Set it in Windows > Preferences > SLC");
110 slcSystem
= new DeployedSlcSystem(slcRuntimePath
);
116 protected boolean checkProjectForEmbedded(IJavaProject project
,
119 IType mainType
= null;
121 mainType
= project
.findType(ANT_MAIN
);
123 mainType
= project
.findType(SLC_MAIN
);
125 if (mainType
== null)
129 } catch (JavaModelException e
) {
136 protected VMRunnerConfiguration
createConfig(SlcSystem deployedSlc
,
137 IFile file
, String mode
, ILaunchConfiguration configuration
)
138 throws CoreException
{
139 VMRunnerConfiguration vmConfig
= new VMRunnerConfiguration(SLC_MAIN
,
140 deployedSlc
.getClasspath());
141 vmConfig
.setVMArguments(getVmArguments(deployedSlc
));
142 vmConfig
.setWorkingDirectory(file
.getLocation().toFile().getParent());
143 vmConfig
.setProgramArguments(getProgramArguments(deployedSlc
, file
,
144 mode
, configuration
));
148 protected String
[] getVmArguments(SlcSystem deployedSlc
) {
149 List
<String
> list
= new Vector
<String
>();
150 if (deployedSlc
.getJavaLibraryPath() != null)
151 list
.add("-Djava.library.path=" + deployedSlc
.getJavaLibraryPath());
152 return list
.toArray(new String
[list
.size()]);
155 protected String
[] getProgramArguments(SlcSystem deployedSlc
, IFile file
,
156 String mode
, ILaunchConfiguration configuration
)
157 throws CoreException
{
158 List
<String
> list
= new Vector
<String
>();
164 list
.add("--script");
165 list
.add(file
.getLocation().toFile().getAbsolutePath());
168 String runtime
= configuration
.getAttribute(
169 SlcScriptUtils
.ATTR_RUNTIME
, "");
170 if (!runtime
.equals("")) {
171 list
.add("--runtime");
176 String targets
= configuration
.getAttribute(
177 SlcScriptUtils
.ATTR_TARGETS
, "");
178 if (!targets
.equals("")) {
179 list
.add("--targets");
184 Properties properties
= new Properties();
185 String str
= configuration
.getAttribute(SlcScriptUtils
.ATTR_PROPERTIES
,
187 ByteArrayInputStream in
= new ByteArrayInputStream(str
.getBytes());
190 } catch (IOException e
) {
191 throw new RuntimeException("Cannot read properties", e
);
196 } catch (IOException e
) {
201 for (Object key
: properties
.keySet()) {
203 StringBuffer buf
= new StringBuffer("");
204 buf
.append(key
).append('=').append(properties
.get(key
));
205 list
.add(buf
.toString());
209 if (mode
.equals(ILaunchManager
.DEBUG_MODE
)) {
210 list
.add("--property");
211 list
.add("log4j.logger.org.argeo.slc=DEBUG");
213 return list
.toArray(new String
[list
.size()]);
217 protected VMRunnerConfiguration
createPre093Config(SlcSystem deployedSlc
,
218 IFile file
, String mode
) throws CoreException
{
219 VMRunnerConfiguration vmConfig
= new VMRunnerConfiguration(ANT_MAIN
,
220 deployedSlc
.getClasspath());
221 vmConfig
.setVMArguments(getPre093VmArguments(deployedSlc
));
222 vmConfig
.setWorkingDirectory(file
.getLocation().toFile().getParent());
223 vmConfig
.setProgramArguments(getPre093ProgramArguments(deployedSlc
,
228 protected String
[] getPre093VmArguments(SlcSystem deployedSlc
) {
229 List
<String
> list
= new Vector
<String
>();
230 // list.add("-Dant.home=" + deployedSlc.getAntHome());
231 if (deployedSlc
.getJavaLibraryPath() != null)
232 list
.add("-Djava.library.path=" + deployedSlc
.getJavaLibraryPath());
233 return list
.toArray(new String
[list
.size()]);
236 protected String
[] getPre093ProgramArguments(SlcSystem deployedSlc
,
237 IFile file
, String mode
) {
238 List
<String
> list
= new Vector
<String
>();
240 list
.add(file
.getLocation().toFile().getAbsolutePath());
241 if (mode
.equals(ILaunchManager
.DEBUG_MODE
)) {
244 return list
.toArray(new String
[list
.size()]);
248 private static void showError(String message
) {
249 Shell shell
= SlcIdeUiPlugin
.getDefault().getWorkbench()
250 .getActiveWorkbenchWindow().getShell();
252 IStatus status
= new Status(IStatus
.ERROR
, SlcIdeUiPlugin
.ID
,
254 ErrorDialog
.openError(shell
, "Error", "Cannot launch SLC script",