Ignore:
Timestamp:
03/07/06 11:38:08 (19 years ago)
Author:
boris
Message:
  • added 'custon omc-path setting'
  • redone the modelica preference page a bit
File:
1 edited

Legend:

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

    r393 r397  
    6262import org.modelica.mdt.core.compiler.ModelicaParser;
    6363import org.modelica.mdt.core.compiler.UnexpectedReplyException;
     64import org.modelica.mdt.core.preferences.PreferenceManager;
    6465import org.modelica.mdt.internal.core.ElementLocation;
    6566import org.modelica.mdt.internal.core.ErrorManager;
     
    8283    private static OmcCommunication omcc;
    8384   
     85   
     86    enum osType { WINDOWS, UNIX };
     87   
    8488    /* what Operating System we're running on */
    85     private static String os;
     89    private static osType os;
    8690   
    8791    /* indicates if we've setup the communication with OMC */
     
    163167    {
    164168        String fileName = null;
    165         if(os.equals("Unix"))
    166         {
    167             /* This mirrors the way OMC creates the object file. */
     169
     170        /* This mirrors the way OMC creates the object file. */     
     171        switch (os)
     172        {
     173        case UNIX:
    168174            String username = System.getenv("USER");
    169175            if(username == null)
     
    172178            }
    173179            fileName = "/tmp/openmodelica." + username + ".objid";
    174         }
    175         else if(os.equals("Windows"))
    176         {
     180            break;
     181        case WINDOWS:
    177182            String temp = System.getenv("TMP");         
    178183            fileName = temp + "\\openmodelica.objid";
     184            break;
     185        default:
     186            ErrorManager.logBug("org.modelica.mdt.omc",
     187                    "os variable set to unexpected os-type");
    179188        }
    180189       
     
    186195   
    187196    /**
     197     * With the help of voodoo magic determines the path to the
     198     * omc binary that user (probably) wants to use and the working
     199     * direcoty of where that binary (most likely) should be started in
     200     *
     201     * This will returns for example 'c:\openmodelica132\omc.exe'
     202     * or '/usr/local/share/openmodelica/omc' depending on
     203     * such factors as: OS type, environment variables settings,
     204     * plugin user preferences, where the first matching
     205     * binary found and the weather outside.
     206     *
     207     * @return full path to the omc binary 
     208     * @throws ConnectException if the path could not be determined
     209     */
     210    private static File[] getOmcBinaryPaths() throws ConnectException
     211    {
     212        String binaryName = "omc";
     213       
     214        if (os == osType.WINDOWS)
     215        {
     216            binaryName += ".exe";
     217        }
     218       
     219        File omcBinary = null;
     220        File omcWorkingDirectory = null;
     221        if (PreferenceManager.getUseStandardOmcPath())
     222        {
     223            /*
     224             * user specified that standard path to omc should be used,
     225             * try to determine the omc path via the OPENMODELICAHOME and
     226             * by checking in it's varius subdirectory for the omc binary file
     227             */
     228            logOMCStatus("Using standard path to omc-binary");
     229           
     230            /*
     231             * Standard path to omc (or omc.exe) binary is encoded in OPENMODELICAHOME
     232             * variable.
     233             */
     234            String openModelicaHome = System.getenv("OPENMODELICAHOME");
     235            if(openModelicaHome == null)
     236            {
     237                final String m = "Environment variable OPENMODELICAHOME not set";
     238                logOMCStatus("Environment variable OPENMODELICAHOME not set,"+
     239                        " don't know how to start OMC from standard path.");
     240                throw new ConnectException(m);
     241            }
     242           
     243            omcWorkingDirectory = new File(openModelicaHome);
     244           
     245            /* the subdirectories where omc binary may be located, hurray for standards! */
     246            String[] subdirs = { "", "bin", "compiler" };
     247           
     248            for (String subdir : subdirs)
     249            {
     250                File file = new File(openModelicaHome +
     251                        subdir + File.separator + binaryName);
     252
     253                if (file.exists())
     254                {
     255                    omcBinary = file;
     256                    logOMCStatus("Using omc-binary at '" + omcBinary.getAbsolutePath() + "'");
     257                    break;
     258                }
     259            }
     260           
     261            if (omcBinary == null)
     262            {
     263                logOMCStatus("Could not fine omc-binary on the standard path");
     264                throw new ConnectException("Unable to start the OpenModelica Compiler, binary not found");
     265            }
     266        }
     267        else
     268        {
     269            omcBinary = new File(PreferenceManager.getCustomOmcPath());
     270           
     271            logOMCStatus("Using userspecified omc-binary at '" +
     272                    omcBinary.getAbsolutePath() + "'");
     273           
     274            if (!omcBinary.exists())
     275            {
     276                logOMCStatus("file '" + omcBinary.getAbsolutePath() + "' does not exist");
     277                throw new ConnectException("Specified omc-binary '" + omcBinary.getAbsolutePath() +
     278                    "' does not exist");
     279            }
     280           
     281            /*
     282             * take an educated guess at where the user wants the binary to be
     283             * started. The guessing heuristics are as follows:
     284             *
     285             * If binary is inside the 'bin' or 'compiler' directory, use the
     286             * above directory as working directory.
     287             *
     288             * Otherwise use the directory where binary is located as working
     289             * directory.
     290             *
     291             * e.g. binary path /foo/bar/bin/omc      => working directory /foo/bar
     292             *      binary path /foo/bar/compiler/omc => working directory /foo/bar
     293             *      binary path /foo/bar/omc          => working directory /foo/bar
     294             */
     295            File parent = omcBinary.getParentFile();
     296           
     297            if (parent.getName().equalsIgnoreCase("bin") ||
     298                parent.getName().equalsIgnoreCase("compiler"))
     299            {
     300                omcWorkingDirectory = parent.getParentFile();
     301            }
     302            else
     303            {
     304                omcWorkingDirectory = parent;
     305            }
     306           
     307        }
     308
     309        return new File[] {omcBinary, omcWorkingDirectory};
     310    }
     311   
     312    /**
    188313     * Start a new OMC server.
    189314     */
    190315    private static void startServer() throws ConnectException
    191316    {
    192         String[] pathsToOmc = new String[3];
    193         File workingDirectory;
    194 
    195         /*
    196          * Path to omc (or omc.exe) can be found in the OPENMODELICAHOME
    197          * variable.
    198          */
    199         String omHome = System.getenv("OPENMODELICAHOME");
    200         if(omHome == null)
    201         {
    202             final String m = "Environment variable OPENMODELICAHOME not set";
    203             logOMCStatus("Environment variable OPENMODELICAHOME not set,"+
    204                     " don't know how to start OMC.");
    205             throw new ConnectException(m);
    206         }
    207        
    208         if(os.equals("Unix"))
    209         {
    210             pathsToOmc[0] = omHome + "/bin/omc";
    211             pathsToOmc[1] = omHome + "/omc";
    212             pathsToOmc[2] = omHome + "/Compiler/omc";
    213         }
    214         else if(os.equals("Windows"))
    215         {
    216             pathsToOmc[0] = omHome + "\\bin\\omc.exe";
    217             pathsToOmc[1] = omHome + "\\omc.exe";
    218             pathsToOmc[2] = omHome + "\\Compiler\\omc.exe";
    219         }
    220        
    221         /* We should start OMC from the OPENMODELICAHOME directory */
    222         workingDirectory = new File(omHome);
    223 
     317        File tmp[] = getOmcBinaryPaths();
     318
     319        File omcBinary = tmp[0];
     320        File workingDirectory = tmp[1];
     321
     322       
    224323        /*
    225324         * Delete old object reference file. We need to do this because we're
     
    234333        }
    235334       
    236         for(int i = 0;i < 3;i++)
    237         {
    238             String command[] = { pathsToOmc[i], "+d=interactiveCorba" };
    239             try
    240             {
    241                 logOMCStatus("Running command " + command[0] + " " + command[1]);
    242                 Runtime.getRuntime().exec(command, null, workingDirectory);
    243                 logOMCStatus("Command run successfully.");
    244                
    245                 break; /* exit the for loop, we've started OMC */
    246             }
    247             catch(IOException e)
    248             {
    249                 if(i < 2)
    250                 {
    251                     logOMCStatus("Error running command " + e.getMessage()
    252                             + ", trying alternative path to the binary.");
    253                    
    254                     /* If the call failed, try another path to the compiler */
    255                 }
    256                 else
    257                 {
    258                     /* If all the paths to OMC were wrong, throw an exception */
    259                    
    260                     logOMCStatus("Unable to start OMC, giving up.");
    261                     throw new ConnectException
    262                         ("Unable to start the OpenModelica Compiler. "
    263                          + "Tried starting " + pathsToOmc[0] + ", "
    264                          + pathsToOmc[1] + " and " + pathsToOmc[2]);
    265                 }
    266             }           
    267         }
    268 
    269         logOMCStatus("Wait for OMC CORBA object reference to appear on disk.");
     335        String command[] = { omcBinary.getAbsolutePath(), "+d=interactiveCorba" };
     336        try
     337        {
     338            logOMCStatus("Running command " + command[0] + " " + command[1]);
     339            logOMCStatus("Setting working directory to " + workingDirectory.getAbsolutePath());
     340            Runtime.getRuntime().exec(command, null, workingDirectory);
     341            logOMCStatus("Command run successfully.");
     342        }
     343        catch(IOException e)
     344        {
     345            logOMCStatus("Error running command " + e.getMessage());
     346            logOMCStatus("Unable to start OMC, giving up.");
     347            throw new ConnectException
     348                ("Unable to start the OpenModelica Compiler. ");
     349        }           
     350
     351        logOMCStatus("Waiting for OMC CORBA object reference to appear on disk.");
    270352       
    271353        /*
     
    282364            catch(InterruptedException e)
    283365            {
    284                 // Ignore
     366                /* ignore */
    285367            }
    286368            ticks++;
    287369           
    288             /* If we've waited for 5 seconds, abort the wait for OMC */
     370            /* If we've waited for around 5 seconds, abort the wait for OMC */
    289371            if(ticks > 50)
    290372            {
     
    326408     * the default is Unix.
    327409     */
    328     private static String getOs()
     410    private static osType getOs()
    329411    {
    330412        String osName = System.getProperty("os.name");
    331413        if(osName.contains("Linux"))
    332414        {
    333             return "Unix";
     415            return osType.UNIX;
    334416        }
    335417        else if(osName.contains("Windows"))
    336418        {
    337             return "Windows";
     419            return osType.WINDOWS;
    338420        }
    339421        else
    340422        {
    341             ErrorManager.logWarning("'" + osName + "' is unsupported OS");
     423            ErrorManager.logWarning("'" + osName + "' not officialy supported OS");
    342424            /* If the OS is not GNU/Linux or Windows, default to Unix */
    343             return "Unix";
     425            return osType.UNIX;
    344426        }
    345427    }
Note: See TracChangeset for help on using the changeset viewer.