KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > madvoc > WebApplication


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

3 package jodd.madvoc;
4
5 import jodd.madvoc.result.ActionResult;
6 import jodd.madvoc.result.ServletDispatcherResult;
7 import jodd.madvoc.interceptor.ActionInterceptor;
8 import jodd.madvoc.interceptor.ServletConfigInterceptor;
9 import jodd.madvoc.interceptor.RenderViewInterceptor;
10 import jodd.servlet.DispatcherUtil;
11 import jodd.servlet.upload.FileUploadFactory;
12 import jodd.servlet.upload.impl.AdaptiveFileUpload;
13
14 import javax.servlet.ServletContext JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.HashMap JavaDoc;
19
20 /**
21  * Contains all configurations and definitions for single web application.
22  * <p>
23  * Custom implementations may override this class to enhance several different
24  * functionalities. Of course, it would be more modular to have separate
25  * interface for each functionality, but this will lead to too-many
26  * interfaces and classes. Anyhow, if needed, one abstract modular
27  * web application may be created as a subclass.
28  */

29 public class WebApplication {
30
31     @SuppressWarnings JavaDoc({"unchecked"})
32     public WebApplication(ServletContext JavaDoc servletContext) {
33         configs = new HashMap JavaDoc<String JavaDoc, ActionConfig>();
34         results = new HashMap JavaDoc<String JavaDoc, ActionResult>();
35         resultsAliases = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
36         this.servletContext = servletContext;
37         defaultActionResultName = ServletDispatcherResult.NAME;
38         encoding = "UTF-8";
39         fileUploadFactory = new AdaptiveFileUpload.Factory();
40         defaultInterceptors = new Class JavaDoc[]{RenderViewInterceptor.class, ServletConfigInterceptor.class};
41     }
42
43
44     // ---------------------------------------------------------------- encoding
45

46     protected String JavaDoc encoding;
47
48     /**
49      * Returns character encoding.
50      */

51     public String JavaDoc getEncoding() {
52         return this.encoding;
53     }
54     /**
55      * Sets web application encoding.
56      */

57     public void setEncoding(String JavaDoc encoding) {
58         this.encoding = encoding;
59     }
60
61
62     // ---------------------------------------------------------------- file upload factory
63

64     protected FileUploadFactory fileUploadFactory;
65
66     /**
67      * Returns current file upload factory.
68      */

69     public FileUploadFactory getFileUploadFactory() {
70         return fileUploadFactory;
71     }
72
73     /**
74      * Sets file upload factory.
75      */

76     public void setFileUploadFactory(FileUploadFactory fileUploadFactory) {
77         this.fileUploadFactory = fileUploadFactory;
78     }
79
80     // ---------------------------------------------------------------- app ctx
81

82     protected final ServletContext JavaDoc servletContext;
83
84     /**
85      * Returns application {@link javax.servlet.ServletContext}.
86      */

87     public ServletContext JavaDoc getServletContext() {
88         return servletContext;
89     }
90
91     // ---------------------------------------------------------------- action configs
92

93     protected final Map JavaDoc<String JavaDoc, ActionConfig> configs;
94
95     /**
96      * Returns all registered action configurations. Should be used with care and
97      * usually only during configuration.
98      */

99     public Map JavaDoc<String JavaDoc, ActionConfig> getAllActionConfigs() {
100         return configs;
101     }
102
103     /**
104      * Registers new action config.
105      */

106     public void register(ActionConfig actionConfig) {
107         configs.put(actionConfig.actionPath, actionConfig);
108     }
109
110     /**
111      * Returns action config assigned to provided actionPath.
112      */

113     public ActionConfig lookupActionConfig(String JavaDoc actionPath) {
114         return configs.get(actionPath);
115     }
116
117
118     /**
119      * Builds action request for current servlet request. Returns <code>null</code> if
120      * servlet path is not registeres as action path.
121      * <p>
122      * Custom implementations may include different logic for converting request path to action path.
123      */

124     public ActionRequest buildActionRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
125         String JavaDoc requestPath = DispatcherUtil.getServletPath(request);
126         ActionConfig config = configs.get(requestPath);
127         if (config == null) {
128             return null;
129         }
130         return buildActionRequest(config, request, response);
131     }
132
133
134     // ---------------------------------------------------------------- default interceptors
135

