KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > servlet > MVCServlet


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.servlet;
8
9
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.http.HttpServlet JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14
15 import org.apache.log4j.Logger;
16
17 import com.inversoft.util.ReflectionException;
18 import com.inversoft.util.ReflectionTools;
19 import com.inversoft.verge.mvc.DefaultMVCMediator;
20 import com.inversoft.verge.mvc.MVCConstants;
21 import com.inversoft.verge.mvc.MVCException;
22 import com.inversoft.verge.mvc.MVCMediator;
23
24
25 /**
26  * <p>
27  * This class is the main entry point to the Inversoft Portal
28  * MVC System. This Servlet is very simple because it simply
29  * looks up the MVCMediator (based on either a web.xml entry
30  * or the default class) and delegates control to that.
31  * </p>
32  *
33  * <p>
34  * The web.xml entry is a context parameter (NOT a servlet
35  * parameter). This allows the MVCMediator to be determined
36  * outside of this servlet. The name of the parameter is:
37  * </p>
38  *
39  * <code>DEFAULT_MVCMEDIATOR_CLASS</code>
40  *
41  * <p>
42  * Something important to notice is that this class caches
43  * an instance of the MVCMediator interface and uses that
44  * instance only.
45  * </p>
46  *
47  * @author Brian Pontarelli
48  * @since 2.0
49  * @version 2.0
50  */

51 public class MVCServlet extends HttpServlet JavaDoc {
52
53     /**
54      * This classes logger
55      */

56     private static final Logger logger = Logger.getLogger(MVCServlet.class);
57
58     /**
59      * The mediator instance for this servlet
60      */

61     private MVCMediator mediator;
62
63
64     /**
65      * Instantiates a new <code>MVCServlet</code>
66      */

67     public MVCServlet() {
68     }
69
70
71     /**
72      * <p>
73      * Tries to determine which MVCMediator to use. This is determined by first
74      * looking in the context parameters for an entry that defines the
75      * implementation to use. If this fails, the class {@link DefaultMVCMediator
76      * DefaultMVCMediator} is used.
77      * </p>
78      *
79      * <p>
80      * If the context parameter is invalid, this logs an error using log4j and
81      * asserts. If the default class is not in the classpath (very very
82      * unlikely), this will assert rather than throw an exception.
83      * </p>
84      *
85      * @asserts If the MVCMediator could not be found or the context parameter
86      * is invalid
87      */

88     public void init() {
89
90         String JavaDoc className =
91             getServletContext().getInitParameter(MVCConstants.CONTEXT_PARAM);
92
93         if (className != null) {
94             try {
95                 Class JavaDoc klass = ReflectionTools.findClass(className, null);
96                 if (!MVCMediator.class.isAssignableFrom(klass)) {
97                     throw new ReflectionException("Invalid class [" + className +
98                         "] not a MVCMediator");
99                 }
100
101                 mediator = (MVCMediator) ReflectionTools.instantiate(klass);
102             } catch (ReflectionException re) {
103                 logger.error(re);
104                 assert false : re.getMessage();
105             }
106         } else {
107             try {
108                 mediator = new DefaultMVCMediator();
109             } catch (NoClassDefFoundError JavaDoc ncdfe) {
110                 logger.error(ncdfe);
111                 assert false : "Default MVCMediator not in classpath";
112             }
113         }
114     }
115
116     /**
117      * Calls {@link #doPost(HttpServletRequest, HttpServletResponse) doPost}
118      *
119      * @param request The HttpServletRequest
120      * @param response The HttpServletResponse
121      * @throws ServletException If doPost throws an exception
122      */

123     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
124     throws ServletException JavaDoc {
125         doPost(request, response);
126     }
127
128     /**
129      * Tries to determine which MVCMediator to use. This is determined by first
130      * looking in the context parameters for an entry that defines the
131      * implementation to use. If this fails, the class {@link DefaultMVCMediator
132      * DefaultMVCMediator} is used.
133      *
134      * @param request The HttpServletRequest which is passed to the mediator
135      * @param response The HttpServletResponse which is passed to the mediator
136      * @throws ServletException If the mediator throws a MVCException
137      */

138     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
139     throws ServletException JavaDoc {
140
141         try {
142             mediator.mediate(request, response);
143         } catch (MVCException mvce) {
144             logger.error("MVC error", mvce);
145             throw new ServletException JavaDoc(mvce);
146         }
147     }
148 }
149
Popular Tags