View Javadoc

1   /*
2    * Copyright (C) The DNA Group. All rights reserved.
3    *
4    * This software is published under the terms of the DNA
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.dna;
9   
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
17      implements LogEnabled
18  {
19      /***
20       * The components logger.
21       */
22      private Logger m_logger;
23  
24      /***
25       * Set the components logger.
26       *
27       * @param logger the logger
28       */
29      public void enableLogging( final Logger logger )
30      {
31          m_logger = logger;
32      }
33  
34      /***
35       * Return the components logger.
36       *
37       * @return the components logger.
38       */
39      protected final Logger getLogger()
40      {
41          return m_logger;
42      }
43  
44      /***
45       * Utility method to setup specified object
46       * with current components logger.
47       *
48       * @param object the object
49       */
50      protected final void setupLogger( final Object object )
51      {
52          setupLogger( object, getLogger() );
53      }
54  
55      /***
56       * Utility method to setup specified object
57       * with a child logger of components current
58       * logger with specified name.
59       *
60       * @param object the object
61       * @param name the name of child logger
62       */
63      protected final void setupLogger( final Object object,
64                                        final String name )
65      {
66          if( null == name )
67          {
68              throw new NullPointerException( "name" );
69          }
70          final Logger childLogger = getLogger().getChildLogger( name );
71          setupLogger( object, childLogger );
72      }
73  
74      /***
75       * Internal implementation method to setup object
76       * with specified logger. If the object implements
77       * {@link LogEnabled} it will be supplied with logger
78       * via the {@link LogEnabled} interface.
79       *
80       * @param object the object
81       * @param logger the logger
82       */
83      private final void setupLogger( final Object object, final Logger logger )
84      {
85          if( object instanceof LogEnabled )
86          {
87              ( (LogEnabled)object ).enableLogging( logger );
88          }
89      }
90  }