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.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  
36  
37  
38  
39  
40  public class ConnectorField extends Field implements ConsoleViewAction {
41    
42  
43  
44    private String keyMap;
45  
46    
47  
48  
49    private int index;
50  
51    
52  
53  
54    private Connector c;
55  
56    
57  
58  
59  
60  
61  
62  
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  
73  
74  
75  
76    public Connector getConnector() {
77      return c;
78    }
79  
80    
81  
82  
83  
84  
85    public void setIndex(int i) {
86      this.index = i;
87    }
88  
89    
90  
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 
110 
111 
112 
113 
114 
115 
116 
117   public String getKeyMapping() {
118     return this.keyMap + "." + (index + 1);
119   }
120 
121   
122 
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 
175 
176 
177 
178 
179 
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 }