KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > app > qws > WebAdminServlet


1 package com.quadcap.app.qws;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.HashMap JavaDoc;
42
43 import javax.servlet.ServletConfig JavaDoc;
44 import javax.servlet.ServletContext JavaDoc;
45 import javax.servlet.ServletException JavaDoc;
46
47 import javax.servlet.http.HttpServlet JavaDoc;
48 import javax.servlet.http.HttpServletRequest JavaDoc;
49 import javax.servlet.http.HttpServletResponse JavaDoc;
50
51 import com.quadcap.http.server22.WebApplication;
52 import com.quadcap.http.server22.WebServer;
53
54 import com.quadcap.util.Debug;
55
56 /**
57  * The controller servlet for the Web Server Admin Tool application.
58  *
59  * @author Stan Bailes
60  */

61 public class WebAdminServlet extends HttpServlet JavaDoc {
62     HashMap JavaDoc actions = new HashMap JavaDoc();
63
64     static HashMap JavaDoc actionClasses = new HashMap JavaDoc();
65     static {
66         actionClasses.put("/load.adm", "com.quadcap.app.qws.ActionLoad");
67         actionClasses.put("/list.adm", "com.quadcap.app.qws.ActionList");
68         actionClasses.put("/reload.adm", "com.quadcap.app.qws.ActionReload");
69         actionClasses.put("/unload.adm", "com.quadcap.app.qws.ActionUnload");
70         actionClasses.put("/stop.adm", "com.quadcap.app.qws.ActionStop");
71     }
72     
73     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
74         super.init(config);
75         ServletContext JavaDoc context = config.getServletContext();
76         WebApplication app = (WebApplication)context;
77         WebServer server = app.getWebServer();
78         context.setAttribute("server", server);
79     }
80
81     public void doPost(HttpServletRequest JavaDoc request,
82                       HttpServletResponse JavaDoc response)
83         throws ServletException JavaDoc
84     {
85         doGet(request, response);
86     }
87     
88     public void doGet(HttpServletRequest JavaDoc request,
89                       HttpServletResponse JavaDoc response)
90         throws ServletException JavaDoc
91     {
92         try {
93             Action action = getAction(request);
94             Debug.println("action = " + action);
95             if (action == null) {
96                 response.sendError(HttpServletResponse.SC_NOT_FOUND,
97                                    "Not Found");
98             } else {
99                 try {
100                     action.service(request, response);
101                 } catch (ServletException JavaDoc e) {
102                     Debug.print(e);
103                     throw e;
104                 }
105             }
106         } catch (ServletException JavaDoc ee) {
107             Debug.print(ee);
108             throw ee;
109         } catch (Throwable JavaDoc t) {
110             Debug.print(t);
111             throw new ServletException JavaDoc(t);
112         }
113     }
114
115     Action getAction(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
116         String JavaDoc s = request.getServletPath();
117         log("pathInfo = " + s);
118         Action action = (Action)actions.get(s);
119         if (action == null) {
120             String JavaDoc actionClass = (String JavaDoc)actionClasses.get(s);
121             if (actionClass != null) {
122                 Class JavaDoc c = Class.forName(actionClass);
123                 action = (Action)c.newInstance();
124                 action.init(getServletConfig());
125                 actions.put(s, action);
126             }
127         }
128         return action;
129     }
130 }
131
Popular Tags