KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > webapps > util > ActionHandler


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 /**
12  * Class ActionHandler: determines the method to invoke for an Application
13  * (typically a Web Application), given an action.
14  *
15  * @author <a HREF="mailto:boury@xo3.com">Stephane Boury</a>,
16  * <a HREF="mailto:tamiotti@xo3.com">Jerome Tamiotti</a>
17  * @version 1.1
18  */

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

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

44     public ActionHandler(String JavaDoc propsFile, String JavaDoc 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 JavaDoc ex) {
59             logger.error("ActionHandler-ActionHandler", ex);
60         } catch (Exception JavaDoc ex) {
61             logger.error("ActionHandler-ActionHandler", ex);
62         }
63     }
64
65     /**
66      * Calls the appropriate method given the action.
67      *
68      * @param action the action that triggers the method to call
69      * @param request the request parameter for the method
70      */

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

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