1 package org
.argeo
.slc
.ui
.launch
.script
;
3 import java
.io
.IOException
;
4 import java
.io
.StringReader
;
7 import java
.util
.Properties
;
8 import java
.util
.Vector
;
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
;
36 public class SlcScriptLaunchDelegate
extends
37 AbstractJavaLaunchConfigurationDelegate
{
38 public static final String ID
= "org.argeo.slc.launch.slcScriptLaunchType";
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";
46 private final static String ANT_MAIN
= "org.apache.tools.ant.Main";
47 private final static String SLC_MAIN
= "org.argeo.slc.cli.SlcMain";
49 public void launch(ILaunchConfiguration configuration
, String mode
,
50 ILaunch launch
, IProgressMonitor monitor
) throws CoreException
{
51 IResource
[] resources
= configuration
.getMappedResources();
53 if (resources
.length
!= 1) {
54 throw new RuntimeException("Can only launch one script.");
56 if (!(resources
[0] instanceof IFile
)) {
57 throw new RuntimeException("Can only launch file.");
59 IFile file
= (IFile
) resources
[0];
60 System
.out
.println("Launched " + file
.getLocation().toFile());
62 boolean pre093
= configuration
.getAttribute(ATTR_PRE093
, false);
64 // Retrieve SLC Runtime
65 SlcSystem slcSystem
= findSlcSystem(file
);
66 if (slcSystem
== null)
69 IVMRunner vmRunner
= slcSystem
.getVmInstall().getVMRunner(mode
);
70 final VMRunnerConfiguration vmConfig
;
72 vmConfig
= createPre093Config(slcSystem
, file
, mode
);
74 vmConfig
= createConfig(slcSystem
, file
, mode
, configuration
);
76 vmRunner
.run(vmConfig
, launch
, null);
79 protected SlcSystem
findSlcSystem(IFile file
) throws CoreException
{
80 SlcSystem slcSystem
= null;
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
);
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");
99 slcSystem
= new DeployedSlcSystem(slcRuntimePath
);
105 protected boolean checkProjectForEmbedded(IJavaProject project
) {
107 IType antmainType
= project
.findType(ANT_MAIN
);
108 if (antmainType
== null)
112 } catch (JavaModelException e
) {
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
));
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()]);
138 protected String
[] getProgramArguments(SlcSystem deployedSlc
, IFile file
,
139 String mode
, ILaunchConfiguration configuration
)
140 throws CoreException
{
141 List
<String
> list
= new Vector
<String
>();
147 list
.add("--script");
148 list
.add(file
.getLocation().toFile().getAbsolutePath());
151 String runtime
= configuration
.getAttribute(ATTR_RUNTIME
, "");
152 if (!runtime
.equals("")) {
153 list
.add("--runtime");
158 String targets
= configuration
.getAttribute(ATTR_RUNTIME
, "");
159 if (!runtime
.equals("")) {
160 list
.add("--targets");
165 Properties properties
= new Properties();
166 StringReader reader
= new StringReader(configuration
.getAttribute(
167 ATTR_PROPERTIES
, ""));
169 properties
.load(reader
);
170 } catch (IOException e
) {
171 throw new RuntimeException("Cannot read properties", e
);
177 for (Object key
: properties
.keySet()) {
179 StringBuffer buf
= new StringBuffer("");
180 buf
.append(key
).append('=').append(properties
.get(key
));
181 list
.add(buf
.toString());
185 if (mode
.equals(ILaunchManager
.DEBUG_MODE
)) {
186 list
.add("--property");
187 list
.add("log4j.logger.org.argeo.slc=DEBUG");
189 return list
.toArray(new String
[list
.size()]);
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
,
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()]);
212 protected String
[] getPre093ProgramArguments(SlcSystem deployedSlc
,
213 IFile file
, String mode
) {
214 List
<String
> list
= new Vector
<String
>();
216 list
.add(file
.getLocation().toFile().getAbsolutePath());
217 if (mode
.equals(ILaunchManager
.DEBUG_MODE
)) {
220 return list
.toArray(new String
[list
.size()]);
224 private void showError(String message
) {
225 Shell shell
= SlcUiLaunchPlugin
.getDefault().getWorkbench()
226 .getActiveWorkbenchWindow().getShell();
228 IStatus status
= new Status(IStatus
.ERROR
, SlcUiLaunchPlugin
.ID
,
230 ErrorDialog
.openError(shell
, "Error", "Cannot launch SLC script",