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 07-oct-2004
21   *
22   * Copyright (c)2004 Grid Systems
23   */
24  package com.gridsystems.config.modules.tomcat;
25  
26  import java.io.IOException;
27  import java.util.ResourceBundle;
28  
29  import com.gridsystems.config.ConsoleConfiguratorView;
30  import com.gridsystems.config.tools.console.ConsoleTools;
31  import com.gridsystems.config.tools.console.ConsoleViewAction;
32  import com.gridsystems.config.tools.console.Field;
33  
34  /**
35   * Type description.
36   *
37   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
38   * @version 1.0
39   */
40  public class ConnectorField extends Field implements ConsoleViewAction {
41    /**
42     * The "super" key map.
43     */
44    private String keyMap;
45  
46    /**
47     * The subindex for this item.
48     */
49    private int index;
50  
51    /**
52     * The connector list being edited by this instance.
53     */
54    private Connector c;
55  
56    /**
57     * Creates a new instance.
58     *
59     * @param bundle  the bundle for i18n
60     * @param c       the connector to edit
61     * @param index   the subindex
62     * @param keyMap  the "container" keymap
63     */
64    public ConnectorField(ResourceBundle bundle, Connector c, int index, String keyMap) {
65      super(bundle);
66      this.c = c;
67      this.keyMap = keyMap;
68      this.index = index;
69    }
70  
71    /**
72     * Gets the wrapped connector instance.
73     *
74     * @return  the connector instance
75     */
76    public Connector getConnector() {
77      return c;
78    }
79  
80    /**
81     * Sets the index of this instance. Used on field removal.
82     *
83     * @param i  the new index
84     */
85    public void setIndex(int i) {
86      this.index = i;
87    }
88  
89    /**
90     * {@inheritDoc}
91     */
92    public String[] getContents() {
93      StringBuffer sb = new StringBuffer();
94      sb.append("    ");
95      sb.append(getKeyMapping());
96      sb.append("       [ ").append(c.getName());
97      while (sb.length() < 30) {
98        sb.append(' ');
99      }
100     sb.append(" - ");
101     sb.append(c.getProtocol());
102     sb.append(" - ");
103     sb.append(c.getPort());
104     sb.append(" ]");
105     return new String[] { sb.toString() };
106   }
107 
108   /**
109    * Gets the keyMap to use for this instance.
110    * <p>
111    * Instances of this class will be presented as list items. Each instance will
112    * build its keyMap from a combination of the "parent keyMap", and an "index" with
113    * its position in the list.
114    *
115    * {@inheritDoc}
116    */
117   public String getKeyMapping() {
118     return this.keyMap + "." + (index + 1);
119   }
120 
121   /**
122    * {@inheritDoc}
123    */
124   public boolean execute(ConsoleConfiguratorView view) {
125     while (true) {
126       try {
127         boolean canDelete = true;
128         if (((TomcatConsoleView)view).grpConns.getConnectors().length == 1) {
129           canDelete = false;
130         }
131         String op;
132         if (canDelete) {
133           op = readLine("Operation [R: remove, E: edit]", "E");
134           op = op.toUpperCase();
135         } else {
136           op = "E";
137         }
138         if (op.equals("E")) {
139           if (c.isUserCreated()) {
140             String name = readLine("Connector name", c.getName());
141             String protocol = readLine("Protocol", c.getProtocol()).trim();
142             String sport = readLine("Port", "" + c.getPort());
143 
144             String[] availProtocols = Connector.getAvailableProtocols();
145             for (int i = 0; i < availProtocols.length; i++) {
146               if (availProtocols[i].equalsIgnoreCase(protocol)) {
147                 c.setProtocol(protocol.toLowerCase());
148                 break;
149               }
150             }
151 
152             int port = c.getPort();
153             try {
154               port = Integer.parseInt(sport);
155             } catch (Exception e) {
156             }
157 
158             c.setName(name);
159             c.setPort(port);
160           } else {
161             String sport = readLine("Port", "" + c.getPort());
162             int port = Integer.parseInt(sport);
163             c.setPort(port);
164           }
165         } else if (op.equals("R")) {
166           ((TomcatConsoleView)view).removeConnector(this.index);
167         }
168         return true;
169       } catch (IOException e) { }
170     }
171   }
172 
173   /**
174    * Reads a line of text from the console.
175    *
176    * @param prompt The prompt to show
177    * @param defValue The default text for empty lines
178    * @return The read text
179    * @throws IOException If an error occurs while reading from the console
180    */
181   protected String readLine(String prompt, String defValue) throws IOException {
182     String comment = prompt + " [" + defValue + "]: ";
183     String line = ConsoleTools.readLine(comment);
184     return (line == null || line.length() == 0) ? defValue : line;
185   }
186 }