Ignore:
Timestamp:
2004-11-23T10:41:36+13:00 (19 years ago)
Author:
mdewsnip
Message:

Tidied up some more local library stuff.

File:
1 edited

Legend:

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

    r8620 r8622  
    3333import javax.swing.*;
    3434import org.greenstone.gatherer.util.GSDLSiteConfig;
    35 import org.greenstone.gatherer.util.Utility;
    3635
    3736
    3837public class LocalLibraryServer
    3938{
    40     static private GSDLSiteConfig gsdlsite_cfg = null;
     39    static final private String ADD_COMMAND = "?a=config&cmd=add-collection&c=";
     40    static final private String RELEASE_COMMAND = "?a=config&cmd=release-collection&c=";
     41    static final private String QUIT_COMMAND = "?a=config&cmd=kill";
     42
     43    static private GSDLSiteConfig gsdlsite_cfg_file = null;
    4144    static private boolean running = false;
    4245
     
    4447    static public void addCollection(String collection_name)
    4548    {
    46     config(GSDLSiteConfig.ADD_COMMAND + collection_name);
     49    config(ADD_COMMAND + collection_name);
    4750    // This is very important -- it ensures that the above command has finished
    4851    config("");
     
    5760    {
    5861    try {
    59         String raw_url = Configuration.exec_address.toString() + command;
    60         URL url = new URL(raw_url);
    61         DebugStream.println("Action: " + raw_url);
     62        URL url = new URL(Configuration.exec_address.toString() + command);
    6263        HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
    6364        int response_code = library_connection.getResponseCode();
    64         if(HttpURLConnection.HTTP_OK <= response_code && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
     65        if (response_code >= HttpURLConnection.HTTP_OK && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
    6566        DebugStream.println("200 - Complete.");
    6667        }
     
    6869        DebugStream.println("404 - Failed.");
    6970        }
    70         url = null;
    7171    }
    7272    catch (Exception ex) {
     
    8484    static public void releaseCollection(String collection_name)
    8585    {
    86     config(GSDLSiteConfig.RELEASE_COMMAND + collection_name);
     86    config(RELEASE_COMMAND + collection_name);
    8787    // This is very important -- it ensures that the above command has finished
    8888    config("");
     
    9292    static public void start(String gsdl_path, String local_library_server_file_path)
    9393    {
     94    // Check the local library server.exe file exists
    9495    File local_library_server_file = new File(local_library_server_file_path);
    9596    if (!local_library_server_file.exists()) {
    9697        DebugStream.println("No local library at given file path.");
    9798
    98         local_library_server_file = new File(gsdl_path + Utility.SERVER_EXE);
     99        local_library_server_file = new File(gsdl_path + "server.exe");
    99100        if (!local_library_server_file.exists()) {
    100101        DebugStream.println("No local library at all.");
     
    103104    }
    104105
    105     // First of all we create a GSDLSiteCFG object and check if a URL is already present, in which case the server is already running.
    106     gsdlsite_cfg = new GSDLSiteConfig(local_library_server_file);
    107     String url = gsdlsite_cfg.getURL();
    108     // If its already running then set exec address.
    109     if(url != null) {
     106    // Check if the server is already running
     107    gsdlsite_cfg_file = new GSDLSiteConfig(local_library_server_file);
     108    String url = gsdlsite_cfg_file.getURL();
     109    if (url != null) {
     110        // If it is already running then set the Greenstone web server address and we're done
    110111        try {
    111112        Configuration.exec_address = new URL(url);
    112         }
    113         catch(Exception error) {
    114         }
    115     }
    116     // Otherwise its time to run the server in a spawned process.
    117     if(Configuration.exec_address == null && local_library_server_file.exists()) {
    118         // Configure for immediate entry. Note that this only works if the gsdlsite.cfg file exists.
    119         gsdlsite_cfg.set();
    120 
    121         // Spawn local library server process
    122         String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg.getSiteConfigFilename();
    123         Gatherer.self.spawnApplication(local_library_server_command);
    124 
    125         // Now we have to wait until program has started. We do this by reloading and checking
    126         ///ystem.err.print("Waiting until the local library has loaded");
     113        return;
     114        }
     115        catch (MalformedURLException exception) {
     116        DebugStream.printStackTrace(exception);
     117        }
     118    }
     119
     120    // Configure the server for immediate entry
     121    gsdlsite_cfg_file.set();
     122
     123    // Spawn local library server process
     124    String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
     125    Gatherer.self.spawnApplication(local_library_server_command);
     126
     127    // Wait until program has started, by reloading and checking the URL field
     128    gsdlsite_cfg_file.load();
     129    int attempt_count = 0;
     130    while (gsdlsite_cfg_file.getURL() == null) {
     131        new OneSecondWait();  // Wait one second (give or take)
     132        gsdlsite_cfg_file.load();
     133        attempt_count++;
     134
     135        // After waiting a minute ask the user whether they want to wait another minute
     136        if (attempt_count == 60) {
     137        int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
     138        if (try_again == JOptionPane.NO_OPTION) {
     139            return;
     140        }
     141        attempt_count = 0;
     142        }
     143    }
     144
     145    // Ta-da. Now the url should be available
     146    try {
     147        Configuration.exec_address = new URL(gsdlsite_cfg_file.getURL());
     148    }
     149    catch (MalformedURLException exception) {
     150        DebugStream.printStackTrace(exception);
     151    }
     152
     153    // A quick test involves opening a connection to get the home page for this collection
     154    try {
     155        DebugStream.println("Try connecting to server on config url: '" + Configuration.exec_address + "'");
     156        URLConnection connection = Configuration.exec_address.openConnection();
     157        connection.getContent();
     158    }
     159    catch (IOException bad_url_connection) {
    127160        try {
    128         gsdlsite_cfg.load();
    129            
    130         int try_again = JOptionPane.YES_OPTION;
    131         int attempt_count = 0;
    132         while(gsdlsite_cfg.getURL() == null && try_again == JOptionPane.YES_OPTION) {
    133             ///ystem.err.print(".");
    134             if(attempt_count == 60) {
    135             attempt_count = 0;
    136             try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
    137             }
    138             else {
    139             new OneSecondWait();  // Wait one second (give or take)
    140             gsdlsite_cfg.load();
    141             attempt_count++;
    142             }
     161        // If this fails then we try changing the url to be localhost
     162        Configuration.exec_address = new URL(gsdlsite_cfg_file.getLocalHostURL());
     163        DebugStream.println("Try connecting to server on local host: '" + Configuration.exec_address + "'");
     164        URLConnection connection = Configuration.exec_address.openConnection();
     165        connection.getContent();
     166        }
     167        catch (IOException worse_url_connection) {
     168        DebugStream.println("Can't connect to server on either address.");
     169        Configuration.exec_address = null;
     170        }
     171    }
     172    }
     173
     174
     175    static public void stop()
     176    {
     177    if (running == false) {
     178        return;
     179    }
     180
     181    // Send the command for it to exit.
     182    config(QUIT_COMMAND);
     183    config("");
     184
     185    // Wait until program has started, by reloading and checking the URL field
     186    gsdlsite_cfg_file.load();
     187    int attempt_count = 0;
     188    while (gsdlsite_cfg_file.getURL() != null) {
     189        new OneSecondWait();  // Wait one second (give or take)
     190        gsdlsite_cfg_file.load();
     191        attempt_count++;
     192
     193        // After waiting a minute ask the user whether they want to wait another minute
     194        if (attempt_count == 60) {
     195        int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
     196        if (try_again == JOptionPane.NO_OPTION) {
     197            return;
    143198        }
    144            
    145         if((url = gsdlsite_cfg.getURL()) != null) {
    146             // Ta-da. Now the url should be available.
    147             Configuration.exec_address = new URL(url);
    148            
    149             // A quick test involves opening a connection to get the home page  for this collection. If this fails then we try changing the url to be localhost.
    150             try {
    151             DebugStream.println("Try connecting to server on config url: '" + Configuration.exec_address + "'");
    152             URLConnection connection = Configuration.exec_address.openConnection();
    153             connection.getContent();
    154             }
    155             catch(IOException bad_url_connection) {
    156             try {
    157                 DebugStream.println("Try connecting to server on local host: '" + gsdlsite_cfg.getLocalHostURL() + "'");
    158                 Configuration.exec_address = new URL(gsdlsite_cfg.getLocalHostURL   ());
    159                 URLConnection connection = Configuration.exec_address.openConnection();
    160                 connection.getContent();
    161             }
    162             catch(IOException worse_url_connection) {
    163                 DebugStream.println("Can't connect to server on either address.");
    164                 Configuration.exec_address = null;
    165             }
    166             }
    167         }
    168         }
    169         catch (Exception exception) {
    170         DebugStream.printStackTrace(exception);
    171         }
    172     }
    173     DebugStream.println("Having started server.exe, exec_address is: " + Configuration.exec_address);
    174     }
    175 
    176 
    177     static public void stop()
    178     {
    179     if (running == true && Configuration.exec_address != null) {
    180         // See if its already exited for some reason.
    181         gsdlsite_cfg.load();
    182         if(gsdlsite_cfg.getURL() != null) {
    183         // Send the command for it to exit.
    184         config(GSDLSiteConfig.QUIT_COMMAND);
    185         // Wait until it exits.
    186         try {
    187             gsdlsite_cfg.load();
    188             int try_again = JOptionPane.YES_OPTION;
    189             int attempt_count = 0;
    190             while(gsdlsite_cfg.getURL() != null && try_again == JOptionPane.YES_OPTION) {
    191             if(attempt_count == 60) {
    192                 attempt_count = 0;
    193                 try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
    194             }
    195             else {
    196                 new OneSecondWait();  // Wait one second (give or take)
    197                 gsdlsite_cfg.load();
    198                 attempt_count++;
    199             }
    200             }
    201             //if(gsdlsite_cfg.getURL() != null) {
    202             //JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    203             //}
    204         }
    205         catch (Exception exception) {
    206             DebugStream.printStackTrace(exception);
    207         }
    208         }
    209         // Restore the gsdlsite_cfg.
    210         if(gsdlsite_cfg != null) {
    211         gsdlsite_cfg.restore();
    212         }
    213         // If the local server is still running then our changed values will get overwritten.
    214         if(gsdlsite_cfg.getURL() != null) {
    215         JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitFailed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    216         }
    217         gsdlsite_cfg = null;
     199        attempt_count = 0;
     200        }
     201    }
     202
     203    //if(gsdlsite_cfg_file.getURL() != null) {
     204    //JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
     205    //}
     206
     207    // Restore the gsdlsite_cfg.
     208    gsdlsite_cfg_file.restore();
     209
     210    // If the local server is still running then our changed values will get overwritten.
     211    if (gsdlsite_cfg_file.getURL() != null) {
     212        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitFailed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    218213    }
    219214    }
Note: See TracChangeset for help on using the changeset viewer.