001: package com.quadcap.app.qws;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.HashMap;
042:
043: import javax.servlet.ServletConfig;
044: import javax.servlet.ServletContext;
045: import javax.servlet.ServletException;
046:
047: import javax.servlet.http.HttpServlet;
048: import javax.servlet.http.HttpServletRequest;
049: import javax.servlet.http.HttpServletResponse;
050:
051: import com.quadcap.http.server22.WebApplication;
052: import com.quadcap.http.server22.WebServer;
053:
054: import com.quadcap.util.Debug;
055:
056: /**
057: * The controller servlet for the Web Server Admin Tool application.
058: *
059: * @author Stan Bailes
060: */
061: public class WebAdminServlet extends HttpServlet {
062: HashMap actions = new HashMap();
063:
064: static HashMap actionClasses = new HashMap();
065: static {
066: actionClasses
067: .put("/load.adm", "com.quadcap.app.qws.ActionLoad");
068: actionClasses
069: .put("/list.adm", "com.quadcap.app.qws.ActionList");
070: actionClasses.put("/reload.adm",
071: "com.quadcap.app.qws.ActionReload");
072: actionClasses.put("/unload.adm",
073: "com.quadcap.app.qws.ActionUnload");
074: actionClasses
075: .put("/stop.adm", "com.quadcap.app.qws.ActionStop");
076: }
077:
078: public void init(ServletConfig config) throws ServletException {
079: super .init(config);
080: ServletContext context = config.getServletContext();
081: WebApplication app = (WebApplication) context;
082: WebServer server = app.getWebServer();
083: context.setAttribute("server", server);
084: }
085:
086: public void doPost(HttpServletRequest request,
087: HttpServletResponse response) throws ServletException {
088: doGet(request, response);
089: }
090:
091: public void doGet(HttpServletRequest request,
092: HttpServletResponse response) throws ServletException {
093: try {
094: Action action = getAction(request);
095: Debug.println("action = " + action);
096: if (action == null) {
097: response.sendError(HttpServletResponse.SC_NOT_FOUND,
098: "Not Found");
099: } else {
100: try {
101: action.service(request, response);
102: } catch (ServletException e) {
103: Debug.print(e);
104: throw e;
105: }
106: }
107: } catch (ServletException ee) {
108: Debug.print(ee);
109: throw ee;
110: } catch (Throwable t) {
111: Debug.print(t);
112: throw new ServletException(t);
113: }
114: }
115:
116: Action getAction(HttpServletRequest request) throws Exception {
117: String s = request.getServletPath();
118: log("pathInfo = " + s);
119: Action action = (Action) actions.get(s);
120: if (action == null) {
121: String actionClass = (String) actionClasses.get(s);
122: if (actionClass != null) {
123: Class c = Class.forName(actionClass);
124: action = (Action) c.newInstance();
125: action.init(getServletConfig());
126: actions.put(s, action);
127: }
128: }
129: return action;
130: }
131: }
|