001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.madvoc;
004:
005: import jodd.madvoc.config.AutomagicMadvocConfig;
006: import jodd.madvoc.config.MadvocConfig;
007: import jodd.util.ClassLoaderUtil;
008:
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011: import javax.servlet.Filter;
012: import javax.servlet.FilterChain;
013: import javax.servlet.FilterConfig;
014: import javax.servlet.ServletContext;
015: import javax.servlet.ServletException;
016: import javax.servlet.ServletRequest;
017: import javax.servlet.ServletResponse;
018: import java.io.IOException;
019: import java.lang.reflect.Constructor;
020:
021: /**
022: * Default Madvoc controller.
023: */
024: public class MadvocDispatcher implements Filter {
025:
026: protected FilterConfig filterConfig;
027: protected WebApplication webapp;
028:
029: /**
030: * Filter initialization.
031: */
032: public void init(FilterConfig filterConfig) throws ServletException {
033: this .filterConfig = filterConfig;
034: webapp = loadWebApplication(filterConfig
035: .getInitParameter("madvoc.webapp"));
036: MadvocConfig config = loadMadvocConfig(filterConfig
037: .getInitParameter("madvoc.config"));
038: config.configure(webapp);
039: }
040:
041: public void destroy() {
042: }
043:
044: // ---------------------------------------------------------------- loading config
045:
046: /**
047: * Loads {@link WebApplication}. If class name is <code>null</code>,
048: * default web application will be loaded.
049: */
050: protected WebApplication loadWebApplication(String className)
051: throws ServletException {
052: if (className == null) {
053: return new WebApplication(filterConfig.getServletContext());
054: }
055:
056: WebApplication webApp;
057: try {
058: Class clazz = ClassLoaderUtil.loadClass(className, this
059: .getClass());
060: Constructor ctor = clazz
061: .getConstructor(ServletContext.class);
062: webApp = (WebApplication) ctor.newInstance(filterConfig
063: .getServletContext());
064: } catch (ClassCastException ccex) {
065: throw new ServletException("Class '" + className
066: + "' is not a Madvoc WebApplication.");
067: } catch (Exception ex) {
068: throw new ServletException("Unable to load class '"
069: + className + "'.");
070: }
071: return webApp;
072: }
073:
074: /**
075: * Loads {@link jodd.madvoc.config.MadvocConfig}. If class name is <code>null</code>,
076: * {@link jodd.madvoc.config.AutomagicMadvocConfig} will be created.
077: */
078: protected MadvocConfig loadMadvocConfig(String className)
079: throws ServletException {
080: if (className == null) {
081: return new AutomagicMadvocConfig();
082: }
083: MadvocConfig config;
084: try {
085: Class clazz = ClassLoaderUtil.loadClass(className, this
086: .getClass());
087: config = (MadvocConfig) clazz.newInstance();
088: } catch (ClassCastException ccex) {
089: throw new ServletException("Class '" + className
090: + "' is not a Madvoc configuration.");
091: } catch (Exception ex) {
092: throw new ServletException("Unable to load class '"
093: + className + "'.");
094: }
095: return config;
096: }
097:
098: // ---------------------------------------------------------------- do filter
099:
100: /**
101: * Builds {@link ActionRequest} and invokes it.
102: */
103: public void doFilter(ServletRequest req, ServletResponse res,
104: FilterChain chain) throws IOException, ServletException {
105: HttpServletRequest request = (HttpServletRequest) req;
106: HttpServletResponse response = (HttpServletResponse) res;
107:
108: ActionRequest actionRequest = webapp.buildActionRequest(
109: request, response);
110: if (actionRequest == null) {
111: // to do: load static content
112: chain.doFilter(request, response);
113: return;
114: }
115: try {
116: String result = actionRequest.invoke();
117: if (actionRequest.isRendered() == false) {
118: actionRequest.render(result);
119: }
120: } catch (Exception ex) {
121: throw new ServletException(ex);
122: }
123: }
124:
125: }
|