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 04-mar-2004
21   *
22   * Copyright (c)2003 Grid Systems
23   */
24  package com.gridsystems.config.modules.tomcat;
25  
26  import java.io.File;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import com.gridsystems.config.Configurator;
31  import com.gridsystems.config.ConfiguratorModel;
32  import com.gridsystems.config.tools.ErrorSupport;
33  import com.gridsystems.config.tools.IpVerifier;
34  import com.gridsystems.config.tools.PasswordVerifier;
35  import com.gridsystems.config.tools.PortVerifier;
36  import com.gridsystems.config.tools.console.AbstractConsoleView;
37  import com.gridsystems.config.tools.console.GroupField;
38  import com.gridsystems.config.tools.console.PasswordField;
39  import com.gridsystems.config.tools.console.ValueField;
40  
41  /**
42   * Console View for the Tomcat configurator.
43   *
44   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
45   * @version 1.0
46   */
47  public class TomcatConsoleView extends AbstractConsoleView {
48  
49    /**
50     * IP Address field.
51     */
52    private ValueField ipField = null;
53  
54    /**
55     * Shutdown port field.
56     */
57    ValueField fldShutdownPort;
58  
59    /**
60     * Keystore directory field.
61     */
62    ValueField fldKeystoreDir;
63  
64    /**
65     * Keystore password field.
66     */
67    PasswordField fldKeystorePass;
68  
69    /**
70     * List of connector edition fields.
71     */
72    ConnectorGroupField grpConns;
73  
74    /**
75     * Creates a new instance of the view.
76     *
77     * @param config The configurator to bind to
78     */
79    public TomcatConsoleView(Configurator config) {
80      super(config);
81  
82      String numPattern = "{0,number,0}";
83      ErrorSupport support = new ErrorSupport(this.getConfigurator());
84      support.addError("badIP", getString("errors.badIP"));
85      support.addError("loopbackIP", getString("errors.loopback.ip.short"));
86      ipField = createValueField("field.num.ip", null);
87      IpVerifier ipVerifier =
88        new IpVerifier(support, "badIP", TomcatConfigModel.ALLOWED_IP_TYPE);
89      ipVerifier.setLoopbackAddressAllowed(false);
90      ipVerifier.setErrorLoopbackId("loopbackIP");
91      ipField.setVerifier(ipVerifier);
92  
93      fldShutdownPort = createValueField("fldShutdownPort", numPattern);
94      fldKeystoreDir = createValueField("fldKeystoreDir", null);
95      fldKeystorePass = createPasswordField("fldKeystorePass");
96  
97      this.addKeyMapping(bundle.getString("field.num.ip.keyMap"), this.ipField);
98  
99      grpConns = new ConnectorGroupField(bundle);
100   }
101 
102   /**
103    * {@inheritDoc}
104    */
105   public String getTitle() {
106     return bundle.getString("config.name");
107   }
108 
109   /**
110    * {@inheritDoc}
111    */
112   public String getSubtitle() {
113     return bundle.getString("config.description");
114   }
115 
116   /**
117    * Removes a connector from the internal list.
118    *
119    * @param i  the position of the connector to remove
120    */
121   public void removeConnector(int i) {
122     // Removes the field from the list
123     grpConns.removeField(i);
124 
125     // Recalculates field key mappings
126     removeKeyMapping((GroupField)grpConns);
127     List fields = grpConns.getFields();
128     for (int pos = i; pos < fields.size(); pos++) {
129       ConnectorField cf = (ConnectorField)fields.get(pos);
130       cf.setIndex(pos);
131     }
132     addKeyMapping((GroupField)grpConns);
133   }
134 
135   /**
136    * Creates a new connector and adds it to the list.
137    */
138   public void addConnector() {
139     int port = 0;
140     for (Iterator it = grpConns.getFields().iterator(); it.hasNext();) {
141       ConnectorField f = (ConnectorField)it.next();
142       Connector cc = f.getConnector();
143       port = Math.max(port, cc.getPort());
144     }
145 
146     Connector c = new Connector(getString("ConnectorEditor.unnamed.text"),
147       port + 1, Connector.HTTP);
148     ConnectorField cf = new ConnectorField(bundle, c, grpConns.getFields().size(),
149                                           bundle.getString("fldConnectorsList.keyMap"));
150     grpConns.addField(cf);
151     addKeyMapping(cf);
152   }
153 
154   /**
155    * {@inheritDoc}
156    */
157   public void getValues(ConfiguratorModel model) {
158     TomcatConfigModel m = (TomcatConfigModel)model;
159     this.ipField.setValue(m.getIp());
160 
161     Connector[] conns = m.getConnectors();
162 
163     grpConns.clearFields();
164     for (int i = 0; i < conns.length; i++) {
165       grpConns.addField(
166           new ConnectorField(this.bundle, conns[i], i,
167               this.bundle.getString("fldConnectorsList.keyMap")));
168     }
169 
170     fldShutdownPort.setValue(new Integer(m.getShutdownPort()));
171     fldKeystoreDir.setValue(m.getKeystoreDir().getAbsolutePath());
172     fldKeystorePass.setValue(m.getKeystorePassword());
173 
174   }
175 
176   /**
177    * {@inheritDoc}
178    */
179   public void setValues(ConfiguratorModel model) {
180     TomcatConfigModel m = (TomcatConfigModel)model;
181 
182     try {
183       m.setIp((String) this.ipField.getValue());
184       m.setConnectors(grpConns.getConnectors());
185 
186       m.setShutdownPort(((Number)fldShutdownPort.getValue()).intValue());
187 
188       m.setKeystoreDir(new File(fldKeystoreDir.getValue().toString()));
189       m.setKeystorePassword(fldKeystorePass.getValue().toString());
190     } catch (Exception e) {
191     }
192   }
193 
194   /**
195    * {@inheritDoc}
196    */
197   protected void getContents(List<Object> list) {
198     list.add(this.ipField);
199     ErrorSupport support = new ErrorSupport(getConfigurator());
200     support.addError("shPortInvalid", getString("errors.shPortInvalid", null));
201 
202     PortVerifier verShut  = new PortVerifier(support, "shPortInvalid");
203     PasswordVerifier verPass = new PasswordVerifier(support, 6, 32);
204 
205     fldShutdownPort.setVerifier(verShut);
206     fldKeystorePass.setVerifier(verPass);
207 
208     GroupField grpHttp = new GroupField(bundle, "pnlHttp.title");
209     GroupField grpHttps = new GroupField(bundle, "pnlHttps.title");
210 
211     grpHttp.addField(fldShutdownPort);
212     grpHttp.addField(grpConns);
213 
214     grpHttps.addField(fldKeystoreDir);
215     grpHttps.addField(fldKeystorePass);
216 
217     list.add(grpHttp);
218     list.add("-");
219     list.add(grpHttps);
220 
221     addKeyMapping((GroupField)grpConns);
222     addKeyMapping(fldShutdownPort);
223     addKeyMapping(fldKeystoreDir);
224     addKeyMapping(fldKeystorePass);
225   }
226 }