1 /*
2 * Copyright (C) The Spice Group. All rights reserved.
3 *
4 * This software is published under the terms of the Spice
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.spice.loggerstore.stores;
9
10 import java.util.HashMap;
11 import java.util.Map;
12 import org.codehaus.spice.loggerstore.LoggerStore;
13 import org.jcontainer.dna.LogEnabled;
14 import org.jcontainer.dna.Logger;
15
16 /***
17 * AbstractLoggerStore is an abstract implementation of LoggerStore for the
18 * functionality common to all Loggers.
19 *
20 * @author <a href="mailto:mauro.talevi at aquilonia.org">Mauro Talevi</a>
21 */
22 public abstract class AbstractLoggerStore
23 implements LoggerStore, LogEnabled
24 {
25 /*** Map of Loggers held in the store */
26 private final Map m_loggers = new HashMap();
27
28 /*** The Logger used by LogEnabled. */
29 private Logger m_logger;
30
31 /*** The root Logger */
32 private Logger m_rootLogger;
33
34 /***
35 * Provide a logger.
36 *
37 * @param logger the logger
38 */
39 public void enableLogging( final Logger logger )
40 {
41 m_logger = logger;
42 }
43
44 /***
45 * Retrieves the root Logger from the store.
46 *
47 * @return the Logger
48 * @throws Exception if unable to retrieve Logger
49 */
50 public Logger getLogger()
51 throws Exception
52 {
53 if( m_logger != null && m_logger.isDebugEnabled() )
54 {
55 final String message = "Root Logger returned";
56 m_logger.debug( message );
57 }
58 final Logger logger = getRootLogger();
59 if( logger == null )
60 {
61 final String message = "Root Logger is not defined";
62 throw new Exception( message );
63 }
64 return logger;
65 }
66
67 /***
68 * Retrieves a Logger hierarchy from the store for a given category name.
69 *
70 * @param name the name of the logger.
71 * @return the Logger
72 * @throws Exception if unable to retrieve Logger
73 */
74 public Logger getLogger( final String name )
75 throws Exception
76 {
77 if( null == name )
78 {
79 throw new NullPointerException( "name" );
80 }
81 Logger logger = retrieveLogger( name );
82 if( logger == null )
83 {
84 if( m_logger != null && m_logger.isDebugEnabled() )
85 {
86 final String message = "Logger named '" +
87 name +
88 "' not defined in configuration. New Logger " +
89 "created and returned.";
90 m_logger.debug( message );
91 }
92 logger = createLogger( name );
93 final Logger logger1 = logger;
94 m_loggers.put( name, logger1 );
95 }
96 return logger;
97 }
98
99 /***
100 * Creates new Logger for the given category. This is logger-implementation
101 * specific and will be implemented in concrete subclasses.
102 */
103 protected abstract Logger createLogger( String name );
104
105 /***
106 * Sets the root Logger.
107 */
108 protected final void setRootLogger( final Logger rootLogger )
109 {
110 m_rootLogger = rootLogger;
111 }
112
113 /***
114 * Returns the root logger.
115 *
116 * @return the root logger.
117 */
118 protected final Logger getRootLogger()
119 {
120 return m_rootLogger;
121 }
122
123 /***
124 * Retrieve Logger from store map.
125 *
126 * @param name the name of the Logger
127 * @return the Logger instance or <code>null</code> if not found in map.
128 */
129 private Logger retrieveLogger( final String name )
130 {
131 Logger logger = (Logger)m_loggers.get( name );
132 if( null != logger )
133 {
134 if( null != m_logger && m_logger.isDebugEnabled() )
135 {
136 final String message = "Retrieved Logger named: " + name;
137 m_logger.debug( message );
138 }
139 }
140
141 return logger;
142 }
143 }
This page was automatically generated by Maven