KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > madvoc > interceptor > EchoInterceptor


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.madvoc.interceptor;
4
5 import jodd.madvoc.ActionRequest;
6
7 /**
8  * Simple interceptor that meassures time and prints out info about invoked actions.
9  * User may inherit it and change the way message is printed.
10  */

11 public class EchoInterceptor implements ActionInterceptor {
12
13     /**
14      * Measure action invocation.
15      */

16     public String JavaDoc intercept(ActionRequest request) throws Exception JavaDoc {
17         printBefore(request);
18         long startTime = System.currentTimeMillis();
19         String JavaDoc result = request.invoke();
20         long executionTime = System.currentTimeMillis() - startTime;
21         printAfter(request, executionTime, result);
22         return result;
23     }
24
25     /**
26      * Prints out the message. User can override this method and modify the way
27      * the message is printed.
28      */

29     protected void printBefore(ActionRequest request) {
30         System.out.println("====> " + request.getActionPath());
31     }
32
33
34     /**
35      * Prints out the message. User can override this method and modify the way
36      * the message is printed.
37      */

38     protected void printAfter(ActionRequest request, long executionTime, String JavaDoc result) {
39         StringBuilder JavaDoc message = new StringBuilder JavaDoc("<---- ");
40         message.append(request.getActionPath()).append(" (").append(result).append(" -> ");
41         message.append(request.getResultName()).append(':').append(request.getResultValue()).append(") in ");
42         message.append(executionTime).append("ms.");
43         System.out.println(message.toString());
44     }
45 }
46
Popular Tags