Initial revision
[mir.git] / source / mir / servlet / ServletModuleDispatch.java
1 /*
2  * Dispatcher
3  */
4
5 package  mir.servlet;
6
7 import  java.lang.reflect.*;
8 import  javax.servlet.http.*;
9 import  mir.servlet.ServletModuleException;
10 import  mir.misc.*;
11
12
13 /**
14  * Dispatcher, der in einer ServletModule-Klasse, die Methode, die per Http-Post oder Get
15  * Parameter "do" angegeben wurde, ausruft.
16  */
17 public final class ServletModuleDispatch {
18
19         static Logfile theLog;
20
21         static {
22                 theLog = Logfile.getInstance("/tmp/smod.dispatch");
23         }
24
25         /**
26          * privater Konstruktor, um versehentliche Instantiierung zu verhindern
27          */
28         private ServletModuleDispatch () {
29         }
30
31         /**
32          *  Die Dispatch-Routine ruft das von dem Hauptservlet kommende ServletModule
33          *  mit dem per HttpServletRequest angegebenen Paramter <code>do</code> auf.
34          *  Ist kein Parameter angegeben, so wird versucht, in die <code>defaultAction</code>
35          *  des ServletModules zu springen.
36          *
37          * @param req Http-Request, das vom Dispatcher an die Methode des
38          *    ServletModules durchgereicht wird
39          * @param res Http-Response, die vom Dispatcher an die Methode des
40          *    ServletModules durchgereicht wird
41          * @param sMod ServletModule, an das dispatched wird.
42          * @param mod Name des Modules als String (für Logfile)
43          */
44
45         public static void dispatch(ServletModule sMod, HttpServletRequest req,
46                 HttpServletResponse res) throws ServletModuleException
47         {
48                         //sMod.predeliver(req,res);
49
50                         String doParam = req.getParameter("do");
51                         //theLog.printInfo("SerletModuleDispatch: " + sMod.toString() + " with method " + doParam);
52                         if (doParam == null) {
53                                 if (sMod.defaultAction() != null) doParam = sMod.defaultAction();
54                                 else throw new ServletModuleException("Parameter do nicht angegeben!");
55                         }
56
57                         Class[] params= { HttpServletRequest.class, HttpServletResponse.class};
58
59                         try {
60                                 Method method = sMod.getClass().getMethod(doParam,params);
61                                 if (method != null) {
62                                         method.invoke(sMod,new Object[] {req,res} );
63                                         return;
64                                 }
65                                 else theLog.printDebugInfo("method lookup unsuccesful");
66                         }
67                         catch ( NoSuchMethodException e) { throw new ServletModuleException("no such method!" + e.toString());}
68                         catch ( SecurityException e) { throw new ServletModuleException("method not allowed!" + e.toString());}
69                         catch ( InvocationTargetException e) {throw new ServletModuleException("target method exception!" + e.getTargetException().toString());}
70                         catch ( IllegalAccessException e) { throw new ServletModuleException("illegal method not allowed!" + e.toString());}
71 //                      catch ( Exception e ) { throw new ServletModuleException(e.toString()); }
72
73                         throw new ServletModuleException("delivery failed! -- ");
74         }
75 }