return new File(home, "bin" + File.separator + "omc" + (onWindows() ? ".exe" : ""));\r
}\r
\r
- private static Process runWithEnvironment(File homeDir, File workDir, File executable, String... parameters) {\r
+ private static Process runWithEnvironment(File homeDir, File workDir, File executable, String... parameters) throws IOException {\r
+ if (!executable.isFile() && !executable.canExecute())\r
+ throw new FileNotFoundException("executable " + executable.getAbsolutePath() + " not found");\r
+\r
// construct the command\r
List<String> command = new ArrayList<String>();\r
command.add(executable.getAbsolutePath());\r
\r
// run the process\r
Process process = null;\r
- try {\r
- process = builder.start();\r
- }\r
- catch (Exception e) {\r
- e.printStackTrace();\r
- }\r
- \r
+ process = builder.start();\r
return process;\r
}\r
\r
- private static Process runOMC(File homeDir, File workDir, String... parameters) {\r
+ private static Process runOMC(File homeDir, File workDir, String... parameters) throws IOException {\r
return runWithEnvironment(homeDir, workDir, getOMCBinary(homeDir), parameters);\r
}\r
\r
public static String getOMVersion(File home) {\r
- return getProcessOutput(runOMC(home, home, "--version"));\r
+ try {\r
+ return getProcessOutput(runOMC(home, home, "--version"));\r
+ } catch (IOException e) {\r
+ return null;\r
+ }\r
}\r
\r
public static String getDefaultOMVersion() {\r
if (installed != null)\r
return installed;\r
\r
- // TODO: what is the correct error to throw?\r
- throw new Error("OpenModelica could not be found");\r
+ return null;\r
}\r
\r
public static File getInstalledOMHome() {\r
* Gets the whole process output in one string\r
* \r
* @param process Process\r
- * @return process output\r
+ * @return process output or <code>null</code> if retrieving the process\r
+ * output fails for some I/O-related reason\r
*/\r
- public static String getProcessOutput(final Process process) {\r
- try {\r
- BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));\r
+ private static String getProcessOutput(final Process process) throws IOException {\r
+ String lf = System.getProperty("line.separator");\r
+ try (BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()))) {\r
StringBuilder output = new StringBuilder();\r
String line;\r
boolean first = true;\r
while ((line = input.readLine()) != null) {\r
if(!first)\r
- output.append(System.getProperty("line.separator"));\r
+ output.append(lf);\r
first = false;\r
output.append(line);\r
}\r
- input.close();\r
return output.toString();\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- return null;\r
}\r
}\r
\r
\r
public static void buildFullModel(SimulationLocation location, IModelicaMonitor monitor) \r
throws ModelicaException {\r
- Process process = runOMC(location.omHome, location.getModelDir(), location.fullModelScriptFile.getAbsolutePath());\r
- printProcessOutput(process, monitor);\r
- \r
try {\r
+ Process process = runOMC(location.omHome, location.getModelDir(), location.fullModelScriptFile.getAbsolutePath());\r
+ printProcessOutput(process, monitor);\r
process.waitFor();\r
+ trimExtraFromFullModel(location);\r
+ } catch (IOException ex) {\r
+ throw new ModelicaException(ex);\r
+ } catch (InterruptedException ex) {\r
+ throw new ModelicaException(ex);\r
}\r
- catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
-\r
- trimExtraFromFullModel(location);\r
}\r
\r
private static void trimExtraFromFullModel(SimulationLocation location) {\r
+ // TODO: consider throwing IOException, not catching them.\r
try {\r
FileInputStream fs = new FileInputStream(location.fullModelFile);\r
InputStreamReader in = new InputStreamReader(fs);\r
*/\r
public static void buildModel(SimulationLocation location, IModelicaMonitor monitor) \r
throws ModelicaException {\r
- \r
- Process process = runOMC(location.omHome, location.getModelDir(), location.modelScriptFile.getAbsolutePath());\r
- printProcessOutput(process, monitor);\r
- \r
try {\r
+ Process process = runOMC(location.omHome, location.getModelDir(), location.modelScriptFile.getAbsolutePath());\r
+ printProcessOutput(process, monitor);\r
process.waitFor();\r
- }\r
- catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- if (!location.executableFile.isFile()) {\r
- // If executable file was not created, something went wrong\r
- throw new ModelicaException("executable file not created");\r
+ if (!location.executableFile.isFile()) {\r
+ // If executable file was not created, something went wrong\r
+ throw new ModelicaException("executable file not created");\r
+ }\r
+ } catch (IOException | InterruptedException ex) {\r
+ throw new ModelicaException(ex);\r
}\r
}\r
\r
return runWithEnvironment(location.omHome, location.getModelDir(), location.executableFile, commandString);\r
}\r
\r
- public static String getFlatModelText(SimulationLocation location, IModelicaMonitor monitor, List<String> additional) {\r
+ public static String getFlatModelText(SimulationLocation location, IModelicaMonitor monitor, List<String> additional) throws IOException {\r
String[] command = new String[additional.size()+1];\r
command[0] = location.modelFile.getAbsolutePath();\r
for (int i = 0; i < additional.size(); i++) {\r
public void doLoadDefault() {\r
IScopeContext context = ConfigurationScope.INSTANCE;\r
Preferences node = context.getNode(Activator.PLUGIN_ID);\r
- node.put(OpenModelicaPreferences.OM_HOME, ModelicaManager.getDefaultOMHome().getAbsolutePath());\r
+ File omhome = ModelicaManager.getDefaultOMHome();\r
+ node.put(OpenModelicaPreferences.OM_HOME, omhome != null ? omhome.getAbsolutePath() : "");\r
load();\r
}\r
};\r
protected boolean doCheckState() {\r
boolean valid = super.doCheckState();\r
if(valid) {\r
- // path is a valid directory\r
+ // path is a valid directory or empty\r
String path = getStringValue();\r
+ if (path.isEmpty())\r
+ return true;\r
File dir = new File(path);\r
String version = ModelicaManager.getOMVersion(dir);\r
if(version == null || version.isEmpty()) {\r
\r
@Override\r
public void doLoadDefault() {\r
- updatePath(ModelicaManager.getDefaultOMHome().getAbsolutePath());\r
+ File omhome = ModelicaManager.getDefaultOMHome();\r
+ updatePath(omhome != null ? omhome.getAbsolutePath() : "");\r
}\r
\r
private void updatePath(String newValue) {\r
\r
};\r
\r
- path.setErrorMessage("Path must be a valid OpenModelica directory");\r
+ path.setErrorMessage("Path must be a valid OpenModelica directory or left empty");\r
+ path.setEmptyStringAllowed(true);\r
addField(path);\r
}\r
\r