View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  /*
19   * Project: KernelConfigurator
20   * Created on 26-feb-2004
21   *
22   * Copyright (c)2003 Grid Systems
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   * Swing View for the Tomcat configurator.
55   *
56   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>, Xmas
57   * @version 1.0
58   */
59  public class TomcatSwingView extends BasePanel {
60    /**
61     * Large text fields width in characters.
62     */
63    public static final int LARGE_FIELD_LENGTH = 10;
64  
65    /**
66     * Normal text fields width in characters.
67     */
68    public static final int NORMAL_FIELD_LENGTH = 5;
69  
70    /**
71     * Class logger.
72     */
73    private static Log log = LogFactory.getLog(TomcatSwingView.class);
74  
75    /**
76     * Host IP Address field.
77     */
78    private JComboBox ipField = new JComboBox(new String[] { });
79  
80    /**
81     * Shutdown port.
82     */
83    JTextField edShPort = new JTextField(NORMAL_FIELD_LENGTH);
84  
85    /**
86     * HTTP port.
87     */
88    ConnectorEditor editor = new ConnectorEditor();
89  
90    /**
91     * Keystore directory.
92     */
93    FileChooserField edKeystoreDir = new FileChooserField();
94  
95    /**
96     * Keystore password.
97     */
98    JTextField edKeystorePas = new JPasswordField(LARGE_FIELD_LENGTH);
99  
100   /**
101    * Default constructor.
102    *
103    * @param config The configurator this view is associated to
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    * Component initialization code.
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     // Init IpField
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    * Builds a panel with HTTP configuration fields.
156    *
157    * @param layout A global layout instance to use
158    * @return The panel
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     // Top panel
180     lout.putConstraint(SpringLayout.WEST, topPanel, 2, SpringLayout.WEST, panel);
181     lout.putConstraint(SpringLayout.NORTH, topPanel, 0, SpringLayout.NORTH, panel);
182     // label 2
183     lout.putConstraint(SpringLayout.WEST, lbl2, 2, SpringLayout.WEST, panel);
184     lout.putConstraint(SpringLayout.NORTH, lbl2, 0, SpringLayout.SOUTH, topPanel);
185     // Editor
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     // Sets the validators
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    * Builds the panel for HTTPS fields.
204    *
205    * @param layout A global layout to use
206    * @return The panel
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 //    JComponent[] guarded = { edHttpsPort, edKeystoreDir, edKeystorePas };
219 //    setGuarded(cbxHttpsEnabled, guarded);
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    * {@inheritDoc}
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    * {@inheritDoc}
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    * Invoked on every change in the editor data.
274    *
275    * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
276    * @version 1.0
277    */
278   private class ConnectorChangeCallback implements Runnable {
279     /**
280      * Synchronizes the "modified" state of the panel, and the "enabled" state
281      * of the HTTPS Support fields.
282      *
283      * {@inheritDoc}
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 }