|
|||||||||||||||||||
| 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 | |||||||||||||||
| LogKitLoggerStoreFactory.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.factories;
|
|
| 9 |
|
|
| 10 |
import java.io.InputStream;
|
|
| 11 |
import java.util.Map;
|
|
| 12 |
import org.apache.avalon.excalibur.logger.LogKitLoggerManager;
|
|
| 13 |
import org.apache.avalon.excalibur.logger.LoggerManager;
|
|
| 14 |
import org.apache.avalon.framework.configuration.Configuration;
|
|
| 15 |
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
|
|
| 16 |
import org.apache.avalon.framework.context.Context;
|
|
| 17 |
import org.apache.avalon.framework.logger.Logger;
|
|
| 18 |
import org.codehaus.spice.loggerstore.LoggerStore;
|
|
| 19 |
import org.codehaus.spice.loggerstore.stores.LogKitLoggerStore;
|
|
| 20 |
|
|
| 21 |
/**
|
|
| 22 |
* LogKitLoggerStoreFactory is an implementation of LoggerStoreFactory for the
|
|
| 23 |
* LogKit Logger.
|
|
| 24 |
*
|
|
| 25 |
* @author <a href="mailto:mauro.talevi at aquilonia.org">Mauro Talevi</a>
|
|
| 26 |
* @author Peter Donald
|
|
| 27 |
* @version $Revision: 1.1 $ $Date: 2003/11/19 18:22:44 $
|
|
| 28 |
*/
|
|
| 29 |
public class LogKitLoggerStoreFactory |
|
| 30 |
extends AbstractLoggerStoreFactory
|
|
| 31 |
{
|
|
| 32 |
/**
|
|
| 33 |
* The LOGGER_MANAGER key. Used to define the classname of the
|
|
| 34 |
* LoggerManager to use in creating a LogKitLoggerStore when not specified
|
|
| 35 |
* in the configuration map.
|
|
| 36 |
*/
|
|
| 37 |
public static final String LOGGER_MANAGER = "org.codehaus.spice.loggerstore.logkit.loggermanager"; |
|
| 38 |
|
|
| 39 |
/** The default LoggerManager class name */
|
|
| 40 |
private static final String DEFAULT_LOGGER_MANAGER = LogKitLoggerManager.class.getName(); |
|
| 41 |
|
|
| 42 |
/**
|
|
| 43 |
* Creates a LoggerStore from a given set of configuration parameters.
|
|
| 44 |
*
|
|
| 45 |
* @param config the Map of parameters for the configuration of the store
|
|
| 46 |
* @return the LoggerStore
|
|
| 47 |
* @throws Exception if unable to create the LoggerStore
|
|
| 48 |
*/
|
|
| 49 | 42 |
protected LoggerStore doCreateLoggerStore( final Map config )
|
| 50 |
throws Exception
|
|
| 51 |
{
|
|
| 52 | 42 |
LoggerManager loggerManager = |
| 53 |
(LoggerManager)config.get( LoggerManager.class.getName() );
|
|
| 54 | 42 |
if( null == loggerManager ) |
| 55 |
{
|
|
| 56 | 16 |
String type = (String)config.get( LOGGER_MANAGER ); |
| 57 | 16 |
if( null == type ) |
| 58 |
{
|
|
| 59 | 9 |
type = DEFAULT_LOGGER_MANAGER; |
| 60 |
} |
|
| 61 | 16 |
final ClassLoader classLoader = getClassLoader( config ); |
| 62 | 16 |
loggerManager = createLoggerManager( type, classLoader ); |
| 63 |
} |
|
| 64 |
|
|
| 65 | 41 |
Logger logger = |
| 66 |
(Logger)config.get( Logger.class.getName() );
|
|
| 67 | 41 |
if( null == logger ) |
| 68 |
{
|
|
| 69 | 40 |
logger = loggerManager.getDefaultLogger(); |
| 70 |
} |
|
| 71 |
|
|
| 72 | 41 |
final Context context = |
| 73 |
(Context)config.get( Context.class.getName() );
|
|
| 74 |
|
|
| 75 | 41 |
final Configuration configuration = |
| 76 |
(Configuration)config.get( Configuration.class.getName() );
|
|
| 77 | 41 |
if( null != configuration ) |
| 78 |
{
|
|
| 79 | 6 |
return new LogKitLoggerStore( loggerManager, |
| 80 |
logger, |
|
| 81 |
context, |
|
| 82 |
configuration ); |
|
| 83 |
} |
|
| 84 |
|
|
| 85 | 35 |
final InputStream resource = getInputStream( config ); |
| 86 | 35 |
if( null != resource ) |
| 87 |
{
|
|
| 88 | 32 |
final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
|
| 89 | 32 |
return new LogKitLoggerStore( loggerManager, |
| 90 |
logger, |
|
| 91 |
context, |
|
| 92 |
builder.build( resource ) ); |
|
| 93 |
} |
|
| 94 |
|
|
| 95 | 3 |
return missingConfiguration();
|
| 96 |
} |
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* Retrieve the classloader from data map. If no classloader is specified
|
|
| 100 |
* then use ContextClassLoader. If ContextClassLoader not specified then use
|
|
| 101 |
* ClassLoader that loaded this class.
|
|
| 102 |
*
|
|
| 103 |
* @param data the configuration data
|
|
| 104 |
* @return a ClassLoader
|
|
| 105 |
*/
|
|
| 106 | 17 |
protected ClassLoader getClassLoader( final Map data )
|
| 107 |
{
|
|
| 108 | 17 |
ClassLoader loader = (ClassLoader)data.get( |
| 109 |
ClassLoader.class.getName() );
|
|
| 110 | 17 |
if( null == loader ) |
| 111 |
{
|
|
| 112 | 16 |
loader = Thread.currentThread().getContextClassLoader(); |
| 113 | 16 |
if( null == loader ) |
| 114 |
{
|
|
| 115 | 1 |
loader = LogKitLoggerStoreFactory.class.getClassLoader();
|
| 116 |
} |
|
| 117 |
} |
|
| 118 | 17 |
return loader;
|
| 119 |
} |
|
| 120 |
|
|
| 121 |
/**
|
|
| 122 |
* Create a {@link LoggerManager} for specified type.
|
|
| 123 |
*
|
|
| 124 |
* @param type the type of the LoggerManager to use.
|
|
| 125 |
* @return the created {@link LoggerManager}
|
|
| 126 |
*/
|
|
| 127 | 16 |
private LoggerManager createLoggerManager( final String type,
|
| 128 |
final ClassLoader classLoader ) |
|
| 129 |
{
|
|
| 130 | 16 |
try
|
| 131 |
{
|
|
| 132 | 16 |
final Class clazz = classLoader.loadClass( type ); |
| 133 | 15 |
return (LoggerManager)clazz.newInstance();
|
| 134 |
} |
|
| 135 |
catch( final Exception e )
|
|
| 136 |
{
|
|
| 137 | 1 |
final String message = |
| 138 |
"Failed to created LoggerManager: " + type;
|
|
| 139 | 1 |
throw new IllegalArgumentException( message ); |
| 140 |
} |
|
| 141 |
} |
|
| 142 |
} |
|
| 143 |
|
|
||||||||||