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

Last change on this file since 29514 was 29514, checked in by ak19, 9 years ago

Now the collectdir flag passed to gli.sh works (tested with GS3). It wasn't working before. The collectdir flag is needed by Jeremey (jts21) for his linux package manager binaries of GS3, since can't use the default collectdir location. They need to specify a different collectdir as the default one that GLI will launch with.

  • Property svn:keywords set to Author Date Id Revision
File size: 65.8 KB
RevLine 
[15506]1/**
[24232]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*/
[15506]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.*;
38
39
40import org.greenstone.gatherer.Configuration;
41import org.greenstone.gatherer.GAuthenticator;
42import org.greenstone.gatherer.FedoraInfo;
43import org.greenstone.gatherer.collection.CollectionManager;
44import org.greenstone.gatherer.feedback.ActionRecorderDialog;
[18644]45import org.greenstone.gatherer.feedback.Base64;
[15506]46import org.greenstone.gatherer.file.FileManager;
47import org.greenstone.gatherer.file.FileAssociationManager;
48import org.greenstone.gatherer.file.RecycleBin;
49import org.greenstone.gatherer.greenstone.Classifiers;
50import org.greenstone.gatherer.greenstone.LocalGreenstone;
51import org.greenstone.gatherer.greenstone.LocalLibraryServer;
52import org.greenstone.gatherer.greenstone.Plugins;
53import org.greenstone.gatherer.greenstone3.ServletConfiguration;
54import org.greenstone.gatherer.gui.GUIManager;
55import org.greenstone.gatherer.gui.URLField;
56import org.greenstone.gatherer.gui.WarningDialog;
57import org.greenstone.gatherer.gui.FedoraLogin;
[23433]58import org.greenstone.gatherer.metadata.FilenameEncoding;
[15506]59import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
[24333]60import org.greenstone.gatherer.util.GS3ServerThread;
[15506]61import org.greenstone.gatherer.util.JarTools;
62import org.greenstone.gatherer.util.StaticStrings;
63import org.greenstone.gatherer.util.Utility;
64
65
66/** Containing the top-level "core" for the Gatherer, this class is the
[24232]67* common core for the GLI application and applet. It first parses the
68* command line arguments, preparing to update the configuration as
69* required. Next it loads several important support classes such as the
70* Configuration and Dictionary. Finally it creates the other important
71* managers and sends them on their way.
72* @author John Thompson, Greenstone Digital Library, University of Waikato
73* @version 2.3
74*/
[15506]75public class Gatherer
76{
[24232]77 /** The name of the GLI. */
78 static final public String PROGRAM_NAME = "Greenstone Librarian Interface";
79 /** The current version of the GLI.
[15506]80 * Note: the gs3-release-maker relies on this variable being declared
[24811]81 * in a line which matches this java regex:
[15506]82 * ^(.*)String\s*PROGRAM_VERSION\s*=\s*"trunk";
83 * If change the declaration and it no longer matches the regex, please
84 * change the regex in the gs3-release-maker code and in this message
85 */
86
[24232]87 static final public String PROGRAM_VERSION = "trunk";
[15506]88
[24232]89 static private Dimension size = new Dimension(800, 540);
90 static public RemoteGreenstoneServer remoteGreenstoneServer = null;
[15506]91
[24232]92 /** Has the exit flag been set? */
93 static final public int EXIT_THEN_RESTART= 2;
94 static public boolean exit = false;
95 static public int exit_status = 0;
[15506]96
[24232]97 static private String gli_directory_path = null;
98 static private String gli_user_directory_path = null;
[15506]99
[24232]100 static public String client_operating_system = null;
[15506]101
[24232]102 /** All of the external applications that must exit before we close the Gatherer. */
103 static private Vector apps = new Vector();
104 static private String non_standard_collect_directory_path = null;
105 static public String open_collection_file_path = null;
[23143]106 static public String gsdlsite_collecthome = "";
[24232]107 /** A public reference to the FileAssociationManager. */
108 static public FileAssociationManager assoc_man;
109 /** A public reference to the CollectionManager. */
110 static public CollectionManager c_man;
111 /** A public reference to the RecycleBin. */
112 static public RecycleBin recycle_bin;
113 /** a reference to the Servlet Configuration is GS3 */
114 static public ServletConfiguration servlet_config;
115 /** A public reference to the FileManager. */
116 static public FileManager f_man;
117 /** A public reference to the GUIManager. */
118 static public GUIManager g_man = null;
119 static private boolean g_man_built = false;
[15506]120
[24232]121 /** We are using the GLI for GS3 */
122 static public boolean GS3 = false;
[15506]123
[24232]124 static public boolean isApplet = false;
125 static public boolean isGsdlRemote = false;
126 static public boolean isLocalLibrary = false;
[15506]127
[24232]128 /* TODO: If we're using local GLI, collections are built locally. If we're using client-GLI
129 * and it contains a gs2build folder in it, then localBuild will also be true (if this is not
130 * turned off in Preferences). If we're remote and this is turned off in Prefs, build remotely. */
131 /*static public boolean buildingLocally = true;*/
132 /** If we're using local GLI, we can always download. If we're using client-GLI, we can only
133 * download if we have a gs2build folder inside it. And if we don't turn off downloadEnabling
134 * in the preferences.
135 */
136 static public boolean isDownloadEnabled = true;
[19306]137
[24232]138 // feedback stuff
139 /** is the feedback feature enabled? */
140 static public boolean feedback_enabled = true;
141 /** the action recorder dialog */
142 static public ActionRecorderDialog feedback_dialog = null;
[15506]143
[24232]144 // Refresh reasons
145 static public final int COLLECTION_OPENED = 0;
146 static public final int COLLECTION_CLOSED = 1;
147 static public final int COLLECTION_REBUILT = 2;
148 static public final int PREFERENCES_CHANGED = 3;
[15506]149
[24232]150 //////kk added////////
151 static public String cgiBase="";
152 /////////////////
[15506]153
[24232]154 /** Magic to allow Enter to fire the default button. */
155 static {
156 KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
157 Keymap map = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);
158 map.removeKeyStrokeBinding(enter);
159 }
[15506]160
[24232]161 static private URL default_gliserver_url=null;
[15506]162
[24232]163 public Gatherer(String[] args)
164 {
165 // Display the version to make error reports a lot more useful
166 System.err.println("Version: " + PROGRAM_VERSION + "\n");
[15506]167
[24232]168 JarTools.initialise(this);
[15506]169
[24232]170 GetOpt go = new GetOpt(args);
[15506]171
[24232]172 // Remember the GSDLOS value
173 client_operating_system = go.client_operating_system;
[15506]174
[24232]175 // If feedback is enabled, set up the recorder dialog
176 if (go.feedback_enabled) {
177 // Use the default locale for now - this will be changed by the Gatherer run method
178 feedback_enabled = true;
179 feedback_dialog = new ActionRecorderDialog(Locale.getDefault());
180 }
[15506]181
[24232]182 // Are we using a remote Greenstone?
183 if (go.use_remote_greenstone) {
184 isGsdlRemote = true;
[15506]185
[24232]186 // We don't have a local Greenstone!
187 go.gsdl3_path=null;
188 go.gsdl3_src_path=null;
[15506]189
[24232]190 // Don't set go.gsdl_path to null, since gdsl_path may still be set
191 // if we have a client-gli containing gs2build folder.
192 // However, keep track of whether we can download.
193 if(go.gsdl_path == null) {
194 isDownloadEnabled = false;
195 }
[19306]196
[24232]197 // We have to use our own collect directory since we can't use the Greenstone one
198 setCollectDirectoryPath(getGLIUserDirectoryPath() + "collect" + File.separator);
199 }
200 // We have a local Greenstone. OR we have a gs2build folder inside
201 // the client-GLI folder (with which the Download panel becomes enabled)
202 if(isDownloadEnabled) {
203 LocalGreenstone.setDirectoryPath(go.gsdl_path);
204 }
[15506]205
[24232]206 // Users may specify a non-standard collect directory (eg. when running one GLI in a network environment)
[29514]207 // Processing of custom non-standard collect directory passed into gli now happens in init()
208 //if (go.collect_directory_path != null) { setCollectDirectoryPath(go.collect_directory_path); }
[15506]209
[24232]210 // More special code for running with a remote Greenstone
211 if (isGsdlRemote) {
212 if (go.fedora_info.isActive()) {
213 Configuration.TEMPLATE_CONFIG_XML = "xml/" + Configuration.FEDORA_CONFIG_PREFIX + "configRemote.xml";
214 }
215 else {
216 Configuration.TEMPLATE_CONFIG_XML = "xml/configRemote.xml";
217 }
[15506]218
[24232]219 Configuration.CONFIG_XML = "configRemote.xml";
[15506]220
[24232]221 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
222 if (!collect_directory.exists() && !collect_directory.mkdir()) {
223 System.err.println("Warning: Unable to make directory: " + collect_directory);
224 }
225 }
226 else {
227 if (go.fedora_info.isActive()) {
228 Configuration.TEMPLATE_CONFIG_XML = "xml/" + Configuration.FEDORA_CONFIG_PREFIX + Configuration.CONFIG_XML;
229 }
230 // else, the CONFIG_XML uses the default config file, which is for the local GS server
231 }
[15506]232
[24232]233 init(go.gsdl_path, go.gsdl3_path, go.gsdl3_src_path,
234 go.fedora_info,
235 go.local_library_path, go.library_url_string,
236 go.gliserver_url_string, go.debug, go.perl_path, go.no_load, go.filename, go.site_name,
[29514]237 go.servlet_path, go.collect_directory_path);
[15506]238 }
239
240
[24232]241 public void init(String gsdl_path, String gsdl3_path, String gsdl3_src_path,
242 FedoraInfo fedora_info,
243 String local_library_path,
244 String library_url_string, String gliserver_url_string, boolean debug_enabled,
245 String perl_path, boolean no_load, String open_collection,
[29514]246 String site_name, String servlet_path, String collect_directory_path)
[24232]247 {
248 if (gsdl3_path != null && !gsdl3_path.equals("")) {
249 this.GS3 = true;
250 } else {
251 gsdl3_path = null;
252 gsdl3_src_path = null;
253 }
[15506]254
[24232]255 // Create the debug stream if required
256 if (debug_enabled) {
257 DebugStream.enableDebugging();
[15506]258
[24232]259 Calendar now = Calendar.getInstance();
260 String debug_file_path = "debug" + now.get(Calendar.DATE) + "-" + now.get(Calendar.MONTH) + "-" + now.get(Calendar.YEAR) + ".txt";
[15506]261
[24232]262 // Debug file is created in the user's GLI directory
263 debug_file_path = getGLIUserDirectoryPath() + debug_file_path;
264 DebugStream.println("Debug file path: " + debug_file_path);
265 DebugStream.setDebugFile(debug_file_path);
266 DebugStream.print(System.getProperties());
[15506]267 }
[24232]268
269 // Delete plugins.dat and classifiers.dat files from previous versions of the GLI (no longer used)
270 File plugins_dat_file = new File(Gatherer.getGLIUserDirectoryPath() + "plugins.dat");
271 if (plugins_dat_file.exists()) {
272 System.err.println("Deleting plugins.dat file...");
273 Utility.delete(plugins_dat_file);
[15506]274 }
[24232]275 File classifiers_dat_file = new File(Gatherer.getGLIUserDirectoryPath() + "classifiers.dat");
276 if (classifiers_dat_file.exists()) {
277 System.err.println("Deleting classifiers.dat file...");
278 Utility.delete(classifiers_dat_file);
279 }
[15506]280
[24232]281 try {
282 // Load GLI config file
283 new Configuration(getGLIUserDirectoryPath(), gsdl_path, gsdl3_path, gsdl3_src_path, site_name,
284 fedora_info);
285
286 // Check we know where Perl is
287 Configuration.perl_path = perl_path;
288 if (isGsdlRemote && !isDownloadEnabled && Utility.isWindows() && Configuration.perl_path != null) {
289 if (Configuration.perl_path.toLowerCase().endsWith("perl.exe")) {
290 Configuration.perl_path = Configuration.perl_path.substring(0, Configuration.perl_path.length() - "perl.exe".length());
291 }
292 if (Configuration.perl_path.endsWith(File.separator)) {
293 Configuration.perl_path = Configuration.perl_path.substring(0, Configuration.perl_path.length() - File.separator.length());
294 }
295 }
[15506]296
[24232]297 // the feedback dialog has been loaded with a default locale,
298 // now set the user specified one
299 if (feedback_enabled && feedback_dialog != null) {
300 feedback_dialog.setLocale(Configuration.getLocale("general.locale", true));
301 }
[15506]302
[24232]303 // Read Dictionary
304 new Dictionary(Configuration.getLocale("general.locale", true), Configuration.getFont("general.font", true));
[15506]305
[24232]306 // check that we are using Sun Java
307 String java_vendor = System.getProperty("java.vendor");
308 if (!java_vendor.equals("Sun Microsystems Inc.")) {
309 System.err.println(Dictionary.get("General.NotSunJava", java_vendor));
310 }
[15506]311
[24232]312 // Unless we're using remote building, we need to know where the local Greenstone is
313 if (!isGsdlRemote && gsdl_path == null) {
314 missingGSDL();
315 }
316
317 if (fedora_info.isActive()) {
[26331]318
319 // if we have either a remote GS 2/3 server, or if we are dealing with a
320 // local GS2, or else if it's a local GS3 but with fedora installed elsewhere,
321 // we ask for fedora login details now
322 File fedora_webapp = new File(gsdl3_src_path+File.separator+"packages"+File.separator+"tomcat"+File.separator+"webapps"+File.separator+"fedora");
323
324 if(isGsdlRemote || !GS3 || !fedora_webapp.exists()) {
[24232]325 popupFedoraInfo();
[26331]326 }
327
328 // else if we have a local GS3 with fedora installed within it, we'll be starting
329 // up fedora further below, together with the local tomcat
[24232]330 }
331
332 // the no_load flag to GLI is processed at the end of handling open_collection_file_path
333 open_collection_file_path = open_collection;
334 if (open_collection_file_path == null) {
335 open_collection_file_path = Configuration.getString(
[23015]336 "general.open_collection"+Configuration.gliPropertyNameSuffix(), true);
[24232]337 }
338
[24294]339 initCollectDirectoryPath();
[24232]340
341 if (no_load || (isGsdlRemote && open_collection_file_path.equals(""))) {
342 open_collection_file_path = null;
343 }
344
345 // Finally, we're ready to find out the version of the remote Greenstone server
346 if(isGsdlRemote) {
347 // instantiate the RemoteGreenstoneServer object first
348 remoteGreenstoneServer = new RemoteGreenstoneServer();
[17612]349
[24232]350 // Set up proxy
351 setProxy();
352 // Now we set up an Authenticator
353 Authenticator.setDefault(new GAuthenticator());
[19304]354
[24232]355 int greenstoneVersion = 2;
356 requestGLIServerURL();
357 gliserver_url_string = Configuration.gliserver_url.toString();
358
359 greenstoneVersion = remoteGreenstoneServer.getGreenstoneVersion();
360 // Display the version to make error reports a lot more useful
361 System.err.println("Remote Greenstone server version: " + greenstoneVersion);
362 if(greenstoneVersion == -1) { // remote server not running
363 Gatherer.exit();
364 }
365 if(greenstoneVersion >= 3) {
366 this.GS3 = true;
367 Configuration.prepareForGS3();
368 }
369
370 if(fedora_info.isActive()) {
371 // when GS server is remote, FEDORA_HOME resides on the remote server side,
372 // but we know the library URL from user-provided information
373 library_url_string = fedora_info.getLibraryURL();
374 } else {
375 library_url_string = remoteGreenstoneServer.getLibraryURL(Configuration.gliserver_url.toString());
376 }
377 // write it into the config file
378 Configuration.setString("general.library_url", true, library_url_string);
379 }
380 else { // local greenstone: Start up the local library server, if that's what we want
381 if (!GS3) {
382 isLocalLibrary = LocalLibraryServer.start(gsdl_path, local_library_path);
383 }
[26206]384 else if (!isGsdlRemote) { // local GS3, start the local tomcat
[26331]385
[24388]386 GS3ServerThread thread = new GS3ServerThread(gsdl3_src_path, "restart");
[24232]387 thread.start();
[26331]388
389 // If fedora installed inside this local GS3, then ask for fedora login details now.
390 if (fedora_info.isActive()) {
391 File fedora_webapp = new File(gsdl3_src_path+File.separator+"packages"+File.separator+"tomcat"+File.separator+"webapps"+File.separator+"fedora");
392
393 if(fedora_webapp.exists()) {
394 System.err.println("**** Waiting for the local Greenstone server to start up fedora...");
395 popupFedoraInfo();
396 }
397 }
[24232]398 }
399 // else web library: GS server is local, but doesn't use the webserver included with GS2
400 }
401
402 // The "-library_url" option overwrites anything in the config files
403 if (library_url_string != null && library_url_string.length() > 0) {
404 try {
405 System.err.println("Setting library_url to " + library_url_string + "...");
406 Configuration.library_url = new URL(library_url_string);
407 }
408 catch (MalformedURLException error) {
409 DebugStream.printStackTrace(error);
410 }
411 }
[15506]412
413
[24232]414 // Check that we now know the Greenstone library URL, since we need this for previewing collections
415 // It is not necessary if an independent GSI was launched and the user hasn't pressed Enter Library yet
416 DebugStream.println("Configuration.library_url = " + Configuration.library_url);
417 if (Configuration.library_url == null) {
418 if(isLocalLibrary) {
419 if(!LocalLibraryServer.isURLPending()) {
420 missingEXEC();
421 }
422 // else LocalLibraryServer is expecting a URL soon, so don't need to display the dialog
423 } else { // GS2 webLibrary or GS3 or remote Greenstone
424 missingEXEC();
425 }
426 }
[15506]427
[24232]428 // The "-gliserver_url" option overwrites anything in the config files
429 if (gliserver_url_string != null && gliserver_url_string.length() > 0) {
430 try {
431 System.err.println("Setting gliserver_url to " + gliserver_url_string + "...");
432 Configuration.gliserver_url = new URL(gliserver_url_string);
433 }
434 catch (MalformedURLException error) {
435 DebugStream.printStackTrace(error);
436 }
437 }
[15506]438
[24232]439 // If we're using a remote Greenstone we need to know where the gliserver script is
440 DebugStream.println("Configuration.gliserver_url = " + Configuration.gliserver_url);
[15506]441
[24232]442 if (GS3) {
443 // Load Greenstone 3 servlet configuration
444 if (isGsdlRemote){
445 servlet_config = new ServletConfiguration(Configuration.gli_user_directory_path);
446 }else{
447 servlet_config= new ServletConfiguration(gsdl3_path);
448 }
449 }
[15506]450
[24232]451 if (GS3 && Configuration.servlet_path == null) {
452 Configuration.servlet_path = servlet_config.getServletPath(Configuration.site_name);
453 }
454
455 // ensure that a directory called 'cache' exists in the GLI user directory
456 File user_cache_dir = new File(Gatherer.getGLIUserCacheDirectoryPath());
457 System.err.println("User cache dir: " + Gatherer.getGLIUserCacheDirectoryPath());
458 if (!user_cache_dir.exists() && !user_cache_dir.mkdir()) {
459 System.err.println("Warning: Unable to make directory: " + user_cache_dir);
460 }
[15506]461
462
[24232]463 if (Gatherer.isGsdlRemote) {
464 DebugStream.println("Not checking for perl path/exe");
465 }
466 else {
467 // Perl path is a little different as it is perfectly ok to
468 // start the GLI without providing a perl path
469 boolean found_perl = false;
470 if (Configuration.perl_path != null) {
471 // See if the file pointed to actually exists
472 File perl_file = new File(Configuration.perl_path);
473 found_perl = perl_file.exists();
474 perl_file = null;
475 }
476 if (Configuration.perl_path == null || !found_perl) {
477 // Run test to see if we can run perl as is.
478 PerlTest perl_test = new PerlTest();
479 if (perl_test.found()) {
480 // If so replace the perl path with the system
481 // default (or null for unix).
482 Configuration.perl_path = perl_test.toString();
483 found_perl = true;
484 }
485 }
486 if (!found_perl) {
487 // Time for an error message.
488 missingPERL();
489 }
490 }
491
[24600]492
493 // Check for ImageMagick - dependent on perl_path
494 if (Gatherer.isGsdlRemote) {
495 DebugStream.println("Not checking for ImageMagick.");
496 }
497 else if (!(new ImageMagickTest()).found()) {
498 // Time for a warning message
499 missingImageMagick();
500 }
501
502 // Check for PDFBox
503 if (Gatherer.isGsdlRemote) {
504 DebugStream.println("Not checking for PDFBox.");
505 }
[29238]506 else {
507 String gs_dir = gsdl_path; // for GS3, pdf-box is in gs2build/ext (not in GS3/ext), for GS2 it's also in GS2/ext
[24600]508 File pdfboxExtensionFolder = new File(gs_dir+File.separator+"ext"+File.separator+"pdf-box");
509 if (!(pdfboxExtensionFolder.exists() && pdfboxExtensionFolder.isDirectory())) {
510 // The user doesn't have PDFBox, inform them of it
511 String zipExtension = Utility.isWindows() ? "zip" : "tar.gz";
512 missingPDFBox(zipExtension, pdfboxExtensionFolder.getParent());
513 }
514 }
515
[24232]516 // Check that the local can support multiple filename encodings
[23433]517 //System.err.println("#### Java identifies current Locale as (file.encoding): "
518 // + System.getProperty("file.encoding"));
[24232]519 if(System.getProperty("file.encoding").equals("UTF-8")){
520 // If the locale is UTF-8, Java will interpret all filename bytes as UTF-8,
521 // which is a destructive process as it will convert characters not recognised
522 // by UTF-8 into the invalid character, rather than preserving the bytecodes.
523 // This has the effect that non-UTF8 encoded filenames on a system set to a
524 // UTF-8 locale are not 'seen' by Java (if they contain non-ASCII characters).
525 multipleFilenameEncodingsNotSupported();
526 FilenameEncoding.MULTIPLE_FILENAME_ENCODINGS_SUPPORTED = false;
527 FilenameEncoding.URL_FILE_SEPARATOR = File.separator;
528 } else {
529 FilenameEncoding.MULTIPLE_FILENAME_ENCODINGS_SUPPORTED = true;
530 FilenameEncoding.URL_FILE_SEPARATOR = "/"; // URL file separator is always "/"
531 }
532
533 // Set the default font for all Swing components.
534 FontUIResource default_font = Configuration.getFont("general.font", true);
535 Enumeration keys = UIManager.getDefaults().keys();
536 while (keys.hasMoreElements()) {
537 Object key = keys.nextElement();
538 Object value = UIManager.get(key);
539 if (value instanceof FontUIResource) {
540 UIManager.put(key, default_font);
541 }
542 }
[15506]543
[24232]544 // At this point (which is where this code originally used to be), we can set up the proxy for the
545 // non-remote case. The remote Greenstone server would already have setup its proxy when required.
546 if(!isGsdlRemote) {
547 setProxy();
548 // Now we set up an Authenticator
549 Authenticator.setDefault(new GAuthenticator());
550 }
551 assoc_man = new FileAssociationManager();
552 // Create File Manager
553 f_man = new FileManager();
554 // Create Collection Manager
555 c_man = new CollectionManager();
556 // Create Recycle Bin
557 recycle_bin = new RecycleBin();
[15506]558
[24232]559 if (GS3) {
560 if (site_name==null) {
561 site_name = Configuration.site_name;
562 servlet_path = null; // need to reset this
563 }
564 if (servlet_path == null) {
565 servlet_path = Configuration.getServletPath();
566 }
567 }
568
569
570 } catch (Exception exception) {
571 DebugStream.printStackTrace(exception);
[15506]572 }
[24232]573
[23031]574
[24232]575 // Create GUI Manager (last) or else suffer the death of a thousand NPE's
576 g_man = new GUIManager(size);
[15506]577
[24232]578 // Get a list of the core Greenstone classifiers and plugins
579 Classifiers.loadClassifiersList(null);
580 Plugins.loadPluginsList(null);
[15506]581
[29514]582 // Users may specify a non-standard collect directory (eg. when running one GLI in a network environment)
583 // Set to any custom collection directory provided to gli. This happens if gli was run as:
584 // ./gli.sh -collectdir </full/path/to/custom/collect>
585 if(collect_directory_path != null) {
586
587 // create a version of the collect_dir_path without a file-separator at end
588 String collectDir = collect_directory_path;
589 if(collectDir.endsWith(File.separator)) {
590 collectDir = collectDir.substring(0, collectDir.length()-1); // remove file separator at end
591 }
592
593 // update .gli/config.xml to contain the version of the path without file-separator at end
594 if(collect_directory_path.equals(getDefaultGSCollectDirectoryPath(false))) {
595 Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(),
596 true, "");
597 } else {
598 Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(),
599 true, collectDir);
600 }
601
602 // set non_standard_collect_directory_path variable. Ensures file_separator at end
603 Gatherer.setCollectDirectoryPath(collect_directory_path);
604
605 // Use version of path without file-separator at end to set collecthome in gsdlsite(3).cfg
606 if(collectDir != null) {
607 collectDir = "\""+collectDir+"\"";
608 }
609 Utility.updatePropertyConfigFile(getGsdlSiteConfigFile(), "collecthome", collectDir);
610 // if gsdlsite.cfg does not exist (if using server.exe for instance), the above method will just return
611 }
612
[24232]613 // If using a remote Greenstone we need to download the collection configurations now
614 if (Gatherer.isGsdlRemote) {
615 if (remoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
616 // !! Something went wrong downloading the collection configurations
617 System.err.println("Error: Could not download collection configurations.");
618 if(!Gatherer.isApplet) { // don't close the browser if it is an applet!
619 System.exit(0);
620 }
621 }
[17628]622 }
[15506]623 }
624
[24232]625
626 /** Returns the correct version of the (local or remote) Greenstone server if init() has already been called. */
627 public static int serverVersionNumber() {
628 return GS3 ? 3 : 2;
629 }
[16329]630
[24232]631 /** Returns "Server: version number" if init() has already been called. */
632 public static String getServerVersionAsString() {
633 return "Server: v" + serverVersionNumber();
634 }
[16329]635
[24232]636 public void openGUI()
637 {
638 // Size and place the frame on the screen
639 Rectangle bounds = Configuration.getBounds("general.bounds", true);
640 if (bounds == null) {
641 // Choose a sensible default value
642 bounds = new Rectangle(0, 0, 640, 480);
643 }
644
645 // Ensure width and height are reasonable
646 size = bounds.getSize();
647 if (size.width < 640) {
648 size.width = 640;
649 }
650 else if (size.width > Configuration.screen_size.width && Configuration.screen_size.width > 0) {
651 size.width = Configuration.screen_size.width;
652 }
653 if (size.height < 480) {
654 size.height = 480;
655 }
656 else if (size.height > Configuration.screen_size.height && Configuration.screen_size.height > 0) {
657 size.height = Configuration.screen_size.height;
658 }
[15506]659
[24232]660 if (!g_man_built) {
[15506]661
[24232]662 g_man.display();
[15506]663
[24232]664 // 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).
665 g_man.setLocation(bounds.x, bounds.y);
666 g_man.setVisible(true);
[15506]667
[24232]668 // After the window has been made visible, check that it is in the correct place
669 // sometimes java places a window not in the correct place,
670 // but with an offset. If so, we work out what the offset is
671 // and change the desired location to take that into account
672 Point location = g_man.getLocation();
673 int x_offset = bounds.x - location.x;
674 int y_offset = bounds.y - location.y;
675 // If not, offset the window to move it into the correct location
676 if (x_offset > 0 || y_offset > 0) {
677 ///ystem.err.println("changing the location to "+(bounds.x + x_offset)+" "+ (bounds.y + y_offset));
678 g_man.setLocation(bounds.x + x_offset, bounds.y + y_offset);
679 }
[15506]680
[24232]681 // 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.
682 g_man.afterDisplay();
683 g_man_built = true;
684 }
685 else {
686 g_man.setVisible(true);
687 }
[15506]688
[24232]689 // Get a list of the core Greenstone classifiers and plugins
690 /*Classifiers.loadClassifiersList(null);
[15506]691 Plugins.loadPluginsList(null);
692
693 // If using a remote Greenstone we need to download the collection configurations now
694 if (Gatherer.isGsdlRemote) {
[24232]695 if (remoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
[15506]696 // !! Something went wrong downloading the collection configurations
697 System.err.println("Error: Could not download collection configurations.");
698 System.exit(0);
[24232]699 }
[15506]700 }*/
701
[24232]702 // If there was a collection left open last time, reopen it
703 if (open_collection_file_path == null || new File(Gatherer.open_collection_file_path).isDirectory()) {
704
[24294]705 //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's disabled until the collection finishes loading. They will be enabled in collectionManager.java
[24232]706 setMenuBarEnabled(true);
707 } else {
[15506]708
[24232]709 // If there was a collection left open last time, reopen it
710 c_man.openCollectionFromLastTime();
711 }
[15506]712 }
713
[24232]714 public static void setMenuBarEnabled(boolean enabled) {
715 g_man.menu_bar.file.setEnabled(enabled);
716 g_man.menu_bar.edit.setEnabled(enabled);
[15506]717 }
718
[24232]719 /** Exits the Gatherer after ensuring that things needing saving are saved.
720 * @see java.io.FileOutputStream
721 * @see java.io.PrintStream
722 * @see java.lang.Exception
723 * @see javax.swing.JOptionPane
724 * @see org.greenstone.gatherer.Configuration
725 * @see org.greenstone.gatherer.collection.CollectionManager
726 * @see org.greenstone.gatherer.gui.GUIManager
727 */
728 static public void exit(int new_exit_status)
729 {
730 DebugStream.println("In Gatherer.exit()...");
731 exit = true;
732 if (new_exit_status != 0) {
733 // default exit_status is already 0
734 // only remember a new exit status if it is non-trivial
735 exit_status = new_exit_status;
736 }
[15506]737
[24232]738 // Save the file associations
739 if (assoc_man != null) {
740 assoc_man.save();
741 assoc_man = null;
742 }
[15506]743
[24232]744 // Get the gui to deallocate
745 if(g_man != null) {
746 g_man.destroy();
747 g_man_built = false;
748 }
749
750 // Flush debug
751 DebugStream.closeDebugStream();
752
753 // If we started a server, we should try to stop it.
754 if (LocalLibraryServer.isRunning() == true) {
755 LocalLibraryServer.stop();
756 }
757
758 // If we're using a remote Greenstone server we need to make sure that all jobs have completed first
759 if (isGsdlRemote) {
760 remoteGreenstoneServer.exit();
[24333]761 } else if (GS3) { // stop the local tomcat web server when running GS3
762 // can't call ant stop from its own thread - what if GLI has exited by then?
763 // issue call to ant stop from the main GLI thread
764 //GS3ServerThread thread = new GS3ServerThread(Configuration.gsdl_path, "stop");
765 //thread.start();
766
767 try {
768 String shellCommand = null;
769 Process p = null;
[24388]770 if (Utility.isWindows()) {
771 // cmd /C "cd "C:\path\to\greenstone3" && ant stop"
772 p = Runtime.getRuntime().exec("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");
[24333]773 } else {
[24388]774 p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""});
[24333]775 }
[29361]776 // doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6
777 // (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine.
778 /*if(p != null && p.waitFor() == 0) {
[29356]779 DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
780 }
781 else {
782 System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
783 //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
[29361]784 }*/
[24333]785 } catch(Exception e) {
786 System.err.println("Exception when trying to stop the tomcat web server: " + e);
787 DebugStream.printStackTrace(e);
788 }
789
[24232]790 }
791
792 // Make sure we haven't started up any processes that are still running
793 if (apps.size() == 0) {
794 // If we're running as an applet we don't actually quit (just hide the main GLI window)
795 if (!Gatherer.isApplet) {
796 // This is the end...
797 System.exit(exit_status);
798 }
799 }
800 else {
801 JOptionPane.showMessageDialog(g_man, Dictionary.get("General.Outstanding_Processes"), Dictionary.get("General.Outstanding_Processes_Title"), JOptionPane.ERROR_MESSAGE);
802 g_man.setVisible(false);
803 }
[15506]804 }
805
[24232]806 static public void exit()
807 {
808 exit(0);
[15506]809 }
810
[24232]811 /** Returns the path of the current collect directory. */
812 static public String getCollectDirectoryPath()
813 {
814 if (non_standard_collect_directory_path != null) {
815 return non_standard_collect_directory_path;
816 }
817
818 return getDefaultGSCollectDirectoryPath(true); // file separator appended
819
[15506]820 }
[22771]821
[23143]822 // if we need to know whether the local server we are running is server.exe vs apache web server
823 static public boolean isPersistentServer() {
824 return (!isGsdlRemote && LocalLibraryServer.isPersistentServer());
825 }
[15506]826
[22771]827 /** Returns the path of the Greenstone "collect" directory. */
[24232]828 static public String getDefaultGSCollectDirectoryPath(boolean appendSeparator) {
[22806]829 String colDir;
830 if (GS3) {
831 colDir = getSitesDirectoryPath() + Configuration.site_name + File.separator + "collect";
[22771]832 }
833 else {
[24232]834 colDir = Configuration.gsdl_path + "collect";
[22771]835 }
[22806]836
837 if(appendSeparator) {
838 colDir += File.separator;
839 }
840 return colDir;
[22771]841 }
[15506]842
[24232]843 /** Returns the path of the GLI directory. */
844 static public String getGLIDirectoryPath()
845 {
846 return gli_directory_path;
847 }
[15506]848
849
[24232]850 /** Returns the path of the GLI "metadata" directory. */
851 static public String getGLIMetadataDirectoryPath()
852 {
853 return getGLIDirectoryPath() + "metadata" + File.separator;
854 }
[15506]855
856
[24232]857 /** Returns the path of the GLI user directory. */
858 static public String getGLIUserDirectoryPath()
859 {
860 return gli_user_directory_path;
861 }
[15506]862
863
[24232]864 /** Returns the path of the GLI user "cache" directory. */
865 static public String getGLIUserCacheDirectoryPath()
866 {
867 return getGLIUserDirectoryPath() + "cache" + File.separator;
868 }
[15506]869
870
[24232]871 /** Returns the path of the GLI user "log" directory. */
872 static public String getGLIUserLogDirectoryPath()
873 {
874 return getGLIUserDirectoryPath() + "log" + File.separator;
875 }
[15506]876
877
[24232]878 static public String getSitesDirectoryPath()
879 {
880 return Configuration.gsdl3_path + "sites" + File.separator;
881 }
[15506]882
883
[24232]884 static public void setCollectDirectoryPath(String collect_directory_path)
885 {
886 non_standard_collect_directory_path = collect_directory_path;
887 if (!non_standard_collect_directory_path.endsWith(File.separator)) {
888 non_standard_collect_directory_path = non_standard_collect_directory_path + File.separator;
889 }
[15506]890 }
891
892
[24232]893 static public void setGLIDirectoryPath(String gli_directory_path_arg)
894 {
895 gli_directory_path = gli_directory_path_arg;
896 }
[15506]897
898
[24232]899 static public void setGLIUserDirectoryPath(String gli_user_directory_path_arg)
900 {
901 gli_user_directory_path = gli_user_directory_path_arg;
[15506]902
[24232]903 // Ensure the GLI user directory exists
904 File gli_user_directory = new File(gli_user_directory_path);
905 if (!gli_user_directory.exists() && !gli_user_directory.mkdirs()) {
906 System.err.println("Error: Unable to make directory: " + gli_user_directory);
907 }
[15506]908 }
909
910
[24294]911 public static void initCollectDirectoryPath() {
[23143]912 String defaultColdir = getDefaultGSCollectDirectoryPath(false); // no file separator at end
913 String coldir = defaultColdir;
914 // If local GS and opening a collection outside the standard GS collect folder,
[24232]915 // need to open the non-standard collect folder that the collection resides in
916 if (!isGsdlRemote
[24294]917 && !open_collection_file_path.startsWith(defaultColdir))
918 {
[23143]919 File collectFolder = null;
920
[24294]921 if(!open_collection_file_path.equals("")) {
[23143]922 if(!open_collection_file_path.endsWith("gli.col")) { // then it's a collect folder
[24232]923 collectFolder = new File(open_collection_file_path);
[23143]924 } else {
[24232]925 // the filepath is a gli.col file. To get the collect folder: the 1st level up
926 // is the collection folder, 2 two levels up is the containing collect folder
927 collectFolder = new File(open_collection_file_path).getParentFile().getParentFile();
[23143]928 }
[24232]929
[23143]930 // Need to deal with colgroups as well: while there's an etc/collect.cfg
931 // in the current collectFolder, move one level up
932 String cfg_file = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
933 if(new File(collectFolder.getAbsolutePath()+File.separator+cfg_file).exists()) { // colgroup
[24232]934 collectFolder = collectFolder.getParentFile();
[23143]935 }
[24296]936
937 // Inform the user that their collecthome is non-standard (not inside GS installation)
938 nonStandardCollectHomeMessage(collectFolder.getAbsolutePath(), defaultColdir); // display message
[23143]939 }
940
941 if(collectFolder == null || !collectFolder.exists()) {
942 // if GLI config file specified no collectDir (open_collection_file_path is "")
943 // OR if dealing with a local server but the collectdir no longer exists,
944 // use the default greenstone collect directory, and write that to affected files
945
946 open_collection_file_path = defaultColdir; // default GS collect dir
947 // Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(), true, "");
948 } else { // use the coldir value specified in the flags to GLI or from the last GLI session
949 coldir = collectFolder.getAbsolutePath();
950 }
951 // set it as the current folder
952 setCollectDirectoryPath(coldir); // will ensure the required file separator at end
953 }
954
955 if(!isGsdlRemote) {
956 // LocalLibraryServer would already have set glisite.cfg to the correct collecthome for server.exe
957 // Here we set collecthome in gsdl(3)site.cfg for the GS2 apache web server and GS3 tomcat server
958 String gsdlsitecfg = getGsdlSiteConfigFile();
959 // update the gsdlsite config file and store the old value for use when we exit GLI
960 if(coldir.equals(defaultColdir)) {
961 gsdlsite_collecthome = Utility.updatePropertyConfigFile(
[24294]962 gsdlsitecfg, "collecthome", null);
[23143]963 } else {
964 gsdlsite_collecthome = Utility.updatePropertyConfigFile(
[24904]965 gsdlsitecfg, "collecthome", "\""+coldir+"\""); // no file separator
[24232]966 // if gsdlsite.cfg does not exist (if using server.exe for instance), the above method will just return
[23143]967 }
968 }
969 }
[24232]970
971 /** depending on the version of GS being run, return the path to the current GS' installation's gsdl(3)site.cfg */
972 public static String getGsdlSiteConfigFile() {
[23143]973 if(Gatherer.GS3) { // web/WEB-INF/cgi/gsdl3site.cfg
974 return Configuration.gsdl3_path + File.separator + "WEB-INF"
[24232]975 + File.separator + "cgi" + File.separator + "gsdl3site.cfg";
[23143]976 } else { // cgi-bin/gsdlsite.cfg
[24875]977 String gsdlarch = System.getenv("GSDLARCH");
978 if(gsdlarch == null) {
979 gsdlarch = "";
980 }
981 return Configuration.gsdl_path /* + File.separator */
982 + "cgi-bin" + File.separator + client_operating_system+gsdlarch + File.separator + "gsdlsite.cfg";
[23143]983 }
[24232]984 }
[23143]985
[24232]986 public static void collectDirectoryHasChanged(
987 String oldCollectPath, String newCollectPath, final Component container)
[24905]988 {
[23015]989 if(oldCollectPath.equals(newCollectPath)) {
990 return; // nothing to be done
991 }
[24232]992
[22831]993 // Will use a busy cursor if the process of changing the collect directory takes more
994 // than half a second/500ms. See http://www.catalysoft.com/articles/busyCursor.html
995 Cursor originalCursor = container.getCursor();
996 java.util.TimerTask timerTask = new java.util.TimerTask() {
997 public void run() {
998 // set the cursor on the container:
999 container.setCursor(new Cursor(Cursor.WAIT_CURSOR));
1000 }
1001 };
1002 java.util.Timer timer = new java.util.Timer();
1003
1004 try {
1005 timer.schedule(timerTask, 500);
[24232]1006
[22831]1007 // first save any open collection in the old location, then close it
1008 if(Gatherer.c_man.getCollection() != null) {
[24232]1009 Gatherer.g_man.saveThenCloseCurrentCollection(); // close the current collection first
[22831]1010 }
1011
1012 // change to new collect path
[23015]1013 if(newCollectPath.equals(getDefaultGSCollectDirectoryPath(true))) {
1014 Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(),
[24232]1015 true, "");
[23015]1016 } else {
1017 Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(),
[24232]1018 true, newCollectPath);
[23015]1019 }
[22831]1020 Gatherer.setCollectDirectoryPath(newCollectPath);
1021
[22605]1022
[22831]1023 // refresh the Documents in Greenstone Collections
1024 //WorkspaceTreeModel.refreshGreenstoneCollectionsNode();
1025 Gatherer.g_man.refreshWorkspaceTreeGreenstoneCollections();
1026
1027 // The web server needs to be told where a new (non-standard) collecthome home is.
[24875]1028 // The web server reads collecthome from cgi-bin/<OS>/gsdlsite.cfg, where the property
[22831]1029 // collecthome can be specified if a non-standard collecthome is to be used. If no
1030 // such property is specified in the file, then it assumes the standard GS collecthome.
1031 // This method does nothing for a remote Greenstone.
1032 if(Gatherer.isGsdlRemote) {
[24232]1033 return;
[22831]1034 }
[23143]1035
[22831]1036 // non-destructive update of gsdl(3)site.cfg (comments preserved)
1037 String collectDir = Gatherer.getCollectDirectoryPath();
1038 //collectDir = "\"" + collectDir.substring(0, collectDir.length()-1) + "\""; // remove file separator at end
[24905]1039 collectDir = collectDir.substring(0, collectDir.length()-1); // remove file separator at end
1040 if(collectDir != null) {
1041 collectDir = "\""+collectDir+"\"";
1042 }
1043 Utility.updatePropertyConfigFile(getGsdlSiteConfigFile(), "collecthome", collectDir);
[24232]1044 // if gsdlsite.cfg does not exist (if using server.exe for instance), the above method will just return
[22911]1045
[22831]1046 if(!Gatherer.GS3 && Gatherer.isLocalLibrary) {
[24232]1047 // for Images in the collection to work, the apache web server
1048 // configuration's COLLECTHOME should be updated on collectdir change.
1049 // Does nothing for server.exe at the moment
[22661]1050
[24232]1051 LocalLibraryServer.reconfigure();
[22831]1052 }
1053 } finally { // Note try-finally section without catch:
1054 // "Java's finally clause is guaranteed to be executed even when
1055 // an exception is thrown and not caught in the current scope."
1056 // See http://www.catalysoft.com/articles/busyCursor.html
1057 // the following code fragment is guaranteed to restore the original
1058 // cursor now the custom actionPerformed() processing is complete, regardless
1059 // of whether the processing method terminates normally or throws an exception
[24294]1060 // and regardless of where in the call stack the exception is caught.
[22831]1061
1062 timer.cancel();
1063 container.setCursor(originalCursor);
1064 }
[24232]1065 }
[22605]1066
1067
[24232]1068 static public void refresh(int refresh_reason)
1069 {
1070 if (g_man != null) {
[15506]1071
[24232]1072 g_man.refresh(refresh_reason, c_man.ready());
1073 }
1074
1075 // Now is a good time to force a garbage collect
1076 System.gc();
[15506]1077 }
1078
1079
[24232]1080 // used to send reload coll messages to the tomcat server
1081 static public void configGS3Server(String site, String command) {
[29222]1082
1083 // Do not do configGS3Server for a GS3 solr collection. Not at present at least, when we're stopping
1084 // and starting the GS3 server from GLI for solr cols, since this function clears the solr.xml file.
1085 if(Gatherer.c_man.isSolrCollection()) {
1086 return;
1087 }
1088
[24232]1089 if (Configuration.library_url == null){
1090 System.out.println("Error: you have not provided the Greenstone Library address.");
1091 return;
[15506]1092
[24232]1093 }
1094
1095 try {
1096 // need to add the servlet name to the exec address
1097 String raw_url = Configuration.library_url.toString() + Configuration.getServletPath() + command;
1098 URL url = new URL(raw_url);
1099 DebugStream.println("Action: " + raw_url);
1100 HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
1101 int response_code = library_connection.getResponseCode();
1102 if(HttpURLConnection.HTTP_OK <= response_code && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
1103 DebugStream.println("200 - Complete.");
1104 }
1105 else {
1106 DebugStream.println("404 - Failed.");
1107 }
1108 url = null;
1109 }
1110 catch(java.net.ConnectException connectException) {
1111 JOptionPane.showMessageDialog(g_man, Dictionary.get("Preferences.Connection.Library_Path_Connection_Failure", Configuration.library_url.toString()), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
1112 DebugStream.println(connectException.getMessage());
1113 }
1114 catch (Exception exception) {
1115 DebugStream.printStackTrace(exception);
1116 }
[15506]1117 }
1118
1119
[24232]1120 /** Used to 'spawn' a new child application when a file is double clicked.
1121 * @param file The file to open
1122 * @see org.greenstone.gatherer.Gatherer.ExternalApplication
1123 */
1124 static public void spawnApplication(File file) {
1125 String [] commands = assoc_man.getCommand(file);
1126 if(commands != null) {
1127 ExternalApplication app = new ExternalApplication(commands);
1128 apps.add(app);
1129 app.start();
1130 }
1131 else {
1132 ///ystem.err.println("No open command available.");
1133 }
[15506]1134 }
1135
1136
[24232]1137 static public void spawnApplication(String command)
1138 {
1139 ExternalApplication app = new ExternalApplication(command);
1140 apps.add(app);
1141 app.start();
1142 }
[15506]1143
[24232]1144 static public void spawnApplication(String command, String ID)
1145 {
1146 ExternalApplication app = new ExternalApplication(command, ID);
1147 apps.add(app);
1148 app.start();
1149 }
[15506]1150
[24232]1151 static public void spawnApplication(String[] commands, String ID)
1152 {
1153 ExternalApplication app = new ExternalApplication(commands, ID);
1154 apps.add(app);
1155 app.start();
1156 }
[21965]1157
[24232]1158 static public void terminateApplication(String ID) {
1159 for(int i = 0; i < apps.size(); i++) {
1160 ExternalApplication app = (ExternalApplication)apps.get(i);
1161 if(app.getID() != null && app.getID().equals(ID)) {
1162 app.stopExternalApplication();
1163 apps.remove(app);
1164 }
1165 }
[18987]1166 }
1167
1168
[24232]1169 /** Used to 'spawn' a new browser application or reset an existing one when the preview button is clicked
1170 * @param url The url to open the browser at
1171 * @see org.greenstone.gatherer.Gatherer.BrowserApplication
1172 */
1173 static public void spawnBrowser(String url) {
1174 String command = assoc_man.getBrowserCommand(url);
1175 if (command != null) {
1176 BrowserApplication app = new BrowserApplication(command, url);
1177 apps.add(app);
1178 app.start();
1179 }
1180 else {
1181 ///ystem.err.println("No browser command available.");
1182 }
[15506]1183 }
1184
1185
[24232]1186 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
1187 */
1188 static public void missingEXEC() {
1189 WarningDialog dialog;
1190 String configPropertyName = "general.library_url"+Configuration.gliPropertyNameSuffix();
[16884]1191
[24232]1192 if (GS3) {
1193 // Warning dialog with no cancel button and no "turn off warning" checkbox
1194 dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC_GS3.Title"), Dictionary.get("MissingEXEC_GS3.Message"), configPropertyName, false, false);
1195 } else { // local case
1196 dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC.Title"), Dictionary.get("MissingEXEC.Message"), configPropertyName, false);
1197 }
[16884]1198
[24232]1199 JTextField field = new URLField.Text(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false));
[18131]1200
[24232]1201 // Set the default library URL to the tomcat server and port number
1202 // specified in the build.properties located in the gsdl3_src_path
1203 if (GS3) {
1204 String host = "localhost";
[24604]1205 String port = "8383";
[24232]1206
1207 File buildPropsFile = new File(Configuration.gsdl3_src_path + File.separator + "build.properties");
1208 if(buildPropsFile.exists()) {
1209 Properties props = new Properties();
1210 try{
1211 props.load(new FileInputStream(buildPropsFile));
1212 host = props.getProperty("tomcat.server", host);
1213 port = props.getProperty("tomcat.port", port);
1214 }catch(Exception e){
1215 DebugStream.println("Could not load build.properties file");
1216 System.err.println("Could not load build.properties file");
1217 }
1218 props = null;
1219 }
1220 String defaultURL = "http://"+host+":"+port+"/"+"greenstone3";
1221 field.setText(defaultURL);
1222 field.selectAll();
[18131]1223 }
[24232]1224 dialog.setValueField(field);
1225 dialog.display();
1226 dialog.dispose();
1227 dialog = null;
[15506]1228
[24232]1229 String library_url_string = Configuration.getString(configPropertyName, true);
1230 if (!library_url_string.equals("")) {
1231 try {
1232 // WarningDialog does not allow invalid URLs, so the following is ignored:
1233 // make sure the URL the user provided contains the http:// prefix
1234 // and then save the corrected URL
1235 if(!library_url_string.startsWith("http://")
1236 && !library_url_string.startsWith("https://")) {
1237 library_url_string = "http://"+library_url_string;
1238 Configuration.setString(configPropertyName, true, configPropertyName);
1239 }
1240 Configuration.library_url = new URL(library_url_string);
1241 }
1242 catch (MalformedURLException exception) {
1243 DebugStream.printStackTrace(exception);
1244 }
[18131]1245 }
[15506]1246 }
1247
1248
1249
[24232]1250 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
1251 */
1252 static private void popupFedoraInfo() {
[15506]1253
[24232]1254 FedoraLogin dialog = new FedoraLogin("Fedora Login", false);
[15506]1255
[24232]1256 if (Configuration.library_url == null) {
1257
1258 String library_url_string = dialog.getLibraryURL();
1259 if (!library_url_string.equals("")) {
1260 try {
1261 Configuration.library_url = new URL(library_url_string);
1262 }
1263 catch (MalformedURLException exception) {
1264 DebugStream.printStackTrace(exception);
1265 }
1266 }
[15506]1267 }
1268
[24232]1269 boolean showLogin = true;
1270 do {
1271 if(!dialog.loginRequested()) { // user pressed cancel to exit the FedoraLogin dialog
1272 System.exit(0);
1273 } else {
1274 showLogin = dialog.loginRequested();
1275 String hostname = dialog.getHostname();
1276 String port = dialog.getPort();
1277 String username = dialog.getUsername();
1278 String password = dialog.getPassword();
1279 String protocol = dialog.getProtocol();
[15506]1280
[24232]1281 Configuration.fedora_info.setHostname(hostname);
1282 Configuration.fedora_info.setPort(port);
1283 Configuration.fedora_info.setUsername(username);
1284 Configuration.fedora_info.setPassword(password);
1285 Configuration.fedora_info.setProtocol(protocol);
1286
1287 String ping_url_str = protocol + "://" + hostname + ":" + port + "/fedora";
1288 String login_str = username + ":" + password;
1289
1290 String login_encoding = Base64.encodeBytes(login_str.getBytes());
1291
1292 try {
1293 URL ping_url = new URL(ping_url_str);
1294 URLConnection uc = ping_url.openConnection();
1295 uc.setRequestProperty ("Authorization", "Basic " + login_encoding);
1296 // Attempt to access some content ...
1297 InputStream content = (InputStream)uc.getInputStream();
1298
1299 // if no exception occurred in the above, we would have come here:
1300 showLogin = false;
1301 dialog.dispose();
1302 }
1303 catch (Exception exception) {
1304 // TODO: move into dictionary
1305 String[] errorMessage = {"Failed to connect to the Fedora server.", "It might not be running, or",
1306 "incorrect username and/or password."};
1307 dialog.setErrorMessage(errorMessage);
1308 //DebugStream.printStackTrace(exception);
1309 // exception occurred, show the dialog again (do this after printing to
1310 // debugStream, else the above does not get done for some reason).
1311 dialog.setVisible(true);
1312 }
1313 }
1314 } while(showLogin);
[15506]1315
[24232]1316 dialog = null; // no more need of the dialog
1317
1318 // Now we are connected.
1319 }
[15506]1320
1321
1322
[24232]1323 static private void requestGLIServerURL()
1324 {
1325 WarningDialog dialog;
1326 String[] defaultURLs = {
1327 "http://localhost:8080/greenstone3/cgi-bin/gliserver.pl",
1328 "http://localhost:8080/gsdl/cgi-bin/gliserver.pl"
1329 };
[16329]1330
[24232]1331 // Warning dialog with no cancel button and no "turn off warning" checkbox
1332 // (since user-input of the gliserver script is mandatory)
1333 dialog = new WarningDialog("warning.MissingGLIServer", Dictionary.get("MissingGLIServer.Title"), Dictionary.get("MissingGLIServer.Message"), "general.gliserver_url", false, false);
[16329]1334
[24232]1335 dialog.setValueField(new URLField.DropDown(Configuration.getColor("coloring.editable_foreground", false),
1336 Configuration.getColor("coloring.editable_background", false),
1337 defaultURLs, "general.gliserver_url",
1338 "general.open_collection"+Configuration.gliPropertyNameSuffix(),
1339 "gliserver.pl"));
[16329]1340
[24232]1341 if (Gatherer.default_gliserver_url!=null){
1342 dialog.setValueField(Gatherer.default_gliserver_url.toString());
1343 }
[16329]1344
[24232]1345 // A WarningDialog cannot always be made to respond (let alone to exit the program) on close. We
1346 // handle the response of this particular WarningDialog here: a URL for gliserver.pl is a crucial
1347 // piece of user-provided data. Therefore, if no URL was entered for gliserver.pl, it'll exit safely.
1348 dialog.addWindowListener(new WindowAdapter() {
1349 public void windowClosing(WindowEvent e) {
1350 Gatherer.exit();
1351 }
1352 });
1353
1354 dialog.display();
1355 dialog.dispose();
1356 dialog = null;
1357
1358
1359 String gliserver_url_string = Configuration.getString("general.gliserver_url", true);
1360 if (!gliserver_url_string.equals("")) {
1361 try {
1362 Configuration.gliserver_url = new URL(gliserver_url_string);
1363 Configuration.setString("general.gliserver_url", true, gliserver_url_string);
1364 }
1365 catch (MalformedURLException exception) {
1366 DebugStream.printStackTrace(exception);
1367 }
[16329]1368 }
[15506]1369 }
1370
1371
[24232]1372 /** Prints a warning message about a missing GSDL path, which although not fatal pretty much ensures nothing will work properly in the GLI.
1373 */
1374 static private void missingGSDL() {
1375 WarningDialog dialog = new WarningDialog("warning.MissingGSDL", Dictionary.get("MissingGSDL.Title"), Dictionary.get("MissingGSDL.Message"), null, false);
1376 dialog.display();
1377 dialog.dispose();
1378 dialog = null;
1379 }
[15506]1380
[24232]1381 /** Prints a warning message about missing a valid ImageMagick path, which although not fatal means building image collections won't work */
1382 static private void missingImageMagick() {
1383 WarningDialog dialog = new WarningDialog("warning.MissingImageMagick", Dictionary.get("MissingImageMagick.Title"), Dictionary.get("MissingImageMagick.Message"), null, false);
1384 dialog.display();
1385 dialog.dispose();
1386 dialog = null;
1387 }
[15506]1388
[24288]1389 /** Prints a message informing the user where they can get PDFBox from to process PDF files of v1.5 and greater */
1390 static private void missingPDFBox(String zipExtension, String extFolder) {
[24811]1391 // point to the correct version of the PDFBox extension for this Greenstone release
1392 String releaseTag = "";
1393 if(!PROGRAM_VERSION.equals("trunk")) { // assume it's a release version
1394 releaseTag = "main/tags/"+PROGRAM_VERSION+"/";
1395 }
1396
1397 WarningDialog dialog = new WarningDialog("warning.MissingPDFBox", Dictionary.get("MissingPDFBox.Title"), Dictionary.get("MissingPDFBox.Message", new String[]{releaseTag, zipExtension, extFolder}), null, false);
[24288]1398 dialog.display();
1399 dialog.dispose();
1400 dialog = null;
1401 }
1402
[24232]1403 /** 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. */
1404 static private void missingPERL() {
1405 WarningDialog dialog = new WarningDialog("warning.MissingPERL", Dictionary.get("MissingPERL.Title"), Dictionary.get("MissingPERL.Message"), null, false);
1406 dialog.display();
1407 dialog.dispose();
1408 dialog = null;
1409 }
[15506]1410
[24294]1411 /** Prints a message informing the user that their collecthome is non-standard (not inside GS installation) */
1412 static private void nonStandardCollectHomeMessage(String open_collection_file_path, String defaultColDir) {
1413 WarningDialog dialog = new WarningDialog("warning.NonStandardCollectHome", Dictionary.get("NonStandardCollectHome.Title"), Dictionary.get("NonStandardCollectHome.Message", new String[]{open_collection_file_path, defaultColDir}), null, false);
1414 dialog.display();
1415 dialog.dispose();
1416 dialog = null;
1417 }
1418
[23433]1419 /** Prints a warning message about the OS not supporting multiple filename encodings. */
[24232]1420 static private void multipleFilenameEncodingsNotSupported() {
[23433]1421 WarningDialog dialog = new WarningDialog("warning.NoEncodingSupport",
[24232]1422 Dictionary.get("NoEncodingSupport.Title"),
1423 Dictionary.get("NoEncodingSupport.Message"), null, false);
[23433]1424 dialog.display();
1425 dialog.dispose();
1426 dialog = null;
[24232]1427 }
[15506]1428
[24232]1429 /** Sets up the proxy connection by setting JVM Environment flags and creating a new Authenticator.
1430 * @see java.lang.Exception
1431 * @see java.lang.System
1432 * @see java.net.Authenticator
1433 * @see org.greenstone.gatherer.Configuration
1434 * @see org.greenstone.gatherer.GAuthenticator
1435 */
1436 static public void setProxy() {
1437 try {// Can throw several exceptions
1438 if(Configuration.get("general.use_proxy", true)) {
1439 System.setProperty("http.proxyType", "4");
1440 System.setProperty("http.proxyHost", Configuration.getString("general.proxy_host", true));
1441 System.setProperty("http.proxyPort", Configuration.getString("general.proxy_port", true));
1442 System.setProperty("http.proxySet", "true");
1443 } else {
1444 System.setProperty("http.proxyHost", "");
1445 System.setProperty("http.proxyPort", "");
1446 System.setProperty("http.proxySet", "false");
1447 }
1448 } catch (Exception error) {
1449 DebugStream.println("Error in Gatherer.initProxy(): " + error);
1450 DebugStream.printStackTrace(error);
1451 }
[15506]1452 }
1453
[24232]1454
1455 /** 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. */
1456 static private class ExternalApplication
[15506]1457 extends Thread {
[24232]1458 private Process process = null;
1459 /** The initial command string given to this sub-process. */
1460 private String command = null;
1461 private String[] commands = null;
[18987]1462
[24232]1463 private String ID = null;
[18987]1464
[24232]1465 /** Constructor.
1466 * @param command The initial command <strong>String</strong>.
1467 */
1468 public ExternalApplication(String command) {
1469 this.command = command;
1470 }
[15506]1471
[24232]1472 public ExternalApplication(String[] commands) {
1473 this.commands = commands;
1474 }
[18987]1475
[24232]1476 public ExternalApplication(String command, String ID) {
1477 this.command = command;
1478 this.ID = ID;
1479 }
[21965]1480
[24232]1481 public ExternalApplication(String[] commands, String ID) {
1482 this.commands = commands;
1483 this.ID = ID;
1484 }
1485
1486 public String getID() {
1487 return ID;
1488 }
[18987]1489
[24232]1490 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
1491 * @see java.lang.Exception
1492 * @see java.lang.Process
1493 * @see java.lang.Runtime
1494 * @see java.lang.System
1495 * @see java.util.Vector
1496 */
1497 public void run() {
1498 // Call an external process using the args.
1499 try {
1500 if(commands != null) {
1501 StringBuffer whole_command = new StringBuffer();
[24358]1502 for(int i = 0; i < commands.length; i++) {
1503 // get rid of any quotes around parameters in file associations
1504 if(commands[i].startsWith("\"") || commands[i].startsWith("\'")) {
1505 commands[i] = commands[i].substring(1);
1506 }
1507 if(commands[i].endsWith("\"") || commands[i].endsWith("\'")) {
1508 commands[i] = commands[i].substring(0, commands[i].length()-1);
1509 }
1510
[24356]1511 if (i>0) {
1512 whole_command.append(" ");
1513 }
[24358]1514 whole_command.append(commands[i]);
[24232]1515 }
1516 DebugStream.println("Running " + whole_command.toString());
1517 Runtime rt = Runtime.getRuntime();
1518 process = rt.exec(commands);
1519 process.waitFor();
1520 }
1521 else {
1522 DebugStream.println("Running " + command);
1523 Runtime rt = Runtime.getRuntime();
1524 process = rt.exec(command);
1525 process.waitFor();
1526 }
1527 }
1528 catch (Exception exception) {
1529 DebugStream.printStackTrace(exception);
1530 }
1531 // Remove ourself from Gatherer list of threads.
1532 apps.remove(this);
1533 // Call exit if we were the last outstanding child process thread.
1534 if (apps.size() == 0 && exit == true) {
1535 // In my opinion (DB) there is no need to exit here,
1536 // the 'run' method ending naturally brings this
1537 // thread to an end. In fact it is potentially
1538 // dangerous to exit here, as the main thread in the
1539 // Gatherer class may be stopped prematurely. As it so
1540 // happens the point at which the ExternalApplication thread
1541 // is asked to stop (Back in the main Gatherer thread) is after
1542 // various configuration files have been saved.
1543 //
1544 // A similar argument holds for BrowserApplication thread below.
1545 System.exit(exit_status);
1546 }
[15506]1547 }
[24232]1548 public void stopExternalApplication() {
1549 if(process != null) {
1550 process.destroy();
1551 }
[15506]1552 }
1553 }
[24232]1554 /** 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. */
1555 static private class BrowserApplication
[15506]1556 extends Thread {
[24232]1557 private Process process = null;
1558 /** The initial command string given to this sub-process. */
1559 private String command = null;
1560 private String url = null;
1561 private String[] commands = null;
[15506]1562
[24232]1563 public BrowserApplication(String command, String url) {
1564 StringTokenizer st = new StringTokenizer(command);
1565 int num_tokens = st.countTokens();
1566 this.commands = new String [num_tokens];
1567 int i=0;
1568 while (st.hasMoreTokens()) {
1569 commands[i] = st.nextToken();
1570 i++;
1571 }
1572 //this.commands = commands;
1573 this.url = url;
1574 }
1575 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
1576 * @see java.lang.Exception
1577 * @see java.lang.Process
1578 * @see java.lang.Runtime
1579 * @see java.lang.System
1580 * @see java.util.Vector
1581 */
1582 public void run() {
1583 // Call an external process using the args.
1584 if(commands == null) {
1585 apps.remove(this);
1586 return;
1587 }
1588 try {
1589 String prog_name = commands[0];
1590 String lower_name = prog_name.toLowerCase();
1591 if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) {
1592 DebugStream.println("found mozilla or netscape, trying remote it");
1593 // mozilla and netscape, try using a remote command to get things in the same window
1594 String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"};
1595 printArray(new_commands);
[15506]1596
[24232]1597 Runtime rt = Runtime.getRuntime();
1598 process = rt.exec(new_commands);
1599 int exitCode = process.waitFor();
1600 if (exitCode != 0) { // if Netscape or mozilla was not open
1601 DebugStream.println("couldn't do remote, trying original command");
1602 printArray(commands);
1603 process = rt.exec(commands); // try the original command
1604 }
1605 } else {
1606 // just run what we have been given
1607 StringBuffer whole_command = new StringBuffer();
1608 for(int i = 0; i < commands.length; i++) {
1609 whole_command.append(commands[i]);
1610 whole_command.append(" ");
1611 }
1612 DebugStream.println("Running " + whole_command.toString());
1613 Runtime rt = Runtime.getRuntime();
1614 process = rt.exec(commands);
1615 process.waitFor();
1616 }
1617 }
1618
1619 catch (Exception exception) {
1620 DebugStream.printStackTrace(exception);
1621 }
1622 // Remove ourself from Gatherer list of threads.
1623 apps.remove(this);
1624 // Call exit if we were the last outstanding child process thread.
1625 if (apps.size() == 0 && exit == true) {
1626 System.exit(exit_status);
1627 }
[15506]1628 }
[24232]1629 public void printArray(String [] array) {
1630 for(int i = 0; i < array.length; i++) {
1631 DebugStream.print(array[i]+" ");
1632 System.err.println(array[i]+" ");
1633 }
1634 }
1635 public void stopBrowserApplication() {
1636 if(process != null) {
1637 process.destroy();
1638 }
1639 }
[15506]1640 }
1641
1642
[24232]1643 private class ImageMagickTest
[15506]1644 {
[24232]1645 public boolean found()
1646 {
[24600]1647 // at this stage, GLI has already sourced setup.bash, and the necessary
1648 // env variables will be available to the perl process we're about to launch
1649 boolean found = false;
1650
[24232]1651 try {
[24600]1652 // run the command `/path/to/perl -S gs-magick.pl identify -version`
1653 ArrayList cmd_list = new ArrayList();
1654 if (!Gatherer.isGsdlRemote) {
1655 if(Configuration.perl_path != null) {
1656 cmd_list.add(Configuration.perl_path);
1657 } else {
1658 System.err.println("***** ImageMagickTest Warning: Perl_path not set, calling 'perl' instead.");
1659 cmd_list.add("perl");
1660 }
1661 cmd_list.add("-S");
1662 }
1663 cmd_list.add("gs-magick.pl");
1664 if(Utility.isWindows()) {
1665 cmd_list.add("identify.exe");
1666 } else {
1667 cmd_list.add("identify");
1668 }
1669 cmd_list.add("-version");
1670
1671 String[] command_parts = (String[]) cmd_list.toArray(new String[0]);
1672
1673 String cmd_str = "";
1674 for(int i = 0; i < command_parts.length; i++) {
1675 cmd_str += command_parts[i] + " ";
1676 }
1677 DebugStream.println("***** Running ImageMagickTest command: " + cmd_str);
1678
1679 Process image_magick_process = Runtime.getRuntime().exec(command_parts);
[24232]1680 image_magick_process.waitFor();
[15506]1681
[24232]1682 //new way of detection of ImageMagick
1683 InputStreamReader isr = new InputStreamReader(image_magick_process.getInputStream());
[15506]1684
[24232]1685 BufferedReader br = new BufferedReader(isr);
[24600]1686 // Capture the standard output stream and seach for two particular occurrences: Version and ImageMagick.
[15506]1687
[24232]1688 String line = br.readLine();
[24600]1689 if (line != null) {
1690 String lc_line = line.toLowerCase();
1691 if (lc_line.indexOf("version") != -1 || lc_line.indexOf("imagemagick") != -1) {
1692 //System.err.println("*** ImageMagickTest Line: " + line);
1693 found = true;
1694 } // else found var remains false
[24232]1695 }
1696
[24600]1697 // Maybe put the close in a finally (but note that it can throw and IOex too)? See
1698 // http://download.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
1699 br.close();
1700 return found;
[24232]1701 //return (image_magick_process.exitValue() == 0);
1702 }
[24600]1703 catch (Exception exception) {
1704 exception.printStackTrace();
1705 return found;
[24232]1706 }
[15506]1707 }
1708 }
1709
1710
[24232]1711 private class PerlTest
1712 {
1713 private String[] command = new String[2];
[15506]1714
[24232]1715 public PerlTest()
1716 {
1717 command[0] = (Utility.isWindows() ? Utility.PERL_EXECUTABLE_WINDOWS : Utility.PERL_EXECUTABLE_UNIX);
1718 command[1] = "-version";
1719 }
1720
1721 public boolean found()
1722 {
1723 try {
1724 Process perl_process = Runtime.getRuntime().exec(command);
1725 perl_process.waitFor();
1726 return (perl_process.exitValue() == 0);
1727 }
1728 catch (Exception exception) {
1729 return false;
1730 }
1731 }
1732
1733 public String toString() {
1734 return command[0];
1735 }
[15506]1736 }
[24232]1737
[15506]1738}
Note: See TracBrowser for help on using the repository browser.