01: /**************************************************************************************
02: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package examples.logging;
08:
09: import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
10:
11: /**
12: * This aspect shows how to implement logging modules using Log4j, 1.4 Logger etc.
13: * (currently showing the use of 1.4 Logger API).
14: *
15: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
16: */
17: public class LoggerIdiom {
18:
19: /**
20: * @Around("methodToLog && target(loggable)")
21: */
22: public Object log(StaticJoinPoint jp, Loggable loggable)
23: throws Throwable {
24: loggable.getLog().log(Level.ALL,
25: "entering " + jp.getSignature());
26: Object result = jp.proceed();
27: loggable.getLog()
28: .log(Level.ALL, "exiting " + jp.getSignature());
29: return result;
30: }
31:
32: /**
33: * @Mixin(
34: * pointcut="loggableClasses",
35: * deploymentModel="perClass"
36: * )
37: */
38: public static class LoggableImpl implements Loggable {
39:
40: private final Logger LOG;
41:
42: public LoggableImpl(Class targetClass) {
43: LOG = Logger.getLogger(targetClass.getName());
44: }
45:
46: public Logger getLog() {
47: return LOG;
48: }
49: }
50:
51: public static interface Loggable {
52: Logger getLog();
53: }
54:
55: // some backport of Java 1.4 logging so that the sample can run on 1.3
56: //TODO : else move to jdk15 samples
57: static class Level {
58: static final Level ALL = new Level();
59: }
60:
61: static class Logger {
62: private static Logger SINGLETON = new Logger();
63:
64: static Logger getLogger(String name) {
65: return SINGLETON;
66: }
67:
68: void log(Level level, String m) {
69: System.out.println("examples.logging.Logger : " + m);
70: }
71: }
72: }
|