Ignore:
Timestamp:
2009-05-18T13:55:00+12:00 (15 years ago)
Author:
ak19
Message:

Part of changes to how GLI deals with an independently launched GSI which uses llssite.cfg (as opposed to one that GLI launches and which uses glisite.cfg). Already commited the corresponding changes made to Gatherer.java and PreviewButton.java

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gli/trunk/src/org/greenstone/gatherer/greenstone/LocalLibraryServer.java

    r19484 r19508  
    140140    static public boolean isRunning()
    141141    {
    142     if (!running) return false;
     142    if (!running) return false; // if the url is pending, then running would also be false (server not started up yet)
     143
    143144    llssite_cfg_file.load(true);
    144     if (llssite_cfg_file.getURL() == null)  return false;
     145    String url = llssite_cfg_file.getURL();
     146    if (url == null) return false;
     147   
     148    // Called by Gatherer to check whether we need to stop the server.
     149    // if the url is pending, then the GSI hasn't started the server up yet
     150    // Already covered in !running
     151    //if (url.equals(LLSSiteConfig.URL_PENDING)) return false;
     152
    145153    return true;
    146154    }
     
    184192
    185193    llssite_cfg_file = new LLSSiteConfig(local_library_server_file);
    186    
     194       
     195    // If the user launched the GSI independent of GLI, but user has not pressed
     196    // Enter Library yet, then we will obtain the previewURL later.
     197    if(LocalLibraryServer.isURLPending()) {
     198        // running is still false when the URL is pending because only GSI is running, not the server
     199        return;
     200    }
     201
    187202    // Spawn local library server process
    188203    String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
     
    236251    static public void stop()
    237252    {
    238     if (running == false) {
     253    if (!running) {
     254        // also the case if the URL is pending in an independently launched GSI
     255        return;
     256    }
     257
     258    // don't (can't) shutdown the GSI/server if it was launched independent of GLI
     259    if(llssite_cfg_file.isIndependentGSI()) {
     260        System.err.println("**** EXITING. Config file exists: " + llssite_cfg_file.exists());
    239261        return;
    240262    }
     
    255277    llssite_cfg_file.load(false);
    256278    int attempt_count = 0;
    257     while (llssite_cfg_file.getURL() != null) {
     279    String url = llssite_cfg_file.getURL();
     280    while (url != null && !url.equals(LLSSiteConfig.URL_PENDING)) { // if pending, the server is already stopped (not running)
    258281        new OneSecondWait();  // Wait one second (give or take)
    259282        llssite_cfg_file.load(false);
     
    269292        attempt_count = 0;
    270293        }
     294        // read the url again to see if it's updated
     295        url = llssite_cfg_file.getURL();
    271296    }
    272297
     
    275300
    276301    // If the local server is still running then our changed values will get overwritten.
    277     if (llssite_cfg_file.getURL() != null) {
     302    url = llssite_cfg_file.getURL();
     303    if (url != null && !url.equals(LLSSiteConfig.URL_PENDING)) {
    278304        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    279305    }
     
    346372    }
    347373
     374    /** Returns true if we're waiting on the user to click on Enter Library button
     375     * in an independently launched GSI (one not launched by GLI). */
     376    static public boolean isURLPending() {
     377    if(Configuration.library_url != null) {
     378        return false; // don't need to do anything as  we already have the url
     379    }
     380
     381    llssite_cfg_file.load(true); // don't force reload, load only if modified
     382
     383    String url = llssite_cfg_file.getURL();
     384    System.err.println("**** url is: " + url);
     385
     386    if(url != null) {
     387        if(url.equals(LLSSiteConfig.URL_PENDING)) {
     388        running = false; // imagine if they restarted an external GSI
     389        return true;
     390        } else {
     391        // a valid URL at last
     392        try {
     393            Configuration.library_url = new URL(url);
     394            System.err.println("**** set the config.library url to: " + Configuration.library_url);
     395            running = true;
     396        }
     397        catch (MalformedURLException exception) {
     398            exception.printStackTrace();
     399            DebugStream.printStackTrace(exception);
     400        }
     401        return false;
     402        }
     403    }
     404
     405    // If either the URL is null--which means no independent GSI (Greenstone server interface
     406    // app) was launched--or if the 'URL' doesn't say it's pending, then we're not waiting
     407    return false;
     408    }
     409
    348410    static public void checkServerRunning() {
    349     if (!running) return; // don't worry about it if its not supposed to be running
     411    if (!running) return; // don't worry about it if it's not supposed to be running
    350412    llssite_cfg_file.load(true); // don't force reload, load only if modified
    351 
     413   
    352414    String url = llssite_cfg_file.getURL();
    353415    if(url != null) {
     
    360422    } else {
    361423        // need to restart the server again
    362         llssite_cfg_file.set();
    363 
     424 
     425        // if we were running an independently launched GSI before, but the GSI has been
     426        // exited, it would need to be relaunched, but from within GLI (dependent) this time.
     427        if(llssite_cfg_file.isIndependentGSI()) {
     428        // first save the current configFile (llssite.cfg), then set to use glisite.cfg
     429        llssite_cfg_file.save();
     430        llssite_cfg_file.relaunchAsDependentGSI(local_library_server_file);
     431        } else {
     432        // We were using glisite.cfg before, don't need to swap config files
     433        llssite_cfg_file.set();
     434        }
    364435        // Spawn local library server process
    365436        String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
     
    391462
    392463    static public class LLSSiteConfig
    393     extends LinkedHashMap {
     464    extends LinkedHashMap
     465    {
     466    private File configFile;
     467
    394468    private File llssite_cfg;
    395469    private File glisite_cfg;
     
    414488    static final private String URL = "url";
    415489
     490    static final public String URL_PENDING = "URL_pending";
     491
    416492    public LLSSiteConfig(File server_exe) {
    417493        debug("New LLSSiteConfig for: " + server_exe.getAbsolutePath());
     
    420496        glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG);
    421497
    422         File configFile = null;
     498        configFile = null;
     499
     500        autoenter_initial = null;
     501        start_browser_initial = null;
     502       
     503        // first test if server was started independently of GLI
     504        // if so, the config file we'd be using would be llssite.cfg
     505        if(!usingLLS_configFile()) { // if we were using llssite_cfg, this would have loaded it in
     506        // else we try using the glisite configfile
     507        useGLISiteCfg(server_exe);
     508        }
     509
     510        if(configFile != null && configFile.exists()) {
     511        lastModified = configFile.lastModified();
     512        }
     513    }
     514
     515    /** Tries to get a glisite.cfg file and then loads it, setting it as the configFile */
     516    public void useGLISiteCfg(File server_exe) {
    423517        if(!glisite_cfg.exists()) { // create it from the templates or the llssite.cfg file
    424 
     518       
    425519        File llssite_cfg_in = new File(server_exe.getParentFile(), LLSSITE_CFG+".in");
    426520        File glisite_cfg_in = new File(server_exe.getParentFile(), GLISITE_CFG+".in");
    427 
     521       
    428522        // need to generate glisite_cfg from glisite_cfg_in, llssite_cfg or llssite.cfg.in
    429523        if(glisite_cfg_in.exists()) {
     
    441535        }
    442536        }
    443 
     537        // use the config file now
    444538        if(glisite_cfg.exists()) {
    445539        configFile = glisite_cfg;
     540        load(false); // force reload
    446541        }
    447 
    448         autoenter_initial = null;
    449         start_browser_initial = null;
    450         if(configFile != null) {
     542    }
     543
     544    /** Tests whether the server interface is up, running independently of GLI
     545     * If so, we don't need to launch the server interface.
     546     * The server interface may not have started up the server itself though
     547     * (in which case the server URL would be URL_PENDING).
     548     * This method returns true if the server interface has already started
     549     * and, if so, it would have loaded in the llssite_cfg configFile.
     550     */
     551    private boolean usingLLS_configFile() {
     552        if(!llssite_cfg.exists()) {
     553        return false;
     554        }
     555
     556        // check if the configfile contains the URL line
     557        configFile = llssite_cfg;
     558        load(false); // force load
     559
     560        if(getURL() == null) {
     561        configFile = null;
     562        clear(); // we're not using llssite_cfg, so clear the values we just read
     563        return false;
     564        }
     565
     566        System.err.println("***** we're using llssite_configfile");
     567        return true;
     568    }
     569   
     570    /** @return true if GSI was started up independently and outside of GLI.
     571     * In such a case, GLI would be using llssite_cfg. */
     572    public boolean isIndependentGSI() {
     573        return (configFile == llssite_cfg);
     574    }
     575
     576    /** Call this when an independently launched GSI server has been stopped
     577     * (one using llssite_cfg) and GLI needs to next launch another server.
     578     * In such a case, since GLI itself is relaunching the GSI, we use glisite_cfg.
     579    */
     580    public void relaunchAsDependentGSI(File server_exe) {
     581        useGLISiteCfg(server_exe);
     582    }
     583
     584    public boolean exists() {
     585        return configFile.exists();
     586    }
     587
     588    public String getLocalHostURL() {
     589        StringBuffer url = new StringBuffer(LOCAL_HOST);
     590        url.append(COLON);
     591        url.append((String)get(PORTNUMBER));
     592        String enterlib = (String)get(ENTERLIB);
     593        if(enterlib == null || enterlib.length() == 0) {
     594        // Use the default /gsdl and hope for the best.
     595        url.append(SEPARATOR);
     596        url.append(GSDL);
     597        }
     598        else {
     599        if(!enterlib.startsWith(SEPARATOR)) {
     600            url.append(SEPARATOR);
     601        }
     602        url.append(enterlib);
     603        }
     604        enterlib = null;
     605        debug("Found Local Library Address: " + url.toString());
     606        return url.toString();
     607    }
     608
     609    /** @return the cmd-line parameter for the configfile used to launch
     610     * the server through GLI: --config <glisite.cfg/llssite.cfg file path>. */
     611    public String getSiteConfigFilename() {
     612        return SPECIFIC_CONFIG + configFile.getAbsolutePath();
     613    }
     614
     615    public String getURL() {
     616        // URL is made from url and portnumber
     617        String url = (String) get(URL);
     618
     619        // server interface is already up, independent of GLI
     620        // but it has not started the server (hence URL is pending)
     621        if(url != null && url.equals(URL_PENDING)) {
     622        return url;
     623        }
     624
     625        if(!Utility.isWindows()) {
     626        return url;
     627        }
     628
     629        if(url != null) {
     630        StringBuffer temp = new StringBuffer(url);
     631        temp.append(COLON);
     632        temp.append((String)get(PORTNUMBER));
     633        String enterlib = (String)get(ENTERLIB);
     634        if(enterlib == null || enterlib.length() == 0) {
     635            // Use the default /greenstone prefix and hope for the best.
     636            temp.append(SEPARATOR);
     637            temp.append(GSDL);
     638        }
     639        else {
     640            if(!enterlib.startsWith(SEPARATOR)) {
     641            temp.append(SEPARATOR);
     642            }
     643            temp.append(enterlib);
     644        }
     645        enterlib = null;
     646        url = temp.toString();
     647        }
     648        debug("Found Local Library Address: " + url);
     649        return url;
     650    }
     651
     652    public boolean isModified() {
     653        return (lastModified != configFile.lastModified());
     654    }
     655
     656    public void load(boolean reloadOnlyIfModified) {       
     657
     658        if(configFile == null) {
     659        debug(configFile.getAbsolutePath() + " cannot be found!");
     660        }
     661
     662        if(isModified()) {
     663        lastModified = configFile.lastModified();
     664        } else if(reloadOnlyIfModified) {
     665        return; // asked to reload only if modified. Don't reload since not modified
     666        }
     667
     668        if(configFile.exists()) {
    451669        debug("Load: " + configFile.getAbsolutePath());
    452670        clear();
     
    473691        }
    474692        }
    475 
    476         if(glisite_cfg.exists()) {
    477         lastModified = glisite_cfg.lastModified();
    478         }
    479     }
    480    
    481     public boolean exists() {
    482         return llssite_cfg.exists();
    483     }
    484 
    485     public String getLocalHostURL() {
    486         StringBuffer url = new StringBuffer(LOCAL_HOST);
    487         url.append(COLON);
    488         url.append((String)get(PORTNUMBER));
    489         String enterlib = (String)get(ENTERLIB);
    490         if(enterlib == null || enterlib.length() == 0) {
    491         // Use the default /gsdl and hope for the best.
    492         url.append(SEPARATOR);
    493         url.append(GSDL);
    494         }
    495693        else {
    496         if(!enterlib.startsWith(SEPARATOR)) {
    497             url.append(SEPARATOR);
    498         }
    499         url.append(enterlib);
    500         }
    501         enterlib = null;
    502         debug("Found Local Library Address: " + url.toString());
    503         return url.toString();
    504     }
    505 
    506     public String getSiteConfigFilename() {
    507         return SPECIFIC_CONFIG + glisite_cfg.getAbsolutePath();
    508     }
    509 
    510     public String getURL() {
    511         // URL is made from url and portnumber
    512         String url = (String) get(URL);
    513 
    514         if(!Utility.isWindows()) {
    515         return url;
    516         }
    517 
    518         if(url != null) {
    519         StringBuffer temp = new StringBuffer(url);
    520         temp.append(COLON);
    521         temp.append((String)get(PORTNUMBER));
    522         String enterlib = (String)get(ENTERLIB);
    523         if(enterlib == null || enterlib.length() == 0) {
    524             // Use the default /gsdl and hope for the best.
    525             temp.append(SEPARATOR);
    526             temp.append(GSDL);
    527         }
    528         else {
    529             if(!enterlib.startsWith(SEPARATOR)) {
    530             temp.append(SEPARATOR);
    531             }
    532             temp.append(enterlib);
    533         }
    534         enterlib = null;
    535         url = temp.toString();
    536         }
    537         debug("Found Local Library Address: " + url);
    538         return url;
    539     }
    540 
    541     public boolean isModified() {
    542         return (lastModified != glisite_cfg.lastModified());
    543     }
    544 
    545     public void load(boolean reloadOnlyIfModified) {       
    546 
    547         if(isModified()) {
    548         lastModified = glisite_cfg.lastModified();
    549         } else if(reloadOnlyIfModified) {
    550         return; // asked to reload only if modified. Don't reload since not modified
    551         }
    552 
    553         if(glisite_cfg.exists()) {
    554         debug("Load: " + glisite_cfg.getAbsolutePath());
    555         clear();
    556         try {
    557             BufferedReader in = new BufferedReader(new FileReader(glisite_cfg));
    558             String line = null;
    559             while((line = in.readLine()) != null) {
    560             String key = null;
    561             String value = null;
    562             int index = -1;
    563             if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
    564                 key = line.substring(0, index);
    565                 value = line.substring(index + 1);
    566             }
    567             else {
    568                 key = line;
    569             }
    570             put(key, value);
    571             }
    572             in.close();
    573         }
    574         catch (Exception error) {
    575             error.printStackTrace();
    576         }
    577         }
    578         else {
    579         debug("No glisite.cfg file can be found!");
     694        debug(configFile.getAbsolutePath() + " cannot be found!");
    580695        }
    581696    }
     
    583698    /** Restore the autoenter value to its initial value, and remove url if present. */
    584699    public void restore() {
    585         if(glisite_cfg != null) {
     700        if(configFile != null) {
    586701        // Delete the file
    587         glisite_cfg.delete();
     702        configFile.delete();
    588703        }
    589704        else {
     
    617732    private void save() {
    618733        //debug("Save: " + llssite_cfg.getAbsolutePath());
    619         debug("Save: " + glisite_cfg.getAbsolutePath());
     734        debug("Save: " + configFile.getAbsolutePath());
    620735        try {
    621736        //BufferedWriter out = new BufferedWriter(new FileWriter(llssite_cfg, false));
    622         BufferedWriter out = new BufferedWriter(new FileWriter(glisite_cfg, false));
     737        BufferedWriter out = new BufferedWriter(new FileWriter(configFile, false));
    623738        for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
    624739            String key = (String) keys.next();
     
    627742            if(value != null) {
    628743            out.write('=');
    629             out.write(value, 0, value.length());
     744           
     745            // if the server is using llssite.cfg, don't overwrite its default
     746            // autoenter and startbrowser values
     747            if(configFile == llssite_cfg && (key == AUTOENTER || key == STARTBROWSER)) {
     748                if(key == AUTOENTER) {
     749                out.write(autoenter_initial, 0, autoenter_initial.length());
     750                } else { // STARTBROWSER
     751                out.write(start_browser_initial, 0, start_browser_initial.length());
     752                }
     753             } else {
     754                 out.write(value, 0, value.length());
     755            }
    630756            }
    631757            out.newLine();
Note: See TracChangeset for help on using the changeset viewer.