]> 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
Adapt SLC plugin to new runtime
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ui.launch / src / main / java / org / argeo / slc / ui / launch / script / SlcScriptLaunchDelegate.java
index 7acbb2920204e4f8217797e3a5d6db669cde0201..be9fdb43712484c0610de024facf206f934467a5 100644 (file)
@@ -1,11 +1,15 @@
 package org.argeo.slc.ui.launch.script;\r
 \r
+import java.io.IOException;\r
+import java.io.StringReader;\r
 import java.util.List;\r
+import java.util.Map;\r
+import java.util.Properties;\r
 import java.util.Vector;\r
 \r
-import org.argeo.slc.ui.launch.DeployedSlcRuntime;\r
-import org.argeo.slc.ui.launch.EmbeddedSlcRuntime;\r
-import org.argeo.slc.ui.launch.SlcRuntime;\r
+import org.argeo.slc.ui.launch.DeployedSlcSystem;\r
+import org.argeo.slc.ui.launch.EmbeddedSlcSystem;\r
+import org.argeo.slc.ui.launch.SlcSystem;\r
 import org.argeo.slc.ui.launch.SlcUiLaunchPlugin;\r
 import org.argeo.slc.ui.launch.preferences.SlcPreferencePage;\r
 import org.eclipse.core.resources.IFile;\r
@@ -27,16 +31,25 @@ import org.eclipse.jdt.launching.IVMRunner;
 import org.eclipse.jdt.launching.VMRunnerConfiguration;\r
 import org.eclipse.jface.dialogs.ErrorDialog;\r
 import org.eclipse.swt.widgets.Shell;\r
+import org.omg.CORBA.VM_CUSTOM;\r
 \r
 public class SlcScriptLaunchDelegate extends\r
                AbstractJavaLaunchConfigurationDelegate {\r
        public static final String ID = "org.argeo.slc.launch.slcScriptLaunchType";\r
 \r
+       public final static String ATTR_SCRIPT = "script";\r
+       public final static String ATTR_PROPERTIES = "properties";\r
+       public final static String ATTR_RUNTIME = "runtime";\r
+       public final static String ATTR_TARGETS = "targets";\r
+       public final static String ATTR_PRE093 = "pre093";\r
+\r
        private final static String ANT_MAIN = "org.apache.tools.ant.Main";\r
+       private final static String SLC_MAIN = "org.argeo.slc.cli.SlcMain";\r
 \r
        public void launch(ILaunchConfiguration configuration, String mode,\r
                        ILaunch launch, IProgressMonitor monitor) throws CoreException {\r
                IResource[] resources = configuration.getMappedResources();\r
+\r
                if (resources.length != 1) {\r
                        throw new RuntimeException("Can only launch one script.");\r
                }\r
@@ -46,40 +59,149 @@ public class SlcScriptLaunchDelegate extends
                IFile file = (IFile) resources[0];\r
                System.out.println("Launched " + file.getLocation().toFile());\r
 \r
+               boolean pre093 = configuration.getAttribute(ATTR_PRE093, false);\r
+\r
                // Retrieve SLC Runtime\r
-               SlcRuntime deployedSlc = null;\r
+               SlcSystem slcSystem = findSlcSystem(file);\r
+               if (slcSystem == null)\r
+                       return;\r
+\r
+               IVMRunner vmRunner = slcSystem.getVmInstall().getVMRunner(mode);\r
+               final VMRunnerConfiguration vmConfig;\r
+               if (pre093) {\r
+                       vmConfig = createPre093Config(slcSystem, file, mode);\r
+               } else {\r
+                       vmConfig = createConfig(slcSystem, file, mode, configuration);\r
+               }\r
+               vmRunner.run(vmConfig, launch, null);\r
+       }\r
+\r
+       protected SlcSystem findSlcSystem(IFile file) throws CoreException {\r
+               SlcSystem slcSystem = null;\r
 \r
                IProject project = file.getProject();\r
                if (project.getNature("org.eclipse.jdt.core.javanature") != null) {\r
                        IJavaProject javaProject = JavaCore.create(project);\r
                        if (checkProjectForEmbedded(javaProject)) {\r
-                               deployedSlc = new EmbeddedSlcRuntime(javaProject);\r
+                               slcSystem = new EmbeddedSlcSystem(javaProject);\r
                        }\r
                }\r
 \r
-               if (deployedSlc == null) {\r
+               if (slcSystem == null) {\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
+                               return null;\r
                        }\r
 \r
-                       deployedSlc = new DeployedSlcRuntime(slcRuntimePath);\r
+                       slcSystem = new DeployedSlcSystem(slcRuntimePath);\r
                }\r
 \r
-               IVMRunner vmRunner = deployedSlc.getVmInstall().getVMRunner(mode);\r
-               VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(ANT_MAIN,\r
+               return slcSystem;\r
+       }\r
+\r
+       protected boolean checkProjectForEmbedded(IJavaProject project) {\r
+               try {\r
+                       IType antmainType = project.findType(ANT_MAIN);\r
+                       if (antmainType == null)\r
+                               return false;\r
+                       else\r
+                               return true;\r
+               } catch (JavaModelException e) {\r
+                       e.printStackTrace();\r
+                       return false;\r
+               }\r
+       }\r
+\r
+       // Regular SLC\r
+       protected VMRunnerConfiguration createConfig(SlcSystem deployedSlc,\r
+                       IFile file, String mode, ILaunchConfiguration configuration)\r
+                       throws CoreException {\r
+               VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(SLC_MAIN,\r
                                deployedSlc.getClasspath());\r
                vmConfig.setVMArguments(getVmArguments(deployedSlc));\r
                vmConfig.setWorkingDirectory(file.getLocation().toFile().getParent());\r
                vmConfig.setProgramArguments(getProgramArguments(deployedSlc, file,\r
-                               mode));\r
-               vmRunner.run(vmConfig, launch, null);\r
+                               mode, configuration));\r
+               return vmConfig;\r
        }\r
 \r
