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.util.*;
|
---|
32 | import javax.swing.*;
|
---|
33 |
|
---|
34 | import org.greenstone.gatherer.DebugStream;
|
---|
35 | import org.greenstone.gatherer.Dictionary;
|
---|
36 | import org.greenstone.gatherer.Gatherer;
|
---|
37 |
|
---|
38 | // ----------------------------------------------------------------------------------------------------
|
---|
39 | // Part of the RemoteGreenstoneServer's QUEUE LAYER
|
---|
40 | // ----------------------------------------------------------------------------------------------------
|
---|
41 |
|
---|
42 | // Moved here. Previously called RemoteGreenstoneServerActionQueue
|
---|
43 | /**
|
---|
44 | * A Thread that maintains a queue of RemoteGreenstoneServer Actions
|
---|
45 | * that are to be executed in FIFO fashion.
|
---|
46 | */
|
---|
47 | class ActionQueue extends Thread
|
---|
48 | {
|
---|
49 | /** The queue of waiting jobs. */
|
---|
50 | @SuppressWarnings("rawtypes")
|
---|
51 | private ArrayList queue = null;
|
---|
52 | private boolean exited;
|
---|
53 |
|
---|
54 | @SuppressWarnings("rawtypes")
|
---|
55 | public ActionQueue()
|
---|
56 | {
|
---|
57 | super("RemoteGreenstoneServerActionQueue");
|
---|
58 | exited = false;
|
---|
59 | if (Gatherer.isGsdlRemote) {
|
---|
60 | queue = new ArrayList();
|
---|
61 | start();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | @SuppressWarnings("unchecked")
|
---|
67 | synchronized public void addAction(RemoteGreenstoneServerAction remote_greenstone_server_action)
|
---|
68 | {
|
---|
69 | queue.add(remote_greenstone_server_action);
|
---|
70 | notifyAll();
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | synchronized public int size()
|
---|
75 | {
|
---|
76 | return queue.size();
|
---|
77 | }
|
---|
78 |
|
---|
79 | synchronized public RemoteGreenstoneServerAction getAction(int i)
|
---|
80 | {
|
---|
81 | return (RemoteGreenstoneServerAction)queue.get(i);
|
---|
82 | }
|
---|
83 |
|
---|
84 | synchronized public boolean hasExited() {
|
---|
85 | return exited;
|
---|
86 | }
|
---|
87 |
|
---|
88 | synchronized public void clear() {
|
---|
89 | queue.clear();
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void run()
|
---|
93 | {
|
---|
94 | boolean exit = false;
|
---|
95 |
|
---|
96 | while (!exit) {
|
---|
97 | RemoteGreenstoneServerAction remote_greenstone_server_action = null;
|
---|
98 |
|
---|
99 | // Wait until we are notify()ed by addAction that there is a new job on the queue
|
---|
100 | try {
|
---|
101 | if(Gatherer.remoteGreenstoneServer != null) {
|
---|
102 | Gatherer.remoteGreenstoneServer.getProgressBar().setAction(null);
|
---|
103 | }
|
---|
104 | synchronized (this) {
|
---|
105 | while(queue.size() <= 0) {
|
---|
106 | wait(); // wait for queue size to become > 0, which is done by external thread (performAction())
|
---|
107 | }
|
---|
108 | // Now there is at least one job on the queue, get the next in line and process it
|
---|
109 | remote_greenstone_server_action = (RemoteGreenstoneServerAction) queue.get(0); //getAction(0) is already synchronized
|
---|
110 | }
|
---|
111 | } catch (InterruptedException exception) {
|
---|
112 | // It may be that GLI is exiting if we get here
|
---|
113 | exit = true;
|
---|
114 | }
|
---|
115 |
|
---|
116 | if(exit) {
|
---|
117 | break;
|
---|
118 | }
|
---|
119 |
|
---|
120 | try {
|
---|
121 | remote_greenstone_server_action.perform();
|
---|
122 |
|
---|
123 | // No exceptions were thrown, so the action was successful
|
---|
124 | remote_greenstone_server_action.processed_successfully = true;
|
---|
125 | }
|
---|
126 | catch (RemoteGreenstoneServerAction.ActionCancelledException exception) {
|
---|
127 | remote_greenstone_server_action.processed_successfully = false;
|
---|
128 | }
|
---|
129 | catch(java.net.ConnectException exception) {
|
---|
130 | if(exception.getMessage().trim().startsWith("Connection refused")) {
|
---|
131 | exit = true;
|
---|
132 | } else {
|
---|
133 | DebugStream.printStackTrace(exception);
|
---|
134 | }
|
---|
135 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
|
---|
136 | remote_greenstone_server_action.processed_successfully = false;
|
---|
137 | }
|
---|
138 | catch (FileNotFoundException exception) {
|
---|
139 | // FileNotFoundException happens when there's no GS server at the user-provided
|
---|
140 | // url (the address of gliserver.pl is wrong).
|
---|
141 | exit = true;
|
---|
142 | DebugStream.printStackTrace(exception);
|
---|
143 | JOptionPane.showMessageDialog(Gatherer.g_man,
|
---|
144 | Dictionary.get("RemoteGreenstoneServer.Error",
|
---|
145 | "No gliserver.pl found. " + exception.getMessage()),
|
---|
146 | Dictionary.get("RemoteGreenstoneServer.Error_Title"),
|
---|
147 | JOptionPane.ERROR_MESSAGE);
|
---|
148 | remote_greenstone_server_action.processed_successfully = false;
|
---|
149 | }
|
---|
150 | catch (Exception exception) {
|
---|
151 | DebugStream.printStackTrace(exception);
|
---|
152 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
|
---|
153 | remote_greenstone_server_action.processed_successfully = false;
|
---|
154 | }
|
---|
155 |
|
---|
156 | // We're done with this action, for better or worse
|
---|
157 | try {
|
---|
158 | remote_greenstone_server_action.processed = true;
|
---|
159 | synchronized(remote_greenstone_server_action) {
|
---|
160 | remote_greenstone_server_action.notifyAll(); // notifies RemoteGreenstoneServer.performAction()
|
---|
161 | }
|
---|
162 | } catch (Exception exception) {
|
---|
163 | System.err.println("RemoteGreenstoneServerActionQueue.run() - exception: " + exception);
|
---|
164 | }
|
---|
165 | synchronized (this) { // remove the action just processed from the queue, since it's done.
|
---|
166 | queue.remove(0);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | // Out of while, means exit = true
|
---|
171 | // Stop the gazillion annoying error messages when the connection was simply
|
---|
172 | // refused or when the user pressed Cancel in the opening dialog, by clearing
|
---|
173 | // the action queue of subsequent actions and setting exited to true.
|
---|
174 | synchronized(this) {
|
---|
175 | queue.clear();
|
---|
176 | exited = true;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | } |
---|