You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@locus.apache.org on 2000/02/18 19:33:04 UTC

cvs commit: jakarta-tomcat/src/admin/contextAdmin contextAdmin.html contextAdmin.jsp

costin      00/02/18 10:33:03

  Added:       src/admin index.html
               src/admin/WEB-INF web.xml
               src/admin/WEB-INF/classes ContextAdmin.java
               src/admin/contextAdmin contextAdmin.html contextAdmin.jsp
  Log:
  Added the admin context - it will be the web-based tomcat admin tool.
  
  Thanks Justyna. The admin context revealed many internal problems,
  we are now able to add and remove contexts at runtime without restarting
  the server.
  
  Submitted by: Justyna Horwat <Ju...@eng.sun.com>
  
  Revision  Changes    Path
  1.1                  jakarta-tomcat/src/admin/index.html
  
  Index: index.html
  ===================================================================
  <html>
  <!--
    Copyright (c) 1999 The Apache Software Foundation.  All rights 
    reserved.
  -->
  
  <head>
    <title>Admin Tools</title>
  </head>
  
  <body bgcolor="white">
  
  <table border=0 cellspacing=5>
    <tr>
      <td><a href="/">
          <img SRC="../tomcat.gif" height=92 width=130 align=LEFT border=0 alt="Tomcat Home Page"></a>
      <td valign=center><h2>Tomcat Administration Tools</h2>
    </tr>
    <tr>
        <td valign=top align=center>
          <b><font face="Arial, Helvetica, sans-serif" size=-5>
          <a href="/">Tomcat Home Page</a>
          </b></font>
    </tr>
  </table>
  
  <br>
  
  <ul>
    <li><a href="./contextAdmin/contextAdmin.html">Context Admin</a>
    <li><a href="/examples/jsp/snp/snoop.jsp">Snoop</a>
  </ul>
  
  </body>
  </html>
  
  
  
  1.1                  jakarta-tomcat/src/admin/WEB-INF/web.xml
  
  Index: web.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
      "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
  
  <web-app>
  </web-app>
  
  
  
  1.1                  jakarta-tomcat/src/admin/WEB-INF/classes/ContextAdmin.java
  
  Index: ContextAdmin.java
  ===================================================================
  import java.util.Vector;
  import java.util.Enumeration;
  import java.io.File;
  import java.net.URL;
  import javax.servlet.http.*;
  
  import org.apache.tomcat.core.Request;
  import org.apache.tomcat.core.HttpServletRequestFacade;
  import org.apache.tomcat.core.Context;
  import org.apache.tomcat.core.ContextManager;
  import org.apache.tomcat.shell.deployment.ContextManagerConfig;
  import org.apache.tomcat.util.RequestUtil;
  
  /**
   * A context administration class. Contexts can be
   * viewed, added, and removed from the context manager.
   *
   */
  
  public class ContextAdmin {
      private ContextManager cm;
      private Request realRequest;
  
      private String submit = null;
      private String addContextPath = null;
      private String addContextDocBase = null;
      private String removeContextName = null;
  
      public void setSubmit(String s) {
  	submit = s;
      }
  
      public void setAddContextPath(String s) {
  	addContextPath = s;
      }
  
      public void setAddContextDocBase(String s) {
  	addContextDocBase = s;
      }
  
      public void setRemoveContextName(String s) {
  	removeContextName = s;
      }
  
      public void init(HttpServletRequest request) {
  	realRequest = ((HttpServletRequestFacade)request).getRealRequest();
  	cm = realRequest.getContext().getContextManager();
      }
  
      public Enumeration getContextNames() {
          return (Enumeration) cm.getContextNames();
      }
  
      public String[] getContextInfo(String contextName) {
  	Enumeration enum;
  	String key;
          Context context;
          Vector v = new Vector();
  
  
  	context = cm.getContext(contextName);
  
  	v.addElement("DOC BASE: " + context.getDocBase());
  	v.addElement("FULL DOC BASE: " + context.getDocumentBase().toString());
  	v.addElement("PATH: " + context.getPath());
  	if (context.getWorkDir() != null)
  	   v.addElement("WORK DIR: " + RequestUtil.URLDecode(context.getWorkDir().getName()));
  
  	v.addElement("DESCRIPTION: " + context.getDescription());
  	v.addElement("SESSION TIMEOUT: " + new Integer(context.getSessionTimeOut()).toString());
  
          enum = context.getInitParameterNames();
  	while (enum.hasMoreElements()) {
  	    key = (String)enum.nextElement();
  	    v.addElement("INIT PARAMETER NAME: " + key);
  	    v.addElement("INIT PARAMETER: " + context.getInitParameter(key));
  	}
  
          enum = context.getAttributeNames();
  	while (enum.hasMoreElements()) {
  	    key = (String)enum.nextElement();
  	    v.addElement("ATTRIBUTE NAME: " + key);
  	    v.addElement("ATTRIBUTE: " + RequestUtil.URLDecode(context.getAttribute(key).toString()));
  	}
  
  	if (context.getDefaultServlet() != null)
  	    v.addElement("DEFAULT SERVLET NAME: " + context.getDefaultServlet().getServletName());
  
  	v.addElement("SERVER INFO: " + context.getEngineHeader());
  
  	String[] s = new String[v.size()];
  	v.copyInto(s);
  	return s;
      }
  
      public String addContext() {
  	if ((addContextPath != null) && (addContextDocBase != null)) {
              Context context = new Context();
              context.setContextManager(cm);
              context.setPath(addContextPath);
              context.setDocBase(addContextDocBase);
  
  	    try {
                  cm.addContext(context);
                  cm.initContext(context);
  	    }
  	    catch(org.apache.tomcat.core.TomcatException ex) {
  	        ex.printStackTrace();
  	    }
  	    return ("Added New Context: " + addContextPath);
  	}
  	else return ("ERROR: Null Context Values");
      }
  
      public String removeContext() {
          if (removeContextName != null) {
              Enumeration enum = cm.getContextNames();
              while (enum.hasMoreElements()) {
  	        String name = (String)enum.nextElement();
  		if (removeContextName.equals(name)) {
  	            try {
  		        cm.removeContext(removeContextName);
  		    }
  	            catch(org.apache.tomcat.core.TomcatException ex) {
  	                ex.printStackTrace();
  	            }
  		    return("Context Name: " + removeContextName + " Removed");
  		}
  	    }
  	    return("Context Name: " + removeContextName + " Not Found");
  	}
  	else return("ERROR: Null Context Name");
  
      }
  }
  
  
  
  1.1                  jakarta-tomcat/src/admin/contextAdmin/contextAdmin.html
  
  Index: contextAdmin.html
  ===================================================================
  <html>
  <!--
    Copyright (c) 1999 The Apache Software Foundation.  All rights 
    reserved.
  -->
  
  <head>
    <title>Admin Context</title>
  </head>
  
  <body bgcolor="white">
  
  <form type=POST action=contextAdmin.jsp>
  
  <table border=0 cellspacing=5>
    <tr>
      <td><a href="/">
          <img SRC="../../tomcat.gif" height=92 width=130 align=LEFT border=0 alt="Tomcat Home Page"></a>
      <td valign=center><h2>Context Administration</h2>
    </tr>
    <tr>
        <td valign=top align=center>
          <b><font face="Arial, Helvetica, sans-serif" size=-5>
          <a href="/admin">Admin Home Page</a>
          </b></font>
    </tr>
  </table>
  
  
  <br>
  
  <table border=0 cellspacing=5>
    <tr>
      <td><td><INPUT TYPE=submit name="submit" value="View All Contexts">
    </tr>
    <tr>
      <td><td>
      <td><td>Path:
      <td><td>Document Base:
    </tr>
    <tr>
      <td><td><INPUT TYPE=submit name="submit" value="Add Context">
      <td><td><INPUT TYPE=text name="addContextPath" size=20>
      <td><td><INPUT TYPE=text name="addContextDocBase" size=20>
    </tr>
    <tr>
      <td><td>
      <td><td>Context Name:
    </tr>
    <tr>
      <td><td><INPUT TYPE=submit name="submit" value="Remove Context">
      <td><td><INPUT TYPE=text name="removeContextName" size=20>
    </tr>
  </table>
  
  </form>
  </body>
  </html>
  
  
  
  1.1                  jakarta-tomcat/src/admin/contextAdmin/contextAdmin.jsp
  
  Index: contextAdmin.jsp
  ===================================================================
  <html>
  <!--
    Copyright (c) 1999 The Apache Software Foundation.  All rights 
    reserved.
  -->
  
  <head>
      <title>Context Admin</title>
  </head>
  
  <body bgcolor="white">
  <font size=5>
  
  <%@ page import="ContextAdmin" %>
  <%@ page import="java.util.Enumeration" %>
  <%@ include file="contextAdmin.html" %>
  
  <hr>
  
  <jsp:useBean id="contextAdmin" scope="page" class="ContextAdmin" />
  <jsp:setProperty name="contextAdmin" property="*" />
  
  <%
      String param = request.getParameter("submit");
  
      if (param != null) {
  
        contextAdmin.init(request);
  
        if (param.equals("View All Contexts")) {
            Enumeration enum = contextAdmin.getContextNames();
            while (enum.hasMoreElements()) {
                String name = (String)enum.nextElement();
  %>
                <ul>
                    <li><font color="#CC0000"><h2>CONTEXT NAME: <%= name %>
                    </h2></font>
  <%
                String[] contextInfoArray = contextAdmin.getContextInfo(name);
                for (int i=0; i<contextInfoArray.length; i++) {
  %>
                    <dd><dt> <%= contextInfoArray[i] %>
  <%
                }
  %>
                </ul>
  <%
            }
        }
        else if (param.equals("Add Context")) {
  %>
            <%= contextAdmin.addContext() %>
  <%
            }
        else if (param.equals("Remove Context")) {
  %>
            <%= contextAdmin.removeContext() %>
  <%
            }
      }
      else out.println("ERROR: Null Request Parameter Value");
  
  %>
  
  <hr>
  
  </body>
  </font>
  </html>