001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.madvoc;
004:
005: import jodd.madvoc.result.ActionResult;
006: import jodd.madvoc.result.ServletDispatcherResult;
007: import jodd.madvoc.interceptor.ActionInterceptor;
008: import jodd.madvoc.interceptor.ServletConfigInterceptor;
009: import jodd.madvoc.interceptor.RenderViewInterceptor;
010: import jodd.servlet.DispatcherUtil;
011: import jodd.servlet.upload.FileUploadFactory;
012: import jodd.servlet.upload.impl.AdaptiveFileUpload;
013:
014: import javax.servlet.ServletContext;
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017: import java.util.Map;
018: import java.util.HashMap;
019:
020: /**
021: * Contains all configurations and definitions for single web application.
022: * <p>
023: * Custom implementations may override this class to enhance several different
024: * functionalities. Of course, it would be more modular to have separate
025: * interface for each functionality, but this will lead to too-many
026: * interfaces and classes. Anyhow, if needed, one abstract modular
027: * web application may be created as a subclass.
028: */
029: public class WebApplication {
030:
031: @SuppressWarnings({"unchecked"})
032: public WebApplication(ServletContext servletContext) {
033: configs = new HashMap<String, ActionConfig>();
034: results = new HashMap<String, ActionResult>();
035: resultsAliases = new HashMap<String, String>();
036: this .servletContext = servletContext;
037: defaultActionResultName = ServletDispatcherResult.NAME;
038: encoding = "UTF-8";
039: fileUploadFactory = new AdaptiveFileUpload.Factory();
040: defaultInterceptors = new Class[] {
041: RenderViewInterceptor.class,
042: ServletConfigInterceptor.class };
043: }
044:
045: // ---------------------------------------------------------------- encoding
046:
047: protected String encoding;
048:
049: /**
050: * Returns character encoding.
051: */
052: public String getEncoding() {
053: return this .encoding;
054: }
055:
056: /**
057: * Sets web application encoding.
058: */
059: public void setEncoding(String encoding) {
060: this .encoding = encoding;
061: }
062:
063: // ---------------------------------------------------------------- file upload factory
064:
065: protected FileUploadFactory fileUploadFactory;
066:
067: /**
068: * Returns current file upload factory.
069: */
070: public FileUploadFactory getFileUploadFactory() {
071: return fileUploadFactory;
072: }
073:
074: /**
075: * Sets file upload factory.
076: */
077: public void setFileUploadFactory(FileUploadFactory fileUploadFactory) {
078: this .fileUploadFactory = fileUploadFactory;
079: }
080:
081: // ---------------------------------------------------------------- app ctx
082:
083: protected final ServletContext servletContext;
084:
085: /**
086: * Returns application {@link javax.servlet.ServletContext}.
087: */
088: public ServletContext getServletContext() {
089: return servletContext;
090: }
091:
092: // ---------------------------------------------------------------- action configs
093:
094: protected final Map<String, ActionConfig> configs;
095:
096: /**
097: * Returns all registered action configurations. Should be used with care and
098: * usually only during configuration.
099: */
100: public Map<String, ActionConfig> getAllActionConfigs() {
101: return configs;
102: }
103:
104: /**
105: * Registers new action config.
106: */
107: public void register(ActionConfig actionConfig) {
108: configs.put(actionConfig.actionPath, actionConfig);
109: }
110:
111: /**
112: * Returns action config assigned to provided actionPath.
113: */
114: public ActionConfig lookupActionConfig(String actionPath) {
115: return configs.get(actionPath);
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 request,
125: HttpServletResponse response) {
126: String requestPath = DispatcherUtil.getServletPath(request);
127: ActionConfig config = configs.get(requestPath);
128: if (config == null) {
129: return null;
130: }
131: return buildActionRequest(config, request, response);
132: }
133:
134: // ---------------------------------------------------------------- default interceptors
135:
136: protected Class<? extends ActionInterceptor>[] defaultInterceptors;
137:
138: /**
139: * Returns default intercetors.
140: */
141: public Class<? extends ActionInterceptor>[] getDefaultInterceptors() {
142: return defaultInterceptors;
143: }
144:
145: /**
146: * Set default interceptors.
147: */
148: public void setDefaultInterceptors(
149: Class<? extends ActionInterceptor>[] defaultInterceptors) {
150: this .defaultInterceptors = defaultInterceptors;
151: }
152:
153: // ---------------------------------------------------------------- result handlers
154:
155: protected final Map<String, ActionResult> results;
156:
157: /**
158: * Registes new action result.
159: */
160: public void register(ActionResult actionResult) {
161: results.put(actionResult.getName(), actionResult);
162: }
163:
164: /**
165: * Returns action result for specified result name.
166: */
167: public ActionResult lookupActionResult(String resultName) {
168: return results.get(resultName);
169: }
170:
171: protected String defaultActionResultName;
172:
173: /**
174: * Specifes default result name. Result must be already
175: * registered before setting it to default.
176: * @see #register(jodd.madvoc.result.ActionResult)
177: */
178: public void setDefaultActionResultName(String name) {
179: defaultActionResultName = name;
180: }
181:
182: /**
183: * Returns default action result name.
184: */
185: public String getDefaultActionResultName() {
186: return defaultActionResultName;
187: }
188:
189: // ---------------------------------------------------------------- result aliases
190:
191: protected Map<String, String> resultsAliases;
192:
193: /**
194: * Sets alias value.
195: */
196: public void setResultAlias(String alias, String 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 getResultAlias(String alias) {
207: String value = resultsAliases.get(alias);
208: if (value != null) {
209: return value;
210: }
211: return alias;
212: }
213:
214: // ---------------------------------------------------------------- object factory
215:
216: /**
217: * Creates new {@link ActionRequest},.
218: */
219: protected ActionRequest buildActionRequest(ActionConfig config,
220: HttpServletRequest request, HttpServletResponse 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(
228: Class<? extends ActionInterceptor> actionInterceptorClass) {
229: ActionInterceptor interceptor;
230: try {
231: interceptor = actionInterceptorClass.newInstance();
232: //interceptor.init(this); toask add intreceptor initialization if needed
233: } catch (InstantiationException iex) {
234: throw new MadvocException(
235: "Unable to create Madvoc action interceptor '"
236: + actionInterceptorClass + "'.", iex);
237: } catch (IllegalAccessException iaex) {
238: throw new MadvocException(
239: "Not enough rights to create Madvoc action interceptor '"
240: + actionInterceptorClass + "'.", iaex);
241: }
242: return interceptor;
243: }
244:
245: /**
246: * Creates a new action object from {@link ActionConfig}.
247: */
248: public Object buildAction(ActionConfig actionConfig) {
249: Object action;
250: try {
251: action = actionConfig.actionClass.newInstance();
252: } catch (InstantiationException iex) {
253: throw new MadvocException(
254: "Unable to create Madvoc action.", iex);
255: } catch (IllegalAccessException iaex) {
256: throw new MadvocException(
257: "Not enough rights to create Madvoc action.", iaex);
258: }
259: return action;
260: }
261:
262: }
|