/* * $Header: /home/cvs/jakarta-tomcat-catalina/webapps/manager/WEB-INF/classes/org/apache/catalina/manager/HTMLManagerServlet.java,v 1.9 2003/11/03 22:01:38 remm Exp $ * $Revision: 1.9 $ * $Date: 2003/11/03 22:01:38 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * * [Additional notices, if required by prior licensing conditions] * */ package org.apache.catalina.manager; import java.io.IOException; import java.io.PrintWriter; import java.net.URL; import java.text.MessageFormat; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; import javax.servlet.ServletException; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.catalina.Container; import org.apache.catalina.Context; import org.apache.catalina.Deployer; import org.apache.catalina.util.ServerInfo; /** * Servlet that extends {@link HTMLManagerServlet} allows administrator * to choose any virtual host on the Tomcat which you want to start or deploy * (Only 'localhost' can be managed by HTMLManagerServlet). * All commnads are same as in HTMLManagerServlet. * Normally, this function will be protected by a security constraint * in the web application deployment descriptor. Only 'admin' role * can access it by default. * * @author TANAKA Yoshihiro * @version $Revision: 1.9 $, $Date: 2003/11/03 22:01:38 $ * @see MultihostManagerServlet */ public class MultihostHTMLManagerServlet extends HTMLManagerServlet { // --------------------------------------------------------- Public Methods /** * Process a GET request for the specified resource * after finding the appropriate deployer of the requested virtual host. * When unknown virtual host is requested, ignore it and * display an error message. * * @param request The servlet request we are processing * @param response The servlet response we are creating * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet-specified error occurs */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Find the deployer of the requested virtual host String host = request.getParameter(VIRTUAL_HOST); Deployer deployer = getDeployer(host); // Unknown virtual host is requested if (deployer == null) { // Prepare our output writer to generate the response message response.setContentType("text/html; charset=" + Constants.CHARSET); String message = sm.getString("managerServlet.unknownHost", host); deployer = getDeployer("localhost"); list(deployer, request, response, message); } else { doGet(deployer, request, response); } } /** * Process a POST request for the specified resource * after finding the appropriate deployer of the requested virtual host. * When unknown virtual host is requested, ignore it and * display an error message. * * @param request The servlet request we are processing * @param response The servlet response we are creating * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet-specified error occurs */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Find the deployer of the requested virtual host String host = request.getParameter(VIRTUAL_HOST); Deployer deployer = getDeployer(host); // Unknown virtual host is requested if (deployer == null) { // Prepare our output writer to generate the response message response.setContentType("text/html; charset=" + Constants.CHARSET); String message = sm.getString("managerServlet.unknownHost", host); deployer = getDeployer("localhost"); list(deployer, request, response, message); } else { doPost(deployer, request, response); } } /** * Render a HTML list of the currently active Contexts in a specified virtual host, * and memory and server status information. * * @param deployer Deployer of the virtual host * @param writer Writer to render to * @param message a message to display */ protected void list(Deployer deployer, HttpServletRequest request, HttpServletResponse response, String message) throws IOException { if (debug >= 1) log("list: Listing contexts for virtual host '" + deployer.getName() + "'"); String host = deployer.getName(); String servletPath = request.getServletPath(); PrintWriter writer = response.getWriter(); // HTML Header Section writer.print(Constants.HTML_HEADER_SECTION); // Body Header Section Object[] args = new Object[2]; args[0] = request.getContextPath(); args[1] = sm.getString("htmlManagerServlet.title"); writer.print(MessageFormat.format (Constants.BODY_HEADER_SECTION, args)); // Message Section args = new Object[3]; args[0] = sm.getString("htmlManagerServlet.messageLabel"); args[1] = (message == null || message.length() == 0) ? "OK" : message; writer.print(MessageFormat.format(Constants.MESSAGE_SECTION, args)); // Manager Section args = new Object[9]; args[0] = sm.getString("htmlManagerServlet.manager"); args[1] = response.encodeURL(request.getContextPath() + servletPath + "/list?" + VIRTUAL_HOST + "=" + host); args[2] = sm.getString("htmlManagerServlet.list"); args[3] = response.encodeURL (request.getContextPath() + "/" + sm.getString("htmlManagerServlet.helpHtmlManagerFile")); args[4] = sm.getString("htmlManagerServlet.helpHtmlManager"); args[5] = response.encodeURL (request.getContextPath() + "/" + sm.getString("htmlManagerServlet.helpManagerFile")); args[6] = sm.getString("htmlManagerServlet.helpManager"); args[7] = response.encodeURL (request.getContextPath() + servletPath + "/status?" + VIRTUAL_HOST + "=" + host); args[8] = sm.getString("statusServlet.title"); writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args)); /*** Choice of virtual host ***/ args = new Object[2]; args[0] = (request.getContextPath() + servletPath + "/list"); args[1] = sm.getString("htmlManagerServlet.virtualHost.choose"); writer.print(MessageFormat.format(VIRTUAL_HOST_HEADER_SECTION, args)); // All hosts Container[] hosts = context.getParent().getParent().findChildren(); for (int i = 0; i < hosts.length; i++) { args = new Object[2]; args[0] = hosts[i].getName(); if (host.equals(args[0])) { args[1] = "selected"; } else { args[1] = ""; } writer.print(MessageFormat.format(VIRTUAL_HOST_DETAIL_SECTION, args)); } args = new Object[1]; args[0] = sm.getString("htmlManagerServlet.virtualHost.button"); writer.print(MessageFormat.format(VIRTUAL_HOST_FOOTER_SECTION, args)); // Apps Header Section args = new Object[6]; args[0] = sm.getString("htmlManagerServlet.appsTitle"); args[1] = sm.getString("htmlManagerServlet.appsPath"); args[2] = sm.getString("htmlManagerServlet.appsName"); args[3] = sm.getString("htmlManagerServlet.appsAvailable"); args[4] = sm.getString("htmlManagerServlet.appsSessions"); args[5] = sm.getString("htmlManagerServlet.appsTasks"); writer.print(MessageFormat.format(APPS_HEADER_SECTION, args)); // Apps Row Section // Create sorted map of deployed applications context paths. String contextPaths[] = deployer.findDeployedApps(); TreeMap sortedContextPathsMap = new TreeMap(); for (int i = 0; i < contextPaths.length; i++) { String displayPath = contextPaths[i]; sortedContextPathsMap.put(displayPath, contextPaths[i]); } String appsStart = sm.getString("htmlManagerServlet.appsStart"); String appsStop = sm.getString("htmlManagerServlet.appsStop"); String appsReload = sm.getString("htmlManagerServlet.appsReload"); String appsUndeploy = sm.getString("htmlManagerServlet.appsUndeploy"); Iterator iterator = sortedContextPathsMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); String displayPath = (String) entry.getKey(); String contextPath = (String) entry.getKey(); Context context = deployer.findDeployedApp(contextPath); if (displayPath.equals("")) { displayPath = "/"; } if (context != null ) { args = new Object[6]; args[0] = displayPath; args[1] = context.getDisplayName(); if (args[1] == null) { args[1] = " "; } args[2] = new Boolean(context.getAvailable()); args[3] = response.encodeURL (request.getContextPath() + servletPath + "/sessions?" + VIRTUAL_HOST + "=" + host + "&path=" + displayPath); if (context.getManager() != null) { args[4] = new Integer (context.getManager().findSessions().length); } else { args[4] = new Integer(0); } /*** Make url for webapps on a virtual host. ***/ StringBuffer url = new StringBuffer(request.getScheme()).append("://").append(host); int port = request.getServerPort(); if (port != 80) { url.append(":").append(Integer.toString(port)); } url.append(displayPath); args[5] = response.encodeURL(url.toString()); writer.print (MessageFormat.format(APPS_ROW_DETAILS_SECTION, args)); args = new Object[8]; args[0] = response.encodeURL (request.getContextPath() + servletPath + "/start?" + VIRTUAL_HOST + "=" + host + "&path=" + displayPath); args[1] = appsStart; args[2] = response.encodeURL (request.getContextPath() + servletPath + "/stop?" + VIRTUAL_HOST + "=" + host + "&path=" + displayPath); args[3] = appsStop; args[4] = response.encodeURL (request.getContextPath() + servletPath + "/reload?" + VIRTUAL_HOST + "=" + host + "&path=" + displayPath); args[5] = appsReload; args[6] = response.encodeURL (request.getContextPath() + servletPath + "/undeploy?" + VIRTUAL_HOST + "=" + host + "&path=" + displayPath); args[7] = appsUndeploy; if (context.getPath().equals(this.context.getPath())) { writer.print(MessageFormat.format( MANAGER_APP_ROW_BUTTON_SECTION, args)); } else if (context.getAvailable()) { writer.print(MessageFormat.format( STARTED_APPS_ROW_BUTTON_SECTION, args)); } else { writer.print(MessageFormat.format( STOPPED_APPS_ROW_BUTTON_SECTION, args)); } } } // Deploy Section args = new Object[9]; args[0] = sm.getString("htmlManagerServlet.deployTitle"); args[1] = sm.getString("htmlManagerServlet.deployServer"); args[2] = response.encodeURL(request.getContextPath() + servletPath + "/deploy"); args[3] = sm.getString("htmlManagerServlet.deployPath"); args[4] = sm.getString("htmlManagerServlet.deployConfig"); args[5] = sm.getString("htmlManagerServlet.deployWar"); args[6] = sm.getString("htmlManagerServlet.deployButton"); args[7] = VIRTUAL_HOST; args[8] = host; writer.print(MessageFormat.format(DEPLOY_SECTION, args)); args = new Object[4]; args[0] = sm.getString("htmlManagerServlet.deployUpload"); args[1] = response.encodeURL(request.getContextPath() + servletPath + "/upload?" + VIRTUAL_HOST + "=" + host); args[2] = sm.getString("htmlManagerServlet.deployUploadFile"); args[3] = sm.getString("htmlManagerServlet.deployButton"); writer.print(MessageFormat.format(UPLOAD_SECTION, args)); // Server Header Section args = new Object[7]; args[0] = sm.getString("htmlManagerServlet.serverTitle"); args[1] = sm.getString("htmlManagerServlet.serverVersion"); args[2] = sm.getString("htmlManagerServlet.serverJVMVersion"); args[3] = sm.getString("htmlManagerServlet.serverJVMVendor"); args[4] = sm.getString("htmlManagerServlet.serverOSName"); args[5] = sm.getString("htmlManagerServlet.serverOSVersion"); args[6] = sm.getString("htmlManagerServlet.serverOSArch"); writer.print(MessageFormat.format (Constants.SERVER_HEADER_SECTION, args)); // Server Row Section args = new Object[6]; args[0] = ServerInfo.getServerInfo(); args[1] = System.getProperty("java.runtime.version"); args[2] = System.getProperty("java.vm.vendor"); args[3] = System.getProperty("os.name"); args[4] = System.getProperty("os.version"); args[5] = System.getProperty("os.arch"); writer.print(MessageFormat.format(Constants.SERVER_ROW_SECTION, args)); // HTML Tail Section writer.print(Constants.HTML_TAIL_SECTION); // Finish up the response writer.flush(); writer.close(); } // ------------------------------------------------------ Private Constants // These HTML sections are broken in relatively small sections, because of // limited number of subsitutions MessageFormat can process // (maximium of 10). protected static final String VIRTUAL_HOST_HEADER_SECTION = "
\n" + " {1}\n" + " \n" + " \n" + "
\n"; protected static final String APPS_ROW_DETAILS_SECTION = "\n" + " {0}" + "\n" + " {1}\n" + " {2}\n" + " " + "{4}\n"; protected static final String DEPLOY_SECTION = "\n" + "
\n" + "\n" + "\n" + " \n" + "\n" + "\n" + " \n" + "\n" + "\n" + " \n" + "\n"; }
{0}
{1}
\n" + "
\n" + "\n" + "\n" + " \n" + " \n" + "\n" + "\n" + " \n" + " \n" + "\n" + "\n" + " \n" + " \n" + "\n" + "\n" + " \n" + " \n" + "\n" + "
\n" + " {3}\n" + " \n" + " \n" + "
\n" + " {4}\n" + " \n" + " \n" + "
\n" + " {5}\n" + " \n" + " \n" + "
\n" + "  \n" + " \n" + " \n" + " \n" + "
\n" + "
\n" + "