From: jplaine Date: Tue, 21 Sep 2010 13:10:09 +0000 (+0000) Subject: Linux (and headless) support X-Git-Tag: simantics-1.2.0~31 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=7437d52dfdcc31e0b6f4a5cc88c02db139a0e80d;p=simantics%2Fsysdyn.git Linux (and headless) support git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18027 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java index 478b81d5..779fcd67 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java @@ -20,14 +20,58 @@ import java.io.PrintStream; import java.util.HashMap; public class ModelicaManager { + + public enum OSType { + APPLE, LINUX, SUN, WINDOWS, UNKNOWN + } + public static OSType calculateOS() { + String osName = System.getProperty("os.name"); + assert osName != null; + osName = osName.toLowerCase(); + if (osName.startsWith("mac os x")) + return OSType.APPLE; + if (osName.startsWith("windows")) + return OSType.WINDOWS; + if (osName.startsWith("linux")) + return OSType.LINUX; + if (osName.startsWith("sun")) + return OSType.SUN; + return OSType.UNKNOWN; + } - - protected static File getOpenModelicaHome() { + public static File getModelicaExecutable() { String dir = System.getenv("OPENMODELICAHOME"); - if(dir == null) - dir = "c:/OpenModelica1.5.0"; - return new File(dir); + + String osName = System.getProperty("os.name"); + OSType os = calculateOS(); + + if (os == OSType.UNKNOWN) + throw new UnsatisfiedLinkError("unknown OS '" + osName + "' cannot load native fastlz library"); + + if(dir == null) { + switch (os) { + case APPLE: + case LINUX: + case SUN: + return new File("/usr/bin/omc"); + case WINDOWS: + return new File("c:/OpenModelica1.5.0/bin/omc.exe"); + default: + throw new UnsatisfiedLinkError("Unsupported operating system: " + os); + } + } else { + switch (os) { + case APPLE: + case LINUX: + case SUN: + return new File(dir+"/bin/omc"); + case WINDOWS: + return new File(dir+"/bin/omc.exe"); + default: + throw new UnsatisfiedLinkError("Unsupported operating system: " + os); + } + } } protected static File createTempDirectory() throws IOException { @@ -119,19 +163,21 @@ public class ModelicaManager { s.close(); } + OSType os = calculateOS(); + String suffix = OSType.WINDOWS.equals(os) ? ".exe" : ""; + return new SimulationLocation( simulationDir, new File(simulationDir, modelName + ".mos"), new File(simulationDir, modelName + "_res.plt"), new File(simulationDir, modelName + "_init.txt"), - new File(simulationDir, modelName + ".exe") + new File(simulationDir, modelName + suffix) ); } public static void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) throws ModelicaException { try { - File modelicaHome = getOpenModelicaHome(); - File omc = new File(modelicaHome, "bin/omc.exe"); + File omc = getModelicaExecutable(); Process process = new ProcessBuilder( omc.getAbsolutePath(), diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF index ca7f9316..802f3295 100644 --- a/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -28,3 +28,4 @@ Export-Package: org.simantics.sysdyn, org.simantics.sysdyn.tableParser Bundle-Activator: org.simantics.sysdyn.Activator Bundle-ActivationPolicy: lazy +Import-Package: org.eclipse.ui diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java index f13e32af..045a8cf5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java @@ -23,6 +23,7 @@ import org.eclipse.ui.console.ConsolePlugin; import org.eclipse.ui.console.IConsoleManager; import org.eclipse.ui.console.MessageConsole; import org.eclipse.ui.console.MessageConsoleStream; +import org.eclipse.ui.PlatformUI; import org.simantics.modelica.IModelicaMonitor; import org.simantics.simulation.experiment.Experiment; import org.simantics.sysdyn.manager.SysdynModel; @@ -31,13 +32,18 @@ public class SimulationJob extends Job { SysdynModel model; Experiment experiment; - ModelicaMonitor monitor; + IModelicaMonitor monitor; public SimulationJob(SysdynModel model, Experiment experiment) { super("Simulate " + model.getConfiguration().getName()); this.model = model; this.experiment = experiment; - this.monitor = new ModelicaMonitor(); + if(PlatformUI.isWorkbenchRunning()) { + this.monitor = new ModelicaMonitor(); + } else { + // Fallback to headless + this.monitor = new HeadlessModelicaMonitor(); + } } @Override @@ -65,6 +71,36 @@ public class SimulationJob extends Job { return "SimulationJob".equals(family); } + private class HeadlessModelicaMonitor implements IModelicaMonitor { + public HeadlessModelicaMonitor() { + } + + @Override + public void message(String message) { + message(message, "hh:mm:ss"); + } + + /** + * Print message to a console with a specified time stamp format + * + * @param message the message to be printed + * @param timeStampFormat simpledateformat timestamp format. null if no timestamp wanted + */ + public void message(String message, String timeStampFormat) { + Calendar cal = Calendar.getInstance(); + SimpleDateFormat sdf = new SimpleDateFormat(timeStampFormat); + String time = sdf.format(cal.getTime()); + + System.out.println("[" + time +"] " + message); + } + + public void clearConsole() { + } + + public void showConsole() { + } + + } private class ModelicaMonitor implements IModelicaMonitor {