1 package org.jahia.utils; 2 3 import java.io.FileInputStream ; 4 import java.io.IOException ; 5 import java.lang.reflect.InvocationTargetException ; 6 import java.lang.reflect.Method ; 7 import java.util.Properties ; 8 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 12 13 20 public class ActionHandler { 21 22 private static org.apache.log4j.Logger logger = 23 org.apache.log4j.Logger.getLogger(ActionHandler.class); 24 25 26 private Class actionClass; 27 28 33 private Object instanceOfClass = null; 34 35 36 37 private Properties methods; 38 39 46 public ActionHandler(String propsFile, String theClass) { 47 48 methods = new Properties (); 49 try { 50 FileInputStream fis = new FileInputStream (propsFile); 51 methods.load(fis); 52 fis.close(); 53 } 54 catch (IOException e) { 55 logger.error("ActionHandler-ActionHandler", e); 56 } 57 58 try { 59 actionClass = Class.forName(theClass); 60 } catch (ClassNotFoundException ex) { 61 logger.error("ActionHandler-ActionHandler", ex); 62 } catch (Exception ex) { 63 logger.error("ActionHandler-ActionHandler", ex); 64 } 65 } 66 67 73 public Object call(String action, HttpServletRequest request) throws NullPointerException { 74 75 String theMethod = methods.getProperty(action); 76 if (theMethod == null) { logger.error("No method found for action " + action); 78 throw new NullPointerException (); 79 } 80 try { 81 Class theParams[] = {Class.forName("javax.servlet.http.HttpServletRequest")}; 82 Method thisMethod = actionClass.getDeclaredMethod(theMethod, theParams); 83 Object args[] = {request}; 84 return thisMethod.invoke(instanceOfClass, args); 85 86 } catch(ClassNotFoundException cnfe) { 87 logger.error("ActionHandler-call", cnfe); 88 throw new NullPointerException (); 89 90 } catch(NoSuchMethodException nsme) { 91 logger.error("ActionHandler-call", nsme); 92 throw new NullPointerException (); 93 94 } catch(IllegalAccessException iae) { 95 logger.error("ActionHandler-call", iae); 96 throw new NullPointerException (); 97 98 } catch(InvocationTargetException ite) { 99 logger.error("Target exception", ite.getTargetException()); 100 logger.error("ActionHandler-call", ite); 101 102 throw new NullPointerException (); 103 } 104 } 105 106 107 114 public Object call( Object instanceOfClass, String action, HttpServletRequest request, HttpServletResponse response) throws NullPointerException { 115 116 String theMethod = methods.getProperty(action); 117 if (theMethod == null) { logger.error("No method found for action " + action); 119 throw new NullPointerException (); 120 } 121 try { 122 Class theParams[] = {Class.forName("javax.servlet.http.HttpServletRequest"),Class.forName("javax.servlet.http.HttpServletResponse")}; 123 Method thisMethod = actionClass.getDeclaredMethod(theMethod, theParams); 124 Object args[] = {request,response}; 125 126 return thisMethod.invoke(instanceOfClass, args); 127 128 } catch(ClassNotFoundException cnfe) { 129 logger.error("Class not found", cnfe); 130 throw new NullPointerException (); 131 132 } catch(NoSuchMethodException nsme) { 133 logger.error("No such method", nsme); 134 throw new NullPointerException (); 135 136 } catch(IllegalAccessException iae) { 137 logger.error("Illegal access exception", iae); 138 throw new NullPointerException (); 139 140 } catch(InvocationTargetException ite) { 141 logger.error("Invocation error", ite); 142 if (ite.getTargetException() != null) { 143 logger.error("Root cause", ite.getTargetException()); 144 } 145 throw new NullPointerException (); 146 } 147 } 148 149 } 150 | Popular Tags |