Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.servlet; | |
34 | ||
35 | import java.io.Serializable; | |
36 | ||
37 | import org.chwf.registry.RegistryException; | |
38 | import org.chwf.registry.UserRegistry; | |
39 | ||
40 | /** | |
41 | * Superclass for controllers. Consult the documentation for more information | |
42 | * about how to write controllers. | |
43 | * | |
44 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
45 | */ | |
46 | 111 | public class Controller implements Serializable { |
47 | ||
48 | /** | |
49 | * Constant indicating that a controller method parameter has no default. | |
50 | * For single-valued parameters, this means that the parameter is required | |
51 | * and its omission will throw a MissingParameterException. For multi-valued | |
52 | * parameters, the default value will be an empty array. For boolean values, | |
53 | * the default value will be <code>false</code>. | |
54 | */ | |
55 | 2 | public static final Object DEFAULT_NONE = "DEFAULT_NONE"; |
56 | ||
57 | /** | |
58 | * Constant indicating that a controller method parameter defaults to | |
59 | * a new object, created using <code>Class.newInstance()</code>. | |
60 | */ | |
61 | 1 | public static final Object DEFAULT_NEW_OBJECT = "DEFAULT_NEW_OBJECT"; |
62 | ||
63 | /** Constant for redirection to the referer page. */ | |
64 | public static final String REFERER_PAGE = "REFERER_PAGE"; | |
65 | ||
66 | /** The invocation context. Transient to avoid accidental serialization. */ | |
67 | private transient InvocationContext context; | |
68 | ||
69 | /** | |
70 | * Factory method to get controller singleton. This method only works if | |
71 | * the user registry is initialized. This is always true for servlet and | |
72 | * JSP invoked via Chrysalis.<p> | |
73 | * | |
74 | * @param controllerClass The controller class. | |
75 | * @return The controller. | |
76 | * @throws RegistryException If the registry is not initialized. | |
77 | */ | |
78 | public static Controller getController(Class controllerClass) | |
79 | throws RegistryException { | |
80 | 93 | return (Controller) UserRegistry.getSingleton(controllerClass); |
81 | } | |
82 | ||
83 | /** | |
84 | * A lifecycle method called at the beginning of each controller invocation. | |
85 | * It can be used to initialize controller resources, like JDBC connections | |
86 | * or EJB references.<p> | |
87 | * | |
88 | * @throws Exception Any exception needed for logic. | |
89 | */ | |
90 | public void start() throws Exception { | |
91 | 36 | } |
92 | ||
93 | /** | |
94 | * <p>A lifecycle method called at the end of each controller invocation. It | |
95 | * can be used to release controller resources and rollback transactions. | |
96 | * This method is <i>always</i> called, even if there were exceptions during | |
97 | * the controller invocation, as if this method were in a <code>finally</code> | |
98 | * block.</p> | |
99 | * | |
100 | * <p>If there were errors, the <code>getError()</code> method will return | |
101 | * the error object. If there were no errors, the <code>hasError()</code> | |
102 | * method returns <code>false</code>. This can be used as a test to determine | |
103 | * whether to commit or rollback transactions. | |
104 | * | |
105 | * <pre> | |
106 | * public void stop() throws Exception { | |
107 | * if(hasError()) { | |
108 | * transaction.rollback(); | |
109 | * } else { | |
110 | * transaction.commit(); | |
111 | * } | |
112 | * }</pre> | |
113 | * | |
114 | * @throws Exception Any exception needed for logic. | |
115 | */ | |
116 | public void stop() throws Exception { | |
117 | 36 | } |
118 | ||
119 | /** | |
120 | * Set the view for the controller. When the invocation is complete, | |
121 | * the system will redirect to this view. The view URL should be a | |
122 | * "context-relative" URL. That is, if the URL begins with a "/", it should | |
123 | * not include the application's context path. The URL will be resolved | |
124 | * relative to the web application root directory. Controllers may call | |
125 | * this method if they must manually control view redirection.<p> | |
126 | * | |
127 | * @param url The URL to redirect to after processing is complete. | |
128 | */ | |
129 | public void setView(String url) { | |
130 | 4 | getContext().setView(url); |
131 | 4 | } |
132 | ||
133 | /** | |
134 | * Set the view and a single parameter value for the controller. This | |
135 | * simplifies the common situation when the controller needs to redirect to | |
136 | * a JSP that requires a single parameter:<p> | |
137 | * | |
138 | * <pre>setView("/showItem.jsp", "itemId", item.getItemId());</pre> | |
139 | * | |
140 | * This results in the redirection URL:<p> | |
141 | * | |
142 | * <pre>/{<i>context-path</i>}/showItem.jsp?itemId={<i>itemId</i>}</pre> | |
143 | * | |
144 | * @param url The URL to redirect to after processing is complete. | |
145 | * @param parameter The parameter name. | |
146 | * @param value The parameter value. This will be converted to a string. | |
147 | */ | |
148 | public void setView(String url, String parameter, Object value) { | |
149 | 1 | getContext().setView(url, parameter, value); |
150 | 1 | } |
151 | ||
152 | /** | |
153 | * Sets a parameter for the view redirection. This parameter will be | |
154 | * appended to the redirection URL. The parameter value will be converted | |
155 | * to a String. The parameter name and value will be URL encoded before | |
156 | * redirection. This method may be called multiple times to set multiple | |
157 | * parameter values:<p> | |
158 | * | |
159 | * <pre> | |
160 | * addViewParameter("id", productId); | |
161 | * addViewParameter("showSpecials", "true");</pre> | |
162 | * | |
163 | * @param parameter The parameter name. | |
164 | * @param value The parameter value. This will be converted to a string. | |
165 | */ | |
166 | public void addViewParameter(String parameter, Object value) { | |
167 | 1 | getContext().addViewParameter(parameter, value); |
168 | 1 | } |
169 | ||
170 | /** | |
171 | * Specifies that the system redirect to the default error page and pass | |
172 | * it the specified error. Any specified view will be ignored. This method | |
173 | * is an alternative to throwing the exception from the controller method.<p> | |
174 | * | |
175 | * @param error The error. | |
176 | */ | |
177 | public void handleError(Throwable error) { | |
178 | 2 | getContext().handleError(error); |
179 | 2 | } |
180 | ||
181 | /** | |
182 | * Specifies that the system redirect to the error page and pass it the | |
183 | * specified error. Any specified view will be ignored. This method lets | |
184 | * the controller programmatically control error flow, allowing the | |
185 | * controller to use different error pages for different kinds of errors.<p> | |
186 | * | |
187 | * @param errorPage The error page. | |
188 | * @param error The error. | |
189 | */ | |
190 | public void handleError(String errorPage, Throwable error) { | |
191 | 2 | getContext().handleError(errorPage, error); |
192 | 2 | } |
193 | ||
194 | /** | |
195 | * Get the error associated with the controller's invocation, if any.<p> | |
196 | * | |
197 | * @return The error associated with the controller invocation, or | |
198 | * <code>null</code> if there is none. | |
199 | */ | |
200 | public Throwable getError() { | |
201 | 4 | return getContext().getError(); |
202 | } | |
203 | ||
204 | /** | |
205 | * Return true if the controller invocation has an error.<p> | |
206 | * | |
207 | * @return True if the controller invocation has an error. | |
208 | */ | |
209 | public boolean hasError() { | |
210 | 2 | return getContext().hasError(); |
211 | } | |
212 | ||
213 | /** | |
214 | * Releases this controller from the user's session. The next time | |
215 | * that user invokes this controller class, a new controller object | |
216 | * will be created with fresh data.<p> | |
217 | * | |
218 | * @throws RegistryException If the registry is not initialized. | |
219 | */ | |
220 | public void release() throws RegistryException { | |
221 | 2 | UserRegistry.releaseSingleton(getClass()); |
222 | 2 | } |
223 | ||
224 | /** | |
225 | * A system method that initializes the controller's invocation context. | |
226 | * Developers should not use this method, except for testing purposes.<p> | |
227 | * | |
228 | * @return The invocation context. | |
229 | */ | |
230 | public InvocationContext initContext() { | |
231 | 67 | this.context = new InvocationContext(); |
232 | 67 | return this.context; |
233 | } | |
234 | ||
235 | /** | |
236 | * Get the invocation context. If no invocation context exists, this method | |
237 | * creates a new one. This context-lazy loading is designed to make testing | |
238 | * easier.<p> | |
239 | * | |
240 | * @return The invocation context. | |
241 | */ | |
242 | private InvocationContext getContext() { | |
243 | 16 | if (this.context == null) { |
244 | 0 | initContext(); |
245 | } | |
246 | 16 | return this.context; |
247 | } | |
248 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |