]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchDelegate.java
Import launch plugin
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ui.launch / src / main / java / org / argeo / slc / ui / launch / script / SlcScriptLaunchDelegate.java
diff --git a/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchDelegate.java b/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchDelegate.java
new file mode 100644 (file)
index 0000000..02a00e0
--- /dev/null
@@ -0,0 +1,120 @@
+package org.argeo.slc.ui.launch.script;\r
+\r
+import java.util.List;\r
+import java.util.Vector;\r
+\r
+import org.eclipse.core.resources.IFile;\r
+import org.eclipse.core.resources.IProject;\r
+import org.eclipse.core.resources.IResource;\r
+import org.eclipse.core.runtime.CoreException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.debug.core.ILaunch;\r
+import org.eclipse.debug.core.ILaunchConfiguration;\r
+import org.eclipse.debug.core.ILaunchManager;\r
+import org.eclipse.jdt.core.IJavaProject;\r
+import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;\r
+import org.eclipse.jdt.launching.IVMInstall;\r
+import org.eclipse.jdt.launching.IVMRunner;\r
+import org.eclipse.jdt.launching.JavaRuntime;\r
+import org.eclipse.jdt.launching.VMRunnerConfiguration;\r
+import org.eclipse.jface.dialogs.ErrorDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+\r
+import org.argeo.slc.ui.launch.DefaultSlcRuntime;\r
+import org.argeo.slc.ui.launch.SlcRuntime;\r
+import org.argeo.slc.ui.launch.SlcUiLaunchPlugin;\r
+import org.argeo.slc.ui.launch.preferences.SlcPreferencePage;\r
+\r
+public class SlcScriptLaunchDelegate extends\r
+               AbstractJavaLaunchConfigurationDelegate {\r
+       public static final String ID = "org.argeo.slc.launch.slcScriptLaunchType";\r
+\r
+       private final static String ANT_MAIN = "org.apache.tools.ant.Main";\r
+\r
+       public void launch(ILaunchConfiguration configuration, String mode,\r
+                       ILaunch launch, IProgressMonitor monitor) throws CoreException {\r
+               IResource[] resources = configuration.getMappedResources();\r
+               if (resources.length != 1) {\r
+                       throw new RuntimeException("Can only launch one script.");\r
+               }\r
+               if (!(resources[0] instanceof IFile)) {\r
+                       throw new RuntimeException("Can only launch file.");\r
+               }\r
+               IFile file = (IFile) resources[0];\r
+               System.out.println("Launched " + file.getLocation().toFile());\r
+\r
+               // Retrieve SLC Runtime\r
+               String slcRuntimePath = SlcUiLaunchPlugin.getDefault()\r
+                               .getPreferenceStore().getString(\r
+                                               SlcPreferencePage.PREF_SLC_RUNTIME_LOCATION);\r
+               if (slcRuntimePath == null || slcRuntimePath.equals("")) {\r
+                       showError("SLC Runtime path is not set. Set it in Windows > Preferences > SLC");\r
+                       return;\r
+               }\r
+               SlcRuntime deployedSlc = new DefaultSlcRuntime(slcRuntimePath);\r
+\r
+               IProject project = file.getProject();\r
+\r
+               IVMInstall vmInstall = null;\r
+               String[] classPath = null;\r
+\r
+               if (project instanceof IJavaProject) {\r
+                       JavaRuntime.getVMInstall((IJavaProject) project);\r
+                       classPath = JavaRuntime\r
+                                       .computeDefaultRuntimeClassPath((IJavaProject) project);\r
+               }\r
+\r
+               if (vmInstall == null)\r
+                       vmInstall = JavaRuntime.getDefaultVMInstall();\r
+               if (vmInstall != null) {\r
+                       IVMRunner vmRunner = vmInstall.getVMRunner(mode);\r
+                       if (vmRunner != null) {\r
+                               if (classPath == null) {\r
+                                       classPath = deployedSlc.getClasspath();\r
+                               }\r
+\r
+                               if (classPath != null) {\r
+                                       VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(\r
+                                                       ANT_MAIN, classPath);\r
+                                       vmConfig.setVMArguments(getVmArguments(deployedSlc));\r
+                                       vmConfig.setWorkingDirectory(file.getLocation().toFile()\r
+                                                       .getParent());\r
+                                       vmConfig.setProgramArguments(getProgramArguments(\r
+                                                       deployedSlc, file, mode));\r
+                                       vmRunner.run(vmConfig, launch, null);\r
+                               }\r
+                       }\r
+               }\r
+\r
+       }\r
+\r
+       private String[] getVmArguments(SlcRuntime deployedSlc) {\r
+               List<String> list = new Vector<String>();\r
+               list.add("-Dant.home=" + deployedSlc.getAntHome());\r
+               list.add("-Djava.library.path=" + deployedSlc.getJavaLibraryPath());\r
+               return list.toArray(new String[list.size()]);\r
+       }\r
+\r
+       private String[] getProgramArguments(SlcRuntime deployedSlc, IFile file,\r
+                       String mode) {\r
+               List<String> list = new Vector<String>();\r
+               list.add("-f");\r
+               list.add(file.getLocation().toFile().getAbsolutePath());\r
+               if (mode.equals(ILaunchManager.DEBUG_MODE)) {\r
+                       list.add("-d");\r
+               }\r
+               return list.toArray(new String[list.size()]);\r
+       }\r
+\r
+       private void showError(String message) {\r
+               Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench()\r
+                               .getActiveWorkbenchWindow().getShell();\r
+\r
+               IStatus status = new Status(IStatus.ERROR, SlcUiLaunchPlugin.ID,\r
+                               message);\r
+               ErrorDialog.openError(shell, "Error", "Cannot launch SLC script",\r
+                               status);\r
+       }\r
+}\r