KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > madvoc > MadvocDispatcher


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.madvoc;
4
5 import jodd.madvoc.config.AutomagicMadvocConfig;
6 import jodd.madvoc.config.MadvocConfig;
7 import jodd.util.ClassLoaderUtil;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11 import javax.servlet.*;
12 import java.io.IOException JavaDoc;
13 import java.lang.reflect.Constructor JavaDoc;
14
15 /**
16  * Default Madvoc controller.
17  */

18 public class MadvocDispatcher implements Filter {
19
20     protected FilterConfig filterConfig;
21     protected WebApplication webapp;
22
23     /**
24      * Filter initialization.
25      */

26     public void init(FilterConfig filterConfig) throws ServletException {
27         this.filterConfig = filterConfig;
28         webapp = loadWebApplication(filterConfig.getInitParameter("madvoc.webapp"));
29         MadvocConfig config = loadMadvocConfig(filterConfig.getInitParameter("madvoc.config"));
30         config.configure(webapp);
31     }
32
33     public void destroy() {
34     }
35
36     // ---------------------------------------------------------------- loading config
37

38     /**
39      * Loads {@link WebApplication}. If class name is <code>null</code>,
40      * default web application will be loaded.
41      */

42     protected WebApplication loadWebApplication(String JavaDoc className) throws ServletException {
43         if (className == null) {
44             return new WebApplication(filterConfig.getServletContext());
45         }
46
47         WebApplication webApp;
48         try {
49             Class JavaDoc clazz = ClassLoaderUtil.loadClass(className, this.getClass());
50             Constructor JavaDoc ctor = clazz.getConstructor(ServletContext.class);
51             webApp = (WebApplication) ctor.newInstance(filterConfig.getServletContext());
52         } catch (ClassCastException JavaDoc ccex) {
53             throw new ServletException("Class '" + className + "' is not a Madvoc WebApplication.");
54         } catch (Exception JavaDoc ex) {
55             throw new ServletException("Unable to load class '" + className + "'.");
56         }
57         return webApp;
58     }
59
60     /**
61      * Loads {@link jodd.madvoc.config.MadvocConfig}. If class name is <code>null</code>,
62      * {@link jodd.madvoc.config.AutomagicMadvocConfig} will be created.
63      */

64     protected MadvocConfig loadMadvocConfig(String JavaDoc className) throws ServletException {
65         if (className == null) {
66             return new AutomagicMadvocConfig();
67         }
68         MadvocConfig config;
69         try {
70             Class JavaDoc clazz = ClassLoaderUtil.loadClass(className, this.getClass());
71             config = (MadvocConfig) clazz.newInstance();
72         } catch (ClassCastException JavaDoc ccex) {
73             throw new ServletException("Class '" + className + "' is not a Madvoc configuration.");
74         } catch (Exception JavaDoc ex) {
75             throw new ServletException("Unable to load class '" + className + "'.");
76         }
77         return config;
78     }
79
80
81     // ---------------------------------------------------------------- do filter
82

83
84     /**
85      * Builds {@link ActionRequest} and invokes it.
86      */

87     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc res, FilterChain chain) throws IOException JavaDoc, ServletException {
88         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
89         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) res;
90
91         ActionRequest actionRequest = webapp.buildActionRequest(request, response);
92         if (actionRequest == null) {
93             // to do: load static content
94
chain.doFilter(request, response);
95             return;
96         }
97         try {
98             String JavaDoc result = actionRequest.invoke();
99             if (actionRequest.isRendered() == false) {
100                 actionRequest.render(result);
101             }
102         } catch (Exception JavaDoc ex) {
103             throw new ServletException(ex);
104         }
105     }
106
107 }
108
Popular Tags