|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractLoggerStore.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 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 | 160 |
public void enableLogging( final Logger logger ) |
| 40 |
{
|
|
| 41 | 160 |
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 | 98 |
public Logger getLogger()
|
| 51 |
throws Exception
|
|
| 52 |
{
|
|
| 53 | 98 |
if( m_logger != null && m_logger.isDebugEnabled() ) |
| 54 |
{
|
|
| 55 | 41 |
final String message = "Root Logger returned";
|
| 56 | 41 |
m_logger.debug( message ); |
| 57 |
} |
|
| 58 | 98 |
final Logger logger = getRootLogger(); |
| 59 | 98 |
if( logger == null ) |
| 60 |
{
|
|
| 61 | 1 |
final String message = "Root Logger is not defined";
|
| 62 | 1 |
throw new Exception( message ); |
| 63 |
} |
|
| 64 | 97 |
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 | 364 |
public Logger getLogger( final String name )
|
| 75 |
throws Exception
|
|
| 76 |
{
|
|
| 77 | 364 |
if( null == name ) |
| 78 |
{
|
|
| 79 | 94 |
throw new NullPointerException( "name" ); |
| 80 |
} |
|
| 81 | 270 |
Logger logger = retrieveLogger( name ); |
| 82 | 270 |
if( logger == null ) |
| 83 |
{
|
|
| 84 | 176 |
if( m_logger != null && m_logger.isDebugEnabled() ) |
| 85 |
{
|
|
| 86 | 71 |
final String message = "Logger named '" +
|
| 87 |
name + |
|
| 88 |
"' not defined in configuration. New Logger " +
|
|
| 89 |
"created and returned.";
|
|
| 90 | 71 |
m_logger.debug( message ); |
| 91 |
} |
|
| 92 | 176 |
logger = createLogger( name ); |
| 93 | 176 |
final Logger logger1 = logger; |
| 94 | 176 |
m_loggers.put( name, logger1 ); |
| 95 |
} |
|
| 96 | 270 |
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 | 97 |
protected final void setRootLogger( final Logger rootLogger ) |
| 109 |
{
|
|
| 110 | 97 |
m_rootLogger = rootLogger; |
| 111 |
} |
|
| 112 |
|
|
| 113 |
/**
|
|
| 114 |
* Returns the root logger.
|
|
| 115 |
*
|
|
| 116 |
* @return the root logger.
|
|
| 117 |
*/
|
|
| 118 | 101 |
protected final Logger getRootLogger()
|
| 119 |
{
|
|
| 120 | 101 |
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 | 270 |
private Logger retrieveLogger( final String name )
|
| 130 |
{
|
|
| 131 | 270 |
Logger logger = (Logger)m_loggers.get( name ); |
| 132 | 270 |
if( null != logger ) |
| 133 |
{
|
|
| 134 | 94 |
if( null != m_logger && m_logger.isDebugEnabled() ) |
| 135 |
{
|
|
| 136 | 39 |
final String message = "Retrieved Logger named: " + name;
|
| 137 | 39 |
m_logger.debug( message ); |
| 138 |
} |
|
| 139 |
} |
|
| 140 |
|
|
| 141 | 270 |
return logger;
|
| 142 |
} |
|
| 143 |
} |
|
| 144 |
|
|
||||||||||