KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > hostmanager > HTMLHostManagerServlet


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

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

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

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

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

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

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

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

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

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

361     // These HTML sections are broken in relatively small sections, because of
362
// limited number of subsitutions MessageFormat can process
363
// (maximium of 10).
364

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