KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > ActionHandler


1 package org.jahia.utils;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.lang.reflect.InvocationTargetException JavaDoc;
6 import java.lang.reflect.Method JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12
13 /**
14  * Class ActionHandler: determines the method to invoke for an Application
15  * (typically a Web Application), given an action.
16  *
17  * @author Stephane Boury, Jerome Tamiotti
18  * @version 1.1
19  */

20 public class ActionHandler {
21
22     private static org.apache.log4j.Logger logger =
23             org.apache.log4j.Logger.getLogger(ActionHandler.class);
24
25     /* The class on which to call the methods */
26     private Class JavaDoc actionClass;
27
28     /**
29      * An instance of the class on which to call the methods
30      * This instance is null here because the class is static,
31      * and the parameter of 'invokeMethod' method will be ignored
32      */

33     private Object JavaDoc instanceOfClass = null;
34
35
36     /* The hashtable containing the method names */
37     private Properties JavaDoc methods;
38
39     /**
40      * Class constructor: builds the hashtable by reading a flat file.
41      *
42      * @param propsFile the full path to the file containing pairs of
43      * "action - method name"
44      * @param theClass the name of the Class on which to call the methods
45      */

46     public ActionHandler(String JavaDoc propsFile, String JavaDoc theClass) {
47
48         methods = new Properties JavaDoc();
49         try {
50             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(propsFile);
51             methods.load(fis);
52             fis.close();
53         }
54         catch (IOException JavaDoc e) {
55             logger.error("ActionHandler-ActionHandler", e);
56         }
57
58         try {
59             actionClass = Class.forName(theClass);
60         } catch (ClassNotFoundException JavaDoc ex) {
61             logger.error("ActionHandler-ActionHandler", ex);
62         } catch (Exception JavaDoc ex) {
63             logger.error("ActionHandler-ActionHandler", ex);
64         }
65     }
66
67     /**
68      * Calls the appropriate method given the action.
69      *
70      * @param action the action that triggers the method to call
71      * @param request the request parameter for the method
72      */

73     public Object JavaDoc call(String JavaDoc action, HttpServletRequest JavaDoc request) throws NullPointerException JavaDoc {
74
75         String JavaDoc theMethod = methods.getProperty(action);
76         if (theMethod == null) { //method not found in list
77
logger.error("No method found for action " + action);
78             throw new NullPointerException JavaDoc();
79         }
80         try {
81             Class JavaDoc theParams[] = {Class.forName("javax.servlet.http.HttpServletRequest")};
82             Method JavaDoc thisMethod = actionClass.getDeclaredMethod(theMethod, theParams);
83             Object JavaDoc args[] = {request};
84             return thisMethod.invoke(instanceOfClass, args);
85
86         } catch(ClassNotFoundException JavaDoc cnfe) {
87             logger.error("ActionHandler-call", cnfe);
88             throw new NullPointerException JavaDoc();
89
90         } catch(NoSuchMethodException JavaDoc nsme) {
91             logger.error("ActionHandler-call", nsme);
92             throw new NullPointerException JavaDoc();
93
94         } catch(IllegalAccessException JavaDoc iae) {
95             logger.error("ActionHandler-call", iae);
96             throw new NullPointerException JavaDoc();
97
98         } catch(InvocationTargetException JavaDoc ite) {
99             logger.error("Target exception", ite.getTargetException());
100             logger.error("ActionHandler-call", ite);
101
102             throw new NullPointerException JavaDoc();
103         }
104     }
105
106
107     /**
108      * Calls the appropriate method given the action.
109      *
110      * @param action the action that triggers the method to call
111      * @param request the request parameter for the method
112      * @param response the response parameter for the method
113      */

114     public Object JavaDoc call( Object JavaDoc instanceOfClass, String JavaDoc action, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws NullPointerException JavaDoc {
115
116         String JavaDoc theMethod = methods.getProperty(action);
117         if (theMethod == null) { //method not found in list
118
logger.error("No method found for action " + action);
119             throw new NullPointerException JavaDoc();
120         }
121         try {
122             Class JavaDoc theParams[] = {Class.forName("javax.servlet.http.HttpServletRequest"),Class.forName("javax.servlet.http.HttpServletResponse")};
123             Method JavaDoc thisMethod = actionClass.getDeclaredMethod(theMethod, theParams);
124             Object JavaDoc args[] = {request,response};
125
126             return thisMethod.invoke(instanceOfClass, args);
127
128         } catch(ClassNotFoundException JavaDoc cnfe) {
129             logger.error("Class not found", cnfe);
130             throw new NullPointerException JavaDoc();
131
132         } catch(NoSuchMethodException JavaDoc nsme) {
133             logger.error("No such method", nsme);
134             throw new NullPointerException JavaDoc();
135
136         } catch(IllegalAccessException JavaDoc iae) {
137             logger.error("Illegal access exception", iae);
138             throw new NullPointerException JavaDoc();
139
140         } catch(InvocationTargetException JavaDoc ite) {
141             logger.error("Invocation error", ite);
142             if (ite.getTargetException() != null) {
143                 logger.error("Root cause", ite.getTargetException());
144             }
145             throw new NullPointerException JavaDoc();
146         }
147     }
148
149 }
150
Popular Tags