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

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

First phase of shifting gli code to use SafeProcess instead of Java Process. This phase takes care of all the easy cases.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 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 String [] env = null;
108 SafeProcess prcs = null;
109
110 if (Utility.isWindows()) {
111 prcs = new SafeProcess(command);
112 }
113 else {
114 if (proxy_url != null && !proxy_url.equals("")) {
115 // Specify proxies as environment variables
116 // Need to manually specify GSDLHOME and GSDLOS also
117 env = new String[4];
118 proxy_url = proxy_url.replaceAll("http://","");
119 env[0] = "http_proxy=http://"+proxy_url;
120 env[1] = "ftp_proxy=ftp://"+proxy_url;
121 env[2] = "GSDLHOME=" + Configuration.gsdl_path;
122 env[3] = "GSDLOS=" + Gatherer.client_operating_system;
123 prcs = new SafeProcess(command, env, null);
124 }
125 else {
126 // Will inherit the GLI's environment, with GSDLHOME and GSDLOS set
127 prcs = new SafeProcess(command);
128 }
129 }
130
131 // special processing of Process' stderr stream:
132 // Search the process standard error stream and seach for two particular occurences.
133 int exitVal = prcs.runProcess(null, new ProcErrStreamLineByLineHandler());
134
135 } catch (Exception ioe) {
136 ioe.printStackTrace();
137 }
138
139 }
140
141 private class ProcErrStreamLineByLineHandler extends SafeProcess.LineByLineHandler
142 {
143 public ProcErrStreamLineByLineHandler() {
144 super(SafeProcess.STDERR); // sets this.source
145 }
146
147 public void gotLine(String line) {
148 // Check the current line from the standard error stream and seach for two particular occurences.
149 if (!line.trim().equals("<<Finished>>")) {
150
151 JLabel a_label = new JLabel(line);
152 a_label.setAlignmentX(Component.LEFT_ALIGNMENT);
153 ServerInfoDialog.this.info_list_pane.add(a_label);
154 }
155 }
156 public void gotException(Exception e) {
157 e.printStackTrace();
158 }
159 }
160}
Note: See TracBrowser for help on using the repository browser.