KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > http > AdminServlet


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.transport.http ;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.server.AxisServer;
21 import org.apache.axis.utils.Messages;
22 import org.apache.axis.AxisFault;
23 import org.apache.axis.ConfigurationException;
24 import org.apache.axis.handlers.soap.SOAPService;
25 import org.apache.axis.description.ServiceDesc;
26 import org.apache.commons.logging.Log;
27
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.xml.namespace.QName JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * Proof-of-concept "management" servlet for Axis.
37  *
38  * Point a browser here to administer the Axis installation.
39  *
40  * Right now just starts and stops the server.
41  *
42  * @author Glen Daniels (gdaniels@apache.org)
43  * @author Steve Loughran
44  * xdoclet tags are not active yet; keep web.xml in sync
45  * @web.servlet name="AdminServlet" display-name="Axis Admin Servlet" load-on-startup="100"
46  * @web.servlet-mapping url-pattern="/servlet/AdminServlet"
47  */

48 public class AdminServlet extends AxisServletBase {
49
50     private static Log log =
51             LogFactory.getLog(AxisServlet.class.getName());
52
53
54     /**
55      * handle a GET request. Commands are only valid when not in production mode
56      * @param request
57      * @param response
58      * @throws ServletException
59      * @throws IOException
60      */

61     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
62         throws ServletException JavaDoc, IOException JavaDoc {
63         response.setContentType("text/html; charset=utf-8");
64         StringBuffer JavaDoc buffer=new StringBuffer JavaDoc(512);
65         buffer.append("<html><head><title>Axis</title></head><body>\n");
66         //REVISIT: what happens if there is no engine?
67
AxisServer server = getEngine();
68
69         //process command
70
String JavaDoc cmd = request.getParameter("cmd");
71         if (cmd != null) {
72             //who called?
73
String JavaDoc callerIP=request.getRemoteAddr();
74             if (isDevelopment()) {
75                 //only in dev mode do these command work
76
if (cmd.equals("start")) {
77                     log.info(Messages.getMessage("adminServiceStart", callerIP));
78                     server.start();
79                 }
80                 else if (cmd.equals("stop")) {
81                     log.info(Messages.getMessage("adminServiceStop", callerIP));
82                     server.stop();
83                 }
84                 else if (cmd.equals("suspend")) {
85                     String JavaDoc name = request.getParameter("service");
86                     log.info(Messages.getMessage("adminServiceSuspend", name, callerIP));
87                     SOAPService service = server.getConfig().getService(new QName JavaDoc("",name));
88                     service.stop();
89                 }
90                 else if (cmd.equals("resume")) {
91                     String JavaDoc name = request.getParameter("service");
92                     log.info(Messages.getMessage("adminServiceResume", name, callerIP));
93                     SOAPService service = server.getConfig().getService(new QName JavaDoc("",name));
94                     service.start();
95                 }
96             } else {
97                 //in production we log a hostile probe. Remember: logs can be
98
//used for DoS attacks themselves.
99
log.info(Messages.getMessage("adminServiceDeny", callerIP));
100             }
101         }
102
103         // display status
104
if (server.isRunning()) {
105             buffer.append("<H2>");
106             buffer.append(Messages.getMessage("serverRun00"));
107             buffer.append("</H2>");
108         }
109         else {
110             buffer.append("<H2>");
111             buffer.append(Messages.getMessage("serverStop00"));
112             buffer.append("</H2>");
113         }
114         //add commands
115
if(isDevelopment()) {
116             buffer.append("<p><a HREF=\"AdminServlet?cmd=start\">start server</a>\n");
117             buffer.append("<p><a HREF=\"AdminServlet?cmd=stop\">stop server</a>\n");
118
119             Iterator JavaDoc i;
120             try {
121                 i = server.getConfig().getDeployedServices();
122             } catch (ConfigurationException configException) {
123                 //turn any internal configuration exceptions back into axis faults
124
//if that is what they are
125
if(configException.getContainedException() instanceof AxisFault) {
126                     throw (AxisFault) configException.getContainedException();
127                 } else {
128                     throw configException;
129                 }
130             }
131             
132             buffer.append("<p><h2>Services</h2>");
133             buffer.append("<ul>");
134             while (i.hasNext()) {
135                 ServiceDesc sd = (ServiceDesc)i.next();
136                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
137                 sb.append("<li>");
138                 String JavaDoc name = sd.getName();
139                 sb.append(name);
140                 SOAPService service = server.getConfig().getService(new QName JavaDoc("",name));
141                 if(service.isRunning()) {
142                     sb.append("&nbsp;&nbsp;<a HREF=\"AdminServlet?cmd=suspend&service=" + name + "\">suspend</a>\n");
143                 } else {
144                     sb.append("&nbsp;&nbsp;<a HREF=\"AdminServlet?cmd=resume&service=" + name + "\">resume</a>\n");
145                 }
146                 sb.append("</li>");
147                 buffer.append(sb.toString());
148             }
149             buffer.append("</ul>");
150         }
151         //print load
152
buffer.append("<p>");
153         buffer.append(Messages.getMessage("adminServiceLoad",
154                 Integer.toString(getLoadCounter())));
155         buffer.append("\n</body></html>\n");
156         response.getWriter().print( new String JavaDoc(buffer) );
157     }
158 }
159
Popular Tags