ee84259b4df1dfb89649c15d090af551f804eb19
[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         private static Logfile theLog;
20         private static final    Class[] SIGNATURE =
21                                                                                                                 { HttpServletRequest.class, HttpServletResponse.class };
22
23
24         static {
25                 theLog = Logfile.getInstance("/tmp/smod.dispatch");
26         }
27
28         /**
29          * privater Konstruktor, um versehentliche Instantiierung zu verhindern
30          */
31         private ServletModuleDispatch () {
32         }
33
34         /**
35          *  Die Dispatch-Routine ruft das von dem Hauptservlet kommende ServletModule
36          *  mit dem per HttpServletRequest angegebenen Paramter <code>do</code> auf.
37          *  Ist kein Parameter angegeben, so wird versucht, in die <code>defaultAction</code>
38          *  des ServletModules zu springen.
39          *
40          * @param req Http-Request, das vom Dispatcher an die Methode des
41          *    ServletModules durchgereicht wird
42          * @param res Http-Response, die vom Dispatcher an die Methode des
43          *    ServletModules durchgereicht wird
44          * @param sMod ServletModule, an das dispatched wird.
45          * @param mod Name des Modules als String (für Logfile)
46          */
47
48         public static void dispatch(ServletModule sMod, HttpServletRequest req,
49                 HttpServletResponse res) throws ServletModuleException, ServletModuleUserException
50         {
51                         //sMod.predeliver(req,res);
52
53                         String doParam = req.getParameter("do");
54                         theLog.printInfo("SerletModuleDispatch: " + sMod.toString() + " with method " + doParam);
55                         if (doParam == null) {
56                                 if (sMod.defaultAction() != null) doParam = sMod.defaultAction();
57                                 else throw new ServletModuleException("no parameter do supplied!");
58                         }
59
60                         try {
61                                 Method method = sMod.getClass().getMethod(doParam,SIGNATURE);
62                                 if (method != null) {
63                                         method.invoke(sMod,new Object[] {req,res} );
64                                         return;
65                                 }
66                                 else theLog.printDebugInfo("method lookup unsuccesful");
67                         }
68                         catch ( NoSuchMethodException e) { throw new ServletModuleException("no such method!" + e.toString());}
69                         catch ( SecurityException e) { throw new ServletModuleException("method not allowed!" + e.toString());}
70                         catch ( InvocationTargetException e) {
71                                 if (e.getTargetException().getClass().getName().equals("mir.servlet.ServletModuleUserException")) {
72                                                 throw new ServletModuleUserException(((ServletModuleUserException)e.getTargetException()).getMsg());
73                                 } else {
74                                                 e.printStackTrace();
75                                                 throw new ServletModuleException(e.getTargetException().toString());
76                                 }
77                         }
78                         catch ( IllegalAccessException e) { throw new ServletModuleException("illegal method not allowed!" + e.toString());}
79
80                         //hopefully we don't get here ...
81                         throw new ServletModuleException("delivery failed! -- ");
82         }
83 }