KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > manager > host > HTMLHostManagerServlet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.manager.host;
20
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.TreeMap JavaDoc;
28
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.catalina.Container;
34 import org.apache.catalina.Host;
35 import org.apache.catalina.util.ServerInfo;
36
37 /**
38 * Servlet that enables remote management of the virtual hosts deployed
39 * on the server. Normally, this functionality will be protected by a security
40 * constraint in the web application deployment descriptor. However,
41 * this requirement can be relaxed during testing.
42 * <p>
43 * The difference between the <code>HostManagerServlet</code> and this
44 * Servlet is that this Servlet prints out a HTML interface which
45 * makes it easier to administrate.
46 * <p>
47 * However if you use a software that parses the output of
48 * <code>HostManagerServlet</code> you won't be able to upgrade
49 * to this Servlet since the output are not in the
50 * same format as from <code>HostManagerServlet</code>
51 *
52 * @author Bip Thelin
53 * @author Malcolm Edgar
54 * @author Glenn L. Nielsen
55 * @author Peter Rossbach
56 * @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
57 * @see ManagerServlet
58 */

59
60 public final class HTMLHostManagerServlet extends HostManagerServlet {
61
62     // --------------------------------------------------------- Public Methods
63

64     /**
65      * Process a GET request for the specified resource.
66      *
67      * @param request The servlet request we are processing
68      * @param response The servlet response we are creating
69      *
70      * @exception IOException if an input/output error occurs
71      * @exception ServletException if a servlet-specified error occurs
72      */

73     public void doGet(HttpServletRequest JavaDoc request,
74                       HttpServletResponse JavaDoc response)
75         throws IOException JavaDoc, ServletException JavaDoc {
76
77         // Identify the request parameters that we need
78
String JavaDoc command = request.getPathInfo();
79
80         String JavaDoc name = request.getParameter("name");
81  
82         // Prepare our output writer to generate the response message
83
response.setContentType("text/html; charset=" + Constants.CHARSET);
84
85         String JavaDoc message = "";
86         // Process the requested command
87
if (command == null) {
88         } else if (command.equals("/add")) {
89             message = add(request, name);
90         } else if (command.equals("/remove")) {
91             message = remove(name);
92         } else if (command.equals("/list")) {
93         } else if (command.equals("/start")) {
94             message = start(name);
95         } else if (command.equals("/stop")) {
96             message = stop(name);
97         } else {
98             message =
99                 sm.getString("hostManagerServlet.unknownCommand", command);
100         }
101
102         list(request, response, message);
103     }
104
105     
106     /**
107      * Add a host using the specified parameters.
108      *
109      * @param name host name
110      */

111     protected String JavaDoc add(HttpServletRequest JavaDoc request,String JavaDoc name) {
112
113         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
114         PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
115
116         super.add(request,printWriter,name,true);
117
118         return stringWriter.toString();
119     }
120
121
122     /**
123      * Remove the specified host.
124      *
125      * @param writer Writer to render results to
126      * @param name host name
127      */

128     protected String JavaDoc remove(String JavaDoc name) {
129
130         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
131         PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
132
133         super.remove(printWriter, name);
134
135         return stringWriter.toString();
136     }
137
138     
139     /**
140      * Start the host with the specified name.
141      *
142      * @param name Host name
143      */

144     protected String JavaDoc start(String JavaDoc name) {
145
146         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
147         PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
148
149         super.start(printWriter, name);
150
151         return stringWriter.toString();
152     }
153
154     
155     /**
156      * Stop the host with the specified name.
157      *
158      * @param name Host name
159      */

160     protected String JavaDoc stop(String JavaDoc name) {
161
162         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
163         PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
164
165         super.stop(printWriter, name);
166
167         return stringWriter.toString();
168     }
169
170     
171     /**
172      * Render a HTML list of the currently active Contexts in our virtual host,
173      * and memory and server status information.
174      *
175      * @param request The request
176      * @param response The response
177      * @param message a message to display
178      */

179     public void list(HttpServletRequest JavaDoc request,
180                      HttpServletResponse JavaDoc response,
181                      String JavaDoc message) throws IOException JavaDoc {
182
183         PrintWriter JavaDoc writer = response.getWriter();
184
185         // HTML Header Section
186
writer.print(Constants.HTML_HEADER_SECTION);
187
188         // Body Header Section
189
Object JavaDoc[] args = new Object JavaDoc[2];
190         args[0] = request.getContextPath();
191         args[1] = sm.getString("htmlHostManagerServlet.title");
192         writer.print(MessageFormat.format
193                      (Constants.BODY_HEADER_SECTION, args));
194
195         // Message Section
196
args = new Object JavaDoc[3];
197         args[0] = sm.getString("htmlHostManagerServlet.messageLabel");
198         args[1] = (message == null || message.length() == 0) ? "OK" : message;
199         writer.print(MessageFormat.format(Constants.MESSAGE_SECTION, args));
200
201         // Manager Section
202
args = new Object JavaDoc[9];
203         args[0] = sm.getString("htmlHostManagerServlet.manager");
204         args[1] = response.encodeURL(request.getContextPath() + "/html/list");
205         args[2] = sm.getString("htmlHostManagerServlet.list");
206         args[3] = response.encodeURL
207             (request.getContextPath() + "/" +
208              sm.getString("htmlHostManagerServlet.helpHtmlManagerFile"));
209         args[4] = sm.getString("htmlHostManagerServlet.helpHtmlManager");
210         args[5] = response.encodeURL
211             (request.getContextPath() + "/" +
212              sm.getString("htmlHostManagerServlet.helpManagerFile"));
213         args[6] = sm.getString("htmlHostManagerServlet.helpManager");
214         args[7] = response.encodeURL("/manager/status");
215         args[8] = sm.getString("statusServlet.title");
216         writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args));
217
218          // Hosts Header Section
219
args = new Object JavaDoc[3];
220         args[0] = sm.getString("htmlHostManagerServlet.hostName");
221         args[1] = sm.getString("htmlHostManagerServlet.hostAliases");
222         args[2] = sm.getString("htmlHostManagerServlet.hostTasks");
223         writer.print(MessageFormat.format(HOSTS_HEADER_SECTION, args));
224
225         // Hosts Row Section
226
// Create sorted map of host names.
227
Container[] children = engine.findChildren();
228         String JavaDoc hostNames[] = new String JavaDoc[children.length];
229         for (int i = 0; i < children.length; i++)
230             hostNames[i] = children[i].getName();
231
232         TreeMap JavaDoc sortedHostNamesMap = new TreeMap JavaDoc();
233
234         for (int i = 0; i < hostNames.length; i++) {
235             String JavaDoc displayPath = hostNames[i];
236             sortedHostNamesMap.put(displayPath, hostNames[i]);
237         }
238
239         String JavaDoc hostsStart = sm.getString("htmlHostManagerServlet.hostsStart");
240         String JavaDoc hostsStop = sm.getString("htmlHostManagerServlet.hostsStop");
241         String JavaDoc hostsRemove = sm.getString("htmlHostManagerServlet.hostsRemove");
242
243         Iterator JavaDoc iterator = sortedHostNamesMap.entrySet().iterator();
244         while (iterator.hasNext()) {
245             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
246             String JavaDoc hostName = (String JavaDoc) entry.getKey();
247             Host host = (Host) engine.findChild(hostName);
248
249             if (host != null ) {
250                 args = new Object JavaDoc[2];
251                 args[0] = hostName;
252                 String JavaDoc[] aliases = host.findAliases();
253                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
254                 if (aliases.length > 0) {
255                     buf.append(aliases[0]);
256                     for (int j = 1; j < aliases.length; j++) {
257                         buf.append(", ").append(aliases[j]);
258                     }
259                 }
260
261                 if (buf.length() == 0) {
262                     buf.append("&nbsp;");
263                 }
264
265                 args[1] = buf.toString();
266                 writer.print
267                     (MessageFormat.format(HOSTS_ROW_DETAILS_SECTION, args));
268
269                 args = new Object JavaDoc[7];
270                 args[0] = response.encodeURL
271                     (request.getContextPath() +
272                      "/html/start?name=" + hostName);
273                 args[1] = hostsStart;
274                 args[2] = response.encodeURL
275                     (request.getContextPath() +
276                      "/html/stop?name=" + hostName);
277                 args[3] = hostsStop;
278                 args[4] = response.encodeURL
279                     (request.getContextPath() +
280                      "/html/remove?name=" + hostName);
281                 args[5] = hostsRemove;
282                 args[6] = hostName;
283                 if (host == this.host) {
284                     writer.print(MessageFormat.format(
285                         MANAGER_HOST_ROW_BUTTON_SECTION, args));
286                 } else {
287                     writer.print(MessageFormat.format(
288                         HOSTS_ROW_BUTTON_SECTION, args));
289                 }
290
291             }
292         }
293
294         // Add Section
295
args = new Object JavaDoc[6];
296         args[0] = sm.getString("htmlHostManagerServlet.addTitle");
297         args[1] = sm.getString("htmlHostManagerServlet.addHost");
298         args[2] = response.encodeURL(request.getContextPath() + "/html/add");
299         args[3] = sm.getString("htmlHostManagerServlet.addName");
300         args[4] = sm.getString("htmlHostManagerServlet.addAliases");
301         args[5] = sm.getString("htmlHostManagerServlet.addAppBase");
302         writer.print(MessageFormat.format(ADD_SECTION_START, args));
303  
304         args = new Object JavaDoc[3];
305         args[0] = sm.getString("htmlHostManagerServlet.addAutoDeploy");
306         args[1] = "autoDeploy";
307         args[2] = "checked";
308         writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
309         args[0] = sm.getString("htmlHostManagerServlet.addDeployOnStartup");
310         args[1] = "deployOnStartup";
311         args[2] = "checked";
312         writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
313         args[0] = sm.getString("htmlHostManagerServlet.addDeployXML");
314         args[1] = "deployXML";
315         args[2] = "checked";
316         writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
317         args[0] = sm.getString("htmlHostManagerServlet.addUnpackWARs");
318         args[1] = "unpackWARs";
319         args[2] = "checked";
320         writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
321         args[0] = sm.getString("htmlHostManagerServlet.addXmlNamespaceAware");
322         args[1] = "xmlNamespaceAware";
323         args[2] = "";
324         writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
325         args[0] = sm.getString("htmlHostManagerServlet.addXmlValidation");
326         args[1] = "xmlValidation";
327         args[2] = "";
328         writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
329
330         args[0] = sm.getString("htmlHostManagerServlet.addManager");
331         args[1] = "manager";
332         args[2] = "checked";
333         writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
334         
335         args = new Object JavaDoc[1];
336         args[0] = sm.getString("htmlHostManagerServlet.addButton");
337         writer.print(MessageFormat.format(ADD_SECTION_END, args));
338
339         // Server Header Section
340
args = new Object JavaDoc[7];
341         args[0] = sm.getString("htmlHostManagerServlet.serverTitle");
342         args[1] = sm.getString("htmlHostManagerServlet.serverVersion");
343         args[2] = sm.getString("htmlHostManagerServlet.serverJVMVersion");
344         args[3] = sm.getString("htmlHostManagerServlet.serverJVMVendor");
345         args[4] = sm.getString("htmlHostManagerServlet.serverOSName");
346         args[5] = sm.getString("htmlHostManagerServlet.serverOSVersion");
347         args[6] = sm.getString("htmlHostManagerServlet.serverOSArch");
348         writer.print(MessageFormat.format
349                      (Constants.SERVER_HEADER_SECTION, args));
350
351         // Server Row Section
352
args = new Object JavaDoc[6];
353         args[0] = ServerInfo.getServerInfo();
354         args[1] = System.getProperty("java.runtime.version");
355         args[2] = System.getProperty("java.vm.vendor");
356         args[3] = System.getProperty("os.name");
357         args[4] = System.getProperty("os.version");
358         args[5] = System.getProperty("os.arch");
359         writer.print(MessageFormat.format(Constants.SERVER_ROW_SECTION, args));
360
361         // HTML Tail Section
362
writer.print(Constants.HTML_TAIL_SECTION);
363
364         // Finish up the response
365
writer.flush();
366         writer.close();
367     }
368
369     
370     // ------------------------------------------------------ Private Constants
371

