source: gli/trunk/src/org/greenstone/gatherer/Gatherer.java@ 14352

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

Configuration.gliserver_url is set up if Configuration.library_url != null for a remote GS3

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