Ignore:
Timestamp:
2008-10-24T17:45:51+13:00 (16 years ago)
Author:
ak19
Message:

Turned class RemoteGreenstoneServer.java from an all-static class into a regular OOP class. It is now also split into three files (the other two being ActionQueue.java and RemoteGreenstoneServerAction.java).

File:
1 edited

Legend:

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

    r16471 r17612  
    5858public class RemoteGreenstoneServer
    5959{
     60    // A PasswordAuthentication object is created whenever it is required
     61    static private PasswordAuthentication remote_greenstone_server_authentication = null;
     62    // static private PasswordAuthentication remote_greenstone_server_authentication = new PasswordAuthentication(System.getProperty("user.name"), new char[] { });
     63
     64    private ActionQueue remote_greenstone_server_action_queue;
     65    private RemoteGreenstoneServer.ProgressBar progress_bar;
     66
     67    public RemoteGreenstoneServer() {
     68    remote_greenstone_server_action_queue = new ActionQueue();
     69    progress_bar = new RemoteGreenstoneServer.ProgressBar();
     70    }
     71
    6072    // ----------------------------------------------------------------------------------------------------
    6173    //   PUBLIC LAYER
    6274    // ----------------------------------------------------------------------------------------------------
    63 
    64 
    65     static public String deleteCollection(String collection_name)
    66     {
    67     return performAction(new RemoteGreenstoneServerDeleteCollectionAction(collection_name));
    68     }
    69 
    70 
    71     static public String deleteCollectionFile(String collection_name, File collection_file)
    72     {
    73     return performAction(new RemoteGreenstoneServerDeleteCollectionFileAction(collection_name, collection_file));
    74     }
    75 
    76 
    77     static public String downloadCollection(String collection_name)
    78     {
    79     return performAction(new RemoteGreenstoneServerDownloadCollectionAction(collection_name));
    80     }
    81 
    82 
    83     static public String downloadCollectionArchives(String collection_name)
    84     {
    85     return performAction(new RemoteGreenstoneServerDownloadCollectionArchivesAction(collection_name));
    86     }
    87 
    88 
    89     static public String downloadCollectionConfigurations()
    90     {
    91     return performAction(new RemoteGreenstoneServerDownloadCollectionConfigurationsAction());
    92     }
    93 
    94 
    95     static public String downloadCollectionFile(String collection_name, File collection_file)
    96     {
    97     return performAction(new RemoteGreenstoneServerDownloadCollectionFileAction(collection_name, collection_file));
     75    public String deleteCollection(String collection_name)
     76    {
     77    return performAction(new RemoteGreenstoneServerAction.DeleteCollectionAction(collection_name));
     78    }
     79
     80
     81    public String deleteCollectionFile(String collection_name, File collection_file)
     82    {
     83    return performAction(new RemoteGreenstoneServerAction.DeleteCollectionFileAction(collection_name, collection_file));
     84    }
     85
     86
     87    public String downloadCollection(String collection_name)
     88    {
     89    return performAction(new RemoteGreenstoneServerAction.DownloadCollectionAction(collection_name));
     90    }
     91
     92
     93    public String downloadCollectionArchives(String collection_name)
     94    {
     95    return performAction(new RemoteGreenstoneServerAction.DownloadCollectionArchivesAction(collection_name));
     96    }
     97
     98
     99    public String downloadCollectionConfigurations()
     100    {
     101    return performAction(new RemoteGreenstoneServerAction.DownloadCollectionConfigurationsAction());
     102    }
     103
     104
     105    public String downloadCollectionFile(String collection_name, File collection_file)
     106    {
     107    return performAction(new RemoteGreenstoneServerAction.DownloadCollectionFileAction(collection_name, collection_file));
    98108    }
    99109   
    100110    // get web.xml from the server -- for a remote gli of GS3
    101     static public String downloadWebXMLFile()
    102     {
    103     return performAction(new RemoteGreenstoneServerDownloadWebXMLFileAction());
    104     }
    105 
    106     static public String getScriptOptions(String script_name, String script_arguments)
    107     {
    108     return performAction(new RemoteGreenstoneServerGetScriptOptionsAction(script_name, script_arguments));
     111    public String downloadWebXMLFile()
     112    {
     113    return performAction(new RemoteGreenstoneServerAction.DownloadWebXMLFileAction());
     114    }
     115
     116    public String getScriptOptions(String script_name, String script_arguments)
     117    {
     118    return performAction(new RemoteGreenstoneServerAction.GetScriptOptionsAction(script_name, script_arguments));
    109119    }
    110120   
    111121    //get all available site names from the server -- for a remote gli of GS3
    112     static public String getSiteNames()
    113     {
    114     return performAction(new RemoteGreenstoneServerGetSiteNamesAction());
    115     }
    116 
    117     static public String moveCollectionFile(String collection_name, File source_collection_file, File target_collection_file)
    118     {
    119     return performAction(new RemoteGreenstoneServerMoveCollectionFileAction(collection_name, source_collection_file, target_collection_file));
    120     }
    121 
    122 
    123     static public String newCollectionDirectory(String collection_name, File new_collection_directory)
    124     {
    125     return performAction(new RemoteGreenstoneServerNewCollectionDirectoryAction(collection_name, new_collection_directory));
    126     }
    127 
    128 
    129     static public String runScript(String collection_name, String script_name, String script_arguments, GShell shell)
    130     {
    131     return performAction(new RemoteGreenstoneServerRunScriptAction(collection_name, script_name, script_arguments, shell));
    132     }
    133 
    134 
    135     static public String uploadCollectionFile(String collection_name, File collection_file)
    136     {
    137     return performAction(new RemoteGreenstoneServerUploadCollectionFilesAction(collection_name, new File[] { collection_file }));
    138     }
    139 
    140 
    141     static public String uploadCollectionFiles(String collection_name, File[] collection_files)
    142     {
    143     return performAction(new RemoteGreenstoneServerUploadCollectionFilesAction(collection_name, collection_files));
    144     }
    145 
    146 
    147     static public String uploadFilesIntoCollection(String collection_name, File[] source_files, File target_collection_directory)
    148     {
    149     return performAction(new RemoteGreenstoneServerUploadFilesIntoCollectionAction(collection_name, source_files, target_collection_directory));
    150     }
    151 
    152     static public boolean exists(String collection_name, File collection_file)
    153     {
    154     String result = performAction(new RemoteGreenstoneServerExistsAction(collection_name, collection_file));
     122    public String getSiteNames()
     123    {
     124    return performAction(new RemoteGreenstoneServerAction.GetSiteNamesAction());
     125    }
     126
     127    public String moveCollectionFile(String collection_name, File source_collection_file, File target_collection_file)
     128    {
     129    return performAction(new RemoteGreenstoneServerAction.MoveCollectionFileAction(
     130                             collection_name, source_collection_file, target_collection_file));
     131    }
     132
     133
     134    public String newCollectionDirectory(String collection_name, File new_collection_directory)
     135    {
     136    return performAction(new RemoteGreenstoneServerAction.NewCollectionDirectoryAction(
     137                             collection_name, new_collection_directory));
     138    }
     139
     140
     141    public String runScript(String collection_name, String script_name, String script_arguments, GShell shell)
     142    {
     143    return performAction(new RemoteGreenstoneServerAction.RunScriptAction(
     144                             collection_name, script_name, script_arguments, shell));
     145    }
     146
     147
     148    public String uploadCollectionFile(String collection_name, File collection_file)
     149    {
     150    return performAction(new RemoteGreenstoneServerAction.UploadCollectionFilesAction(
     151                             collection_name, new File[] { collection_file }));
     152    }
     153
     154
     155    public String uploadCollectionFiles(String collection_name, File[] collection_files)
     156    {
     157    return performAction(new RemoteGreenstoneServerAction.UploadCollectionFilesAction(collection_name, collection_files));
     158    }
     159
     160
     161    public String uploadFilesIntoCollection(String collection_name, File[] source_files, File target_collection_directory)
     162    {
     163    return performAction(new RemoteGreenstoneServerAction.UploadFilesIntoCollectionAction(
     164                             collection_name, source_files, target_collection_directory));
     165    }
     166
     167    public boolean exists(String collection_name, File collection_file)
     168    {
     169    String result = performAction(new RemoteGreenstoneServerAction.ExistsAction(collection_name, collection_file));
    155170    if(result.indexOf("exists") != -1) {
    156171        return true;
     
    162177    }
    163178
    164     static public int getGreenstoneVersion()
     179    public int getGreenstoneVersion()
    165180    {
    166181    // returns message "Greenstone version is: <version number of the Greenstone remote server>"
    167     String result = performAction(new RemoteGreenstoneServerVersionAction());
     182    String result = performAction(new RemoteGreenstoneServerAction.VersionAction());
    168183    int index = result.indexOf(":");
    169184    if(index != -1) {
     
    175190
    176191    /** For constructing the preview command (the library URL) with */
    177     static public String getLibraryURL(String serverHomeURL)
     192    public String getLibraryURL(String serverHomeURL)
    178193    {
    179194    // returns message "Greenstone library URL suffix is: <e.g. /greenstone3/library or /gsdl/cgi-bin/library>"
    180     String libSuffix = performAction(new RemoteGreenstoneServerLibraryURLSuffixAction());
     195    String libSuffix = performAction(new RemoteGreenstoneServerAction.LibraryURLSuffixAction());
    181196    int index = libSuffix.indexOf(":");
    182197    if(index != -1) {
     
    200215
    201216
    202     static public void exit()
     217    public void exit()
    203218    {
    204219    System.err.println("Exiting, number of jobs on queue: " + remote_greenstone_server_action_queue.size());
     
    223238
    224239    /** Returns null if we cannot wait for the action to finish, "" if the action failed, or the action output. */
    225     static private String performAction(RemoteGreenstoneServerAction remote_greenstone_server_action)
     240    private String performAction(RemoteGreenstoneServerAction remote_greenstone_server_action)
    226241    {
    227242    // Add the action to the queue
     
    255270
    256271
    257     static private RemoteGreenstoneServerActionQueue remote_greenstone_server_action_queue = new RemoteGreenstoneServerActionQueue();
    258 
    259 
    260     static private class RemoteGreenstoneServerActionQueue
    261     extends Thread
    262     {
    263     /** The queue of waiting jobs. */
    264     private ArrayList queue = null;
    265 
    266 
    267     public RemoteGreenstoneServerActionQueue()
    268     {
    269         if (Gatherer.isGsdlRemote) {
    270         queue = new ArrayList();
    271         start();
    272         }
    273     }
    274 
    275 
    276     synchronized public void addAction(RemoteGreenstoneServerAction remote_greenstone_server_action)
    277     {
    278         queue.add(remote_greenstone_server_action);
    279         notifyAll();
    280     }
    281 
    282 
    283     public int size()
    284     {
    285         return queue.size();
    286     }
    287 
    288 
    289     public void run()
    290     {
    291         boolean exit = false;
    292         while (true) {
    293         // If there are jobs on the queue, get the next in line and process it
    294         if (queue.size() > 0) {
    295             RemoteGreenstoneServerAction remote_greenstone_server_action = (RemoteGreenstoneServerAction) queue.get(0);
    296 
    297             try {
    298             remote_greenstone_server_action.perform();
    299            
    300             // No exceptions were thrown, so the action was successful
    301             remote_greenstone_server_action.processed_successfully = true;
    302             }
    303             catch (RemoteGreenstoneServerActionCancelledException exception) {
    304             remote_greenstone_server_action.processed_successfully = false;
    305             exit = true;
    306             }
    307             catch(java.net.ConnectException exception) {
    308             if(exception.getMessage().trim().startsWith("Connection refused")) {
    309                 exit = true;
    310             } else {
    311                 DebugStream.printStackTrace(exception);
    312             }
    313             JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
    314             remote_greenstone_server_action.processed_successfully = false;         
    315             }
    316             catch (FileNotFoundException exception) {
    317             // FileNotFoundException happens when there's no GS server at the user-provided
    318             // url (the address of gliserver.pl is wrong).
    319             exit = true;
    320             DebugStream.printStackTrace(exception);
    321             JOptionPane.showMessageDialog(Gatherer.g_man,
    322                               Dictionary.get("RemoteGreenstoneServer.Error",
    323                                      "No gliserver.pl found. " + exception.getMessage()),
    324                               Dictionary.get("RemoteGreenstoneServer.Error_Title"),
    325                               JOptionPane.ERROR_MESSAGE);
    326             remote_greenstone_server_action.processed_successfully = false;
    327             }
    328             catch (Exception exception) {
    329             DebugStream.printStackTrace(exception);
    330             JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
    331             remote_greenstone_server_action.processed_successfully = false;
    332             }
    333 
    334             // We're done with this action, for better or worse
    335             remote_greenstone_server_action.processed = true;
    336             queue.remove(0);
    337         }
    338 
    339         // Otherwise the queue is empty
    340         else {
    341             progress_bar.setAction(null);
    342 
    343             // Wait until we are notify()ed by addAction that there is a new job on the queue
    344             synchronized (this) {
    345             try {
    346                 wait();
    347             }
    348             catch (InterruptedException exception) { }
    349             }
    350         }
    351        
    352         // stop the gazillion annoying error messages when the connection was simply
    353         // refused or when the user pressed Cancel in the opening dialog, by exitting
    354         // cleanly in one go.
    355         if(exit == true) {
    356             Gatherer.exit();
    357         }
    358         }
    359     }
    360     }
    361 
    362 
    363272    // ----------------------------------------------------------------------------------------------------
    364273    //   PROGRESS BAR
    365274    // ----------------------------------------------------------------------------------------------------
    366275
    367 
    368     static private RemoteGreenstoneServerProgressBar progress_bar = new RemoteGreenstoneServerProgressBar();
    369 
    370 
    371     static private class RemoteGreenstoneServerProgressBar
     276    static class ProgressBar
    372277    extends JProgressBar
    373278    {
    374     public RemoteGreenstoneServerProgressBar()
     279    public ProgressBar()
    375280    {
    376281        setBackground(Configuration.getColor("coloring.collection_tree_background", false));
     
    404309    }
    405310    }
    406 
    407 
    408     static public RemoteGreenstoneServerProgressBar getProgressBar()
     311   
     312    public RemoteGreenstoneServer.ProgressBar getProgressBar()
    409313    {
    410314    return progress_bar;
     
    413317
    414318    // ----------------------------------------------------------------------------------------------------
    415     //   ACTIONS
    416     // ----------------------------------------------------------------------------------------------------
    417 
    418 
    419     static private abstract class RemoteGreenstoneServerAction
    420     {
    421     public String action_output = null;
    422     public boolean processed = false;
    423     public boolean processed_successfully;
    424 
    425     abstract public void perform()
    426         throws Exception;
    427     }
    428 
    429 
    430     static private class RemoteGreenstoneServerActionCancelledException
    431     extends Exception
    432     {
    433     }
    434 
    435 
    436     /**
    437      * --------------------------------------------------------------------------------------------
    438      *    DELETE COLLECTION
    439      * --------------------------------------------------------------------------------------------
    440      */
    441     static private class RemoteGreenstoneServerDeleteCollectionAction
    442     extends RemoteGreenstoneServerAction
    443     {
    444     private String collection_name;
    445 
    446     public RemoteGreenstoneServerDeleteCollectionAction(String collection_name)
    447     {
    448         this.collection_name = collection_name;
    449     }
    450 
    451     public void perform()
    452         throws Exception
    453     {
    454         progress_bar.setAction("Deleting collection " + collection_name + "...");
    455 
    456         String delete_collection_command = "cmd=delete-collection";
    457         delete_collection_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    458         action_output = sendCommandToServer(delete_collection_command, null);
    459     }
    460     }
    461 
    462 
    463     /**
    464      * --------------------------------------------------------------------------------------------
    465      *    DELETE COLLECTION FILE
    466      * --------------------------------------------------------------------------------------------
    467      */
    468     static private class RemoteGreenstoneServerDeleteCollectionFileAction
    469     extends RemoteGreenstoneServerAction
    470     {
    471     private String collection_name;
    472     private File collection_file;
    473 
    474     public RemoteGreenstoneServerDeleteCollectionFileAction(String collection_name, File collection_file)
    475     {
    476         this.collection_name = collection_name;
    477         this.collection_file = collection_file;
    478     }
    479 
    480     public void perform()
    481         throws Exception
    482     {
    483         String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
    484         String collection_file_relative_path = getPathRelativeToDirectory(collection_file, collection_directory_path);
    485         collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
    486         progress_bar.setAction("Deleting collection file " + collection_file_relative_path + "...");
    487 
    488         String delete_collection_file_command = "cmd=delete-collection-file";
    489         delete_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    490         delete_collection_file_command += "&file=" + URLEncoder.encode(collection_file_relative_path, "UTF-8");
    491         action_output = sendCommandToServer(delete_collection_file_command, null);
    492     }
    493     }
    494 
    495 
    496     /**
    497      * --------------------------------------------------------------------------------------------
    498      *    DOWNLOAD COLLECTION
    499      * --------------------------------------------------------------------------------------------
    500      */
    501     static private class RemoteGreenstoneServerDownloadCollectionAction
    502     extends RemoteGreenstoneServerAction
    503     {
    504     private String collection_name;
    505 
    506     public RemoteGreenstoneServerDownloadCollectionAction(String collection_name)
    507     {
    508         this.collection_name = collection_name;
    509     }
    510 
    511     public void perform()
    512         throws Exception
    513     {
    514         progress_bar.setAction("Downloading remote collection " + collection_name + "...");
    515 
    516         String download_collection_command = "cmd=download-collection";
    517         download_collection_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    518         String zip_file_path = Gatherer.getCollectDirectoryPath() + collection_name + ".zip";
    519         action_output = downloadFile(download_collection_command, zip_file_path);
    520 
    521         // Delete the existing (local) collection directory
    522         Utility.delete(new File(CollectionManager.getCollectionDirectoryPath(collection_name)));
    523 
    524         // Unzip the collection just downloaded
    525         UnzipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());
    526     }
    527     }
    528 
    529 
    530     /**
    531      * --------------------------------------------------------------------------------------------
    532      *    DOWNLOAD COLLECTION ARCHIVES
    533      * --------------------------------------------------------------------------------------------
    534      */
    535     static private class RemoteGreenstoneServerDownloadCollectionArchivesAction
    536     extends RemoteGreenstoneServerAction
    537     {
    538     private String collection_name;
    539 
    540     public RemoteGreenstoneServerDownloadCollectionArchivesAction(String collection_name)
    541     {
    542         this.collection_name = collection_name;
    543     }
    544 
    545     public void perform()
    546         throws Exception
    547     {
    548         progress_bar.setAction("Downloading collection archives for " + collection_name + "...");
    549 
    550         String download_collection_archives_command = "cmd=download-collection-archives";
    551         download_collection_archives_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    552         String zip_file_path = Gatherer.getCollectDirectoryPath() + collection_name + "-archives.zip";
    553         action_output = downloadFile(download_collection_archives_command, zip_file_path);
    554 
    555         // Delete the existing (local) collection archives
    556         Utility.delete(new File(CollectionManager.getLoadedCollectionArchivesDirectoryPath()));
    557 
    558         // Unzip the collection archives just downloaded
    559         UnzipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());
    560     }
    561     }
    562 
    563 
    564     /**
    565      * --------------------------------------------------------------------------------------------
    566      *    DOWNLOAD COLLECTION CONFIGURATIONS
    567      * --------------------------------------------------------------------------------------------
    568      */
    569     static private class RemoteGreenstoneServerDownloadCollectionConfigurationsAction
    570     extends RemoteGreenstoneServerAction
    571     {
    572     public RemoteGreenstoneServerDownloadCollectionConfigurationsAction()
    573     {
    574     }
    575 
    576     public void perform()
    577         throws Exception
    578     {
    579         progress_bar.setAction("Downloading collection configurations...");
    580 
    581         // Delete the existing (local) collect directory
    582         Utility.delete(new File(Gatherer.getCollectDirectoryPath()));
    583         new File(Gatherer.getCollectDirectoryPath()).mkdirs();
    584 
    585         String download_collection_configurations_command = "cmd=download-collection-configurations";
    586         String zip_file_path = Gatherer.getCollectDirectoryPath() + "collections.zip";
    587         action_output = downloadFile(download_collection_configurations_command, zip_file_path);
    588 
    589         // Unzip the collection configurations just downloaded
    590         UnzipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());
    591     }
    592     }
    593 
    594      /**
    595      * --------------------------------------------------------------------------------------------
    596      *    DISCOVERING WHAT VERSION THE REMOTE GREENSTONE SERVER IS (2 or 3)
    597      * --------------------------------------------------------------------------------------------
    598      */
    599 
    600     static private class RemoteGreenstoneServerVersionAction
    601     extends RemoteGreenstoneServerAction
    602     {
    603     public void perform()
    604         throws Exception
    605     {
    606         action_output = sendCommandToServer("cmd=greenstone-server-version", null);
    607     }   
    608     }
    609 
    610     static private class RemoteGreenstoneServerLibraryURLSuffixAction
    611     extends RemoteGreenstoneServerAction
    612     {
    613     public void perform()
    614         throws Exception
    615     {
    616         action_output = sendCommandToServer("cmd=get-library-url-suffix", null);
    617     }   
    618     }
    619    
    620     /**
    621      * --------------------------------------------------------------------------------------------
    622      *    CHECKING IF A FILE/FOLDER EXISTS ON SERVER SIDE
    623      * --------------------------------------------------------------------------------------------
    624      */
    625     static private class RemoteGreenstoneServerExistsAction
    626     extends RemoteGreenstoneServerAction
    627     {
    628     private String collection_name;
    629     private File collection_file;
    630    
    631     public RemoteGreenstoneServerExistsAction(String collection_name, File collection_file)
    632     {
    633         this.collection_name = collection_name;
    634         this.collection_file = collection_file;
    635     }
    636    
    637     public void perform()
    638         throws Exception
    639     {
    640         String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
    641         String collection_file_relative_path = getPathRelativeToDirectory(collection_file, collection_directory_path);
    642         collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
    643         File file = new File(collection_directory_path, collection_file_relative_path);
    644 
    645         String file_exists_command = "cmd=file-exists";
    646         file_exists_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    647         file_exists_command += "&file=" + URLEncoder.encode(collection_file_relative_path, "UTF-8");
    648         // returns either "File <filename> exists" or "File <filename> does not exist"
    649         // for the file/folder collection_file
    650         action_output = sendCommandToServer(file_exists_command, null);
    651     }   
    652     }
    653    
    654 
    655     /**
    656      * --------------------------------------------------------------------------------------------
    657      *    DOWNLOAD COLLECTION FILE
    658      * --------------------------------------------------------------------------------------------
    659      */
    660     static private class RemoteGreenstoneServerDownloadCollectionFileAction
    661     extends RemoteGreenstoneServerAction
    662     {
    663     private String collection_name;
    664     private File collection_file;
    665 
    666     public RemoteGreenstoneServerDownloadCollectionFileAction(String collection_name, File collection_file)
    667     {
    668         this.collection_name = collection_name;
    669         this.collection_file = collection_file;
    670     }
    671 
    672     public void perform()
    673         throws Exception
    674     {
    675         String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
    676         String collection_file_relative_path = getPathRelativeToDirectory(collection_file, collection_directory_path);
    677         collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
    678         progress_bar.setAction("Downloading collection file " + collection_file_relative_path + "...");
    679 
    680         String download_collection_file_command = "cmd=download-collection-file";
    681         download_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    682         download_collection_file_command += "&file=" + URLEncoder.encode(collection_file_relative_path, "UTF-8");
    683         String zip_file_name = collection_name + "-" + collection_file.getName() + ".zip";
    684         String zip_file_path = collection_directory_path + zip_file_name;
    685         action_output = downloadFile(download_collection_file_command, zip_file_path);
    686 
    687         // Unzip the collection file just downloaded
    688         UnzipTools.unzipFile(zip_file_path, collection_directory_path);
    689     }
    690     }
    691 
    692     /**
    693      * --------------------------------------------------------------------------------------------
    694      *    DOWNLOAD web.xml FILE --for a remote GS3
    695      * --------------------------------------------------------------------------------------------
    696      */
    697     static private class RemoteGreenstoneServerDownloadWebXMLFileAction
    698     extends RemoteGreenstoneServerAction
    699     {
    700     public RemoteGreenstoneServerDownloadWebXMLFileAction()
    701     {}
    702 
    703     public void perform()
    704         throws Exception
    705     {
    706         String web_xml_directory_path=(Configuration.gli_user_directory_path);
    707         String download_web_xml_file_command = "cmd=download-web-xml-file";
    708         download_web_xml_file_command += "&file=" + URLEncoder.encode("web.xml", "UTF-8");
    709         String zip_file_name = "web-xml.zip";
    710         String zip_file_path = web_xml_directory_path + zip_file_name;
    711         action_output = downloadFile(download_web_xml_file_command, zip_file_path);
    712 
    713         // Unzip the web.xml file just downloaded
    714         UnzipTools.unzipFile(zip_file_path,web_xml_directory_path);
    715     }
    716     }
    717 
    718     /**
    719      * --------------------------------------------------------------------------------------------
    720      *    GET SCRIPT OPTIONS
    721      * --------------------------------------------------------------------------------------------
    722      */
    723     static private class RemoteGreenstoneServerGetScriptOptionsAction
    724     extends RemoteGreenstoneServerAction
    725     {
    726     private String script_name;
    727     private String script_arguments;
    728 
    729     public RemoteGreenstoneServerGetScriptOptionsAction(String script_name, String script_arguments)
    730     {
    731         this.script_name = script_name;
    732         this.script_arguments = script_arguments;
    733     }
    734 
    735     public void perform()
    736         throws Exception
    737     {
    738         progress_bar.setAction("Getting options for " + script_name + "...");
    739 
    740         String get_script_options_command = "cmd=get-script-options";
    741         get_script_options_command += "&script=" + script_name;
    742         get_script_options_command += "&xml=";
    743         get_script_options_command += "&language=" + Configuration.getLanguage();
    744         get_script_options_command += script_arguments;
    745         action_output = sendCommandToServer(get_script_options_command, null);
    746     }
    747     }
    748 
    749     /**
    750      * --------------------------------------------------------------------------------------------
    751      *    GET ALL NAMES OF SITES // for a remote GS3
    752      * --------------------------------------------------------------------------------------------
    753      */
    754     static private class RemoteGreenstoneServerGetSiteNamesAction
    755     extends RemoteGreenstoneServerAction
    756     {
    757     public RemoteGreenstoneServerGetSiteNamesAction()
    758     {}
    759 
    760     public void perform()
    761         throws Exception
    762     {
    763         progress_bar.setAction("Getting names of sites ...");
    764 
    765         String get_script_options_command = "cmd=get-site-names";
    766         action_output = sendCommandToServer(get_script_options_command, null);
    767     }
    768     }
    769 
    770     /**
    771      * --------------------------------------------------------------------------------------------
    772      *    MOVE COLLECTION FILE
    773      * --------------------------------------------------------------------------------------------
    774      */
    775     static private class RemoteGreenstoneServerMoveCollectionFileAction
    776     extends RemoteGreenstoneServerAction
    777     {
    778     private String collection_name;
    779     private File source_collection_file;
    780     private File target_collection_file;
    781 
    782     public RemoteGreenstoneServerMoveCollectionFileAction(String collection_name, File source_collection_file, File target_collection_file)
    783     {
    784         this.collection_name = collection_name;
    785         this.source_collection_file = source_collection_file;
    786         this.target_collection_file = target_collection_file;
    787     }
    788 
    789     public void perform()
    790         throws Exception
    791     {
    792         String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
    793         String source_collection_file_relative_path = getPathRelativeToDirectory(source_collection_file, collection_directory_path);
    794         source_collection_file_relative_path = source_collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
    795         String target_collection_file_relative_path = getPathRelativeToDirectory(target_collection_file, collection_directory_path);
    796         target_collection_file_relative_path = target_collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
    797         progress_bar.setAction("Moving file " + source_collection_file_relative_path + " -> " + target_collection_file_relative_path + "...");
    798 
    799         String move_collection_file_command = "cmd=move-collection-file";
    800         move_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    801         move_collection_file_command += "&source=" + URLEncoder.encode(source_collection_file_relative_path, "UTF-8");
    802         move_collection_file_command += "&target=" + URLEncoder.encode(target_collection_file_relative_path, "UTF-8");
    803         action_output = sendCommandToServer(move_collection_file_command, null);
    804     }
    805     }
    806 
    807 
    808     /**
    809      * --------------------------------------------------------------------------------------------
    810      *    NEW COLLECTION DIRECTORY
    811      * --------------------------------------------------------------------------------------------
    812      */
    813     static private class RemoteGreenstoneServerNewCollectionDirectoryAction
    814     extends RemoteGreenstoneServerAction
    815     {
    816     private String collection_name;
    817     private File new_collection_directory;
    818 
    819     public RemoteGreenstoneServerNewCollectionDirectoryAction(String collection_name, File new_collection_directory)
    820     {
    821         this.collection_name = collection_name;
    822         this.new_collection_directory = new_collection_directory;
    823     }
    824 
    825     public void perform()
    826         throws Exception
    827     {
    828         String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
    829         String new_collection_directory_relative_path = getPathRelativeToDirectory(new_collection_directory, collection_directory_path);
    830         new_collection_directory_relative_path = new_collection_directory_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
    831         progress_bar.setAction("Creating new directory " + new_collection_directory_relative_path + "...");
    832 
    833         String new_collection_directory_command = "cmd=new-collection-directory";
    834         new_collection_directory_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    835         new_collection_directory_command += "&directory=" + URLEncoder.encode(new_collection_directory_relative_path, "UTF-8");
    836         action_output = sendCommandToServer(new_collection_directory_command, null);
    837     }
    838     }
    839 
    840 
    841     /**
    842      * --------------------------------------------------------------------------------------------
    843      *    RUN SCRIPT
    844      * --------------------------------------------------------------------------------------------
    845      */
    846     static private class RemoteGreenstoneServerRunScriptAction
    847     extends RemoteGreenstoneServerAction
    848     {
    849     private String collection_name;
    850     private String script_name;
    851     private String script_arguments;
    852     private GShell shell;
    853 
    854     public RemoteGreenstoneServerRunScriptAction(String collection_name, String script_name, String script_arguments, GShell shell)
    855     {
    856         this.collection_name = collection_name;
    857         this.script_name = script_name;
    858         this.script_arguments = script_arguments;
    859         this.shell = shell;
    860     }
    861 
    862     public void perform()
    863         throws Exception
    864     {
    865         progress_bar.setAction("Running " + script_name + "...");
    866 
    867         String run_script_command = "cmd=run-script";
    868         run_script_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    869         run_script_command += "&script=" + script_name;
    870         run_script_command += "&language=" + Configuration.getLanguage();
    871         run_script_command += script_arguments;
    872         action_output = sendCommandToServer(run_script_command, shell);
    873     }
    874     }
    875 
    876 
    877     /**
    878      * --------------------------------------------------------------------------------------------
    879      *    UPLOAD COLLECTION FILE
    880      * --------------------------------------------------------------------------------------------
    881      */
    882     static private class RemoteGreenstoneServerUploadCollectionFilesAction
    883     extends RemoteGreenstoneServerAction
    884     {
    885     private String collection_name;
    886     private File[] collection_files;
    887 
    888 
    889     public RemoteGreenstoneServerUploadCollectionFilesAction(String collection_name, File[] collection_files)
    890     {
    891         this.collection_name = collection_name;
    892         this.collection_files = collection_files;
    893     }
    894 
    895 
    896     public void perform()
    897         throws Exception
    898     {
    899         progress_bar.setAction("Uploading collection files...");
    900 
    901         // Determine the file paths relative to the collection directory
    902         String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
    903         String[] collection_file_relative_paths = new String[collection_files.length];
    904         for (int i = 0; i < collection_files.length; i++) {
    905         collection_file_relative_paths[i] = getPathRelativeToDirectory(collection_files[i], collection_directory_path);
    906         }
    907 
    908         // Zip up the files to send to the server
    909         String zip_file_name = collection_name + "-" + System.currentTimeMillis() + ".zip";
    910         String zip_file_path = collection_directory_path + zip_file_name;
    911         ZipTools.zipFiles(zip_file_path, collection_directory_path, collection_file_relative_paths);
    912        
    913         // Upload the zip file
    914         String upload_collection_file_command = "cmd=upload-collection-file";
    915         upload_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    916         upload_collection_file_command += "&file=" + URLEncoder.encode(zip_file_name, "UTF-8");
    917         upload_collection_file_command += "&directory=";
    918         upload_collection_file_command += "&zip=true";
    919         action_output = uploadFile(upload_collection_file_command, zip_file_path);
    920     }
    921     }
    922 
    923 
    924     /**
    925      * --------------------------------------------------------------------------------------------
    926      *    UPLOAD FILES INTO COLLECTION
    927      * --------------------------------------------------------------------------------------------
    928      */
    929     static private class RemoteGreenstoneServerUploadFilesIntoCollectionAction
    930     extends RemoteGreenstoneServerAction
    931     {
    932     private String collection_name;
    933     private File[] source_files;
    934     private File target_collection_directory;
    935 
    936 
    937     public RemoteGreenstoneServerUploadFilesIntoCollectionAction(String collection_name, File[] source_files, File target_collection_directory)
    938     {
    939         this.collection_name = collection_name;
    940         this.source_files = source_files;
    941         this.target_collection_directory = target_collection_directory;
    942     }
    943 
    944 
    945     public void perform()
    946         throws Exception
    947     {
    948         String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
    949         String target_collection_directory_relative_path = getPathRelativeToDirectory(target_collection_directory, collection_directory_path);
    950         target_collection_directory_relative_path = target_collection_directory_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
    951         progress_bar.setAction("Uploading files into collection...");
    952        
    953         String zip_file_name = collection_name + "-" + System.currentTimeMillis() + ".zip";
    954         String zip_file_path = Gatherer.getCollectDirectoryPath() + zip_file_name;
    955         DebugStream.println("Zip file path: " + zip_file_path);
    956 
    957         String base_directory_path = source_files[0].getParentFile().getAbsolutePath();
    958         DebugStream.println("Base directory path: " + base_directory_path);
    959         String[] source_file_relative_paths = new String[source_files.length];
    960         for (int i = 0; i < source_files.length; i++) {
    961         DebugStream.println("Source file path: " + source_files[i]);
    962         source_file_relative_paths[i] = getPathRelativeToDirectory(source_files[i], base_directory_path);
    963         }
    964        
    965         ZipTools.zipFiles(zip_file_path, base_directory_path, source_file_relative_paths);
    966 
    967         String upload_collection_file_command = "cmd=upload-collection-file";
    968         upload_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
    969         upload_collection_file_command += "&file=" + URLEncoder.encode(zip_file_name, "UTF-8");
    970         upload_collection_file_command += "&directory=" + URLEncoder.encode(target_collection_directory_relative_path, "UTF-8");
    971         upload_collection_file_command += "&zip=true";
    972         action_output = uploadFile(upload_collection_file_command, zip_file_path);
    973     }
    974     }
    975 
    976 
    977     // ----------------------------------------------------------------------------------------------------
    978319    //   AUTHENTICATION LAYER
    979320    // ----------------------------------------------------------------------------------------------------
    980 
    981 
    982     static private PasswordAuthentication remote_greenstone_server_authentication = null;
    983     // static private PasswordAuthentication remote_greenstone_server_authentication = new PasswordAuthentication(System.getProperty("user.name"), new char[] { });
    984 
    985     static public void set_remote_greenstone_server_authentication_to_null(){
    986     remote_greenstone_server_authentication = null;
    987     }
    988321
    989322    static private class RemoteGreenstoneServerAuthenticateTask
     
    1018351    }
    1019352    }
    1020 
    1021 
    1022     static private void authenticateUser()
    1023     throws RemoteGreenstoneServerActionCancelledException
     353   
     354    public void set_remote_greenstone_server_authentication_to_null() {
     355    remote_greenstone_server_authentication = null;
     356    }
     357
     358    private void authenticateUser()
     359    throws RemoteGreenstoneServerAction.ActionCancelledException
    1024360    {
    1025361    // If we don't have any authentication information then ask for it now
     
    1035371        // If it is still null then the user has cancelled the authentication, so the action is cancelled
    1036372        if (remote_greenstone_server_authentication == null) {
    1037         throw new RemoteGreenstoneServerActionCancelledException();
    1038         }
    1039     }
    1040     }
    1041 
    1042 
    1043     static public String getUsername()
     373        throw new RemoteGreenstoneServerAction.ActionCancelledException();
     374        }
     375    }
     376    }
     377
     378
     379    public String getUsername()
    1044380    {
    1045381    if (remote_greenstone_server_authentication != null) {
     
    1057393
    1058394    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
    1059     static private String downloadFile(String gliserver_args, String file_path)
     395    String downloadFile(String gliserver_args, String file_path)
    1060396    throws Exception
    1061397    {
     
    1081417        if (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Steal_Lock_Message", command_output.substring("ERROR: Collection is locked by: ".length())), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
    1082418            // The user has decided to cancel the action
    1083             throw new RemoteGreenstoneServerActionCancelledException();
     419            throw new RemoteGreenstoneServerAction.ActionCancelledException();
    1084420        }
    1085421
     
    1101437     * string embedded in the given gliserver_args. No authentication is required for either
    1102438     * of the commands greenstone-server-version and get-library-url-suffix. */
    1103     static private boolean isAuthenticationRequired(String gliserver_args) {
     439    private boolean isAuthenticationRequired(String gliserver_args) {
    1104440    return ((gliserver_args.indexOf("greenstone-server-version") == -1)
    1105441        && (gliserver_args.indexOf("get-library-url-suffix") == -1));
     
    1107443
    1108444
    1109     /** Returns the command output if the action completed, throws some kind of exception otherwise. */
    1110     static private String sendCommandToServer(String gliserver_args, GShell shell)
     445    /** Returns the command output if the action completed, throws some kind of exception otherwise.
     446     *  Package access, so that RemoteGreenstoneServerAction.java can call this.
     447     */
     448    String sendCommandToServer(String gliserver_args, GShell shell)
    1111449    throws Exception
    1112450    {
     
    1135473        if (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Steal_Lock_Message", command_output.substring("ERROR: Collection is locked by: ".length())), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
    1136474            // The user has decided to cancel the action
    1137             throw new RemoteGreenstoneServerActionCancelledException();
     475            throw new RemoteGreenstoneServerAction.ActionCancelledException();
    1138476        }
    1139477
     
    1157495
    1158496    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
    1159     static private String uploadFile(String gliserver_args, String file_path)
     497    String uploadFile(String gliserver_args, String file_path)
    1160498    throws Exception
    1161499    {
     
    1182520        if (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Steal_Lock_Message", command_output.substring("ERROR: Collection is locked by: ".length())), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
    1183521            // The user has decided to cancel the action
    1184             throw new RemoteGreenstoneServerActionCancelledException();
     522            throw new RemoteGreenstoneServerAction.ActionCancelledException();
    1185523        }
    1186524
     
    1206544
    1207545    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
    1208     static private String downloadFileInternal(String download_cgi, String cgi_args, String file_path)
     546    private String downloadFileInternal(String download_cgi, String cgi_args, String file_path)
    1209547    throws Exception
    1210548    {
     
    1262600
    1263601    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
    1264     static private String sendCommandToServerInternal(String gliserver_url_string, String cgi_args, GShell shell)
     602    private String sendCommandToServerInternal(String gliserver_url_string, String cgi_args, GShell shell)
    1265603    throws Exception
    1266604    {
     
    1290628        shell.fireMessage(gliserver_output_line);
    1291629        if (shell.hasSignalledStop()) {
    1292             throw new RemoteGreenstoneServerActionCancelledException();
     630            throw new RemoteGreenstoneServerAction.ActionCancelledException();
    1293631        }
    1294632        }
     
    1303641           
    1304642    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
    1305     static private String uploadFileInternal(String upload_cgi, String cgi_args, String file_path)
     643    private String uploadFileInternal(String upload_cgi, String cgi_args, String file_path)
    1306644    throws Exception
    1307645    {
     
    1433771
    1434772
    1435     static public String getPathRelativeToDirectory(File file, String directory_path)
     773    public String getPathRelativeToDirectory(File file, String directory_path)
    1436774    {
    1437775    String file_path = file.getAbsolutePath();
Note: See TracChangeset for help on using the changeset viewer.