372     // These HTML sections are broken in relatively small sections, because of
373
// limited number of subsitutions MessageFormat can process
374
// (maximium of 10).
375

376     private static final String JavaDoc HOSTS_HEADER_SECTION =
377         "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n" +
378         "<tr>\n" +
379         " <td colspan=\"5\" class=\"title\">{0}</td>\n" +
380         "</tr>\n" +
381         "<tr>\n" +
382         " <td class=\"header-left\"><small>{0}</small></td>\n" +
383         " <td class=\"header-center\"><small>{1}</small></td>\n" +
384         " <td class=\"header-center\"><small>{2}</small></td>\n" +
385         "</tr>\n";
386
387     private static final String JavaDoc HOSTS_ROW_DETAILS_SECTION =
388         "<tr>\n" +
389         " <td class=\"row-left\"><small><a HREF=\"http://{0}\">{0}</a>" +
390         "</small></td>\n" +
391         " <td class=\"row-center\"><small>{1}</small></td>\n";
392
393     private static final String JavaDoc MANAGER_HOST_ROW_BUTTON_SECTION =
394         " <td class=\"row-left\">\n" +
395         " <small>\n" +
396         " &nbsp;{1}&nbsp;\n" +
397         " &nbsp;{3}&nbsp;\n" +
398         " &nbsp;{5}&nbsp;\n" +
399         " </small>\n" +
400         " </td>\n" +
401         "</tr>\n";
402
403     private static final String JavaDoc HOSTS_ROW_BUTTON_SECTION =
404         " <td class=\"row-left\" NOWRAP>\n" +
405         " <small>\n" +
406         " &nbsp;<a HREF=\"{0}\" onclick=\"return(confirm(''{1} {6}\\n\\nAre you sure?''))\">{1}</a>&nbsp;\n" +
407         " &nbsp;<a HREF=\"{2}\" onclick=\"return(confirm(''{3} {6}\\n\\nAre you sure?''))\">{3}</a>&nbsp;\n" +
408         " &nbsp;<a HREF=\"{4}\" onclick=\"return(confirm(''{5} {6}\\n\\nAre you sure?''))\">{5}</a>&nbsp;\n" +
409         " </small>\n" +
410         " </td>\n" +
411         "</tr>\n";
412
413     private static final String JavaDoc ADD_SECTION_START =
414         "</table>\n" +
415         "<br>\n" +
416         "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n" +
417         "<tr>\n" +
418         " <td colspan=\"2\" class=\"title\">{0}</td>\n" +
419         "</tr>\n" +
420         "<tr>\n" +
421         " <td colspan=\"2\" class=\"header-left\"><small>{1}</small></td>\n" +
422         "</tr>\n" +
423         "<tr>\n" +
424         " <td colspan=\"2\">\n" +
425         "<form method=\"get\" action=\"{2}\">\n" +
426         "<table cellspacing=\"0\" cellpadding=\"3\">\n" +
427         "<tr>\n" +
428         " <td class=\"row-right\">\n" +
429         " <small>{3}</small>\n" +
430         " </td>\n" +
431         " <td class=\"row-left\">\n" +
432         " <input type=\"text\" name=\"name\" size=\"20\">\n" +
433         " </td>\n" +
434         "</tr>\n" +
435         "<tr>\n" +
436         " <td class=\"row-right\">\n" +
437         " <small>{4}</small>\n" +
438         " </td>\n" +
439         " <td class=\"row-left\">\n" +
440         " <input type=\"text\" name=\"aliases\" size=\"64\">\n" +
441         " </td>\n" +
442         "</tr>\n" +
443         "<tr>\n" +
444         " <td class=\"row-right\">\n" +
445         " <small>{5}</small>\n" +
446         " </td>\n" +
447         " <td class=\"row-left\">\n" +
448         " <input type=\"text\" name=\"appBase\" size=\"64\">\n" +
449         " </td>\n" +
450         "</tr>\n" ;
451     
452         private static final String JavaDoc ADD_SECTION_BOOLEAN =
453         "<tr>\n" +
454         " <td class=\"row-right\">\n" +
455         " <small>{0}</small>\n" +
456         " </td>\n" +
457         " <td class=\"row-left\">\n" +
458         " <input type=\"checkbox\" name=\"{1}\" {2}>\n" +
459         " </td>\n" +
460         "</tr>\n" ;
461         
462         private static final String JavaDoc ADD_SECTION_END =
463         "<tr>\n" +
464         " <td class=\"row-right\">\n" +
465         " &nbsp;\n" +
466         " </td>\n" +
467         " <td class=\"row-left\">\n" +
468         " <input type=\"submit\" value=\"{0}\">\n" +
469         " </td>\n" +
470         "</tr>\n" +
471          "</table>\n" +
472         "</form>\n" +
473         "</td>\n" +
474         "</tr>\n" +
475         "</table>\n" +
476         "<br>\n" +
477         "\n";
478
479 }
480
Popular Tags