001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/controller/actions/DefaultAction.java,v 1.1.1.1 2004/03/25 12:08:38 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:38 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.kernel.controller.actions;
042:
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045: import org.apache.struts.action.Action;
046: import org.apache.struts.action.ActionForm;
047: import org.apache.struts.action.ActionForward;
048: import org.apache.struts.action.ActionMapping;
049:
050: import es.udc.mypersonalizer.kernel.log.Log;
051: import es.udc.mypersonalizer.kernel.log.LogManager;
052: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
053: import es.udc.mypersonalizer.kernel.util.exceptions.IllegalConcurrentAccessException;
054:
055: /**
056: * Template action for stuts action classes that implements some of
057: * the functionality needed for all subclasses like exception handling. <p>
058: *
059: * If during execution of the action, an
060: * <code>IllegalConcurrentAccessException</code> is thrown, it tries to find an
061: * <code>ActionForward</code> with name "IllegalConcurrentAccess" and returns it.
062: * In case of any other exception, it tries to find an
063: * <code>ActionForward</code> with name "InternalError" and returns it.
064: *
065: * @author Daniel Fernandez
066: * @author Fernando Bellas
067: * @since 1.0
068: */
069: public abstract class DefaultAction extends Action {
070:
071: /**
072: * The global forward name for the Illegal Concurrent Access page.<p>
073: * This name must be "IllegalConcurrentAccess".
074: */
075: public static final String ILLEGAL_CONCURRENT_ACCESS_GLOBAL_FORWARD = "IllegalConcurrentAccess";
076:
077: /**
078: * The global forward name for the Internal Error page.<p>
079: * This name must be "InternalError".
080: */
081: public static final String INTERNAL_ERROR_GLOBAL_FORWARD = "InternalError";
082:
083: /**
084: * The per-action forward name for the success page. If an action is able to
085: * execute all of its tasks correctly, it performs a forward to the
086: * mandatory struts forward named "Success".<p>
087: *
088: * Every controller action must have a forward named "Success".
089: */
090: public static final String ACTION_SUCCESS_FORWARD = "Success";
091:
092: /**
093: * The per-action forward name for the fail page. This forward is needed
094: * in case an action can fail (without generation an exception). For
095: * example, due to a bad data input, the user should have to be informed
096: * of the problem without generating an exception, just a warning of any
097: * kind.<p>
098: * This forward identifies the page which will report the errors, and
099: * only actions which can fail without generating severe exception
100: * conditions must define it.
101: */
102: public static final String ACTION_FAIL_FORWARD = "Fail";
103:
104: /**
105: * This is the path of the action that executes MainPageAction.<p>
106: * Must be set to "/MainPage.do"
107: */
108: public static final String MAIN_PAGE_FORWARD_PATH = "/MainPage.do";
109:
110: /**
111: * Default prefix for error messages keys in localized resource files.<p>
112: *
113: * This prefix is "<code>ErrorMessages.</code>".
114: */
115: public static final String ERROR_MESSAGES_PREFIX = "ErrorMessages.";
116:
117: /**
118: * The mypersonalizer log name.
119: */
120: private static final String MYPERSONALIZER_LOG_NAME = LogNamingConventions.MYPERSONALIZER;
121:
122: /**
123: * Executes an action calling doExecute. If an exception is
124: * thrown, it executes doOnError.
125: *
126: * @return an ActionForward with the result of the action
127: * @param mapping the action's ActionMapping
128: * @param form the ActionForm associatied with this class
129: * @param request the request object
130: * @param response the response object
131: */
132: public ActionForward execute(ActionMapping mapping,
133: ActionForm form, HttpServletRequest request,
134: HttpServletResponse response) {
135:
136: ActionForward actionForward = null;
137:
138: try {
139: actionForward = doExecute(mapping, form, request, response);
140: } catch (Exception e) {
141: actionForward = doOnError(mapping, form, request, response,
142: e);
143: }
144:
145: return actionForward;
146: }
147:
148: /**
149: * Executes the action
150: *
151: * @return an ActionForward with the result of the action
152: * @param mapping the action's ActionMapping
153: * @param form the ActionForm associatied with this class
154: * @param request the request object
155: * @param response the response object
156: * @throws Exception Any exception can be thrown by the action. However,
157: * it is recommended to handle all exceptions and provide meaningful
158: * information to the final user.
159: */
160: protected abstract ActionForward doExecute(ActionMapping mapping,
161: ActionForm form, HttpServletRequest request,
162: HttpServletResponse response) throws Exception;
163:
164: /**
165: * Performs whichever necessary actions to handle an exception
166: * thrown during execution
167: *
168: * @return an ActionForward with exception-handling page
169: * @param mapping the action's ActionMapping
170: * @param form the ActionForm associatied with this class
171: * @param request the request object
172: * @param response the response object
173: * @param exception the exception to process
174: */
175: protected ActionForward doOnError(ActionMapping mapping,
176: ActionForm form, HttpServletRequest request,
177: HttpServletResponse response, Exception exception) {
178:
179: Log mypersonalizerLog = LogManager
180: .getLog(MYPERSONALIZER_LOG_NAME);
181:
182: request.setAttribute("exception", exception);
183:
184: if (exception instanceof IllegalConcurrentAccessException) {
185:
186: mypersonalizerLog.write(
187: "An illegal concurrent access happened", exception,
188: this .getClass());
189:
190: return mapping
191: .findForward(ILLEGAL_CONCURRENT_ACCESS_GLOBAL_FORWARD);
192:
193: } else {
194:
195: mypersonalizerLog.write("An internal error happened",
196: exception, this.getClass());
197:
198: return mapping.findForward(INTERNAL_ERROR_GLOBAL_FORWARD);
199:
200: }
201: }
202:
203: }
|