]> git.argeo.org Git - gpl/argeo-slc.git/blob - SlcScriptLaunchDelegate.java
7acbb2920204e4f8217797e3a5d6db669cde0201
[gpl/argeo-slc.git] / SlcScriptLaunchDelegate.java
1 package org.argeo.slc.ui.launch.script;
2
3 import java.util.List;
4 import java.util.Vector;
5
6 import org.argeo.slc.ui.launch.DeployedSlcRuntime;
7 import org.argeo.slc.ui.launch.EmbeddedSlcRuntime;
8 import org.argeo.slc.ui.launch.SlcRuntime;
9 import org.argeo.slc.ui.launch.SlcUiLaunchPlugin;
10 import org.argeo.slc.ui.launch.preferences.SlcPreferencePage;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.debug.core.ILaunch;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.ILaunchManager;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IType;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.core.JavaModelException;
25 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
26 import org.eclipse.jdt.launching.IVMRunner;
27 import org.eclipse.jdt.launching.VMRunnerConfiguration;
28 import org.eclipse.jface.dialogs.ErrorDialog;
29 import org.eclipse.swt.widgets.Shell;
30
31 public class SlcScriptLaunchDelegate extends
32 AbstractJavaLaunchConfigurationDelegate {
33 public static final String ID = "org.argeo.slc.launch.slcScriptLaunchType";
34
35 private final static String ANT_MAIN = "org.apache.tools.ant.Main";
36
37 public void launch(ILaunchConfiguration configuration, String mode,
38 ILaunch launch, IProgressMonitor monitor) throws CoreException {
39 IResource[] resources = configuration.getMappedResources();
40 if (resources.length != 1) {
41 throw new RuntimeException("Can only launch one script.");
42 }
43 if (!(resources[0] instanceof IFile)) {
44 throw new RuntimeException("Can only launch file.");
45 }
46 IFile file = (IFile) resources[0];
47 System.out.println("Launched " + file.getLocation().toFile());
48
49 // Retrieve SLC Runtime
50 SlcRuntime deployedSlc = null;
51
52 IProject project = file.getProject();
53 if (project.getNature("org.eclipse.jdt.core.javanature") != null) {
54 IJavaProject javaProject = JavaCore.create(project);
55 if (checkProjectForEmbedded(javaProject)) {
56 deployedSlc = new EmbeddedSlcRuntime(javaProject);
57 }
58 }
59
60 if (deployedSlc == null) {
61 String slcRuntimePath = SlcUiLaunchPlugin.getDefault()
62 .getPreferenceStore().getString(
63 SlcPreferencePage.PREF_SLC_RUNTIME_LOCATION);
64 if (slcRuntimePath == null || slcRuntimePath.equals("")) {
65 showError("SLC Runtime path is not set. Set it in Windows > Preferences > SLC");
66 return;
67 }
68
69 deployedSlc = new DeployedSlcRuntime(slcRuntimePath);
70 }
71
72 IVMRunner vmRunner = deployedSlc.getVmInstall().getVMRunner(mode);
73 VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(ANT_MAIN,
74 deployedSlc.getClasspath());
75 vmConfig.setVMArguments(getVmArguments(deployedSlc));
76 vmConfig.setWorkingDirectory(file.getLocation().toFile().getParent());
77 vmConfig.setProgramArguments(getProgramArguments(deployedSlc, file,
78 mode));
79 vmRunner.run(vmConfig, launch, null);
80 }
81
82 private String[] getVmArguments(SlcRuntime deployedSlc) {
83 List<String> list = new Vector<String>();
84 // list.add("-Dant.home=" + deployedSlc.getAntHome());
85 if (deployedSlc.getJavaLibraryPath() != null)
86 list.add("-Djava.library.path=" + deployedSlc.getJavaLibraryPath());
87 return list.toArray(new String[list.size()]);
88 }
89
90 private String[] getProgramArguments(SlcRuntime deployedSlc, IFile file,
91 String mode) {
92 List<String> list = new Vector<String>();
93 list.add("-f");
94 list.add(file.getLocation().toFile().getAbsolutePath());
95 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
96 list.add("-d");
97 }
98 return list.toArray(new String[list.size()]);
99 }
100
101 private void showError(String message) {
102 Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench()
103 .getActiveWorkbenchWindow().getShell();
104
105 IStatus status = new Status(IStatus.ERROR, SlcUiLaunchPlugin.ID,
106 message);
107 ErrorDialog.openError(shell, "Error", "Cannot launch SLC script",
108 status);
109 }
110
111 protected boolean checkProjectForEmbedded(IJavaProject project) {
112 try {
113 IType antmainType = project.findType(ANT_MAIN);
114 if (antmainType == null)
115 return false;
116 else
117 return true;
118 } catch (JavaModelException e) {
119 e.printStackTrace();
120 return false;
121 }
122 }
123 }