/** *######################################################################### * * A component of the Greenstone Librarian Interface application, part of * the Greenstone digital library suite from the New Zealand Digital * Library Project at the University of Waikato, New Zealand. * * Author: Michael Dewsnip, NZDL Project, University of Waikato * * Copyright (C) 2005 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer.remote; import java.io.*; import java.net.*; import java.util.*; import java.util.zip.*; import javax.swing.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.DebugStream; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.FedoraInfo; import org.greenstone.gatherer.GAuthenticator; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.collection.CollectionManager; import org.greenstone.gatherer.shell.GShell; import org.greenstone.gatherer.util.UnzipTools; import org.greenstone.gatherer.util.Utility; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.*; import org.apache.commons.httpclient.params.*; import org.apache.commons.httpclient.HttpStatus; // ---------------------------------------------------------------------------------------------------- // Part of the RemoteGreenstoneServer's QUEUE LAYER // ---------------------------------------------------------------------------------------------------- // Moved here. Previously called RemoteGreenstoneServerActionQueue /** * A Thread that maintains a queue of RemoteGreenstoneServer Actions * that are to be executed in FIFO fashion. */ class ActionQueue extends Thread { /** The queue of waiting jobs. */ private ArrayList queue = null; private boolean exited; public ActionQueue() { super("RemoteGreenstoneServerActionQueue"); exited = false; if (Gatherer.isGsdlRemote) { queue = new ArrayList(); start(); } } synchronized public void addAction(RemoteGreenstoneServerAction remote_greenstone_server_action) { queue.add(remote_greenstone_server_action); notifyAll(); } synchronized public int size() { return queue.size(); } synchronized public RemoteGreenstoneServerAction getAction(int i) { return (RemoteGreenstoneServerAction)queue.get(i); } synchronized public boolean hasExited() { return exited; } synchronized public void clear() { queue.clear(); } public void run() { boolean exit = false; while (!exit) { RemoteGreenstoneServerAction remote_greenstone_server_action = null; // Wait until we are notify()ed by addAction that there is a new job on the queue try { if(Gatherer.remoteGreenstoneServer != null) { Gatherer.remoteGreenstoneServer.getProgressBar().setAction(null); } synchronized (this) { while(queue.size() <= 0) { wait(); // wait for queue size to become > 0, which is done by external thread (performAction()) } // Now there is at least one job on the queue, get the next in line and process it remote_greenstone_server_action = (RemoteGreenstoneServerAction) queue.get(0); //getAction(0) is already synchronized } } catch (InterruptedException exception) { // It may be that GLI is exiting if we get here exit = true; } if(exit) { break; } try { remote_greenstone_server_action.perform(); // No exceptions were thrown, so the action was successful remote_greenstone_server_action.processed_successfully = true; } catch (RemoteGreenstoneServerAction.ActionCancelledException exception) { remote_greenstone_server_action.processed_successfully = false; } catch(java.net.ConnectException exception) { if(exception.getMessage().trim().startsWith("Connection refused")) { exit = true; } else { DebugStream.printStackTrace(exception); } JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE); remote_greenstone_server_action.processed_successfully = false; } catch (FileNotFoundException exception) { // FileNotFoundException happens when there's no GS server at the user-provided // url (the address of gliserver.pl is wrong). exit = true; DebugStream.printStackTrace(exception); JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", "No gliserver.pl found. " + exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE); remote_greenstone_server_action.processed_successfully = false; } catch (Exception exception) { DebugStream.printStackTrace(exception); JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE); remote_greenstone_server_action.processed_successfully = false; } // We're done with this action, for better or worse try { remote_greenstone_server_action.processed = true; synchronized(remote_greenstone_server_action) { remote_greenstone_server_action.notifyAll(); // notifies RemoteGreenstoneServer.performAction() } } catch (Exception exception) { System.err.println("RemoteGreenstoneServerActionQueue.run() - exception: " + exception); } synchronized (this) { // remove the action just processed from the queue, since it's done. queue.remove(0); } } // Out of while, means exit = true // Stop the gazillion annoying error messages when the connection was simply // refused or when the user pressed Cancel in the opening dialog, by clearing // the action queue of subsequent actions and setting exited to true. synchronized(this) { queue.clear(); exited = true; } } }