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 |
|
---|
28 | package org.greenstone.gatherer.remote;
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import java.net.*;
|
---|
32 | import java.util.*;
|
---|
33 | import java.util.zip.*;
|
---|
34 | import javax.swing.*;
|
---|
35 | import org.greenstone.gatherer.Configuration;
|
---|
36 | import org.greenstone.gatherer.DebugStream;
|
---|
37 | import org.greenstone.gatherer.Dictionary;
|
---|
38 | import org.greenstone.gatherer.FedoraInfo;
|
---|
39 | import org.greenstone.gatherer.GAuthenticator;
|
---|
40 | import org.greenstone.gatherer.Gatherer;
|
---|
41 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
42 | import org.greenstone.gatherer.shell.GShell;
|
---|
43 | import org.greenstone.gatherer.util.UnzipTools;
|
---|
44 | import org.greenstone.gatherer.util.Utility;
|
---|
45 | import org.apache.commons.httpclient.HttpClient;
|
---|
46 | import org.apache.commons.httpclient.methods.PostMethod;
|
---|
47 | import org.apache.commons.httpclient.methods.GetMethod;
|
---|
48 | import org.apache.commons.httpclient.HttpException;
|
---|
49 | import org.apache.commons.httpclient.methods.multipart.FilePart;
|
---|
50 | import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
|
---|
51 | import org.apache.commons.httpclient.methods.multipart.Part;
|
---|
52 | import org.apache.commons.httpclient.methods.multipart.*;
|
---|
53 | import org.apache.commons.httpclient.params.*;
|
---|
54 | import 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 | */
|
---|
65 | class 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 | if(Gatherer.remoteGreenstoneServer != null) {
|
---|
117 | Gatherer.remoteGreenstoneServer.getProgressBar().setAction(null);
|
---|
118 | }
|
---|
119 | synchronized (this) {
|
---|
120 | while(queue.size() <= 0) {
|
---|
121 | wait(); // wait for queue size to become > 0, which is done by external thread (performAction())
|
---|
122 | }
|
---|
123 | // Now there is at least one job on the queue, get the next in line and process it
|
---|
124 | remote_greenstone_server_action = (RemoteGreenstoneServerAction) queue.get(0); //getAction(0) is already synchronized
|
---|
125 | }
|
---|
126 | } catch (InterruptedException exception) {
|
---|
127 | // It may be that GLI is exiting if we get here
|
---|
128 | exit = true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if(exit) {
|
---|
132 | break;
|
---|
133 | }
|
---|
134 |
|
---|
135 | try {
|
---|
136 | remote_greenstone_server_action.perform();
|
---|
137 |
|
---|
138 | // No exceptions were thrown, so the action was successful
|
---|
139 | remote_greenstone_server_action.processed_successfully = true;
|
---|
140 | }
|
---|
141 | catch (RemoteGreenstoneServerAction.ActionCancelledException exception) {
|
---|
142 | remote_greenstone_server_action.processed_successfully = false;
|
---|
143 | }
|
---|
144 | catch(java.net.ConnectException exception) {
|
---|
145 | if(exception.getMessage().trim().startsWith("Connection refused")) {
|
---|
146 | exit = true;
|
---|
147 | } else {
|
---|
148 | DebugStream.printStackTrace(exception);
|
---|
149 | }
|
---|
150 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
|
---|
151 | remote_greenstone_server_action.processed_successfully = false;
|
---|
152 | }
|
---|
153 | catch (FileNotFoundException exception) {
|
---|
154 | // FileNotFoundException happens when there's no GS server at the user-provided
|
---|
155 | // url (the address of gliserver.pl is wrong).
|
---|
156 | exit = true;
|
---|
157 | DebugStream.printStackTrace(exception);
|
---|
158 | JOptionPane.showMessageDialog(Gatherer.g_man,
|
---|
159 | Dictionary.get("RemoteGreenstoneServer.Error",
|
---|
160 | "No gliserver.pl found. " + exception.getMessage()),
|
---|
161 | Dictionary.get("RemoteGreenstoneServer.Error_Title"),
|
---|
162 | JOptionPane.ERROR_MESSAGE);
|
---|
163 | remote_greenstone_server_action.processed_successfully = false;
|
---|
164 | }
|
---|
165 | catch (Exception exception) {
|
---|
166 | DebugStream.printStackTrace(exception);
|
---|
167 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
|
---|
168 | remote_greenstone_server_action.processed_successfully = false;
|
---|
169 | }
|
---|
170 |
|
---|
171 | // We're done with this action, for better or worse
|
---|
172 | try {
|
---|
173 | remote_greenstone_server_action.processed = true;
|
---|
174 | synchronized(remote_greenstone_server_action) {
|
---|
175 | remote_greenstone_server_action.notifyAll(); // notifies RemoteGreenstoneServer.performAction()
|
---|
176 | }
|
---|
177 | } catch (Exception exception) {
|
---|
178 | System.err.println("RemoteGreenstoneServerActionQueue.run() - exception: " + exception);
|
---|
179 | }
|
---|
180 | synchronized (this) { // remove the action just processed from the queue, since it's done.
|
---|
181 | queue.remove(0);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | // Out of while, means exit = true
|
---|
186 | // Stop the gazillion annoying error messages when the connection was simply
|
---|
187 | // refused or when the user pressed Cancel in the opening dialog, by clearing
|
---|
188 | // the action queue of subsequent actions and setting exited to true.
|
---|
189 | synchronized(this) {
|
---|
190 | queue.clear();
|
---|
191 | exited = true;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | } |
---|