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