KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > servlet > CommandServlet


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.servlet;
10
11 import java.io.IOException JavaDoc;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServlet JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20 import org.apache.log4j.Logger;
21
22 /**
23  * This servlet is used to execute command coming from another
24  * context through a dispatching request.
25  *
26  * The command invocation is detyped to allow it working on redeploy
27  * and avoid class cast exception.
28  *
29  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
30  * @version $Revision: 1.3 $
31  */

32 public class CommandServlet extends HttpServlet JavaDoc
33 {
34
35    private static final Logger log = Logger.getLogger(CommandServlet.class);
36
37    protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
38    {
39       Object JavaDoc cmd = req.getAttribute(ServletCommand.REQ_ATT_KEY);
40       if (cmd != null)
41       {
42          try
43          {
44             Method JavaDoc methods = cmd.getClass().getMethod(
45                   "execute",
46                   new Class JavaDoc[]{
47                      HttpServletRequest JavaDoc.class,
48                      HttpServletResponse JavaDoc.class});
49             Object JavaDoc result = methods.invoke(cmd, new Object JavaDoc[]{req,resp});
50             req.setAttribute(ServletCommand.REQ_ATT_KEY, result);
51          }
52          catch (NoSuchMethodException JavaDoc e)
53          {
54             throw new ServletException JavaDoc("No execute method found on the command", e);
55          }
56          catch (InvocationTargetException JavaDoc e)
57          {
58             // Log the wrappee
59
Throwable JavaDoc wrappee = e.getTargetException();
60             log.error("Exception in command invocation", wrappee);
61
62             // Rethrow the checked ServletException
63
if (wrappee instanceof ServletException JavaDoc)
64             {
65                throw (ServletException JavaDoc)wrappee;
66             }
67             // Rethrow the checked IOException
68
if (wrappee instanceof IOException JavaDoc)
69             {
70                throw (IOException JavaDoc)wrappee;
71             }
72             // Rethrow RuntimeException
73
if (wrappee instanceof RuntimeException JavaDoc)
74             {
75                throw (RuntimeException JavaDoc)wrappee;
76             }
77             // Rethrow Error
78
if (wrappee instanceof Error JavaDoc)
79             {
80                throw (Error JavaDoc)wrappee;
81             }
82             // Here we wrap it and rethrow
83
throw new ServletException JavaDoc("The invoked command threw an exception", wrappee);
84          }
85          catch (IllegalAccessException JavaDoc e)
86          {
87             throw new ServletException JavaDoc("Unexpected IllegalAccessException during command invocation", e);
88          }
89       }
90       else
91       {
92          throw new ServletException JavaDoc("No command found");
93       }
94    }
95
96    protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
97    {
98       doGet(req, resp);
99    }
100 }
101
Popular Tags