Documentation
Examples
Tutorials
JAR files
Leaks
Errata
/shared/HelperBaseCh4.java
/shared/HelperBaseCh4.java
package shared; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Level; import org.apache.log4j.Logger; public abstract class HelperBaseCh4 { protected enum SessionData { READ, IGNORE }; private Method methodDefault = null; protected HttpServletRequest request; protected HttpServletResponse response; protected Logger logger; public HelperBaseCh4(HttpServletRequest request, HttpServletResponse response) { this.request = request; this.response = response; logger = Logger.getLogger("bytesizebook.webdev"); logger.setLevel(Level.DEBUG); } protected void doGet() throws ServletException, IOException { response.getWriter().print("The doGet method must be overridden" + " in the class that extends HelperBase."); } protected void doPost() throws ServletException, IOException { response.getWriter().print("The doPost method must be overridden" + " in the class that extends HelperBase."); } protected abstract void copyFromSession(Object helper); public void addHelperToSession(String name, SessionData state) { if (SessionData.READ == state) { Object sessionObj = request.getSession().getAttribute(name); if (sessionObj != null) { copyFromSession(sessionObj); } } request.getSession().setAttribute(name, this); } public void addHelperToSession(String name, boolean checkSession) { if (checkSession) { Object sessionObj = request.getSession().getAttribute(name); if (sessionObj != null) { copyFromSession(sessionObj); } } request.getSession().setAttribute(name, this); } protected String executeButtonMethod() throws ServletException, IOException { String result = ""; methodDefault = null; //Look for the most extended class. Class clazz = this.getClass(); Class enclosingClass = clazz.getEnclosingClass(); while (enclosingClass != null) { clazz = this.getClass(); enclosingClass = clazz.getEnclosingClass(); } //Start looking for methods from the most extended class. //If not found there, then start looking at the base classes. try { result = executeButtonMethod(clazz, true); } catch (Exception ex) { writeError(request, response, "Button Method Error", ex); return ""; } return result; } protected String executeButtonMethod(Class clazz, boolean searchForDefault) throws IllegalAccessException, InvocationTargetException { String result = ""; //retrieve all the methods Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { ButtonMethod annotation = method.getAnnotation(ButtonMethod.class); //look for those with button method annotations if (annotation != null) { //look for the default method if (searchForDefault && annotation.isDefault()) { methodDefault = method; } //if the button name matches, execute the method if (request.getParameter(annotation.buttonName()) != null) { result = invokeButtonMethod(method); break; } } } //If the result is still empty, then the method was not found. Look in //the super class. if (result.equals("")) { Class superClass = clazz.getSuperclass(); if (superClass != null) { result = executeButtonMethod(superClass, methodDefault == null); } //If no method was found in the super classes, then use the default if (result.equals("")) { if (methodDefault != null) { result = invokeButtonMethod(methodDefault); } else { logger.error( "(executeButtonMethod) No default method " + "was specified, but one was needed."); result = "No default method was specified,."; } } } return result; } protected String invokeButtonMethod(Method buttonMethod) throws IllegalAccessException, InvocationTargetException { String resultInvoke = "Could not invoke method"; try { resultInvoke = (String) buttonMethod.invoke(this, (Object[]) null); } catch (IllegalAccessException iae) { logger.error("(invoke) Button method is not public.", iae); throw iae; } catch (InvocationTargetException ite) { logger.error("(invoke) Button method exception", ite); throw ite; } return resultInvoke; } public void fillBeanFromRequest(Object data) { try { org.apache.commons.beanutils.BeanUtils.populate(data, request.getParameterMap()); } catch (IllegalAccessException iae) { logger.error("Populate - Illegal Access.", iae); } catch (InvocationTargetException ite) { logger.error("Populate - Invocation Target.", ite); } } private void populateThrow(Object data) throws IOException, ServletException { try { org.apache.commons.beanutils.BeanUtils.populate(data, request.getParameterMap()); } catch (IllegalAccessException iae) { logger.error("Populate - Illegal Access.", iae); writeError(request, response, "Populate - Illegal Access.", iae); } catch (InvocationTargetException ite) { logger.error("Populate - Invocation Target.", ite); writeError(request, response, "Populate - Invocation Target.", ite); } } static public void writeError( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String title, Exception ex) throws IOException, ServletException { java.io.PrintWriter out = response.getWriter(); response.setContentType("text/html"); out.println(""); out.println(" "); out.println("
" + title + "
"); out.println(" "); out.println(" "); out.println("
" + title + "
"); if (ex.getMessage() != null) { out.println("
" + ex.getMessage() + "
"); } if (ex.getCause() != null) { out.println("
" + ex.getCause() + "
"); } StackTraceElement[] trace = ex.getStackTrace(); if (trace != null && trace.length > 0) { out.print("
"); } ex.printStackTrace(out); out.println("
"); out.println(" "); out.println(""); out.close(); } }
Contact the author