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

Last change on this file since 10368 was 10368, checked in by mdewsnip, 19 years ago

Removed some occurrences of "gsdl_path", and added some checks so it is only used when a local Greenstone exists.

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