Ignore:
Timestamp:
06/12/12 04:08:37 (12 years ago)
Author:
adrpo
Message:
  • start omc in temp directory.
  • do not display error if OPENMODELICALIBRARY is not set.
  • if OPENMODELICAHOME is not set search for omc in the path.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/org.modelica.mdt.omc/src/org/modelica/mdt/omc/OMCProxy.java

    r1234 r1537  
    6161import java.util.Collections;
    6262import java.util.HashMap;
     63import java.util.Iterator;
    6364import java.util.LinkedList;
    6465import java.util.Map;
     66import java.util.Map.Entry;
     67import java.util.Set;
    6568import java.util.StringTokenizer;
    6669
     
    253256     * binary found and the weather outside.
    254257     *
    255      * @return full path to the omc binary
     258     * @return full path to the omc binary and the working folder
    256259     * @throws ConnectException if the path could not be determined
    257260     */
     
    267270        File omcBinary = null;
    268271        File omcWorkingDirectory = null;
     272        File openModelicaHomeDirectory = null;
    269273        if (PreferenceManager.getUseStandardOmcPath())
    270274        {
     
    278282            /*
    279283             * 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!
    281285             */
    282286            String openModelicaHome = System.getenv("OPENMODELICAHOME");
    283287            if(openModelicaHome == null)
    284288            {
    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);
    292305
    293306            /* 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" };
    295309
    296310            for (String subdir : subdirs)
    297311            {
    298312
    299                 String path = omcWorkingDirectory.getAbsolutePath() + File.separator;
     313                String path = openModelicaHomeDirectory.getAbsolutePath() + File.separator;
    300314                path += subdir.equals("") ? binaryName :  subdir + File.separator + binaryName;
    301315
     
    316330            if (omcBinary == null)
    317331            {
    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);
    319333                throw new ConnectException("Unable to start the OpenModelica Compiler, binary not found");
    320334            }
     
    334348
    335349            /*
    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
    348351             */
    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       
    381357        return new File[] {omcBinary, omcWorkingDirectory};
    382358    }
     
    13321308            try
    13331309            {
     1310                String[] env = null;
    13341311                // 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);
    13361337                //create thread for reading inputStream (process' stdout)
    13371338                outThread= new StreamReaderThread(proc.getInputStream(),System.out);
     
    14131414    }
    14141415
     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   
    14151437}
Note: See TracChangeset for help on using the changeset viewer.