[12529] | 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 org.greenstone.gatherer.cdm.Argument;
|
---|
| 11 | import org.greenstone.gatherer.Configuration;
|
---|
| 12 | import org.greenstone.gatherer.DebugStream;
|
---|
| 13 | import org.greenstone.gatherer.Dictionary;
|
---|
[13016] | 14 | import org.greenstone.gatherer.Gatherer;
|
---|
[13594] | 15 | import org.greenstone.gatherer.greenstone.LocalGreenstone;
|
---|
[12529] | 16 | import org.greenstone.gatherer.gui.*;
|
---|
[31636] | 17 | import org.greenstone.gatherer.util.SafeProcess;
|
---|
[12529] | 18 | import org.greenstone.gatherer.util.Utility;
|
---|
| 19 |
|
---|
| 20 | public 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();
|
---|
[20924] | 85 | commands.add(Configuration.perl_path);
|
---|
| 86 | commands.add("-S");
|
---|
[12529] | 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);
|
---|
[31877] | 106 | try {
|
---|
[31860] | 107 | SafeProcess prcs = null;
|
---|
| 108 |
|
---|
[31877] | 109 | if (proxy_url != null && !proxy_url.equals("")) {
|
---|
[13016] | 110 | // Specify proxies as environment variables
|
---|
| 111 | // Need to manually specify GSDLHOME and GSDLOS also
|
---|
[31877] | 112 |
|
---|
[31878] | 113 | proxy_url = proxy_url.replaceAll("http://","");
|
---|
[31877] | 114 |
|
---|
[31878] | 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;
|
---|
[31820] | 121 | env[3] = "GSDLHOME=" + Configuration.gsdl_path;
|
---|
| 122 | env[4] = "GSDLOS=" + Gatherer.client_operating_system;
|
---|
[31878] | 123 | // teach it where the wgetrc file lives, in gs2build/bin/<os>:
|
---|
| 124 | env[5] = "WGETRC=" + LocalGreenstone.getBinOSDirectoryPath(Gatherer.client_operating_system)+"wgetrc";
|
---|
| 125 | // Issue discovered on Windows: If PATH is not made available to perl, so that wget or something else needed by
|
---|
| 126 | // WgetDownload.pm's open3() call is not on the PATH, then the perl open() call to run wget fails.
|
---|
| 127 | // So make PATH available to get open3() working:
|
---|
| 128 | env[6] = "PATH="+System.getenv("PATH");
|
---|
| 129 |
|
---|
[31636] | 130 | prcs = new SafeProcess(command, env, null);
|
---|
[13016] | 131 | }
|
---|
| 132 | else {
|
---|
| 133 | // Will inherit the GLI's environment, with GSDLHOME and GSDLOS set
|
---|
[31636] | 134 | prcs = new SafeProcess(command);
|
---|
[12529] | 135 | }
|
---|
[31636] | 136 |
|
---|
| 137 | // special processing of Process' stderr stream:
|
---|
| 138 | // Search the process standard error stream and seach for two particular occurences.
|
---|
| 139 | int exitVal = prcs.runProcess(null, new ProcErrStreamLineByLineHandler());
|
---|
| 140 |
|
---|
| 141 | } catch (Exception ioe) {
|
---|
| 142 | ioe.printStackTrace();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | }
|
---|
[12529] | 146 |
|
---|
[31636] | 147 | private class ProcErrStreamLineByLineHandler extends SafeProcess.LineByLineHandler
|
---|
| 148 | {
|
---|
| 149 | public ProcErrStreamLineByLineHandler() {
|
---|
| 150 | super(SafeProcess.STDERR); // sets this.source
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | public void gotLine(String line) {
|
---|
| 154 | // Check the current line from the standard error stream and seach for two particular occurences.
|
---|
| 155 | if (!line.trim().equals("<<Finished>>")) {
|
---|
[12529] | 156 |
|
---|
| 157 | JLabel a_label = new JLabel(line);
|
---|
| 158 | a_label.setAlignmentX(Component.LEFT_ALIGNMENT);
|
---|
[31636] | 159 | ServerInfoDialog.this.info_list_pane.add(a_label);
|
---|
[12529] | 160 | }
|
---|
| 161 | }
|
---|
[31636] | 162 | public void gotException(Exception e) {
|
---|
| 163 | e.printStackTrace();
|
---|
| 164 | }
|
---|
[12529] | 165 | }
|
---|
| 166 | }
|
---|