KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webjmx > servlet > JMXAction


1 /*
2  * Copyright (C) WebJMX.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the WebJMX License version 2.0.
6  * See the terms of the WebJMX License in the documentation provided with this software.
7  */

8 /*
9  * JMXAction.java
10  *
11  * Created on December 7, 2001, 11:13 AM
12  */

13  
14 package org.webjmx.servlet;
15
16 import java.lang.reflect.*;
17 import java.io.*;
18 import java.util.*;
19 import javax.management.*;
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22
23 import org.webjmx.api.*;
24 import org.webjmx.tags.*;
25
26 /** This servlet receives all HTML form posts from JMX taglib formMBean tags. It
27  * processes the posted info and makes setAttribute, invoke, createMBean or unregister
28  * calls on an MBeanServer. If the processing was successful, then it forwards
29  * the request to a URL. If there was a failure it can forward the request onto
30  * a different URL.
31  *
32  * @author John Aronson
33  */

34
35 public class JMXAction extends HttpServlet implements JMXTaglibConstants
36 {
37     /** Initializes the servlet.
38      * @param config the ServletConfig object that contains configutation information for this servlet
39      * @throws ServletException if an exception occurs that interrupts the servlet's normal operation
40      *
41      */

42     public void init(ServletConfig config) throws ServletException
43     {
44         super.init(config);
45     }
46
47     /** Destroys the servlet.
48     */

49     public void destroy()
50     { }
51
52     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
53      * @param request servlet request
54      * @param response servlet response
55      * @throws ServletException
56      * @throws IOException
57      */

58     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
59         throws ServletException, IOException
60     {
61         try
62         {
63             JMXProcessor.handleRequest(request.getParameterMap());
64         }catch(ReflectionException re)
65         {
66             re.getTargetException().printStackTrace();
67             handleError(getServletContext(), request, response, re);
68             return;
69         }catch(Exception JavaDoc ex)
70         {
71             ex.printStackTrace();
72             handleError(getServletContext(), request, response, ex);
73             return;
74         }
75         
76         //write response
77
Object JavaDoc o = request.getSession().getAttribute(TAGLIB_FORWARD);
78         if(Boolean.getBoolean(DEBUG_PROP)) log("forward: " +o);
79         if(o != null)
80         {
81             response.sendRedirect((String JavaDoc)o);
82             return;
83         }
84
85         //write a default reply
86
response.setContentType("text/html");
87         java.io.PrintWriter JavaDoc out = response.getWriter();
88         out.println("<html>");
89         out.println("<head>");
90         out.println("<title>Servlet</title>");
91         out.println("</head>");
92         out.println("<body><H1>MBean Updated!<br>No forwarding URL was given.<H1>");
93         out.println("</body>");
94         out.println("</html>");
95         out.close();
96     }
97     
98     private static void handleError(ServletContext context, HttpServletRequest request,
99         HttpServletResponse response, Exception JavaDoc ex)
100     {
101         try
102         {
103             Object JavaDoc o = request.getSession().getAttribute(TAGLIB_ERROR);
104             if(o != null)
105                  response.sendRedirect((String JavaDoc)o);
106                 
107             response.sendError(response.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
108         }catch(Exception JavaDoc ee)
109         {
110             context.log("Exception caught while processing error condition: " +ex.getMessage(), ee);
111         }
112     }
113     
114     /** Handles the HTTP <code>GET</code> method.
115      * @param request servlet request
116      * @param response servlet response
117      * @throws ServletException
118      * @throws IOException */

119     protected void doGet(HttpServletRequest request, HttpServletResponse response)
120         throws ServletException, java.io.IOException JavaDoc
121     {
122         processRequest(request, response);
123     }
124
125     /** Handles the HTTP <code>POST</code> method.
126      * @param request servlet request
127      * @param response servlet response
128      * @throws ServletException
129      * @throws IOException */

130     protected void doPost(HttpServletRequest request, HttpServletResponse response)
131         throws ServletException, java.io.IOException JavaDoc
132     {
133         processRequest(request, response);
134     }
135
136     /** Returns a short description of the servlet.
137      * @return descriptive string */

138     public String JavaDoc getServletInfo()
139     {
140         return "JMX taglib action servlet";
141     }
142 }
143
Popular Tags