|
|||||||||||||||||||
| 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 | |||||||||||||||
| InitialLoggerStoreFactory.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.net.URL;
|
|
| 12 |
import java.util.Enumeration;
|
|
| 13 |
import java.util.HashMap;
|
|
| 14 |
import java.util.Map;
|
|
| 15 |
import java.util.Properties;
|
|
| 16 |
import org.codehaus.spice.loggerstore.LoggerStore;
|
|
| 17 |
import org.codehaus.spice.loggerstore.LoggerStoreFactory;
|
|
| 18 |
|
|
| 19 |
/**
|
|
| 20 |
* This is the initial LoggerStoreFactory tyhat the user accesses to create
|
|
| 21 |
* their LoggerStore when the type is configurable.
|
|
| 22 |
*
|
|
| 23 |
* @author Peter Donald
|
|
| 24 |
* @author <a href="mailto:mauro.talevi at aquilonia.org">Mauro Talevi</a>
|
|
| 25 |
* @version $Revision: 1.1 $ $Date: 2003/11/19 18:22:44 $
|
|
| 26 |
*/
|
|
| 27 |
public class InitialLoggerStoreFactory |
|
| 28 |
implements LoggerStoreFactory
|
|
| 29 |
{
|
|
| 30 |
/**
|
|
| 31 |
* The INITIAL_FACTORY key. Used to define the classname of the initial
|
|
| 32 |
* LoggerStoreFactory. If not specified will attempt to use the
|
|
| 33 |
* ConsoleLoggerStoreFactory.
|
|
| 34 |
*/
|
|
| 35 |
public static final String INITIAL_FACTORY = "org.codehaus.spice.loggerstore.factory"; |
|
| 36 |
|
|
| 37 |
/**
|
|
| 38 |
* The name of properties file loaded from ClassLoader. This property file
|
|
| 39 |
* will be used to load default configuration settings if user failed to
|
|
| 40 |
* specify them.
|
|
| 41 |
*/
|
|
| 42 |
public static final String DEFAULT_PROPERTIES = "META-INF/spice/loggerstore.properties"; |
|
| 43 |
|
|
| 44 |
/**
|
|
| 45 |
* Create LoggerStore by first determining the correct LoggerStoreFactory to
|
|
| 46 |
* use and then delegating to that factory. See Class Javadocs for the
|
|
| 47 |
* process of locating LoggerStore.
|
|
| 48 |
*
|
|
| 49 |
* @param config the input configuration
|
|
| 50 |
* @return the LoggerStore
|
|
| 51 |
* @throws Exception if unable to create the LoggerStore for any reason.
|
|
| 52 |
*/
|
|
| 53 | 36 |
public LoggerStore createLoggerStore( final Map config )
|
| 54 |
throws Exception
|
|
| 55 |
{
|
|
| 56 | 36 |
final ClassLoader classLoader = getClassLoader( config ); |
| 57 |
|
|
| 58 | 36 |
String type = (String)config.get( INITIAL_FACTORY ); |
| 59 | 36 |
Map data = config; |
| 60 | 36 |
if( null == type ) |
| 61 |
{
|
|
| 62 | 4 |
data = loadDefaultConfig( data, classLoader ); |
| 63 | 4 |
type = (String)data.get( INITIAL_FACTORY ); |
| 64 |
} |
|
| 65 | 36 |
final LoggerStoreFactory factory = |
| 66 |
createLoggerStoreFactory( type, classLoader ); |
|
| 67 | 34 |
return factory.createLoggerStore( data );
|
| 68 |
} |
|
| 69 |
|
|
| 70 |
/**
|
|
| 71 |
* Retrieve the classloader from data map. If no classloader is specified
|
|
| 72 |
* then use ContextClassLoader. If ContextClassLoader not specified then use
|
|
| 73 |
* ClassLoader that loaded this class.
|
|
| 74 |
*
|
|
| 75 |
* @param data the configuration data
|
|
| 76 |
* @return a ClassLoader
|
|
| 77 |
*/
|
|
| 78 | 36 |
private ClassLoader getClassLoader( final Map data )
|
| 79 |
{
|
|
| 80 | 36 |
ClassLoader loader = (ClassLoader)data.get( |
| 81 |
ClassLoader.class.getName() );
|
|
| 82 | 36 |
if( null == loader ) |
| 83 |
{
|
|
| 84 | 34 |
loader = Thread.currentThread().getContextClassLoader(); |
| 85 | 34 |
if( null == loader ) |
| 86 |
{
|
|
| 87 | 3 |
loader = InitialLoggerStoreFactory.class.getClassLoader();
|
| 88 |
} |
|
| 89 |
} |
|
| 90 | 36 |
return loader;
|
| 91 |
} |
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* Load the default properties for LoggerStoreFactory.
|
|
| 95 |
*
|
|
| 96 |
* @param initial the input data
|
|
| 97 |
* @param classLoader the classLoader to load properties files from
|
|
| 98 |
* @return the new configuration data
|
|
| 99 |
* @throws Exception if unable to load properties
|
|
| 100 |
*/
|
|
| 101 | 4 |
private Map loadDefaultConfig( final Map initial,
|
| 102 |
final ClassLoader classLoader ) |
|
| 103 |
throws Exception
|
|
| 104 |
{
|
|
| 105 | 4 |
final HashMap map = new HashMap();
|
| 106 |
|
|
| 107 | 4 |
final Enumeration resources = |
| 108 |
classLoader.getResources( DEFAULT_PROPERTIES ); |
|
| 109 | 4 |
while( resources.hasMoreElements() )
|
| 110 |
{
|
|
| 111 | 3 |
final URL url = (URL)resources.nextElement(); |
| 112 | 3 |
final InputStream stream = url.openStream(); |
| 113 | 3 |
final Properties properties = new Properties();
|
| 114 | 3 |
properties.load( stream ); |
| 115 | 3 |
map.putAll( properties ); |
| 116 |
} |
|
| 117 |
|
|
| 118 | 4 |
map.putAll( initial ); |
| 119 | 4 |
return map;
|
| 120 |
} |
|
| 121 |
|
|
| 122 |
/**
|
|
| 123 |
* Create a {@link LoggerStoreFactory} for specified loggerType.
|
|
| 124 |
*
|
|
| 125 |
* @param type the type of the Logger to use.
|
|
| 126 |
* @return the created {@link LoggerStoreFactory}
|
|
| 127 |
*/
|
|
| 128 | 36 |
private LoggerStoreFactory createLoggerStoreFactory( final String type,
|
| 129 |
final ClassLoader classLoader ) |
|
| 130 |
{
|
|
| 131 | 36 |
if( null == type ) |
| 132 |
{
|
|
| 133 | 1 |
final String message = "No LoggerStoreFactory type specified.";
|
| 134 | 1 |
throw new IllegalStateException( message ); |
| 135 |
} |
|
| 136 |
|
|
| 137 | 35 |
try
|
| 138 |
{
|
|
| 139 | 35 |
final Class clazz = classLoader.loadClass( type ); |
| 140 | 34 |
return (LoggerStoreFactory)clazz.newInstance();
|
| 141 |
} |
|
| 142 |
catch( final Exception e )
|
|
| 143 |
{
|
|
| 144 | 1 |
final String message = |
| 145 |
"Failed to created LoggerStoreFactory " + type;
|
|
| 146 | 1 |
throw new IllegalArgumentException( message ); |
| 147 |
} |
|
| 148 |
} |
|
| 149 |
} |
|
| 150 |
|
|
||||||||||