source: gli/tags/2-74/src/org/greenstone/gatherer/Gatherer.java@ 14422

Last change on this file since 14422 was 14422, checked in by qq6, 17 years ago

Couldn't get either the classifier or plugin list, when the remote gli applet was running.I decided to move this part back into init().

  • Property svn:keywords set to Author Date Id Revision
File size: 36.7 KB
Line 
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 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 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 */
27package org.greenstone.gatherer;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.io.*;
32import java.lang.*;
33import java.net.*;
34import java.util.*;
35import javax.swing.*;
36import javax.swing.plaf.*;
37import javax.swing.text.*;
38import org.greenstone.gatherer.Configuration;
39import org.greenstone.gatherer.GAuthenticator;
40import org.greenstone.gatherer.collection.CollectionManager;
41import org.greenstone.gatherer.feedback.ActionRecorderDialog;
42import org.greenstone.gatherer.file.FileManager;
43import org.greenstone.gatherer.file.FileAssociationManager;
44import org.greenstone.gatherer.file.RecycleBin;
45import org.greenstone.gatherer.greenstone.Classifiers;
46import org.greenstone.gatherer.greenstone.LocalGreenstone;
47import org.greenstone.gatherer.greenstone.LocalLibraryServer;
48import org.greenstone.gatherer.greenstone.Plugins;
49import org.greenstone.gatherer.greenstone3.ServletConfiguration;
50import org.greenstone.gatherer.gui.GUIManager;
51import org.greenstone.gatherer.gui.URLField;
52import org.greenstone.gatherer.gui.WarningDialog;
53import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
54import org.greenstone.gatherer.util.JarTools;
55import org.greenstone.gatherer.util.StaticStrings;
56import org.greenstone.gatherer.util.Utility;
57
58
59/** Containing the top-level "core" for the Gatherer, this class is the
60 * common core for the GLI application and applet. It first parses the
61 * command line arguments, preparing to update the configuration as
62 * required. Next it loads several important support classes such as the
63 * Configuration and Dictionary. Finally it creates the other important
64 * managers and sends them on their way.
65 * @author John Thompson, Greenstone Digital Library, University of Waikato
66 * @version 2.3
67 */
68public class Gatherer
69{
70 /** The name of the GLI. */
71 static final public String PROGRAM_NAME = "Greenstone Librarian Interface";
72 /** The current version of the GLI. */
73 static final public String PROGRAM_VERSION = "v2.74";
74
75 static private Dimension size = new Dimension(800, 540);
76
77 /** Has the exit flag been set? */
78 static final public int EXIT_THEN_RESTART= 2;
79 static public boolean exit = false;
80 static public int exit_status = 0;
81
82 static private String gli_directory_path = null;
83 static private String gli_user_directory_path = null;
84
85 static public String client_operating_system = null;
86
87 /** All of the external applications that must exit before we close the Gatherer. */
88 static private Vector apps = new Vector();
89 static private String non_standard_collect_directory_path = null;
90 static public String open_collection_file_path = null;
91 /** A public reference to the FileAssociationManager. */
92 static public FileAssociationManager assoc_man;
93 /** A public reference to the CollectionManager. */
94 static public CollectionManager c_man;
95 /** A public reference to the RecycleBin. */
96 static public RecycleBin recycle_bin;
97 /** a reference to the Servlet Configuration is GS3 */
98 static public ServletConfiguration servlet_config;
99 /** A public reference to the FileManager. */
100 static public FileManager f_man;
101 /** A public reference to the GUIManager. */
102 static public GUIManager g_man = null;
103 static private boolean g_man_built = false;
104
105 /** We are using the GLI for GS3 */
106 static public boolean GS3 = false;
107
108 static public boolean isApplet = false;
109 static public boolean isGsdlRemote = false;
110
111 // feedback stuff
112 /** is the feedback feature enabled? */
113 static public boolean feedback_enabled = true;
114 /** the action recorder dialog */
115 static public ActionRecorderDialog feedback_dialog = null;
116
117 // Refresh reasons
118 static public final int COLLECTION_OPENED = 0;
119 static public final int COLLECTION_CLOSED = 1;
120 static public final int COLLECTION_REBUILT = 2;
121 static public final int PREFERENCES_CHANGED = 3;
122
123 //////kk added////////
124 static public String cgiBase="";
125 /////////////////
126
127 /** Magic to allow Enter to fire the default button. */
128 static {
129 KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
130 Keymap map = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);
131 map.removeKeyStrokeBinding(enter);
132 }
133
134
135 public Gatherer(String[] args)
136 {
137 // Display the version to make error reports a lot more useful
138 System.err.println("Version: " + PROGRAM_VERSION + "\n");
139
140 JarTools.initialise(this);
141
142 GetOpt go = new GetOpt(args);
143
144 // Remember the GSDLOS value
145 client_operating_system = go.client_operating_system;
146
147 // If feedback is enabled, set up the recorder dialog
148 if (go.feedback_enabled) {
149 // Use the default locale for now - this will be changed by the Gatherer run method
150 feedback_enabled = true;
151 feedback_dialog = new ActionRecorderDialog(Locale.getDefault());
152 }
153
154 // Are we using a remote Greenstone?
155 if (go.use_remote_greenstone) {
156 isGsdlRemote = true;
157
158 // We don't have a local Greenstone!
159 go.gsdl_path = null;
160
161 // We have to use our own collect directory since we can't use the Greenstone one
162 setCollectDirectoryPath(getGLIUserDirectoryPath() + "collect" + File.separator);
163 }
164 // No, we have a local Greenstone
165 else {
166 LocalGreenstone.setDirectoryPath(go.gsdl_path);
167 }
168
169 // Users may specify a non-standard collect directory (eg. when running one GLI in a network environment)
170 if (go.collect_directory_path != null) {
171 setCollectDirectoryPath(go.collect_directory_path);
172 }
173
174 // More special code for running with a remote Greenstone
175 if (isGsdlRemote) {
176 Configuration.TEMPLATE_CONFIG_XML = "xml/configRemote.xml";
177 Configuration.CONFIG_XML = "configRemote.xml";
178 Configuration.GS3_CONFIG_XML = "config3Remote.xml";
179
180 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
181 if (!collect_directory.exists() && !collect_directory.mkdir()) {
182 System.err.println("Warning: Unable to make directory: " + collect_directory);
183 }
184 }
185
186 init(go.gsdl_path, go.gsdl3_path, go.gsdl3_src_path, go.local_library_path, go.library_url_string,
187 go.gliserver_url_string, go.debug, go.perl_path, go.no_load, go.filename, go.site_name,
188 go.servlet_path);
189 }
190
191
192 public void init(String gsdl_path, String gsdl3_path, String gsdl3_src_path, String local_library_path,
193 String library_url_string, String gliserver_url_string, boolean debug_enabled,
194 String perl_path, boolean no_load, String open_collection,
195 String site_name, String servlet_path)
196 {
197 if (gsdl3_path != null && !gsdl3_path.equals("")) {
198 this.GS3 = true;
199 } else {
200 gsdl3_path = null;
201 gsdl3_src_path = null;
202 }
203
204 // Create the debug stream if required
205 if (debug_enabled) {
206 DebugStream.enableDebugging();
207
208 Calendar now = Calendar.getInstance();
209 String debug_file_path = "debug" + now.get(Calendar.DATE) + "-" + now.get(Calendar.MONTH) + "-" + now.get(Calendar.YEAR) + ".txt";
210
211 // Debug file is created in the user's GLI directory
212 debug_file_path = getGLIUserDirectoryPath() + debug_file_path;
213 DebugStream.println("Debug file path: " + debug_file_path);
214 DebugStream.setDebugFile(debug_file_path);
215 DebugStream.print(System.getProperties());
216 }
217
218 // Delete plugins.dat and classifiers.dat files from previous versions of the GLI (no longer used)
219 File plugins_dat_file = new File(Gatherer.getGLIUserDirectoryPath() + "plugins.dat");
220 if (plugins_dat_file.exists()) {
221 System.err.println("Deleting plugins.dat file...");
222 Utility.delete(plugins_dat_file);
223 }
224 File classifiers_dat_file = new File(Gatherer.getGLIUserDirectoryPath() + "classifiers.dat");
225 if (classifiers_dat_file.exists()) {
226 System.err.println("Deleting classifiers.dat file...");
227 Utility.delete(classifiers_dat_file);
228 }
229
230 try {
231 // Load GLI config file
232 new Configuration(getGLIUserDirectoryPath(), gsdl_path, gsdl3_path, gsdl3_src_path, site_name);
233
234 if (GS3) {
235 // Load Greenstone 3 servlet configuration
236 servlet_config = new ServletConfiguration(gsdl3_path);
237 }
238
239 // Check we know where Perl is
240 Configuration.perl_path = perl_path;
241 if (isGsdlRemote && Utility.isWindows() && Configuration.perl_path != null) {
242 if (Configuration.perl_path.toLowerCase().endsWith("perl.exe")) {
243 Configuration.perl_path = Configuration.perl_path.substring(0, Configuration.perl_path.length() - "perl.exe".length());
244 }
245 if (Configuration.perl_path.endsWith(File.separator)) {
246 Configuration.perl_path = Configuration.perl_path.substring(0, Configuration.perl_path.length() - File.separator.length());
247 }
248 }
249
250 if (GS3 && Configuration.servlet_path == null) {
251 Configuration.servlet_path = servlet_config.getServletPath(Configuration.site_name);
252 }
253
254 // the feedback dialog has been loaded with a default locale,
255 // now set the user specified one
256 if (feedback_enabled && feedback_dialog != null) {
257 feedback_dialog.setLocale(Configuration.getLocale("general.locale", true));
258 }
259
260 // Read Dictionary
261 new Dictionary(Configuration.getLocale("general.locale", true), Configuration.getFont("general.font", true));
262
263 // check that we are using Sun Java
264 String java_vendor = System.getProperty("java.vendor");
265 if (!java_vendor.equals("Sun Microsystems Inc.")) {
266 System.err.println(Dictionary.get("General.NotSunJava", java_vendor));
267 }
268
269 // Unless we're using remote building, we need to know where the local Greenstone is
270 if (!isGsdlRemote && gsdl_path == null) {
271 missingGSDL();
272 }
273
274 // Start up the local library server, if that's what we want
275 if (Utility.isWindows() && local_library_path != null && !isGsdlRemote && !GS3) {
276 LocalLibraryServer.start(gsdl_path, local_library_path);
277 }
278
279 // The "-library_url" option overwrites anything in the config files
280 if (library_url_string != null && library_url_string.length() > 0) {
281 try {
282 System.err.println("Setting library_url to " + library_url_string + "...");
283 Configuration.library_url = new URL(library_url_string);
284 }
285 catch (MalformedURLException error) {
286 DebugStream.printStackTrace(error);
287 }
288 }
289
290 // Check that we now know the Greenstone library URL, since we need this for previewing collections
291 DebugStream.println("Configuration.library_url = " + Configuration.library_url);
292 if (Configuration.library_url == null) {
293 missingEXEC();
294 }
295
296 // The "-gliserver_url" option overwrites anything in the config files
297 if (gliserver_url_string != null && gliserver_url_string.length() > 0) {
298 try {
299 System.err.println("Setting gliserver_url to " + gliserver_url_string + "...");
300 Configuration.gliserver_url = new URL(gliserver_url_string);
301 }
302 catch (MalformedURLException error) {
303 DebugStream.printStackTrace(error);
304 }
305 }
306
307 // If we're using a remote Greenstone we need to know where the gliserver script is
308 DebugStream.println("Configuration.gliserver_url = " + Configuration.gliserver_url);
309 if (isGsdlRemote) {
310 if (Configuration.gliserver_url == null) {
311 missingGLIServer();
312 }
313 if (Configuration.gliserver_url != null) {
314 gliserver_url_string = Configuration.gliserver_url.toString();
315 }
316 }
317
318 // Check for ImageMagick
319 if (Gatherer.isGsdlRemote) {
320 DebugStream.println("Not checking for ImageMagick.");
321 }
322 else if (!(new ImageMagickTest()).found()) {
323 // Time for a warning message
324 missingImageMagick();
325 }
326
327 if (Gatherer.isGsdlRemote) {
328 DebugStream.println("Not checking for perl path/exe");
329 }
330 else {
331 // Perl path is a little different as it is perfectly ok to
332 // start the GLI without providing a perl path
333 boolean found_perl = false;
334 if (Configuration.perl_path != null) {
335 // See if the file pointed to actually exists
336 File perl_file = new File(Configuration.perl_path);
337 found_perl = perl_file.exists();
338 perl_file = null;
339 }
340 if (Configuration.perl_path == null || !found_perl) {
341 // Run test to see if we can run perl as is.
342 PerlTest perl_test = new PerlTest();
343 if (perl_test.found()) {
344 // If so replace the perl path with the system
345 // default (or null for unix).
346 Configuration.perl_path = perl_test.toString();
347 found_perl = true;
348 }
349 }
350 if (!found_perl) {
351 // Time for an error message.
352 missingPERL();
353 }
354 }
355
356 // Set the default font for all Swing components.
357 FontUIResource default_font = Configuration.getFont("general.font", true);
358 Enumeration keys = UIManager.getDefaults().keys();
359 while (keys.hasMoreElements()) {
360 Object key = keys.nextElement();
361 Object value = UIManager.get(key);
362 if (value instanceof FontUIResource) {
363 UIManager.put(key, default_font);
364 }
365 }
366
367 // Set up proxy
368 setProxy();
369 // Now we set up an Authenticator
370 Authenticator.setDefault(new GAuthenticator());
371
372 assoc_man = new FileAssociationManager();
373 // Create File Manager
374 f_man = new FileManager();
375 // Create Collection Manager
376 c_man = new CollectionManager();
377 // Create Recycle Bin
378 recycle_bin = new RecycleBin();
379
380 if (GS3) {
381 if (site_name==null) {
382 site_name = Configuration.site_name;
383 servlet_path = null; // need to reset this
384 }
385 if (servlet_path == null) {
386 servlet_path = Configuration.getServletPath();
387 }
388 }
389
390 open_collection_file_path = open_collection;
391 if (open_collection_file_path == null) {
392 open_collection_file_path = Configuration.getString("general.open_collection", true);
393 }
394 if (no_load || open_collection_file_path.equals("")) {
395 open_collection_file_path = null;
396 }
397 }
398 catch (Exception exception) {
399 DebugStream.printStackTrace(exception);
400 }
401
402 // Create GUI Manager (last) or else suffer the death of a thousand NPE's
403 g_man = new GUIManager(size);
404
405 // Get a list of the core Greenstone classifiers and plugins
406 Classifiers.loadClassifiersList(null);
407 Plugins.loadPluginsList(null);
408
409 // If using a remote Greenstone we need to download the collection configurations now
410 if (Gatherer.isGsdlRemote) {
411 if (RemoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
412 // !! Something went wrong downloading the collection configurations
413 System.err.println("Error: Could not download collection configurations.");
414 System.exit(0);
415 }
416 }
417 }
418
419
420 public void openGUI() {
421 // Size and place the frame on the screen
422 Rectangle bounds = Configuration.getBounds("general.bounds", true);
423 if (bounds == null) {
424 // Choose a sensible default value
425 bounds = new Rectangle(0, 0, 640, 480);
426 }
427
428 // Ensure width and height are reasonable
429 size = bounds.getSize();
430 if (size.width < 640) {
431 size.width = 640;
432 } else if (size.width > Configuration.screen_size.width && Configuration.screen_size.width > 0) {
433 size.width = Configuration.screen_size.width;
434 }
435 if (size.height < 480) {
436 size.height = 480;
437 } else if (size.height > Configuration.screen_size.height && Configuration.screen_size.height > 0) {
438 size.height = Configuration.screen_size.height;
439 }
440
441 if (!g_man_built) {
442
443 g_man.display();
444
445 // Place the window in the desired location on the screen, if this is do-able (not under most linux window managers apparently. In fact you're lucky if they listen to any of your screen size requests).
446 g_man.setLocation(bounds.x, bounds.y);
447 g_man.setVisible(true);
448
449 // After the window has been made visible, check that it is in the correct place
450 // sometimes java places a window not in the correct place,
451 // but with an offset. If so, we work out what the offset is
452 // and change the desired location to take that into account
453 Point location = g_man.getLocation();
454 int x_offset = bounds.x - location.x;
455 int y_offset = bounds.y - location.y;
456 // If not, offset the window to move it into the correct location
457 if (x_offset > 0 || y_offset > 0) {
458 ///ystem.err.println("changing the location to "+(bounds.x + x_offset)+" "+ (bounds.y + y_offset));
459 g_man.setLocation(bounds.x + x_offset, bounds.y + y_offset);
460 }
461
462 // The 'after-display' triggers several events which don't occur until after the visual components are actually available on screen. Examples of these would be the various html renderings, as they can't happen offscreen.
463 g_man.afterDisplay();
464 g_man_built = true;
465 } else {
466 g_man.setVisible(true);
467 }
468
469 //Couldn't get either the classifier or plugin list, when the remote gli applet was running.I decided to move this part back into init().
470 // Get a list of the core Greenstone classifiers and plugins
471 /* Classifiers.loadClassifiersList(null);
472 Plugins.loadPluginsList(null);
473
474 // If using a remote Greenstone we need to download the collection configurations now
475 if (Gatherer.isGsdlRemote) {
476 if (RemoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
477 // !! Something went wrong downloading the collection configurations
478 System.err.println("Error: Could not download collection configurations.");
479 System.exit(0);
480 }
481 }*/
482
483 if (open_collection_file_path == null) {
484
485 //the menu bar items, file and edit, are disabled from the moment of their creation. if there is no left-over collection from the last session, enable them; otherwise it is untill the collection finishes loading, will they be enabled in collectionManager.java
486 setMenuBarEnabled(true);
487 } else {
488
489 // If there was a collection left open last time, reopen it
490 c_man.openCollectionFromLastTime();
491 }
492 }
493 public static void setMenuBarEnabled(boolean enabled) {
494 g_man.menu_bar.file.setEnabled(enabled);
495 g_man.menu_bar.edit.setEnabled(enabled);
496 }
497
498 /** Exits the Gatherer after ensuring that things needing saving are saved.
499 * @see java.io.FileOutputStream
500 * @see java.io.PrintStream
501 * @see java.lang.Exception
502 * @see javax.swing.JOptionPane
503 * @see org.greenstone.gatherer.Configuration
504 * @see org.greenstone.gatherer.collection.CollectionManager
505 * @see org.greenstone.gatherer.gui.GUIManager
506 */
507 static public void exit(int new_exit_status)
508 {
509 DebugStream.println("In Gatherer.exit()...");
510 exit = true;
511 if (new_exit_status != 0) {
512 // default exit_status is already 0
513 // only remember a new exit status if it is non-trivial
514 exit_status = new_exit_status;
515 }
516
517 // Save the file associations
518 if (assoc_man != null) {
519 assoc_man.save();
520 assoc_man = null;
521 }
522
523 // Get the gui to deallocate
524 g_man.destroy();
525 g_man_built = false;
526
527 // Flush debug
528 DebugStream.closeDebugStream();
529
530 // If we started a server, we should try to stop it.
531 if (LocalLibraryServer.isRunning() == true) {
532 LocalLibraryServer.stop();
533 }
534
535 // If we're using a remote Greenstone server we need to make sure that all jobs have completed first
536 if (isGsdlRemote) {
537 RemoteGreenstoneServer.exit();
538 }
539
540 // Make sure we haven't started up any processes that are still running
541 if (apps.size() == 0) {
542 // If we're running as an applet we don't actually quit (just hide the main GLI window)
543 if (!Gatherer.isApplet) {
544 // This is the end...
545 System.exit(exit_status);
546 }
547 }
548 else {
549 JOptionPane.showMessageDialog(g_man, Dictionary.get("General.Outstanding_Processes"), Dictionary.get("General.Outstanding_Processes_Title"), JOptionPane.ERROR_MESSAGE);
550 g_man.setVisible(false);
551 }
552 }
553
554 static public void exit()
555 {
556 exit(0);
557 }
558
559 /** Returns the path of the Greenstone "collect" directory. */
560 static public String getCollectDirectoryPath()
561 {
562 if (non_standard_collect_directory_path != null) {
563 return non_standard_collect_directory_path;
564 }
565
566 if (!GS3) {
567 return Configuration.gsdl_path + "collect" + File.separator;
568 }
569 else {
570 return getSitesDirectoryPath() + Configuration.site_name + File.separator + "collect" + File.separator;
571 }
572 }
573
574
575 /** Returns the path of the GLI directory. */
576 static public String getGLIDirectoryPath()
577 {
578 return gli_directory_path;
579 }
580
581
582 /** Returns the path of the GLI "metadata" directory. */
583 static public String getGLIMetadataDirectoryPath()
584 {
585 return getGLIDirectoryPath() + "metadata" + File.separator;
586 }
587
588
589 /** Returns the path of the GLI user directory. */
590 static public String getGLIUserDirectoryPath()
591 {
592 return gli_user_directory_path;
593 }
594
595
596 /** Returns the path of the GLI user "cache" directory. */
597 static public String getGLIUserCacheDirectoryPath()
598 {
599 return getGLIUserDirectoryPath() + "cache" + File.separator;
600 }
601
602
603 /** Returns the path of the GLI user "log" directory. */
604 static public String getGLIUserLogDirectoryPath()
605 {
606 return getGLIUserDirectoryPath() + "log" + File.separator;
607 }
608
609
610 static public String getSitesDirectoryPath()
611 {
612 return Configuration.gsdl3_path + "sites" + File.separator;
613 }
614
615
616 static public void setCollectDirectoryPath(String collect_directory_path)
617 {
618 non_standard_collect_directory_path = collect_directory_path;
619 if (!non_standard_collect_directory_path.endsWith(File.separator)) {
620 non_standard_collect_directory_path = non_standard_collect_directory_path + File.separator;
621 }
622 }
623
624
625 static public void setGLIDirectoryPath(String gli_directory_path_arg)
626 {
627 gli_directory_path = gli_directory_path_arg;
628 }
629
630
631 static public void setGLIUserDirectoryPath(String gli_user_directory_path_arg)
632 {
633 gli_user_directory_path = gli_user_directory_path_arg;
634
635 // Ensure the GLI user directory exists
636 File gli_user_directory = new File(gli_user_directory_path);
637 if (!gli_user_directory.exists() && !gli_user_directory.mkdirs()) {
638 System.err.println("Error: Unable to make directory: " + gli_user_directory);
639 }
640 }
641
642
643 static public void refresh(int refresh_reason)
644 {
645 if (g_man != null) {
646
647 g_man.refresh(refresh_reason, c_man.ready());
648 }
649
650 // Now is a good time to force a garbage collect
651 System.gc();
652 }
653
654
655 // used to send reload coll messages to the tomcat server
656 static public void configGS3Server(String site, String command) {
657 if (Configuration.library_url == null){
658 System.out.println("Error: you have not provide the Greenstone Library address.");
659 return;
660
661 }
662
663 try {
664 // need to add the servlet name to the exec address
665 String raw_url = Configuration.library_url.toString() + Configuration.getServletPath() + command;
666 URL url = new URL(raw_url);
667 DebugStream.println("Action: " + raw_url);
668 HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
669 int response_code = library_connection.getResponseCode();
670 if(HttpURLConnection.HTTP_OK <= response_code && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
671 DebugStream.println("200 - Complete.");
672 }
673 else {
674 DebugStream.println("404 - Failed.");
675 }
676 url = null;
677 }
678 catch (Exception exception) {
679 DebugStream.printStackTrace(exception);
680 }
681 }
682
683
684 /** Used to 'spawn' a new child application when a file is double clicked.
685 * @param file The file to open
686 * @see org.greenstone.gatherer.Gatherer.ExternalApplication
687 */
688 static public void spawnApplication(File file) {
689 String [] commands = assoc_man.getCommand(file);
690 if(commands != null) {
691 ExternalApplication app = new ExternalApplication(commands);
692 apps.add(app);
693 app.start();
694 }
695 else {
696 ///ystem.err.println("No open command available.");
697 }
698 }
699
700
701 static public void spawnApplication(String command)
702 {
703 ExternalApplication app = new ExternalApplication(command);
704 apps.add(app);
705 app.start();
706 }
707
708
709 /** Used to 'spawn' a new browser application or reset an existing one when the preview button is clicked
710 * @param url The url to open the browser at
711 * @see org.greenstone.gatherer.Gatherer.BrowserApplication
712 */
713 static public void spawnBrowser(String url) {
714 String command = assoc_man.getBrowserCommand(url);
715 if (command != null) {
716 BrowserApplication app = new BrowserApplication(command, url);
717 apps.add(app);
718 app.start();
719 }
720 else {
721 ///ystem.err.println("No browser command available.");
722 }
723 }
724
725
726 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
727 */
728 static private void missingEXEC() {
729 WarningDialog dialog;
730 if (GS3) {
731 dialog = new WarningDialog("warning.MissingEXEC_GS3", Dictionary.get("MissingEXEC_GS3.Title"), Dictionary.get("MissingEXEC_GS3.Message"), "general.library_url", false);
732 }
733 else {
734 dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC.Title"), Dictionary.get("MissingEXEC.Message"), "general.library_url", false);
735 }
736 dialog.setValueField(new URLField(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false)));
737 dialog.display();
738 dialog.dispose();
739 dialog = null;
740
741 String library_url_string = Configuration.getString("general.library_url", true);
742 if (!library_url_string.equals("")) {
743 try {
744 Configuration.library_url = new URL(library_url_string);
745 }
746 catch (MalformedURLException exception) {
747 DebugStream.printStackTrace(exception);
748 }
749 }
750 }
751
752
753 static private void missingGLIServer()
754 {
755 WarningDialog dialog = new WarningDialog("warning.MissingGLIServer", Dictionary.get("MissingGLIServer.Title"), Dictionary.get("MissingGLIServer.Message"), "general.gliserver_url", false);
756 dialog.setValueField(new URLField(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false)));
757 dialog.display();
758 dialog.dispose();
759 dialog = null;
760
761 String gliserver_url_string = Configuration.getString("general.gliserver_url", true);
762 if (!gliserver_url_string.equals("")) {
763 try {
764 Configuration.gliserver_url = new URL(gliserver_url_string);
765 }
766 catch (MalformedURLException exception) {
767 DebugStream.printStackTrace(exception);
768 }
769 }
770 }
771
772
773 /** Prints a warning message about a missing GSDL path, which although not fatal pretty much ensures nothing will work properly in the GLI.
774 */
775 static private void missingGSDL() {
776 WarningDialog dialog = new WarningDialog("warning.MissingGSDL", Dictionary.get("MissingGSDL.Title"), Dictionary.get("MissingGSDL.Message"), null, false);
777 dialog.display();
778 dialog.dispose();
779 dialog = null;
780 }
781
782 /** Prints a warning message about missing a valid ImageMagick path, which although not fatal means building image collections won't work */
783 static private void missingImageMagick() {
784 WarningDialog dialog = new WarningDialog("warning.MissingImageMagick", Dictionary.get("MissingImageMagick.Title"), Dictionary.get("MissingImageMagick.Message"), null, false);
785 dialog.display();
786 dialog.dispose();
787 dialog = null;
788 }
789
790 /** Prints a warning message about missing a valid PERL path, which although not fatal pretty much ensures no collection creation/building will work properly in the GLI. */
791 static private void missingPERL() {
792 WarningDialog dialog = new WarningDialog("warning.MissingPERL", Dictionary.get("MissingPERL.Title"), Dictionary.get("MissingPERL.Message"), null, false);
793 dialog.display();
794 dialog.dispose();
795 dialog = null;
796 }
797
798
799 /** Sets up the proxy connection by setting JVM Environment flags and creating a new Authenticator.
800 * @see java.lang.Exception
801 * @see java.lang.System
802 * @see java.net.Authenticator
803 * @see org.greenstone.gatherer.Configuration
804 * @see org.greenstone.gatherer.GAuthenticator
805 */
806 static public void setProxy() {
807 try {// Can throw several exceptions
808 if(Configuration.get("general.use_proxy", true)) {
809 System.setProperty("http.proxyType", "4");
810 System.setProperty("http.proxyHost", Configuration.getString("general.proxy_host", true));
811 System.setProperty("http.proxyPort", Configuration.getString("general.proxy_port", true));
812 System.setProperty("http.proxySet", "true");
813 } else {
814 System.setProperty("http.proxyHost", "");
815 System.setProperty("http.proxyPort", "");
816 System.setProperty("http.proxySet", "false");
817 }
818 } catch (Exception error) {
819 DebugStream.println("Error in Gatherer.initProxy(): " + error);
820 DebugStream.printStackTrace(error);
821 }
822 }
823
824
825 /** This private class contains an instance of an external application running within a JVM shell. It is important that this process sits in its own thread, but its more important that when we exit the Gatherer we don't actually System.exit(0) the Gatherer object until the user has volunteerily ended all of these child processes. Otherwise when we quit the Gatherer any changes the users may have made in external programs will be lost and the child processes are automatically deallocated. */
826 static private class ExternalApplication
827 extends Thread {
828 private Process process = null;
829 /** The initial command string given to this sub-process. */
830 private String command = null;
831 private String[] commands = null;
832 /** Constructor.
833 * @param command The initial command <strong>String</strong>.
834 */
835 public ExternalApplication(String command) {
836 this.command = command;
837 }
838
839 public ExternalApplication(String[] commands) {
840 this.commands = commands;
841 }
842 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
843 * @see java.lang.Exception
844 * @see java.lang.Process
845 * @see java.lang.Runtime
846 * @see java.lang.System
847 * @see java.util.Vector
848 */
849 public void run() {
850 // Call an external process using the args.
851 try {
852 if(commands != null) {
853 StringBuffer whole_command = new StringBuffer();
854 for(int i = 0; i < commands.length; i++) {
855 whole_command.append(commands[i]);
856 whole_command.append(" ");
857 }
858 DebugStream.println("Running " + whole_command.toString());
859 Runtime rt = Runtime.getRuntime();
860 process = rt.exec(commands);
861 process.waitFor();
862 }
863 else {
864 DebugStream.println("Running " + command);
865 Runtime rt = Runtime.getRuntime();
866 process = rt.exec(command);
867 process.waitFor();
868 }
869 }
870 catch (Exception exception) {
871 DebugStream.printStackTrace(exception);
872 }
873 // Remove ourself from Gatherer list of threads.
874 apps.remove(this);
875 // Call exit if we were the last outstanding child process thread.
876 if (apps.size() == 0 && exit == true) {
877 // In my opinion (DB) there is no need to exit here,
878 // the 'run' method ending naturally brings this
879 // thread to an end. In fact it is potentially
880 // dangerous to exit here, as the main thread in the
881 // Gatherer class may be stopped prematurely. As it so
882 // happens the point at which the ExternalApplication thread
883 // is asked to stop (Back in the main Gatherer thread) is after
884 // various configuration files have been saved.
885 //
886 // A similar argument holds for BrowserApplication thread below.
887 System.exit(exit_status);
888 }
889 }
890 public void stopExternalApplication() {
891 if(process != null) {
892 process.destroy();
893 }
894 }
895 }
896 /** This private class contains an instance of an external application running within a JVM shell. It is important that this process sits in its own thread, but its more important that when we exit the Gatherer we don't actually System.exit(0) the Gatherer object until the user has volunteerily ended all of these child processes. Otherwise when we quit the Gatherer any changes the users may have made in external programs will be lost and the child processes are automatically deallocated. */
897 static private class BrowserApplication
898 extends Thread {
899 private Process process = null;
900 /** The initial command string given to this sub-process. */
901 private String command = null;
902 private String url = null;
903 private String[] commands = null;
904
905 public BrowserApplication(String command, String url) {
906 StringTokenizer st = new StringTokenizer(command);
907 int num_tokens = st.countTokens();
908 this.commands = new String [num_tokens];
909 int i=0;
910 while (st.hasMoreTokens()) {
911 commands[i] = st.nextToken();
912 i++;
913 }
914 //this.commands = commands;
915 this.url = url;
916 }
917 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
918 * @see java.lang.Exception
919 * @see java.lang.Process
920 * @see java.lang.Runtime
921 * @see java.lang.System
922 * @see java.util.Vector
923 */
924 public void run() {
925 // Call an external process using the args.
926 if(commands == null) {
927 apps.remove(this);
928 return;
929 }
930 try {
931 String prog_name = commands[0];
932 String lower_name = prog_name.toLowerCase();
933 if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) {
934 DebugStream.println("found mozilla or netscape, trying remote it");
935 // mozilla and netscape, try using a remote command to get things in the same window
936 String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"};
937 printArray(new_commands);
938
939 Runtime rt = Runtime.getRuntime();
940 process = rt.exec(new_commands);
941 int exitCode = process.waitFor();
942 if (exitCode != 0) { // if Netscape or mozilla was not open
943 DebugStream.println("couldn't do remote, trying original command");
944 printArray(commands);
945 process = rt.exec(commands); // try the original command
946 }
947 } else {
948 // just run what we have been given
949 StringBuffer whole_command = new StringBuffer();
950 for(int i = 0; i < commands.length; i++) {
951 whole_command.append(commands[i]);
952 whole_command.append(" ");
953 }
954 DebugStream.println("Running " + whole_command.toString());
955 Runtime rt = Runtime.getRuntime();
956 process = rt.exec(commands);
957 process.waitFor();
958 }
959 }
960
961 catch (Exception exception) {
962 DebugStream.printStackTrace(exception);
963 }
964 // Remove ourself from Gatherer list of threads.
965 apps.remove(this);
966 // Call exit if we were the last outstanding child process thread.
967 if (apps.size() == 0 && exit == true) {
968 System.exit(exit_status);
969 }
970 }
971 public void printArray(String [] array) {
972 for(int i = 0; i < array.length; i++) {
973 DebugStream.print(array[i]+" ");
974 System.err.println(array[i]+" ");
975 }
976 }
977 public void stopBrowserApplication() {
978 if(process != null) {
979 process.destroy();
980 }
981 }
982 }
983
984
985 private class ImageMagickTest
986 {
987 public boolean found()
988 {
989 try {
990 String[] command = new String[2];
991 command[0] = (Utility.isWindows() ? "identify.exe" : "identify");
992 command[1] = "-version";
993 Process image_magick_process = Runtime.getRuntime().exec(command);
994 image_magick_process.waitFor();
995
996 //new way of detection of ImageMagick
997 InputStreamReader isr = new InputStreamReader(image_magick_process.getInputStream());
998 BufferedReader br = new BufferedReader(isr);
999 // Capture the standard output stream and seach for two particular occurances: Version and ImageMagick.
1000 String line = br.readLine().toLowerCase();
1001 if (line.indexOf("version") != -1 || line.indexOf("imagemagick") != -1) {
1002 return true;
1003 } else {
1004 return false;
1005 }
1006 //return (image_magick_process.exitValue() == 0);
1007 }
1008 catch (IOException exception) {
1009 return false;
1010 }
1011 catch (InterruptedException exception) {
1012 return false;
1013 }
1014 }
1015 }
1016
1017
1018 private class PerlTest
1019 {
1020 private String[] command = new String[2];
1021
1022 public PerlTest()
1023 {
1024 command[0] = (Utility.isWindows() ? Utility.PERL_EXECUTABLE_WINDOWS : Utility.PERL_EXECUTABLE_UNIX);
1025 command[1] = "-version";
1026 }
1027
1028 public boolean found()
1029 {
1030 try {
1031 Process perl_process = Runtime.getRuntime().exec(command);
1032 perl_process.waitFor();
1033 return (perl_process.exitValue() == 0);
1034 }
1035 catch (Exception exception) {
1036 return false;
1037 }
1038 }
1039
1040 public String toString() {
1041 return command[0];
1042 }
1043 }
1044}
Note: See TracBrowser for help on using the repository browser.