001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.web.servlet;
007
008import javax.servlet.*;
009import javax.servlet.http.*;
010import java.sql.*;
011import java.io.*;
012import java.util.*;
013
014import fc.io.*;
015import fc.jdbc.*;
016import fc.web.*;
017
018/** 
019Allows tweaking of configuration parameters (such as log levels) at
020runtime.
021<br>
022<b>
023This servlet should be configured/deployed to be invokable only from the
024localhost or trusted network(s).
025</b>
026@author hursh jain
027*/
028public class AdminServlet extends FCBaseServlet 
029{
030/*IMPL NOTE:
031sunchronize stuff that can be tweaked here with ContextInit
032*/
033public void init(ServletConfig conf) throws ServletException 
034  {
035  super.init(conf);
036  }
037
038public void doGet(HttpServletRequest request, HttpServletResponse response) 
039throws ServletException, IOException
040  {
041  PrintWriter out = response.getWriter(); 
042  try {
043    showSQLForm(request, response);
044    }
045  catch (SQLException ex) {
046    out.print("<pre>");
047    ex.printStackTrace(out);
048    out.print("</pre>");
049    throw new ServletException(ex);
050    }
051  }
052  
053public void doPost(HttpServletRequest request, HttpServletResponse response) 
054throws ServletException, IOException
055  {
056  response.setContentType("text/html");
057  PrintWriter out = response.getWriter();
058  Connection con = null;
059  ServletContext context = getServletContext();
060
061  try 
062  { 
063  //1. Set the default context log
064  String loglevel = request.getParameter("log.level");
065  //log.bug("----------> context loglevel", loglevel);
066  if (loglevel != null && ! loglevel.equals("n/a")) 
067    {
068    log.bug("Adjusting log level for the application log; new level=", loglevel);
069    WebApp.getInstance(appName).getAppLog().setLevel(loglevel);
070    }
071
072  //2. set servlet specific log levels
073  Map allservlets = WebApp.getInstance(appName).allServletsMap;
074  
075  Enumeration e = request.getParameterNames();
076  while (e.hasMoreElements()) 
077    {
078    String param_name = (String) e.nextElement();
079    if (param_name.startsWith("servlet.log."))
080      {
081      loglevel = request.getParameter(param_name);
082      if (loglevel != null && ! loglevel.equals("n/a")) 
083        {
084        log.bug("Adjusting log level for: ", param_name, "; new level=", loglevel);
085        String log_name = param_name.substring(12, param_name.length()); //"servlet.log.X",X=12
086        FCBaseServlet servlet = (FCBaseServlet) allservlets.get(log_name);
087        if (servlet == null) {
088          log.bug("Error setting new level for", param_name, "; log not found in all_logs map");
089          }
090        else {
091          Log log = servlet.getLog();
092          log.setLevel(loglevel);
093          }
094        }
095      }   
096    } //~while
097  showSQLForm(request, response);
098  } //~try
099  catch (SQLException ex) {
100    out.print("<pre>");
101    ex.printStackTrace(out);
102    out.print("</pre>");
103    throw new ServletException(ex);
104    }
105  }  //~doPost
106
107
108void showSQLForm(HttpServletRequest request, HttpServletResponse response) 
109throws ServletException, IOException, SQLException
110  {
111  ServletContext context = getServletContext();
112
113  //we need to calculate this on the fly because the servlet may
114  //be invoked via some deployment specific servlet mapping name
115  String actionURL = request.getServletPath();
116  
117  response.setContentType("text/html");
118  PrintWriter out = response.getWriter();
119  out.println("<html><head></head><body bgcolor='#FFFFFF'><center>");
120
121  out.print("<form method=post action=" + actionURL + ">");
122
123  out.println("<h1>Admin Servlet</h1><hr>");
124
125  //CONNECTION MANAGER
126  out.println("<h3>Installed Connection Managers Information</h3>");
127  out.println(WebApp.getInstance(appName).connectionManagers);
128  out.println("<hr>");
129  out.println("Default Connection Manager");
130  out.println(WebApp.getInstance(appName).getConnectionMgr());
131  out.println("<hr>");
132
133  //LOGGERS
134  out.println("<h3>Current Logging Information</h3>");
135  out.println("<table border=1>");
136  out.println("<tr bgcolor=#cdcdcd><td width=50%><b>Context/Default log</b></td><td>Current Level</td><td>Select a new level</td></tr>");
137
138  Log defaultlog =  WebApp.getInstance(appName).getAppLog();
139  out.println("<tr><td>");
140  out.println("This is the default log used by the fc framework and by the ServletContext (and stored in the context under the <tt>ContextInit.DEFAULT_LOG_KEY</tt> key)");
141  out.println("</td><td>");
142  out.println(defaultlog.getLevel());
143  out.println("</td><td>");
144  printLogSelect(out, "defaultlog");
145  out.println("</td></tr>");
146
147  out.println("<tr><td colspan=3>&nbsp</td></tr>");
148  out.println("<tr bgcolor=#cdcdcd><td><b>Per Servlet/JSP logs</b></td><td>Current Level</td><td>Select a new level</td></tr>");
149
150  Map allservlets = WebApp.getInstance(appName).allServletsMap;
151  Iterator it = allservlets.entrySet().iterator();
152  while (it.hasNext()) 
153    {
154    Map.Entry entry = (Map.Entry) it.next();
155    FCBaseServlet servlet = (FCBaseServlet) entry.getValue();
156    Log log = servlet.getLog();
157    out.print("<tr><td>[<font color=blue>");
158    out.print(servlet);
159    out.print("</font>]<br>");
160    out.print(servlet.stats());
161    out.print("</td><td>");
162    out.print(log.getLevel());
163    out.print("</td><td>");
164    printLogSelect(out, 
165        "servlet.log." + servlet.getClass().getName());
166    out.println("</td></tr>");
167    }
168  out.println("</table>");
169  out.println("<hr>");
170  
171  //FORM END
172  out.print("<INPUT TYPE=submit name=submit value=submit></INPUT>");
173  out.print("</form></center></body></html>");
174  out.flush();
175  }
176
177void printLogSelect(PrintWriter out, Object selectname)
178  {
179  out.print("<select name='");
180  out.print(selectname);
181  out.print("'>");
182  out.println("<option value='n/a'>--[select]--</option>");
183  Iterator it = log.getLevelNames();
184  while (it.hasNext()) {
185    Object level_name = it.next();
186    out.print("<option value='");
187    out.print(level_name);
188    out.print("'>");
189    out.print(level_name);
190    out.println("</option>");
191    }
192  out.println("</select>");
193  }
194
195}