136     protected Class JavaDoc<? extends ActionInterceptor>[] defaultInterceptors;
137
138     /**
139      * Returns default intercetors.
140      */

141     public Class JavaDoc<? extends ActionInterceptor>[] getDefaultInterceptors() {
142         return defaultInterceptors;
143     }
144
145     /**
146      * Set default interceptors.
147      */

148     public void setDefaultInterceptors(Class JavaDoc<? extends ActionInterceptor>[] defaultInterceptors) {
149         this.defaultInterceptors = defaultInterceptors;
150     }
151
152     // ---------------------------------------------------------------- result handlers
153

154     protected final Map JavaDoc<String JavaDoc, ActionResult> results;
155
156     /**
157      * Registes new action result.
158      */

159     public void register(ActionResult actionResult) {
160         results.put(actionResult.getName(), actionResult);
161     }
162
163     /**
164      * Returns action result for specified result name.
165      */

166     public ActionResult lookupActionResult(String JavaDoc resultName) {
167         return results.get(resultName);
168     }
169
170     protected String JavaDoc defaultActionResultName;
171
172     /**
173      * Specifes default result name. Result must be already
174      * registered before setting it to default.
175      * @see #register(jodd.madvoc.result.ActionResult)
176      */

177     public void setDefaultActionResultName(String JavaDoc name) {
178         defaultActionResultName = name;
179     }
180
181     /**
182      * Returns default action result name.
183      */

184     public String JavaDoc getDefaultActionResultName() {
185         return defaultActionResultName;
186     }
187
188
189     // ---------------------------------------------------------------- result aliases
190

191     protected Map JavaDoc<String JavaDoc, String JavaDoc> resultsAliases;
192
193     /**
194      * Sets alias value.
195      */

196     public void setResultAlias(String JavaDoc alias, String JavaDoc target) {
197         if (alias.startsWith("/") == false) {
198             alias = '/' + alias;
199         }
200         resultsAliases.put(alias, target);
201     }
202
203     /**
204      * Returns result alias or its value result if no alias is defined.
205      */

206     public String JavaDoc getResultAlias(String JavaDoc alias) {
207         String JavaDoc value = resultsAliases.get(alias);
208         if (value != null) {
209             return value;
210         }
211         return alias;
212     }
213
214
215     // ---------------------------------------------------------------- object factory
216

217     /**
218      * Creates new {@link ActionRequest},.
219      */

220     protected ActionRequest buildActionRequest(ActionConfig config, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
221         return new ActionRequest(this, config, request, response);
222     }
223
224     /**
225      * Creates new {@link jodd.madvoc.interceptor.ActionInterceptor}.
226      */

227     public ActionInterceptor buildInterceptor(Class JavaDoc<? extends ActionInterceptor> actionInterceptorClass) {
228         ActionInterceptor interceptor;
229         try {
230             interceptor = actionInterceptorClass.newInstance();
231             //interceptor.init(this); toask add intreceptor initialization if needed
232
} catch (InstantiationException JavaDoc iex) {
233             throw new MadvocException("Unable to create Madvoc action interceptor '" + actionInterceptorClass + "'.", iex);
234         } catch (IllegalAccessException JavaDoc iaex) {
235             throw new MadvocException("Not enough rights to create Madvoc action interceptor '" + actionInterceptorClass + "'.", iaex);
236         }
237         return interceptor;
238     }
239
240     /**
241      * Creates a new action object from {@link ActionConfig}.
242      */

243     public Object JavaDoc buildAction(ActionConfig actionConfig) {
244         Object JavaDoc action;
245         try {
246             action = actionConfig.actionClass.newInstance();
247         } catch (InstantiationException JavaDoc iex) {
248             throw new MadvocException("Unable to create Madvoc action.", iex);
249         } catch (IllegalAccessException JavaDoc iaex) {
250             throw new MadvocException("Not enough rights to create Madvoc action.", iaex);
251         }
252         return action;
253     }
254
255 }
256
Popular Tags