source: main/trunk/gli/src/org/greenstone/gatherer/download/ServerInfoDialog.java@ 31877

Last change on this file since 31877 was 31877, checked in by ak19, 7 years ago

Getting Windows wget to use proxy settings in environment. They didn't need to be in CAPS, as Windows has the side effect of setting env vars in both the original case of the letters of the env variable name as well as in all caps, and unsetting either version unsets both. On Windows however, I noticed that Perl was not on the PATH after open3() in WgetDownload.pm::useWget() failed with an unclear error message. So now the PATH is also propagated from Java to the perl code for downloading using wget.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1package org.greenstone.gatherer.download;
2
3import java.awt.*;
4import java.awt.event.*;
5import javax.swing.*;
6import javax.swing.border.*;
7import javax.swing.event.*;
8import java.io.*;
9import java.util.ArrayList;
10import org.greenstone.gatherer.cdm.Argument;
11import org.greenstone.gatherer.Configuration;
12import org.greenstone.gatherer.DebugStream;
13import org.greenstone.gatherer.Dictionary;
14import org.greenstone.gatherer.Gatherer;
15import org.greenstone.gatherer.greenstone.LocalGreenstone;
16import org.greenstone.gatherer.gui.*;
17import org.greenstone.gatherer.util.SafeProcess;
18import org.greenstone.gatherer.util.Utility;
19
20public class ServerInfoDialog extends JDialog
21{
22 static private Dimension SIZE = new Dimension(500, 300);
23 private String url;
24 private String mode;
25 private JPanel info_list_pane;
26 private JScrollPane info_scroll_pane;
27 private JDialog info_dialog;
28 private Download download;
29 private String proxy_url;
30
31 public ServerInfoDialog(String url, String proxy_url, String mode, Download download) {
32 super();
33 this.url = url;
34 this.mode = mode;
35 this.download = download;
36 this.proxy_url = proxy_url;
37 this.info_dialog = this;
38
39 this.setSize(SIZE);
40
41 JButton close_button = new GLIButton(Dictionary.get("General.Close"),Dictionary.get("General.Close_Tooltip"));
42 close_button.setEnabled(true);
43 close_button.addActionListener(new ActionListener() {
44 public void actionPerformed(ActionEvent event) {
45 info_dialog.dispose();
46 }
47 });
48
49 JPanel button_pane = new JPanel();
50 button_pane.setLayout(new FlowLayout());
51 button_pane.setBorder(BorderFactory.createEtchedBorder());
52 button_pane.add(close_button);
53
54 info_list_pane = new JPanel();
55 info_list_pane.setLayout(new BoxLayout(info_list_pane, BoxLayout.Y_AXIS));
56 info_list_pane.setBorder(BorderFactory.createEtchedBorder());
57 getServerInfo();
58
59 info_scroll_pane = new JScrollPane(info_list_pane);
60
61 JPanel information_pane = new JPanel();
62 information_pane.setLayout(new BorderLayout());
63 information_pane.setBorder(BorderFactory.createEtchedBorder());
64 information_pane.add(info_scroll_pane,BorderLayout.CENTER);
65
66 JPanel main_pane = new JPanel();
67 main_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
68 main_pane.setLayout(new BorderLayout());
69 main_pane.add(information_pane,BorderLayout.CENTER);
70 main_pane.add(button_pane,BorderLayout.SOUTH);
71
72 Container main_container = this.getContentPane();
73 main_container.add(main_pane);
74
75 // Show
76 Dimension screen_size = Configuration.screen_size;
77 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
78 setVisible(true);
79
80 }
81
82 private void getServerInfo()
83 {
84 ArrayList commands = new ArrayList();
85 commands.add(Configuration.perl_path);
86 commands.add("-S");
87 commands.add(LocalGreenstone.getBinScriptDirectoryPath()+"downloadfrom.pl");
88 commands.add("-info");
89 commands.add("-download_mode");
90 commands.add(mode);
91
92 ArrayList all_arg = download.getArguments(true,false);
93 for (int i = 0; i < all_arg.size(); i++) {
94 Argument argument = (Argument) all_arg.get(i);
95
96 if(argument.isAssigned()) {
97 commands.add("-" + argument.getName());
98 if(argument.getType() != Argument.FLAG) {
99 commands.add(argument.getValue());
100 }
101 }
102 }
103
104 String[] command = (String[]) commands.toArray(new String[0]);
105 DebugStream.println("Getting server info: "+commands);
106 try {
107 SafeProcess prcs = null;
108
109 if (proxy_url != null && !proxy_url.equals("")) {
110 // Specify proxies as environment variables
111 // Need to manually specify GSDLHOME and GSDLOS also
112
113 proxy_url = proxy_url.replaceAll("http://","");
114
115 String [] env = new String[7];
116
117 env[0] = "http_proxy=http://"+proxy_url;
118 env[1] = "https_proxy=http://"+proxy_url; // yes, HTTP protocol for https:// too
119 // see also https://wiki.archlinux.org/index.php/proxy_settings
120 env[2] = "ftp_proxy=ftp://"+proxy_url;
121 env[3] = "GSDLHOME=" + Configuration.gsdl_path;
122 env[4] = "GSDLOS=" + Gatherer.client_operating_system;
123 env[5] = "WGETRC=" + LocalGreenstone.getBinOSDirectoryPath(Gatherer.client_operating_system)+"wgetrc"; // teach it where the wgetrc file lives, in gs2build/bin/<os>
124 // If PATH is not made available, so that Perl is not on the PATH, then WgetDownload.pm's open3() call
125 // to run wget fails with cryptic failure message. So make PATH available for open3():
126 env[6] = "PATH="+System.getenv("PATH");
127
128 prcs = new SafeProcess(command, env, null);
129 }
130 else {
131 // Will inherit the GLI's environment, with GSDLHOME and GSDLOS set
132 prcs = new SafeProcess(command);
133 }
134
135 // special processing of Process' stderr stream:
136 // Search the process standard error stream and seach for two particular occurences.
137 int exitVal = prcs.runProcess(null, new ProcErrStreamLineByLineHandler());
138
139 } catch (Exception ioe) {
140 ioe.printStackTrace();
141 }
142
143 }
144
145 private class ProcErrStreamLineByLineHandler extends SafeProcess.LineByLineHandler
146 {
147 public ProcErrStreamLineByLineHandler() {
148 super(SafeProcess.STDERR); // sets this.source
149 }
150
151 public void gotLine(String line) {
152 // Check the current line from the standard error stream and seach for two particular occurences.
153 if (!line.trim().equals("<<Finished>>")) {
154
155 JLabel a_label = new JLabel(line);
156 a_label.setAlignmentX(Component.LEFT_ALIGNMENT);
157 ServerInfoDialog.this.info_list_pane.add(a_label);
158 }
159 }
160 public void gotException(Exception e) {
161 e.printStackTrace();
162 }
163 }
164}
Note: See TracBrowser for help on using the repository browser.