-       private String[] getVmArguments(SlcRuntime deployedSlc) {\r
+       protected String[] getVmArguments(SlcSystem deployedSlc) {\r
+               List<String> list = new Vector<String>();\r
+               if (deployedSlc.getJavaLibraryPath() != null)\r
+                       list.add("-Djava.library.path=" + deployedSlc.getJavaLibraryPath());\r
+               return list.toArray(new String[list.size()]);\r
+       }\r
+\r
+       protected String[] getProgramArguments(SlcSystem deployedSlc, IFile file,\r
+                       String mode, ILaunchConfiguration configuration)\r
+                       throws CoreException {\r
+               List<String> list = new Vector<String>();\r
+\r
+               list.add("--mode");\r
+               list.add("single");\r
+\r
+               // Script\r
+               list.add("--script");\r
+               list.add(file.getLocation().toFile().getAbsolutePath());\r
+\r
+               // Runtime\r
+               String runtime = configuration.getAttribute(ATTR_RUNTIME, "");\r
+               if (!runtime.equals("")) {\r
+                       list.add("--runtime");\r
+                       list.add(runtime);\r
+               }\r
+\r
+               // Targets\r
+               String targets = configuration.getAttribute(ATTR_RUNTIME, "");\r
+               if (!runtime.equals("")) {\r
+                       list.add("--targets");\r
+                       list.add(targets);\r
+               }\r
+\r
+               // Properties\r
+               Properties properties = new Properties();\r
+               StringReader reader = new StringReader(configuration.getAttribute(\r
+                               ATTR_PROPERTIES, ""));\r
+               try {\r
+                       properties.load(reader);\r
+               } catch (IOException e) {\r
+                       throw new RuntimeException("Cannot read properties", e);\r
+               } finally {\r
+                       if (reader != null)\r
+                               reader.close();\r
+               }\r
+\r
+               for (Object key : properties.keySet()) {\r
+                       list.add("-p");\r
+                       StringBuffer buf = new StringBuffer("");\r
+                       buf.append(key).append('=').append(properties.get(key));\r
+                       list.add(buf.toString());\r
+               }\r
+\r
+               // Debug mode\r
+               if (mode.equals(ILaunchManager.DEBUG_MODE)) {\r
+                       list.add("--property");\r
+                       list.add("log4j.logger.org.argeo.slc=DEBUG");\r
+               }\r
+               return list.toArray(new String[list.size()]);\r
+       }\r
+\r
+       // Pre SLC v0.9.3\r
+       protected VMRunnerConfiguration createPre093Config(SlcSystem deployedSlc,\r
+                       IFile file, String mode) throws CoreException {\r
+               VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(ANT_MAIN,\r
+                               deployedSlc.getClasspath());\r
+               vmConfig.setVMArguments(getPre093VmArguments(deployedSlc));\r
+               vmConfig.setWorkingDirectory(file.getLocation().toFile().getParent());\r
+               vmConfig.setProgramArguments(getPre093ProgramArguments(deployedSlc,\r
+                               file, mode));\r
+               return vmConfig;\r
+       }\r
+\r
+       protected String[] getPre093VmArguments(SlcSystem deployedSlc) {\r
                List<String> list = new Vector<String>();\r
                // list.add("-Dant.home=" + deployedSlc.getAntHome());\r
                if (deployedSlc.getJavaLibraryPath() != null)\r
@@ -87,8 +209,8 @@ public class SlcScriptLaunchDelegate extends
                return list.toArray(new String[list.size()]);\r
        }\r
 \r
-       private String[] getProgramArguments(SlcRuntime deployedSlc, IFile file,\r
-                       String mode) {\r
+       protected String[] getPre093ProgramArguments(SlcSystem deployedSlc,\r
+                       IFile file, String mode) {\r
                List<String> list = new Vector<String>();\r
                list.add("-f");\r
                list.add(file.getLocation().toFile().getAbsolutePath());\r
@@ -98,6 +220,7 @@ public class SlcScriptLaunchDelegate extends
                return list.toArray(new String[list.size()]);\r
        }\r
 \r
+       // Utilities\r
        private void showError(String message) {\r
                Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench()\r
                                .getActiveWorkbenchWindow().getShell();\r
@@ -108,16 +231,4 @@ public class SlcScriptLaunchDelegate extends
                                status);\r
        }\r
 \r
-       protected boolean checkProjectForEmbedded(IJavaProject project) {\r
-               try {\r
-                       IType antmainType = project.findType(ANT_MAIN);\r
-                       if (antmainType == null)\r
-                               return false;\r
-                       else\r
-                               return true;\r
-               } catch (JavaModelException e) {\r
-                       e.printStackTrace();\r
-                       return false;\r
-               }\r
-       }\r
 }\r