1 17 18 package org.apache.geronimo.jmxdebug.web.velocity; 19 20 import java.io.IOException ; 21 import java.lang.reflect.Method ; 22 import java.util.Iterator ; 23 import java.util.Properties ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServlet ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.velocity.Template; 31 import org.apache.velocity.VelocityContext; 32 import org.apache.velocity.app.VelocityEngine; 33 import org.apache.velocity.runtime.RuntimeConstants; 34 35 41 public abstract class BasicVelocityActionServlet extends HttpServlet { 42 public static final String DEFAULT_PROPS = "org/apache/geronimo/jmxdebug/web/velocity/velocity.defaults"; 43 44 47 private static final Class [] DISPATCH_ARGS = {HttpServletRequest .class, HttpServletResponse .class}; 48 49 52 private final VelocityEngine velocityEngine = new VelocityEngine(); 53 54 57 public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException { 58 59 String action = req.getParameter(getActionVerb()); 61 if (action == null || action.length() == 0) { 62 action = "defaultAction"; 63 } 64 65 try { 67 Method method = this.getClass().getMethod(action, DISPATCH_ARGS); 68 method.invoke(this, new Object []{req, res}); 69 } catch (NoSuchMethodException nsme) { 70 unknownAction(req, res); 71 } catch (Exception e) { 72 log("BasicVelocityActionServlet.service() : exception", e); 73 } 74 } 75 76 public void init() throws ServletException { 77 Properties p = new Properties (); 78 79 try { 81 p.load(getClass().getClassLoader().getResourceAsStream(DEFAULT_PROPS)); 82 } catch (Exception e) { 83 log("BasicVelocityActionServlet : default " + DEFAULT_PROPS + " not found.", e); 84 throw new ServletException (e); 85 } 86 87 for (Iterator iterator = p.keySet().iterator(); iterator.hasNext();) { 89 String key = (String ) iterator.next(); 90 velocityEngine.setProperty(key, p.getProperty(key)); 91 } 92 93 ServletLogger sl = new ServletLogger(getServletContext()); 95 velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, sl); 96 97 ServletAppContext vssac = new ServletAppContext(getServletContext()); 99 velocityEngine.setApplicationAttribute(WebappLoader.KEY, vssac); 100 101 try { 103 velocityEngine.init(); 104 } catch (Exception e) { 105 log("BasicVelocityActionServlet", e); 106 throw new ServletException (e); 107 } 108 } 109 110 113 protected abstract String getActionVerb(); 114 115 118 public abstract void defaultAction(HttpServletRequest req, HttpServletResponse res) 119 throws ServletException , IOException ; 120 121 124 public abstract void unknownAction(HttpServletRequest req, HttpServletResponse res) 125 throws ServletException , IOException ; 126 127 protected VelocityEngine getVelocityEngine() { 128 return this.velocityEngine; 129 } 130 131 132 protected boolean renderTemplate(HttpServletRequest req, 133 HttpServletResponse res, 134 VelocityContext velocityContext, 135 String templateName) { 136 137 try { 138 Template template = getVelocityEngine().getTemplate(templateName); 139 template.merge(velocityContext, res.getWriter()); 140 return true; 141 } catch (Exception e) { 142 log("Error rendering template: " + templateName, e); 143 } 144 145 return false; 146 } 147 148 151 public class ServletAppContext implements WebappLoader.WebappLoaderAppContext { 152 ServletContext servletContext = null; 153 154 ServletAppContext(ServletContext sc) { 155 servletContext = sc; 156 } 157 158 public ServletContext getServletContext() { 159 return servletContext; 160 } 161 } 162 } 163 | Popular Tags |