KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > decorator > CachingDecorator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.avalon.excalibur.logger.decorator;
20
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.avalon.excalibur.logger.LoggerManager;
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 /**
27  * This class implements LoggerManager interface by
28  * passing all the job to a wrapped LoggerManager,
29  * but the returened Loggers are cached.
30  *
31  * All operations of this class are synchronized via
32  * a single lock. As the <code>LoggerManager</code> is
33  * not expected to be a performance bottleneck probably
34  * this design will be good enough.
35  *
36  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
37  * @version CVS $Revision: 1.3 $ $Date: 2004/03/10 13:54:50 $
38  * @since 4.0
39  */

40 public class CachingDecorator extends LoggerManagerDecorator
41 {
42     /**
43      * Logger-s cache.
44      * All access synchronized( m_loggers ).
45      */

46     private final Map JavaDoc m_loggers = new HashMap JavaDoc();
47     /**
48      * This variable caches the result of
49      * getDefaultLogger(). This class will
50      * treat getDefaultLogger() and getLoggerForCategory("")
51      * on our wrapped LoggerManager as being potentially
52      * different, although all of the existing adapters
53      * probably return the same Logger for both.
54      *
55      * Access synchronized( this );
56      */

57     private Logger m_defaultLogger = null;
58
59     /**
60      * Creates a <code>CachingDecorator</code> instance.
61      */

62     public CachingDecorator( final LoggerManager loggerManager )
63     {
64         super( loggerManager );
65     }
66
67     /**
68      * Return the Logger for the specified category.
69      */

70     public Logger getLoggerForCategory( final String JavaDoc categoryName )
71     {
72         synchronized( m_loggers )
73         {
74             Logger logger = (Logger) m_loggers.get( categoryName );
75
76             if ( logger == null )
77             {
78                 logger = m_loggerManager.getLoggerForCategory( categoryName );
79
80                 if ( logger == null )
81                 {
82                     final String JavaDoc message = "getLoggerForCategory('" +
83                             categoryName + "')";
84                     throw new NullPointerException JavaDoc( message );
85                 }
86
87                 m_loggers.put( categoryName, logger );
88             }
89
90             return logger;
91         }
92     }
93
94     /**
95      * Return the default Logger. Although it is expected
96      * that the wrapped loggerManager will return the same
97      * as getLoggerForCategory("") we cache the value separtely.
98      */

99     public Logger getDefaultLogger()
100     {
101         synchronized( this )
102         {
103             if ( m_defaultLogger == null )
104             {
105                 m_defaultLogger = m_loggerManager.getDefaultLogger();
106
107                 if ( m_defaultLogger == null )
108                 {
109                     throw new NullPointerException JavaDoc ( "getDefaultLogger()" );
110                 }
111             }
112             return m_defaultLogger;
113         }
114     }
115 }
116
Popular Tags