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

Last change on this file since 12297 was 12225, checked in by davidb, 18 years ago

Instead of GLI saying it needs to restart when the language interface has changed,
it now exits (having gone through the usual routine that saves the session etc)
with level 2. This is then used by gli.sh/gli.bat to loop around again and
relaunch GLI. Any other exit status, and gli.sh/gli.bat stops as before.

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