01: /*
02: * Copyright (c) 2002-2006 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.xwork.interceptor;
06:
07: import com.opensymphony.xwork.ActionInvocation;
08: import org.apache.commons.logging.Log;
09: import org.apache.commons.logging.LogFactory;
10:
11: /**
12: * <!-- START SNIPPET: description -->
13: * This interceptor logs the the start and end of the execution an action (in English-only, not internationalized).
14: * <!-- END SNIPPET: description -->
15: *
16: * <!-- START SNIPPET: parameters -->
17: * There are no parameters for this interceptor.
18: * <!-- END SNIPPET: parameters -->
19: *
20: * <!-- START SNIPPET: extending -->
21: * There are no obvious extensions to the existing interceptor.
22: * <!-- END SNIPPET: extending -->
23: *
24: * <pre>
25: * <!-- START SNIPPET: example -->
26: * <!-- prints out a message before and after the immediate action execution -->
27: * <action name="someAction" class="com.examples.SomeAction">
28: * <interceptor-ref name="completeStack"/>
29: * <interceptor-ref name="logger"/>
30: * <result name="success">good_result.ftl</result>
31: * </action>
32: *
33: * <!-- prints out a message before any more interceptors continue and after they have finished -->
34: * <action name="someAction" class="com.examples.SomeAction">
35: * <interceptor-ref name="logger"/>
36: * <interceptor-ref name="completeStack"/>
37: * <result name="success">good_result.ftl</result>
38: * </action>
39: * <!-- END SNIPPET: example -->
40: * </pre>
41: *
42: * @author Jason Carreira
43: */
44: public class LoggingInterceptor extends AroundInterceptor {
45: private static final Log log = LogFactory
46: .getLog(LoggingInterceptor.class);
47: private static final String FINISH_MESSAGE = "Finishing execution stack for action ";
48: private static final String START_MESSAGE = "Starting execution stack for action ";
49:
50: protected void after(ActionInvocation invocation, String result)
51: throws Exception {
52: logMessage(invocation, FINISH_MESSAGE);
53: }
54:
55: protected void before(ActionInvocation invocation) throws Exception {
56: logMessage(invocation, START_MESSAGE);
57: }
58:
59: private void logMessage(ActionInvocation invocation,
60: String baseMessage) {
61: if (log.isInfoEnabled()) {
62: StringBuffer message = new StringBuffer(baseMessage);
63: String namespace = invocation.getProxy().getNamespace();
64:
65: if ((namespace != null) && (namespace.trim().length() > 0)) {
66: message.append(namespace).append("/");
67: }
68:
69: message.append(invocation.getProxy().getActionName());
70: log.info(message.toString());
71: }
72: }
73:
74: }
|