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