1 9 package org.jboss.portal.server.servlet; 10 11 import java.io.IOException ; 12 import java.lang.reflect.InvocationTargetException ; 13 import java.lang.reflect.Method ; 14 15 import javax.servlet.ServletException ; 16 import javax.servlet.http.HttpServlet ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 20 import org.apache.log4j.Logger; 21 22 32 public class CommandServlet extends HttpServlet 33 { 34 35 private static final Logger log = Logger.getLogger(CommandServlet.class); 36 37 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException 38 { 39 Object cmd = req.getAttribute(ServletCommand.REQ_ATT_KEY); 40 if (cmd != null) 41 { 42 try 43 { 44 Method methods = cmd.getClass().getMethod( 45 "execute", 46 new Class []{ 47 HttpServletRequest .class, 48 HttpServletResponse .class}); 49 Object result = methods.invoke(cmd, new Object []{req,resp}); 50 req.setAttribute(ServletCommand.REQ_ATT_KEY, result); 51 } 52 catch (NoSuchMethodException e) 53 { 54 throw new ServletException ("No execute method found on the command", e); 55 } 56 catch (InvocationTargetException e) 57 { 58 Throwable wrappee = e.getTargetException(); 60 log.error("Exception in command invocation", wrappee); 61 62 if (wrappee instanceof ServletException ) 64 { 65 throw (ServletException )wrappee; 66 } 67 if (wrappee instanceof IOException ) 69 { 70 throw (IOException )wrappee; 71 } 72 if (wrappee instanceof RuntimeException ) 74 { 75 throw (RuntimeException )wrappee; 76 } 77 if (wrappee instanceof Error ) 79 { 80 throw (Error )wrappee; 81 } 82 throw new ServletException ("The invoked command threw an exception", wrappee); 84 } 85 catch (IllegalAccessException e) 86 { 87 throw new ServletException ("Unexpected IllegalAccessException during command invocation", e); 88 } 89 } 90 else 91 { 92 throw new ServletException ("No command found"); 93 } 94 } 95 96 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException 97 { 98 doGet(req, resp); 99 } 100 } 101 | Popular Tags |