1 package org
.argeo
.slc
.ui
.launch
.script
;
3 import java
.io
.IOException
;
4 import java
.io
.StringReader
;
6 import java
.util
.Properties
;
7 import java
.util
.Vector
;
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
;
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 public void launch(ILaunchConfiguration configuration
, String mode
,
47 ILaunch launch
, IProgressMonitor monitor
) throws CoreException
{
48 if (!saveBeforeLaunch(configuration
, mode
, monitor
))
51 String scriptLocation
= configuration
.getAttribute(
52 SlcScriptUtils
.ATTR_SCRIPT
, "");
53 if (scriptLocation
.equals(""))
54 abort("Script has to be provided", null, 1);
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
);
63 if (files
.length
== 0)
64 abort("Coulkd not find related file", null, 1);
66 IFile file
= (IFile
) files
[0];
68 .logMessage("Launching " + file
.getLocation().toFile(), null);
70 boolean pre093
= configuration
.getAttribute(SlcScriptUtils
.ATTR_PRE093
,
73 // Retrieve SLC Runtime
74 SlcSystem slcSystem
= findSlcSystem(file
, pre093
);
75 if (slcSystem
== null)
78 IVMRunner vmRunner
= slcSystem
.getVmInstall().getVMRunner(mode
);
79 final VMRunnerConfiguration vmConfig
;
81 vmConfig
= createPre093Config(slcSystem
, file
, mode
);
83 vmConfig
= createConfig(slcSystem
, file
, mode
, configuration
);
85 vmRunner
.run(vmConfig
, launch
, monitor
);
88 protected SlcSystem
findSlcSystem(IFile file
, boolean pre093
)
89 throws CoreException
{
90 SlcSystem slcSystem
= null;
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
);
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");
109 slcSystem
= new DeployedSlcSystem(slcRuntimePath
);
115 protected boolean checkProjectForEmbedded(IJavaProject project
,
118 IType mainType
= null;
120 mainType
= project
.findType(ANT_MAIN
);
122 mainType
= project
.findType(SLC_MAIN
);
124 if (mainType
== null)
128 } catch (JavaModelException e
) {
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
));
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()]);
154 protected String
[] getProgramArguments(SlcSystem deployedSlc
, IFile file
,
155 String mode
, ILaunchConfiguration configuration
)
156 throws CoreException
{
157 List
<String
> list
= new Vector
<String
>();
163 list
.add("--script");
164 list
.add(file
.getLocation().toFile().getAbsolutePath());
167 String runtime
= configuration
.getAttribute(
168 SlcScriptUtils
.ATTR_RUNTIME
, "");
169 if (!runtime
.equals("")) {
170 list
.add("--runtime");
175 String targets
= configuration
.getAttribute(
176 SlcScriptUtils
.ATTR_TARGETS
, "");
177 if (!targets
.equals("")) {
178 list
.add("--targets");
183 Properties properties
= new Properties();
184 StringReader reader
= new StringReader(configuration
.getAttribute(
185 SlcScriptUtils
.ATTR_PROPERTIES
, ""));
187 properties
.load(reader
);
188 } catch (IOException e
) {
189 throw new RuntimeException("Cannot read properties", e
);
195 for (Object key
: properties
.keySet()) {
197 StringBuffer buf
= new StringBuffer("");
198 buf
.append(key
).append('=').append(properties
.get(key
));
199 list
.add(buf
.toString());
203 if (mode
.equals(ILaunchManager
.DEBUG_MODE
)) {
204 list
.add("--property");
205 list
.add("log4j.logger.org.argeo.slc=DEBUG");
207 return list
.toArray(new String
[list
.size()]);
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
,
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()]);
230 protected String
[] getPre093ProgramArguments(SlcSystem deployedSlc
,
231 IFile file
, String mode
) {
232 List
<String
> list
= new Vector
<String
>();
234 list
.add(file
.getLocation().toFile().getAbsolutePath());
235 if (mode
.equals(ILaunchManager
.DEBUG_MODE
)) {
238 return list
.toArray(new String
[list
.size()]);
242 private static void showError(String message
) {
243 Shell shell
= SlcUiLaunchPlugin
.getDefault().getWorkbench()
244 .getActiveWorkbenchWindow().getShell();
246 IStatus status
= new Status(IStatus
.ERROR
, SlcUiLaunchPlugin
.ID
,
248 ErrorDialog
.openError(shell
, "Error", "Cannot launch SLC script",