01: /*
02: * Copyright (C) The DNA Group. All rights reserved.
03: *
04: * This software is published under the terms of the DNA
05: * Software License version 1.1, a copy of which has been included
06: * with this distribution in the LICENSE.txt file.
07: */
08: package org.codehaus.dna;
09:
10: /**
11: * Abstract utility class that components can extend to
12: * make it easy to implement logging.
13: *
14: * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
15: */
16: public abstract class AbstractLogEnabled implements LogEnabled {
17: /**
18: * The components logger.
19: */
20: private Logger m_logger;
21:
22: /**
23: * Set the components logger.
24: *
25: * @param logger the logger
26: */
27: public void enableLogging(final Logger logger) {
28: m_logger = logger;
29: }
30:
31: /**
32: * Return the components logger.
33: *
34: * @return the components logger.
35: */
36: protected final Logger getLogger() {
37: return m_logger;
38: }
39:
40: /**
41: * Utility method to setup specified object
42: * with current components logger.
43: *
44: * @param object the object
45: */
46: protected final void setupLogger(final Object object) {
47: setupLogger(object, getLogger());
48: }
49:
50: /**
51: * Utility method to setup specified object
52: * with a child logger of components current
53: * logger with specified name.
54: *
55: * @param object the object
56: * @param name the name of child logger
57: */
58: protected final void setupLogger(final Object object,
59: final String name) {
60: if (null == name) {
61: throw new NullPointerException("name");
62: }
63: final Logger childLogger = getLogger().getChildLogger(name);
64: setupLogger(object, childLogger);
65: }
66:
67: /**
68: * Internal implementation method to setup object
69: * with specified logger. If the object implements
70: * {@link LogEnabled} it will be supplied with logger
71: * via the {@link LogEnabled} interface.
72: *
73: * @param object the object
74: * @param logger the logger
75: */
76: private final void setupLogger(final Object object,
77: final Logger logger) {
78: if (object instanceof LogEnabled) {
79: ((LogEnabled) object).enableLogging(logger);
80: }
81: }
82: }
|