source: gli/branches/rtl-gli/src/org/greenstone/gatherer/remote/ActionQueue.java@ 18361

Last change on this file since 18361 was 18361, checked in by kjdon, 15 years ago

updated the rtl-gli branch with files from trunk. Result of a merge 14807:18318

File size: 7.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Greenstone Librarian Interface application, part of
5 * the Greenstone digital library suite from the New Zealand Digital
6 * Library Project at the University of Waikato, New Zealand.
7 *
8 * Author: Michael Dewsnip, NZDL Project, University of Waikato
9 *
10 * Copyright (C) 2005 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27
28package org.greenstone.gatherer.remote;
29
30import java.io.*;
31import java.net.*;
32import java.util.*;
33import java.util.zip.*;
34import javax.swing.*;
35import org.greenstone.gatherer.Configuration;
36import org.greenstone.gatherer.DebugStream;
37import org.greenstone.gatherer.Dictionary;
38import org.greenstone.gatherer.FedoraInfo;
39import org.greenstone.gatherer.GAuthenticator;
40import org.greenstone.gatherer.Gatherer;
41import org.greenstone.gatherer.collection.CollectionManager;
42import org.greenstone.gatherer.shell.GShell;
43import org.greenstone.gatherer.util.UnzipTools;
44import org.greenstone.gatherer.util.Utility;
45import org.apache.commons.httpclient.HttpClient;
46import org.apache.commons.httpclient.methods.PostMethod;
47import org.apache.commons.httpclient.methods.GetMethod;
48import org.apache.commons.httpclient.HttpException;
49import org.apache.commons.httpclient.methods.multipart.FilePart;
50import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
51import org.apache.commons.httpclient.methods.multipart.Part;
52import org.apache.commons.httpclient.methods.multipart.*;
53import org.apache.commons.httpclient.params.*;
54import org.apache.commons.httpclient.HttpStatus;
55
56// ----------------------------------------------------------------------------------------------------
57// Part of the RemoteGreenstoneServer's QUEUE LAYER
58// ----------------------------------------------------------------------------------------------------
59
60// Moved here. Previously called RemoteGreenstoneServerActionQueue
61/**
62 * A Thread that maintains a queue of RemoteGreenstoneServer Actions
63 * that are to be executed in FIFO fashion.
64*/
65class ActionQueue extends Thread
66{
67 /** The queue of waiting jobs. */
68 private ArrayList queue = null;
69 private boolean exited;
70
71 public ActionQueue()
72 {
73 super("RemoteGreenstoneServerActionQueue");
74 exited = false;
75 if (Gatherer.isGsdlRemote) {
76 queue = new ArrayList();
77 start();
78 }
79 }
80
81
82 synchronized public void addAction(RemoteGreenstoneServerAction remote_greenstone_server_action)
83 {
84 queue.add(remote_greenstone_server_action);
85 notifyAll();
86 }
87
88
89 synchronized public int size()
90 {
91 return queue.size();
92 }
93
94 synchronized public RemoteGreenstoneServerAction getAction(int i)
95 {
96 return (RemoteGreenstoneServerAction)queue.get(i);
97 }
98
99 synchronized public boolean hasExited() {
100 return exited;
101 }
102
103 synchronized public void clear() {
104 queue.clear();
105 }
106
107 public void run()
108 {
109 boolean exit = false;
110
111 while (!exit) {
112 RemoteGreenstoneServerAction remote_greenstone_server_action = null;
113
114 // Wait until we are notify()ed by addAction that there is a new job on the queue
115 try {
116 Gatherer.remoteGreenstoneServer.getProgressBar().setAction(null);
117 synchronized (this) {
118 while(queue.size() <= 0) {
119 wait(); // wait for queue size to become > 0, which is done by external thread (performAction())
120 }
121 // Now there is at least one job on the queue, get the next in line and process it
122 remote_greenstone_server_action = (RemoteGreenstoneServerAction) queue.get(0); //getAction(0) is already synchronized
123 }
124 } catch (InterruptedException exception) {
125 // It may be that GLI is exiting if we get here
126 exit = true;
127 }
128
129 if(exit) {
130 break;
131 }
132
133 try {
134 remote_greenstone_server_action.perform();
135
136 // No exceptions were thrown, so the action was successful
137 remote_greenstone_server_action.processed_successfully = true;
138 }
139 catch (RemoteGreenstoneServerAction.ActionCancelledException exception) {
140 remote_greenstone_server_action.processed_successfully = false;
141 }
142 catch(java.net.ConnectException exception) {
143 if(exception.getMessage().trim().startsWith("Connection refused")) {
144 exit = true;
145 } else {
146 DebugStream.printStackTrace(exception);
147 }
148 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
149 remote_greenstone_server_action.processed_successfully = false;
150 }
151 catch (FileNotFoundException exception) {
152 // FileNotFoundException happens when there's no GS server at the user-provided
153 // url (the address of gliserver.pl is wrong).
154 exit = true;
155 DebugStream.printStackTrace(exception);
156 JOptionPane.showMessageDialog(Gatherer.g_man,
157 Dictionary.get("RemoteGreenstoneServer.Error",
158 "No gliserver.pl found. " + exception.getMessage()),
159 Dictionary.get("RemoteGreenstoneServer.Error_Title"),
160 JOptionPane.ERROR_MESSAGE);
161 remote_greenstone_server_action.processed_successfully = false;
162 }
163 catch (Exception exception) {
164 DebugStream.printStackTrace(exception);
165 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
166 remote_greenstone_server_action.processed_successfully = false;
167 }
168
169 // We're done with this action, for better or worse
170 try {
171 remote_greenstone_server_action.processed = true;
172 synchronized(remote_greenstone_server_action) {
173 remote_greenstone_server_action.notifyAll(); // notifies RemoteGreenstoneServer.performAction()
174 }
175 } catch (Exception exception) {
176 System.err.println("RemoteGreenstoneServerActionQueue.run() - exception: " + exception);
177 }
178 synchronized (this) { // remove the action just processed from the queue, since it's done.
179 queue.remove(0);
180 }
181 }
182
183 // Out of while, means exit = true
184 // Stop the gazillion annoying error messages when the connection was simply
185 // refused or when the user pressed Cancel in the opening dialog, by clearing
186 // the action queue of subsequent actions and setting exited to true.
187 synchronized(this) {
188 queue.clear();
189 exited = true;
190 }
191 }
192}
Note: See TracBrowser for help on using the repository browser.