1 7 package com.inversoft.verge.mvc.servlet; 8 9 10 import javax.servlet.ServletException ; 11 import javax.servlet.http.HttpServlet ; 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 15 import org.apache.log4j.Logger; 16 17 import com.inversoft.util.ReflectionException; 18 import com.inversoft.util.ReflectionTools; 19 import com.inversoft.verge.mvc.DefaultMVCMediator; 20 import com.inversoft.verge.mvc.MVCConstants; 21 import com.inversoft.verge.mvc.MVCException; 22 import com.inversoft.verge.mvc.MVCMediator; 23 24 25 51 public class MVCServlet extends HttpServlet { 52 53 56 private static final Logger logger = Logger.getLogger(MVCServlet.class); 57 58 61 private MVCMediator mediator; 62 63 64 67 public MVCServlet() { 68 } 69 70 71 88 public void init() { 89 90 String className = 91 getServletContext().getInitParameter(MVCConstants.CONTEXT_PARAM); 92 93 if (className != null) { 94 try { 95 Class klass = ReflectionTools.findClass(className, null); 96 if (!MVCMediator.class.isAssignableFrom(klass)) { 97 throw new ReflectionException("Invalid class [" + className + 98 "] not a MVCMediator"); 99 } 100 101 mediator = (MVCMediator) ReflectionTools.instantiate(klass); 102 } catch (ReflectionException re) { 103 logger.error(re); 104 assert false : re.getMessage(); 105 } 106 } else { 107 try { 108 mediator = new DefaultMVCMediator(); 109 } catch (NoClassDefFoundError ncdfe) { 110 logger.error(ncdfe); 111 assert false : "Default MVCMediator not in classpath"; 112 } 113 } 114 } 115 116 123 public void doGet(HttpServletRequest request, HttpServletResponse response) 124 throws ServletException { 125 doPost(request, response); 126 } 127 128 138 public void doPost(HttpServletRequest request, HttpServletResponse response) 139 throws ServletException { 140 141 try { 142 mediator.mediate(request, response); 143 } catch (MVCException mvce) { 144 logger.error("MVC error", mvce); 145 throw new ServletException (mvce); 146 } 147 } 148 } 149 | Popular Tags |