Changeset 1537 for trunk/org.modelica.mdt.omc/src/org/modelica/mdt
- Timestamp:
- 06/12/12 04:08:37 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/org.modelica.mdt.omc/src/org/modelica/mdt/omc/OMCProxy.java
r1234 r1537 61 61 import java.util.Collections; 62 62 import java.util.HashMap; 63 import java.util.Iterator; 63 64 import java.util.LinkedList; 64 65 import java.util.Map; 66 import java.util.Map.Entry; 67 import java.util.Set; 65 68 import java.util.StringTokenizer; 66 69 … … 253 256 * binary found and the weather outside. 254 257 * 255 * @return full path to the omc binary 258 * @return full path to the omc binary and the working folder 256 259 * @throws ConnectException if the path could not be determined 257 260 */ … … 267 270 File omcBinary = null; 268 271 File omcWorkingDirectory = null; 272 File openModelicaHomeDirectory = null; 269 273 if (PreferenceManager.getUseStandardOmcPath()) 270 274 { … … 278 282 /* 279 283 * Standard path to omc (or omc.exe) binary is encoded in OPENMODELICAHOME 280 * variable. 284 * variable. If we don't find it just search the path! 281 285 */ 282 286 String openModelicaHome = System.getenv("OPENMODELICAHOME"); 283 287 if(openModelicaHome == null) 284 288 { 285 final String m = "Environment variable OPENMODELICAHOME not set"; 286 logOMCStatus("Environment variable OPENMODELICAHOME not set,"+ 287 " don't know how to start OMC from standard path.", true); 288 throw new ConnectException(m); 289 } 290 291 omcWorkingDirectory = new File(openModelicaHome); 289 logOMCStatus("OPENMODELICAHOME environment variable is NULL, trying the PATH variable", true); 290 File omc = findExecutableOnPath(binaryName); 291 if (omc != null) 292 { 293 logOMCStatus("Found omc executable in the path here: " + omc.getAbsolutePath(), true); 294 openModelicaHome = omc.getParentFile().getParentFile().getAbsolutePath(); 295 } 296 else 297 { 298 final String m = "Environment variable OPENMODELICAHOME is not set and we could not find: " + binaryName + " in the PATH"; 299 logOMCStatus(m, true); 300 throw new ConnectException(m); 301 } 302 } 303 304 openModelicaHomeDirectory = new File(openModelicaHome); 292 305 293 306 /* the subdirectories where omc binary may be located, hurray for standards! */ 294 String[] subdirs = { "", "bin", "Compiler" }; 307 // adrpo 2012-06-12 do not support the old ways! "/omc" and "Compiler/omc" 308 String[] subdirs = { "bin" }; 295 309 296 310 for (String subdir : subdirs) 297 311 { 298 312 299 String path = o mcWorkingDirectory.getAbsolutePath() + File.separator;313 String path = openModelicaHomeDirectory.getAbsolutePath() + File.separator; 300 314 path += subdir.equals("") ? binaryName : subdir + File.separator + binaryName; 301 315 … … 316 330 if (omcBinary == null) 317 331 { 318 logOMCStatus("Could not find omc-binary on the OPENMODELICAHOME path ", true);332 logOMCStatus("Could not find omc-binary on the OPENMODELICAHOME path or in the PATH variable", true); 319 333 throw new ConnectException("Unable to start the OpenModelica Compiler, binary not found"); 320 334 } … … 334 348 335 349 /* 336 * take an educated guess at where the user wants the binary to be 337 * started. The guessing heuristics are as follows: 338 * 339 * If binary is inside the 'bin' or 'compiler' directory, use the 340 * above directory as working directory. 341 * 342 * Otherwise use the directory where binary is located as working 343 * directory. 344 * 345 * e.g. binary path /foo/bar/bin/omc => working directory /foo/bar/work 346 * binary path /foo/bar/compiler/omc => working directory /foo/bar/work 347 * binary path /foo/bar/omc => working directory /foo/bar 350 * always start in /tmp 348 351 */ 349 File parent = omcBinary.getParentFile(); 350 351 if (parent.getName().equalsIgnoreCase("bin") || 352 parent.getName().equalsIgnoreCase("compiler")) 353 { 354 omcWorkingDirectory = parent.getParentFile(); 355 File work = new File(omcWorkingDirectory.getAbsolutePath()+File.pathSeparator+"tmp"); 356 if (work.exists()) 357 { 358 omcWorkingDirectory = work; 359 } 360 else 361 { 362 work = new File(omcWorkingDirectory.getAbsolutePath()+File.pathSeparator+"work"); 363 if (work.exists()) 364 { 365 omcWorkingDirectory = work; 366 } 367 else 368 { 369 // start in tmp. 370 } 371 } 372 } 373 else 374 { 375 // start in tmp 376 omcWorkingDirectory = parent; 377 } 378 379 } 380 352 } 353 // set the working directory to temp/OpenModelica 354 omcWorkingDirectory = new File(System.getProperty("java.io.tmpdir")); 355 logOMCStatus("Using working directory '" + omcWorkingDirectory.getAbsolutePath() + "'", true); 356 381 357 return new File[] {omcBinary, omcWorkingDirectory}; 382 358 } … … 1332 1308 try 1333 1309 { 1310 String[] env = null; 1334 1311 // prepare buffers for process output and error streams 1335 proc=Runtime.getRuntime().exec(cmd, null, workingDirectory); 1312 if (System.getenv("OPENMODELICAHOME") == null) 1313 { 1314 Map<String, String> envMap = System.getenv(); 1315 Set<Entry<String, String>> entrySet = envMap.entrySet(); 1316 Collection<String> lst = new ArrayList<String>(); 1317 String x = "OPENMODELICAHOME=" + omcBinary.getParentFile().getParentFile().getAbsolutePath(); 1318 lst.add(x); 1319 1320 if (System.getenv("OPENMODELICALIBRARY") == null) 1321 { 1322 String y = "OPENMODELICALIBRARY=" + 1323 omcBinary.getParentFile().getParentFile().getAbsolutePath() + 1324 "/lib/omlibrary"; 1325 lst.add(y); 1326 } 1327 1328 Iterator<Entry<String, String>> i = entrySet.iterator(); 1329 while (i.hasNext()) 1330 { 1331 Entry<String,String> z = i.next(); 1332 lst.add(z.getKey() + "=" + z.getValue()); 1333 } 1334 env = lst.toArray(new String[lst.size()]); 1335 } 1336 proc=Runtime.getRuntime().exec(cmd, env, workingDirectory); 1336 1337 //create thread for reading inputStream (process' stdout) 1337 1338 outThread= new StreamReaderThread(proc.getInputStream(),System.out); … … 1413 1414 } 1414 1415 1416 private File findExecutableOnPath(String executableName) 1417 { 1418 String systemPath = System.getenv("PATH"); 1419 if (systemPath == null) // try with small letters 1420 systemPath = System.getenv("path"); 1421 String[] pathDirs = systemPath.split(File.pathSeparator); 1422 1423 File fullyQualifiedExecutable = null; 1424 for (String pathDir : pathDirs) 1425 { 1426 File file = new File(pathDir, executableName); 1427 if (file.isFile()) 1428 { 1429 fullyQualifiedExecutable = file; 1430 break; 1431 } 1432 } 1433 return fullyQualifiedExecutable; 1434 } 1435 1436 1415 1437 }
Note: See TracChangeset
for help on using the changeset viewer.