Source Code Cross Referenced for TimerInterceptor.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 org.apache.commons.logging.Log;
009:        import org.apache.commons.logging.LogFactory;
010:
011:        /**
012:         * <!-- START SNIPPET: description -->
013:         * This interceptor logs the amount of time in milliseconds. In order for this interceptor to work properly, the
014:         * logging framework must be set to at least the
015:         * <a href="http://jakarta.apache.org/commons/logging/api/org/apache/commons/logging/Log.html">INFO</a> level.
016:         * This interceptor relies on the
017:         * <a href="http://jakarta.apache.org/commons/logging/">Commons Logging API</a> to report its execution-time value.
018:         * <!-- END SNIPPET: description -->
019:         *
020:         * <!-- START SNIPPET: parameters -->
021:         *
022:         * <ul>
023:         *
024:         * <li>logLevel (optional) - what log level should we use (<code>trace, debug, info, warn, error, fatal</code>)? - defaut is <code>info</code></li>
025:         *
026:         * <li>logCategory (optional) - If provided we would use this category (eg. <code>com.mycompany.app</code>).
027:         * Default is to use <code>com.opensymphony.xwork.interceptor.TimerInterceptor</code>.</li>
028:         *
029:         * </ul>
030:         *
031:         * The parameters above enables us to log all action execution times in our own logfile.
032:         *
033:         * <!-- END SNIPPET: parameters -->
034:         *
035:         * <!-- START SNIPPET: extending -->
036:         * This interceptor can be extended to provide custom message format. Users should override the
037:         * <code>invokeUnderTiming</code> method.
038:         * <!-- END SNIPPET: extending -->
039:         *
040:         * <pre>
041:         * <!-- START SNIPPET: example -->
042:         * &lt;!-- records only the action's execution time --&gt;
043:         * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
044:         *     &lt;interceptor-ref name="completeStack"/&gt;
045:         *     &lt;interceptor-ref name="timer"/&gt;
046:         *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
047:         * &lt;/action&gt;
048:         *
049:         * &lt;!-- records action's execution time as well as other interceptors--&gt;
050:         * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
051:         *     &lt;interceptor-ref name="timer"/&gt;
052:         *     &lt;interceptor-ref name="completeStack"/&gt;
053:         *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
054:         * &lt;/action&gt;
055:         * <!-- END SNIPPET: example -->
056:         * </pre>
057:         *
058:         * This second example uses our own log category at debug level.
059:         *
060:         * <pre>
061:         * <!-- START SNIPPET: example2 -->
062:         * &lt;!-- records only the action's execution time --&gt;
063:         * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
064:         *     &lt;interceptor-ref name="completeStack"/&gt;
065:         *     &lt;interceptor-ref name="timer"&gt;
066:         *         &lt;param name="logLevel"&gt;debug&lt;/param&gt;
067:         *         &lt;param name="logCategory"&gt;com.mycompany.myapp.actiontime&lt;/param&gt;
068:         *     &lt;interceptor-ref/&gt;
069:         *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
070:         * &lt;/action&gt;
071:         *
072:         * &lt;!-- records action's execution time as well as other interceptors--&gt;
073:         * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
074:         *     &lt;interceptor-ref name="timer"/&gt;
075:         *     &lt;interceptor-ref name="completeStack"/&gt;
076:         *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
077:         * &lt;/action&gt;
078:         * <!-- END SNIPPET: example2 -->
079:         * </pre>
080:         *
081:         * @author Jason Carreira
082:         * @author Claus Ibsen
083:         */
084:        public class TimerInterceptor implements  Interceptor {
085:            protected static final Log log = LogFactory
086:                    .getLog(TimerInterceptor.class);
087:
088:            protected Log categoryLogger;
089:            protected String logCategory;
090:            protected String logLevel;
091:
092:            public String getLogCategory() {
093:                return logCategory;
094:            }
095:
096:            public void setLogCategory(String logCatgory) {
097:                this .logCategory = logCatgory;
098:            }
099:
100:            public String getLogLevel() {
101:                return logLevel;
102:            }
103:
104:            public void setLogLevel(String logLevel) {
105:                this .logLevel = logLevel;
106:            }
107:
108:            public void destroy() {
109:            }
110:
111:            public void init() {
112:            }
113:
114:            public String intercept(ActionInvocation invocation)
115:                    throws Exception {
116:                if (!shouldLog()) {
117:                    return invocation.invoke();
118:                } else {
119:                    return invokeUnderTiming(invocation);
120:                }
121:            }
122:
123:            /**
124:             * Is called to invoke the action invocation and time the execution time.
125:             *
126:             * @param invocation  the action invocation.
127:             * @return the result of the action execution.
128:             * @throws Exception  can be thrown from the action.
129:             */
130:            protected String invokeUnderTiming(ActionInvocation invocation)
131:                    throws Exception {
132:                long startTime = System.currentTimeMillis();
133:                String result = invocation.invoke();
134:                long executionTime = System.currentTimeMillis() - startTime;
135:
136:                StringBuffer message = new StringBuffer(100);
137:                message.append("Executed action [");
138:                String namespace = invocation.getProxy().getNamespace();
139:                if ((namespace != null) && (namespace.trim().length() > 0)) {
140:                    message.append(namespace).append("/");
141:                }
142:                message.append(invocation.getProxy().getActionName());
143:                message.append("!");
144:                message.append(invocation.getProxy().getMethod());
145:                message.append("] took ").append(executionTime).append(" ms.");
146:
147:                doLog(getLoggerToUse(), message.toString());
148:
149:                return result;
150:            }
151:
152:            /**
153:             * Determines if we should log the time.
154:             *
155:             * @return  true to log, false to not log.
156:             */
157:            protected boolean shouldLog() {
158:                // default check first
159:                if (logLevel == null && logCategory == null) {
160:                    return log.isInfoEnabled();
161:                }
162:
163:                // okay user have set some parameters
164:                return isLoggerEnabled(getLoggerToUse(), logLevel);
165:            }
166:
167:            /**
168:             * Get's the logger to use.
169:             *
170:             * @return the logger to use.
171:             */
172:            protected Log getLoggerToUse() {
173:                if (logCategory != null) {
174:                    if (categoryLogger == null) {
175:                        // init category logger
176:                        categoryLogger = LogFactory.getLog(logCategory);
177:                        if (logLevel == null) {
178:                            logLevel = "info"; // use info as default if not provided
179:                        }
180:                    }
181:                    return categoryLogger;
182:                }
183:
184:                return log;
185:            }
186:
187:            /**
188:             * Performs the actual logging.
189:             *
190:             * @param logger  the provided logger to use.
191:             * @param message  the message to log.
192:             */
193:            protected void doLog(Log logger, String message) {
194:                if (logLevel == null) {
195:                    logger.info(message);
196:                    return;
197:                }
198:
199:                if ("debug".equalsIgnoreCase(logLevel)) {
200:                    logger.debug(message);
201:                } else if ("info".equalsIgnoreCase(logLevel)) {
202:                    logger.info(message);
203:                } else if ("warn".equalsIgnoreCase(logLevel)) {
204:                    logger.warn(message);
205:                } else if ("error".equalsIgnoreCase(logLevel)) {
206:                    logger.error(message);
207:                } else if ("fatal".equalsIgnoreCase(logLevel)) {
208:                    logger.fatal(message);
209:                } else if ("trace".equalsIgnoreCase(logLevel)) {
210:                    logger.trace(message);
211:                } else {
212:                    throw new IllegalArgumentException("LogLevel [" + logLevel
213:                            + "] is not supported");
214:                }
215:            }
216:
217:            /**
218:             * Is the given logger enalbed at the given level?
219:             *
220:             * @param logger  the logger.
221:             * @param level   the level to check if <code>isXXXEnabled</code>.
222:             * @return <tt>true</tt> if enabled, <tt>false</tt> if not.
223:             */
224:            private static boolean isLoggerEnabled(Log logger, String level) {
225:                if ("debug".equalsIgnoreCase(level)) {
226:                    return logger.isDebugEnabled();
227:                } else if ("info".equalsIgnoreCase(level)) {
228:                    return logger.isInfoEnabled();
229:                } else if ("warn".equalsIgnoreCase(level)) {
230:                    return logger.isWarnEnabled();
231:                } else if ("error".equalsIgnoreCase(level)) {
232:                    return logger.isErrorEnabled();
233:                } else if ("fatal".equalsIgnoreCase(level)) {
234:                    return logger.isFatalEnabled();
235:                } else if ("trace".equalsIgnoreCase(level)) {
236:                    return logger.isTraceEnabled();
237:                } else {
238:                    throw new IllegalArgumentException("LogLevel [" + level
239:                            + "] is not supported");
240:                }
241:            }
242:
243:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.