1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 | package org.greenstone.gatherer.shell;
|
---|
38 |
|
---|
39 | import java.awt.Component;
|
---|
40 | import java.util.ArrayList;
|
---|
41 | import org.greenstone.gatherer.Configuration;
|
---|
42 | import org.greenstone.gatherer.DebugStream;
|
---|
43 | import org.greenstone.gatherer.Dictionary;
|
---|
44 | import org.greenstone.gatherer.Gatherer;
|
---|
45 | import org.greenstone.gatherer.cdm.CollectionDesignManager;
|
---|
46 | import org.greenstone.gatherer.gui.GProgressBar;
|
---|
47 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
48 | /** This implementation of <i>GShellProgressMonitor</i> is designed to parse and translate the progress of a buildcol.pl call.
|
---|
49 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
50 | * @version 2.3
|
---|
51 | */
|
---|
52 | /* Wendy's feeble attempt at writing a schedule progress monitor. While it's not really needed, I'm writing it so that the schedule.pl prompts will go away. ;) This also may need other "ELEMENTS" added to it.
|
---|
53 | */
|
---|
54 |
|
---|
55 | public class GScheduleProgressMonitor
|
---|
56 | implements GShellProgressMonitor {
|
---|
57 | /** The minimum value = 0 */
|
---|
58 | static final private int MIN = 0;
|
---|
59 | /** In order to get a smoothish progress bar, set the maximum to a large number = 1000000 */
|
---|
60 | static final private int MAX = 1000000;
|
---|
61 | /** The various states the Schedule progress state machine can be in, starting at the base state. There are only two stages - perl script generation and cron building - so i'm not sure we need too many steps. */
|
---|
62 | static final int BASE = -1;
|
---|
63 | static final int SCHEDULE = 0;
|
---|
64 | static final int DONE = 1;
|
---|
65 | /** Now the language independant sentinel strings. */
|
---|
66 | static final String SCHEDULE_ELEMENT = "Schedule";
|
---|
67 | static final String SCHEDULE_DELETE = "Delete";
|
---|
68 | static final String SCHEDULE_DONE="Done";
|
---|
69 |
|
---|
70 | static final private String ARGUMENT_ATTRIBUTE = "a";
|
---|
71 | static final private String CLASSIFIER_ATTRIBUTE = "c";
|
---|
72 | static final private String PLUGIN_ATTRIBUTE = "p";
|
---|
73 |
|
---|
74 | /** Indicates if the GUI has asked the process this object monitors to stop. */
|
---|
75 | private boolean stop = false;
|
---|
76 | /** The current number of stages we have completed. */
|
---|
77 | private int current_stages = 0;
|
---|
78 | /** The number of stages we are expecting to complete. */
|
---|
79 | private int expected_stages = 0;
|
---|
80 |
|
---|
81 | private int threshold = Configuration.LIBRARIAN_MODE;
|
---|
82 | /** The current state we are in. The state will change depending on the next flag recieved. */
|
---|
83 | private int state = BASE;
|
---|
84 | /** The progress bar this monitor updates. */
|
---|
85 | //private GProgressBar progress_bar;
|
---|
86 | /** A progress bar that is shared between this this listener and the Import and build monitor. not sure we need this. */
|
---|
87 | //private GProgressBar shared_progress_bar;
|
---|
88 |
|
---|
89 | /** Construct a new GScheduleProgressMonitor. */
|
---|
90 | public GScheduleProgressMonitor() {
|
---|
91 | //this.shared_progress_bar = shared_progress_bar;
|
---|
92 | //progress_bar = new GProgressBar();
|
---|
93 | //progress_bar.setMaximum(MAX);
|
---|
94 | //progress_bar.setMinimum(MIN);
|
---|
95 | //progress_bar.setString(null);
|
---|
96 | //progress_bar.setStringPainted(true);
|
---|
97 | //setValue(MIN);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Method to register a new progress bar with this monitor.
|
---|
101 | * @param progress_bar The new <strong>GProgressBar</strong>.
|
---|
102 | */
|
---|
103 | /*note - we don't really need this. Wendy */
|
---|
104 | public void addProgressBar(GProgressBar progress_bar) {
|
---|
105 | //this.progress_bar = progress_bar;
|
---|
106 | //progress_bar.setMaximum(MAX);
|
---|
107 | //progress_bar.setMinimum(MIN);
|
---|
108 | //progress_bar.setStringPainted(false);
|
---|
109 | //progress_bar.setString(null);
|
---|
110 | //setValue(MIN);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** Determine the script exit value according to the progress monitor. This gets around a problem where several script failures actually result with a successful exit value.
|
---|
114 | * @return A <i>int</i> with a value of zero if and only if the script was successful.
|
---|
115 | */
|
---|
116 | public int exitValue() {
|
---|
117 | if(state == BASE) {
|
---|
118 | return 0;
|
---|
119 | }
|
---|
120 | return 1;
|
---|
121 | }
|
---|
122 | /** Method to retrieve whatever control is being used as the progress indicator. Usually a <strong>GProgressBar</strong> but there may be others implemented later.
|
---|
123 | * @return A <strong>Component</strong> on which the progress of the process is being displayed.
|
---|
124 | */
|
---|
125 | public Component getProgress() {
|
---|
126 | //return progress_bar;
|
---|
127 | return null;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public GProgressBar getSharedProgress() {
|
---|
131 | //return shared_progress_bar;
|
---|
132 | //try returning null;
|
---|
133 | return null;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void messageOnProgressBar(String message)
|
---|
137 | {
|
---|
138 | //if (message!=null && !message.equals("")) {
|
---|
139 | //progress_bar.setString(message);
|
---|
140 | //shared_progress_bar.setString(message);
|
---|
141 | //}
|
---|
142 | //else {
|
---|
143 | //progress_bar.setString(null);
|
---|
144 | //shared_progress_bar.setString(null);
|
---|
145 | //}
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Method to determine the state of the stop flag, which may be set by the visual component that created this monitor.
|
---|
149 | * @return A <strong>boolean</strong> indicating if the process has been asked to stop.
|
---|
150 | */
|
---|
151 | public synchronized boolean hasSignalledStop() {
|
---|
152 | return stop;
|
---|
153 | }
|
---|
154 | /** Inform the progress bar that it should programatically increment progress by one step. */
|
---|
155 | public void increment() {
|
---|
156 | // I'm not sure we really need this for scheduling, so i'm going to ignore it for now.
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** This method is used to 'feed in' a line of text captured from the process.
|
---|
160 | * @param queue a queue which at the moment should contain a single GShellEvent
|
---|
161 | */
|
---|
162 | public void process(ArrayList queue) {
|
---|
163 | GShellEvent event = (GShellEvent) queue.get(0);
|
---|
164 | // Remove 'schedule.pl> ' bit
|
---|
165 | String line = event.getMessage();
|
---|
166 | line = line.substring(line.indexOf(StaticStrings.GREATER_THAN_CHARACTER) + 1);
|
---|
167 | line = line.trim();
|
---|
168 | // We are only really interested in processing pseudo-XML tags
|
---|
169 | if(line.startsWith(StaticStrings.LESS_THAN_CHARACTER) && line.endsWith(StaticStrings.GREATER_THAN_CHARACTER)) {
|
---|
170 | // All events are vetoed
|
---|
171 | event.veto();
|
---|
172 |
|
---|
173 | // Create a new element from it
|
---|
174 | GShellElement element = new GShellElement(line);
|
---|
175 | // Now change states within the state machine as appropriate. A state change may subsequently incur a progress update.
|
---|
176 | String name = null;
|
---|
177 | String value = null;
|
---|
178 |
|
---|
179 | name = element.getElementName();
|
---|
180 | if(name.equals(SCHEDULE_ELEMENT)) {
|
---|
181 | //progress_bar.setIndeterminate(false);
|
---|
182 | // Produce a lower detail message if required
|
---|
183 | if(Configuration.getMode() <= threshold && !stop) {
|
---|
184 | queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Schedule.ScheduleBegun1"), event.getStatus()));
|
---|
185 | }
|
---|
186 | }
|
---|
187 | else if(name.equals(SCHEDULE_DELETE)) {
|
---|
188 | if(Configuration.getMode() <= threshold) {
|
---|
189 | queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Schedule.ScheduleDelete"), event.getStatus()));
|
---|
190 | }
|
---|
191 | }
|
---|
192 | else if(name.equals(SCHEDULE_DONE)) {
|
---|
193 | if(Configuration.getMode() <= threshold) {
|
---|
194 | queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Schedule.ScheduleComplete1"), event.getStatus()));
|
---|
195 | }
|
---|
196 | }
|
---|
197 | else {
|
---|
198 | // Unknown command, go indeterminate
|
---|
199 | DebugStream.println("Unknown name: " + name);
|
---|
200 | //progress_bar.setIndeterminate(true);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 |
|
---|
205 | }
|
---|
206 | // If we are dealing with lower detail modes then veto any other messages. We do this here as the progress monitors are garuenteed to recieve this message before anything else
|
---|
207 | else if(Configuration.getMode() <= threshold) {
|
---|
208 | event.veto();
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | public void reset() {
|
---|
213 | current_stages = 0;
|
---|
214 | expected_stages = 0;
|
---|
215 | //progress_bar.setIndeterminate(false);
|
---|
216 | //progress_bar.setString(null);
|
---|
217 | setValue(MIN);
|
---|
218 | state = BASE;
|
---|
219 | //progress_bar.updateUI();
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void saving() {
|
---|
223 | }
|
---|
224 |
|
---|
225 | public void setSharedProgressBar(GProgressBar shared_progress_bar) {
|
---|
226 | //this.shared_progress_bar = shared_progress_bar;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Since the creator of this process monitor is actually in the GUI, this class provides the simpliest way to send a cancel process message between the two.
|
---|
230 | * @param state The desired state of the stop flag as a <strong>boolean</strong>.
|
---|
231 | */
|
---|
232 | public synchronized void setStop(boolean state) {
|
---|
233 | this.stop = state;
|
---|
234 | //progress_bar.setIndeterminate(false);
|
---|
235 | }
|
---|
236 | /** This method resets this monitor to the start, reseting the process parsing and progress bar.
|
---|
237 | * TODO Everthing.
|
---|
238 | */
|
---|
239 | public void start() {
|
---|
240 | stop = false;
|
---|
241 | setValue(MIN);
|
---|
242 | expected_stages = 1; // Always compress text, create info database and auxiliary creation
|
---|
243 |
|
---|
244 |
|
---|
245 | }
|
---|
246 | /** This method indicates the process is complete.
|
---|
247 | * TODO Everthing.
|
---|
248 | */
|
---|
249 | public void stop() {
|
---|
250 | //progress_bar.setIndeterminate(false);
|
---|
251 | setValue(MAX);
|
---|
252 | }
|
---|
253 |
|
---|
254 | private void setValue(int value) {
|
---|
255 | //progress_bar.setValue(value);
|
---|
256 | // Incrementing the shared progress bar is a little more problematic as we may very well be trying to set it to MIN but instead set it to halfway if we're not careful.
|
---|
257 | //if(value > MIN) {
|
---|
258 | // shared_progress_bar.setValue(MAX + value);
|
---|
259 | //}
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|