From 613239966af79eddc37f2fcf59aab542bb8fad8c Mon Sep 17 00:00:00 2001 From: lehtonen Date: Thu, 21 May 2015 16:50:57 +0000 Subject: [PATCH] Merged /sysdyn/trunk:r31333 to /sysdyn/branches/1.9 for 1.9.1. refs #5865 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches/1.9@31343 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.modelica/.classpath | 2 +- .../.settings/org.eclipse.jdt.core.prefs | 7 +- org.simantics.modelica/META-INF/MANIFEST.MF | 2 +- .../simantics/modelica/ModelicaException.java | 8 ++ .../simantics/modelica/ModelicaManager.java | 77 +++++++++---------- .../ModelicaPreferenceInitializer.java | 3 +- .../preferences/ModelicaPreferencePage.java | 13 +++- 7 files changed, 59 insertions(+), 53 deletions(-) diff --git a/org.simantics.modelica/.classpath b/org.simantics.modelica/.classpath index 8a8f1668..b1dabee3 100644 --- a/org.simantics.modelica/.classpath +++ b/org.simantics.modelica/.classpath @@ -1,6 +1,6 @@ - + diff --git a/org.simantics.modelica/.settings/org.eclipse.jdt.core.prefs b/org.simantics.modelica/.settings/org.eclipse.jdt.core.prefs index f25d3a62..11f6e462 100644 --- a/org.simantics.modelica/.settings/org.eclipse.jdt.core.prefs +++ b/org.simantics.modelica/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,7 @@ -#Tue Jan 26 16:43:56 EET 2010 eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 -org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.6 +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/org.simantics.modelica/META-INF/MANIFEST.MF b/org.simantics.modelica/META-INF/MANIFEST.MF index 2cf62ac4..3e74b2a6 100644 --- a/org.simantics.modelica/META-INF/MANIFEST.MF +++ b/org.simantics.modelica/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: Modelica Bundle-SymbolicName: org.simantics.modelica;singleton:=true Bundle-Version: 1.1.0.qualifier -Bundle-RequiredExecutionEnvironment: JavaSE-1.6 +Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Require-Bundle: org.eclipse.osgi;bundle-version="3.6.0", org.eclipse.core.runtime;bundle-version="3.6.0", org.simantics.utils;bundle-version="1.1.0", diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaException.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaException.java index 32c99c96..3f06e2e8 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaException.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaException.java @@ -19,4 +19,12 @@ public class ModelicaException extends Exception { super(message); } + public ModelicaException(String message, Throwable cause) { + super(message, cause); + } + + public ModelicaException(Throwable cause) { + super(cause); + } + } diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java index 77656b8e..ae702400 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java @@ -76,7 +76,10 @@ public class ModelicaManager { return new File(home, "bin" + File.separator + "omc" + (onWindows() ? ".exe" : "")); } - private static Process runWithEnvironment(File homeDir, File workDir, File executable, String... parameters) { + private static Process runWithEnvironment(File homeDir, File workDir, File executable, String... parameters) throws IOException { + if (!executable.isFile() && !executable.canExecute()) + throw new FileNotFoundException("executable " + executable.getAbsolutePath() + " not found"); + // construct the command List command = new ArrayList(); command.add(executable.getAbsolutePath()); @@ -118,22 +121,20 @@ public class ModelicaManager { // run the process Process process = null; - try { - process = builder.start(); - } - catch (Exception e) { - e.printStackTrace(); - } - + process = builder.start(); return process; } - private static Process runOMC(File homeDir, File workDir, String... parameters) { + private static Process runOMC(File homeDir, File workDir, String... parameters) throws IOException { return runWithEnvironment(homeDir, workDir, getOMCBinary(homeDir), parameters); } public static String getOMVersion(File home) { - return getProcessOutput(runOMC(home, home, "--version")); + try { + return getProcessOutput(runOMC(home, home, "--version")); + } catch (IOException e) { + return null; + } } public static String getDefaultOMVersion() { @@ -173,8 +174,7 @@ public class ModelicaManager { if (installed != null) return installed; - // TODO: what is the correct error to throw? - throw new Error("OpenModelica could not be found"); + return null; } public static File getInstalledOMHome() { @@ -206,25 +206,22 @@ public class ModelicaManager { * Gets the whole process output in one string * * @param process Process - * @return process output + * @return process output or null if retrieving the process + * output fails for some I/O-related reason */ - public static String getProcessOutput(final Process process) { - try { - BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); + private static String getProcessOutput(final Process process) throws IOException { + String lf = System.getProperty("line.separator"); + try (BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()))) { StringBuilder output = new StringBuilder(); String line; boolean first = true; while ((line = input.readLine()) != null) { if(!first) - output.append(System.getProperty("line.separator")); + output.append(lf); first = false; output.append(line); } - input.close(); return output.toString(); - } catch (Exception e) { - e.printStackTrace(); - return null; } } @@ -413,20 +410,20 @@ public class ModelicaManager { public static void buildFullModel(SimulationLocation location, IModelicaMonitor monitor) throws ModelicaException { - Process process = runOMC(location.omHome, location.getModelDir(), location.fullModelScriptFile.getAbsolutePath()); - printProcessOutput(process, monitor); - try { + Process process = runOMC(location.omHome, location.getModelDir(), location.fullModelScriptFile.getAbsolutePath()); + printProcessOutput(process, monitor); process.waitFor(); + trimExtraFromFullModel(location); + } catch (IOException ex) { + throw new ModelicaException(ex); + } catch (InterruptedException ex) { + throw new ModelicaException(ex); } - catch (InterruptedException e) { - e.printStackTrace(); - } - - trimExtraFromFullModel(location); } private static void trimExtraFromFullModel(SimulationLocation location) { + // TODO: consider throwing IOException, not catching them. try { FileInputStream fs = new FileInputStream(location.fullModelFile); InputStreamReader in = new InputStreamReader(fs); @@ -472,20 +469,16 @@ public class ModelicaManager { */ public static void buildModel(SimulationLocation location, IModelicaMonitor monitor) throws ModelicaException { - - Process process = runOMC(location.omHome, location.getModelDir(), location.modelScriptFile.getAbsolutePath()); - printProcessOutput(process, monitor); - try { + Process process = runOMC(location.omHome, location.getModelDir(), location.modelScriptFile.getAbsolutePath()); + printProcessOutput(process, monitor); process.waitFor(); - } - catch (InterruptedException e) { - e.printStackTrace(); - } - - if (!location.executableFile.isFile()) { - // If executable file was not created, something went wrong - throw new ModelicaException("executable file not created"); + if (!location.executableFile.isFile()) { + // If executable file was not created, something went wrong + throw new ModelicaException("executable file not created"); + } + } catch (IOException | InterruptedException ex) { + throw new ModelicaException(ex); } } @@ -519,7 +512,7 @@ public class ModelicaManager { return runWithEnvironment(location.omHome, location.getModelDir(), location.executableFile, commandString); } - public static String getFlatModelText(SimulationLocation location, IModelicaMonitor monitor, List additional) { + public static String getFlatModelText(SimulationLocation location, IModelicaMonitor monitor, List additional) throws IOException { String[] command = new String[additional.size()+1]; command[0] = location.modelFile.getAbsolutePath(); for (int i = 0; i < additional.size(); i++) { diff --git a/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java b/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java index 553aeaba..2f33fb31 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java +++ b/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java @@ -46,7 +46,8 @@ public class ModelicaPreferenceInitializer extends AbstractPreferenceInitializer } private void useDefault(Preferences node) { - node.put(OpenModelicaPreferences.OM_HOME, ModelicaManager.getDefaultOMHome().getAbsolutePath()); + File omhome = ModelicaManager.getDefaultOMHome(); + node.put(OpenModelicaPreferences.OM_HOME, omhome != null ? omhome.getAbsolutePath() : ""); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java index 0ab5acee..f97816a5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java @@ -86,7 +86,8 @@ public class ModelicaPreferencePage extends FieldEditorPreferencePage implements public void doLoadDefault() { IScopeContext context = ConfigurationScope.INSTANCE; Preferences node = context.getNode(Activator.PLUGIN_ID); - node.put(OpenModelicaPreferences.OM_HOME, ModelicaManager.getDefaultOMHome().getAbsolutePath()); + File omhome = ModelicaManager.getDefaultOMHome(); + node.put(OpenModelicaPreferences.OM_HOME, omhome != null ? omhome.getAbsolutePath() : ""); load(); } }; @@ -113,8 +114,10 @@ public class ModelicaPreferencePage extends FieldEditorPreferencePage implements protected boolean doCheckState() { boolean valid = super.doCheckState(); if(valid) { - // path is a valid directory + // path is a valid directory or empty String path = getStringValue(); + if (path.isEmpty()) + return true; File dir = new File(path); String version = ModelicaManager.getOMVersion(dir); if(version == null || version.isEmpty()) { @@ -134,7 +137,8 @@ public class ModelicaPreferencePage extends FieldEditorPreferencePage implements @Override public void doLoadDefault() { - updatePath(ModelicaManager.getDefaultOMHome().getAbsolutePath()); + File omhome = ModelicaManager.getDefaultOMHome(); + updatePath(omhome != null ? omhome.getAbsolutePath() : ""); } private void updatePath(String newValue) { @@ -149,7 +153,8 @@ public class ModelicaPreferencePage extends FieldEditorPreferencePage implements }; - path.setErrorMessage("Path must be a valid OpenModelica directory"); + path.setErrorMessage("Path must be a valid OpenModelica directory or left empty"); + path.setEmptyStringAllowed(true); addField(path); } -- 2.47.1