Source Code Cross Referenced for ExceptionMappingInterceptor.java in  » J2EE » webwork-2.2.6 » com » opensymphony » xwork » interceptor » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » webwork 2.2.6 » com.opensymphony.xwork.interceptor 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2006 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.xwork.interceptor;
006:
007:        import com.opensymphony.xwork.ActionInvocation;
008:        import com.opensymphony.xwork.config.entities.ExceptionMappingConfig;
009:        import org.apache.commons.logging.Log;
010:        import org.apache.commons.logging.LogFactory;
011:
012:        import java.util.Iterator;
013:        import java.util.List;
014:
015:        /**
016:         * <!-- START SNIPPET: description -->
017:         *
018:         * This interceptor forms the core functionality of the exception handling feature. Exception handling allows you to map
019:         * an exception to a result code, just as if the action returned a result code instead of throwing an unexpected
020:         * exception. When an exception is encountered, it is wrapped with an {@link ExceptionHolder} and pushed on the stack,
021:         * providing easy access to the exception from within your result.
022:         *
023:         * <b>Note:</b> While you can configure exception mapping in your configuration file at any point, the configuration
024:         * will not have any effect if this interceptor is not in the interceptor stack for your actions. It is recommended that
025:         * you make this interceptor the first interceptor on the stack, ensuring that it has full access to catch any
026:         * exception, even those caused by other interceptors.
027:         *
028:         * <!-- END SNIPPET: description -->
029:         *
030:         * <p/> <u>Interceptor parameters:</u>
031:         *
032:         * <!-- START SNIPPET: parameters -->
033:         *
034:         * <ul>
035:         *
036:         * <li>logEnabled (optional) - Should exceptions also be logged? (boolean true|false)</li>
037:         * 
038:         * <li>logLevel (optional) - what log level should we use (<code>trace, debug, info, warn, error, fatal</code>)? - defaut is <code>debug</code></li>
039:         * 
040:         * <li>logCategory (optional) - If provided we would use this category (eg. <code>com.mycompany.app</code>).
041:         * Default is to use <code>com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor</code>.</li>
042:         *
043:         * </ul>
044:         *
045:         * The parameters above enables us to log all thrown exceptions with stacktace in our own logfile,
046:         * and present a friendly webpage (with no stacktrace) to the end user.
047:         *
048:         * <!-- END SNIPPET: parameters -->
049:         *
050:         * <p/> <u>Extending the interceptor:</u>
051:         *
052:         * <p/>
053:         *
054:         * <!-- START SNIPPET: extending -->
055:         *
056:         * If you want to add custom handling for publishing the Exception, you may override
057:         * {@link #publishException(com.opensymphony.xwork.ActionInvocation, ExceptionHolder)}. The default implementation
058:         * pushes the given ExceptionHolder on value stack. A custom implementation could add additional logging etc.
059:         *
060:         * <!-- END SNIPPET: extending -->
061:         *
062:         * <p/> <u>Example code:</u>
063:         *
064:         * <pre>
065:         * <!-- START SNIPPET: example -->
066:         * &lt;xwork&gt;
067:         *     &lt;include file="webwork-default.xml"/&gt;
068:         *
069:         *     &lt;package name="default" extends="webwork-default"&gt;
070:         *         &lt;global-results&gt;
071:         *             &lt;result name="error" type="freemarker"&gt;error.ftl&lt;/result&gt;
072:         *         &lt;/global-results&gt;
073:         *
074:         *         &lt;global-exception-mappings&gt;
075:         *             &lt;exception-mapping exception="java.lang.Exception" result="error"/&gt;
076:         *         &lt;/global-exception-mappings&gt;
077:         *
078:         *         &lt;action name="test"&gt;
079:         *             &lt;interceptor-ref name="exception"/&gt;
080:         *             &lt;interceptor-ref name="basicStack"/&gt;
081:         *             &lt;exception-mapping exception="com.acme.CustomException" result="custom_error"/&gt;
082:         *             &lt;result name="custom_error"&gt;custom_error.ftl&lt;/result&gt;
083:         *             &lt;result name="success" type="freemarker"&gt;test.ftl&lt;/result&gt;
084:         *         &lt;/action&gt;
085:         *     &lt;/package&gt;
086:         * &lt;/xwork&gt;
087:         * <!-- END SNIPPET: example -->
088:         * </pre>
089:         * 
090:         * <p/>
091:         * This second example will also log the exceptions using our own category
092:         * <code>com.mycompany.app.unhandled<code> at WARN level. 
093:         * 
094:         * <pre>
095:         * <!-- START SNIPPET: example2 -->
096:         * &lt;xwork&gt;
097:         *   &lt;include file="webwork-default.xml"/&gt;
098:         *     
099:         *   &lt;package name="something" extends="webwork-default"&gt;
100:         *      &lt;interceptors&gt;
101:         *          &lt;interceptor-stack name="exceptionmapping-stack"&gt;
102:         *              &lt;interceptor-ref name="exception"&gt;
103:         *                  &lt;param name="logEnabled"&gt;true&lt;/param&gt;
104:         *                  &lt;param name="logCategory"&gt;com.mycompany.app.unhandled&lt;/param&gt;
105:         *                  &lt;param name="logLevel"&gt;WARN&lt;/param&gt;	        		
106:         *              &lt;/interceptor-ref&gt;	
107:         *              &lt;interceptor-ref name="i18n"/&gt;
108:         *              &lt;interceptor-ref name="static-params"/&gt;
109:         *              &lt;interceptor-ref name="params"/&gt;
110:         *              &lt;interceptor-ref name="validation"&gt;
111:         *                  &lt;param name="excludeMethods"&gt;input,back,cancel,browse&lt;/param&gt;
112:         *              &lt;/interceptor-ref&gt;
113:         *          &lt;/interceptor-stack&gt;
114:         *      &lt;/interceptors&gt;
115:         *
116:         *      &lt;default-interceptor-ref name="exceptionmapping-stack"/&gt;
117:         *    
118:         *      &lt;global-results&gt;
119:         *           &lt;result name="unhandledException"&gt;/unhandled-exception.jsp&lt;/result&gt;
120:         *      &lt;/global-results&gt;
121:         *
122:         *      &lt;global-exception-mappings&gt;
123:         *           &lt;exception-mapping exception="java.lang.Exception" result="unhandledException"/&gt;
124:         *      &lt;/global-exception-mappings&gt;
125:         *        
126:         *      &lt;action name="exceptionDemo" class="com.opensymphony.webwork.showcase.exceptionmapping.ExceptionMappingAction"&gt;
127:         *          &lt;exception-mapping exception="com.opensymphony.webwork.showcase.exceptionmapping.ExceptionMappingException"
128:         *                             result="damm"/&gt;
129:         *          &lt;result name="input"&gt;index.jsp&lt;/result&gt;
130:         *          &lt;result name="success"&gt;success.jsp&lt;/result&gt;            
131:         *          &lt;result name="damm"&gt;damm.jsp&lt;/result&gt;
132:         *      &lt;/action&gt;
133:         *
134:         *   &lt;/package&gt;
135:         * &lt;/xwork&gt;
136:         * <!-- END SNIPPET: example2 -->
137:         * </pre>
138:         *
139:         * @author Matthew E. Porter (matthew dot porter at metissian dot com) 
140:         * @author Claus Ibsen
141:         */
142:        public class ExceptionMappingInterceptor implements  Interceptor {
143:
144:            protected static final Log log = LogFactory
145:                    .getLog(ExceptionMappingInterceptor.class);
146:
147:            protected Log categoryLogger;
148:            protected boolean logEnabled = false;
149:            protected String logCategory;
150:            protected String logLevel;
151:
152:            public boolean isLogEnabled() {
153:                return logEnabled;
154:            }
155:
156:            public void setLogEnabled(boolean logEnabled) {
157:                this .logEnabled = logEnabled;
158:            }
159:
160:            public String getLogCategory() {
161:                return logCategory;
162:            }
163:
164:            public void setLogCategory(String logCatgory) {
165:                this .logCategory = logCatgory;
166:            }
167:
168:            public String getLogLevel() {
169:                return logLevel;
170:            }
171:
172:            public void setLogLevel(String logLevel) {
173:                this .logLevel = logLevel;
174:            }
175:
176:            public void destroy() {
177:            }
178:
179:            public void init() {
180:            }
181:
182:            public String intercept(ActionInvocation invocation)
183:                    throws Exception {
184:                String result;
185:
186:                try {
187:                    result = invocation.invoke();
188:                } catch (Exception e) {
189:                    if (logEnabled) {
190:                        handleLogging(e);
191:                    }
192:                    List exceptionMappings = invocation.getProxy().getConfig()
193:                            .getExceptionMappings();
194:                    String mappedResult = this .findResultFromExceptions(
195:                            exceptionMappings, e);
196:                    if (mappedResult != null) {
197:                        result = mappedResult;
198:                        publishException(invocation, new ExceptionHolder(e));
199:                    } else {
200:                        throw e;
201:                    }
202:                }
203:
204:                return result;
205:            }
206:
207:            /**
208:             * Handles the logging of the exception.
209:             * 
210:             * @param e  the exception to log.
211:             */
212:            protected void handleLogging(Exception e) {
213:                if (logCategory != null) {
214:                    if (categoryLogger == null) {
215:                        // init category logger
216:                        categoryLogger = LogFactory.getLog(logCategory);
217:                    }
218:                    doLog(categoryLogger, e);
219:                } else {
220:                    doLog(log, e);
221:                }
222:            }
223:
224:            /**
225:             * Performs the actual logging.
226:             * 
227:             * @param logger  the provided logger to use.
228:             * @param e  the exception to log.
229:             */
230:            protected void doLog(Log logger, Exception e) {
231:                if (logLevel == null) {
232:                    logger.debug(e.getMessage(), e);
233:                    return;
234:                }
235:
236:                if ("trace".equalsIgnoreCase(logLevel)) {
237:                    logger.trace(e.getMessage(), e);
238:                } else if ("debug".equalsIgnoreCase(logLevel)) {
239:                    logger.debug(e.getMessage(), e);
240:                } else if ("info".equalsIgnoreCase(logLevel)) {
241:                    logger.info(e.getMessage(), e);
242:                } else if ("warn".equalsIgnoreCase(logLevel)) {
243:                    logger.warn(e.getMessage(), e);
244:                } else if ("error".equalsIgnoreCase(logLevel)) {
245:                    logger.error(e.getMessage(), e);
246:                } else if ("fatal".equalsIgnoreCase(logLevel)) {
247:                    logger.fatal(e.getMessage(), e);
248:                } else {
249:                    throw new IllegalArgumentException("LogLevel [" + logLevel
250:                            + "] is not supported");
251:                }
252:            }
253:
254:            private String findResultFromExceptions(List exceptionMappings,
255:                    Throwable t) {
256:                String result = null;
257:
258:                // Check for specific exception mappings.
259:                if (exceptionMappings != null) {
260:                    int deepest = Integer.MAX_VALUE;
261:                    for (Iterator iter = exceptionMappings.iterator(); iter
262:                            .hasNext();) {
263:                        ExceptionMappingConfig exceptionMappingConfig = (ExceptionMappingConfig) iter
264:                                .next();
265:                        int depth = getDepth(exceptionMappingConfig
266:                                .getExceptionClassName(), t);
267:                        if (depth >= 0 && depth < deepest) {
268:                            deepest = depth;
269:                            result = exceptionMappingConfig.getResult();
270:                        }
271:                    }
272:                }
273:
274:                return result;
275:            }
276:
277:            /**
278:             * Return the depth to the superclass matching. 0 means ex matches exactly. Returns -1 if there's no match.
279:             * Otherwise, returns depth. Lowest depth wins.
280:             *
281:             * @param exceptionMapping  the mapping classname
282:             * @param t  the cause
283:             * @return the depth, if not found -1 is returned.
284:             */
285:            public int getDepth(String exceptionMapping, Throwable t) {
286:                return getDepth(exceptionMapping, t.getClass(), 0);
287:            }
288:
289:            private int getDepth(String exceptionMapping, Class exceptionClass,
290:                    int depth) {
291:                if (exceptionClass.getName().indexOf(exceptionMapping) != -1) {
292:                    // Found it!
293:                    return depth;
294:                }
295:                // If we've gone as far as we can go and haven't found it...
296:                if (exceptionClass.equals(Throwable.class)) {
297:                    return -1;
298:                }
299:                return getDepth(exceptionMapping, exceptionClass
300:                        .getSuperclass(), depth + 1);
301:            }
302:
303:            /**
304:             * Default implementation to handle ExceptionHolder publishing. Pushes given ExceptionHolder on the stack.
305:             * Subclasses may override this to customize publishing.
306:             *
307:             * @param invocation The invocation to publish Exception for.
308:             * @param exceptionHolder The exceptionHolder wrapping the Exception to publish.
309:             */
310:            protected void publishException(ActionInvocation invocation,
311:                    ExceptionHolder exceptionHolder) {
312:                invocation.getStack().push(exceptionHolder);
313:            }
314:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.