1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.gridsystems.config.modules.tomcat;
25
26 import java.awt.GridBagConstraints;
27 import java.awt.GridBagLayout;
28
29 import javax.swing.JComboBox;
30 import javax.swing.JLabel;
31 import javax.swing.JOptionPane;
32 import javax.swing.JPanel;
33 import javax.swing.JPasswordField;
34 import javax.swing.JTextField;
35 import javax.swing.SpringLayout;
36 import javax.swing.border.EmptyBorder;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 import com.gridsystems.config.Configurator;
42 import com.gridsystems.config.ConfiguratorModel;
43 import com.gridsystems.config.app.UI;
44 import com.gridsystems.config.tools.ErrorSupport;
45 import com.gridsystems.config.tools.IpVerifier;
46 import com.gridsystems.config.tools.PasswordVerifier;
47 import com.gridsystems.config.tools.PortVerifier;
48 import com.gridsystems.config.tools.swing.BasePanel;
49 import com.gridsystems.config.tools.swing.FileChooserField;
50 import com.gridsystems.config.tools.swing.SwingTools;
51 import com.gridsystems.utils.NetUtils;
52
53
54
55
56
57
58
59 public class TomcatSwingView extends BasePanel {
60
61
62
63 public static final int LARGE_FIELD_LENGTH = 10;
64
65
66
67
68 public static final int NORMAL_FIELD_LENGTH = 5;
69
70
71
72
73 private static Log log = LogFactory.getLog(TomcatSwingView.class);
74
75
76
77
78 private JComboBox ipField = new JComboBox(new String[] { });
79
80
81
82
83 JTextField edShPort = new JTextField(NORMAL_FIELD_LENGTH);
84
85
86
87
88 ConnectorEditor editor = new ConnectorEditor();
89
90
91
92
93 FileChooserField edKeystoreDir = new FileChooserField();
94
95
96
97
98 JTextField edKeystorePas = new JPasswordField(LARGE_FIELD_LENGTH);
99
100
101
102
103
104
105 public TomcatSwingView(Configurator config) {
106 super(config);
107 setBundle(TomcatConfigurator.BUNDLE);
108
109 setIcon(SwingTools.loadIcon(TomcatConfigurator.class, "icon.gif"));
110 setSmallIcon(SwingTools.loadIcon(TomcatConfigurator.class, "small.gif"));
111
112 init();
113 }
114
115
116
117
118 private void init() {
119 ErrorSupport support = new ErrorSupport(getConfigurator());
120 support.addError("badIP", getString("errors.badIP"));
121 support.addError("loopbackIP", getString("errors.loopback.ip.short"));
122
123 IpVerifier ipVerifier =
124 new IpVerifier(support, "badIP", TomcatConfigModel.ALLOWED_IP_TYPE);
125 ipVerifier.setLoopbackAddressAllowed(false);
126 ipVerifier.setErrorLoopbackId("loopbackIP");
127
128 ipField.setEditable(true);
129 String[] ips = NetUtils.getAllIPs();
130 for (int i = 0; i < ips.length; i++) {
131 ipField.addItem(ips[i]);
132 }
133 ipField.setInputVerifier(ipVerifier);
134
135 GridBagLayout layout = new GridBagLayout();
136 this.setLayout(layout);
137
138 JPanel group1 = buildHttpPanel(layout);
139 JPanel group2 = buildHttpsPanel(layout);
140
141 GridBagConstraints gc = new GridBagConstraints();
142 gc.gridwidth = GridBagConstraints.REMAINDER;
143 gc.anchor = GridBagConstraints.PAGE_START;
144 gc.weightx = 1.0;
145 gc.weighty = 1.0;
146 gc.fill = GridBagConstraints.BOTH;
147 this.add(group1, gc);
148
149 gc.anchor = GridBagConstraints.PAGE_START;
150 gc.weighty = 0.0;
151 this.add(group2, gc);
152 }
153
154
155
156
157
158
159
160 private JPanel buildHttpPanel(GridBagLayout layout) {
161 String title = getString("pnlHttp.title");
162
163 JPanel topPanel = new JPanel(new GridBagLayout());
164 topPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
165
166 SwingTools.addField(topPanel, getString("field.ip"), this.ipField, 0);
167 SwingTools.addField(topPanel, getString("pnlHttp.lblShutdownPort"), edShPort, 1);
168 JPanel panel = SwingTools.getGroupPanel(title, layout);
169
170 JLabel lbl2 = new JLabel(getString("pnlHttp.lblConnections"));
171 editor.setChangeCallback(new ConnectorChangeCallback());
172 SpringLayout lout = new SpringLayout();
173 panel.setLayout(lout);
174
175 panel.add(topPanel);
176 panel.add(lbl2);
177 panel.add(editor);
178
179
180 lout.putConstraint(SpringLayout.WEST, topPanel, 2, SpringLayout.WEST, panel);
181 lout.putConstraint(SpringLayout.NORTH, topPanel, 0, SpringLayout.NORTH, panel);
182
183 lout.putConstraint(SpringLayout.WEST, lbl2, 2, SpringLayout.WEST, panel);
184 lout.putConstraint(SpringLayout.NORTH, lbl2, 0, SpringLayout.SOUTH, topPanel);
185
186 lout.putConstraint(SpringLayout.NORTH, editor, 2, SpringLayout.SOUTH, lbl2);
187 lout.putConstraint(SpringLayout.WEST, editor, 2, SpringLayout.WEST, panel);
188 lout.putConstraint(SpringLayout.SOUTH, panel, 2, SpringLayout.SOUTH, editor);
189 lout.putConstraint(SpringLayout.EAST, panel, 2, SpringLayout.EAST, editor);
190
191
192 ErrorSupport support = new ErrorSupport(getConfigurator());
193 support.addError("shPortInvalid", getString("errors.shPortInvalid"));
194 edShPort.setInputVerifier(new PortVerifier(support, "shPortInvalid"));
195 edKeystorePas.setInputVerifier(new PasswordVerifier(support, 6, 32));
196 watch(edShPort);
197 watch(this.ipField);
198
199 return panel;
200 }
201
202
203
204
205
206
207
208 private JPanel buildHttpsPanel(GridBagLayout layout) {
209 String title = getString("pnlHttps.title");
210 JPanel panel = SwingTools.getGroupPanel(title, layout);
211
212
213 GridBagConstraints gc = new GridBagConstraints();
214 gc.gridwidth = 2;
215 gc.weightx = 1.0;
216 gc.anchor = GridBagConstraints.LINE_START;
217 gc.fill = GridBagConstraints.BOTH;
218
219
220
221 edKeystoreDir.setMode(FileChooserField.DIRECTORIES_ONLY);
222
223 SwingTools.addField(panel, getString("pnlHttps.lblKsLocation"), edKeystoreDir, 1);
224 SwingTools.addField(panel, getString("pnlHttps.lblKsPassword"), edKeystorePas, 2);
225
226 watch(edKeystorePas);
227 watch(edKeystoreDir);
228
229 return panel;
230 }
231
232
233
234
235 public void getValues(ConfiguratorModel model) {
236 TomcatConfigModel tcm = (TomcatConfigModel)model;
237
238 boolean flag = tcm.isHttpsEnabled();
239 editor.setConnectors(tcm.getConnectors());
240 edShPort.setText(Integer.toString(tcm.getShutdownPort()));
241 this.ipField.setSelectedItem(tcm.getIp());
242 edKeystoreDir.setValue(tcm.getKeystoreDir());
243 edKeystorePas.setText(tcm.getKeystorePassword());
244 edKeystoreDir.setEnabled(flag);
245 edKeystorePas.setEnabled(flag);
246
247 setModified(this, false);
248 }
249
250
251
252
253 public void setValues(ConfiguratorModel model) {
254 TomcatConfigModel tcm = (TomcatConfigModel)model;
255
256 try {
257 tcm.setIp((String)this.ipField.getSelectedItem());
258 tcm.setShutdownPort(edShPort.getText());
259 tcm.setConnectors(editor.getConnectors());
260 tcm.setKeystoreDir(edKeystoreDir.getFile());
261 tcm.setKeystorePassword(edKeystorePas.getText());
262 setModified(this, false);
263 } catch (Exception e) {
264 log.warn("Error setting the values into the model", e);
265 String title = UI.getString("app.title");
266 JOptionPane.showMessageDialog(this, e.getMessage(), title,
267 JOptionPane.ERROR_MESSAGE);
268 }
269
270 }
271
272
273
274
275
276
277
278 private class ConnectorChangeCallback implements Runnable {
279
280
281
282
283
284
285 public void run() {
286 TomcatSwingView.this.setModified(editor, true);
287
288 boolean flag = editor.containSecureConnector();
289 edKeystoreDir.setEnabled(flag);
290 edKeystorePas.setEnabled(flag);
291 }
292 }
293 }