View Javadoc
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 public LoggerStore createLoggerStore( final Map config ) 54 throws Exception 55 { 56 final ClassLoader classLoader = getClassLoader( config ); 57 58 String type = (String)config.get( INITIAL_FACTORY ); 59 Map data = config; 60 if( null == type ) 61 { 62 data = loadDefaultConfig( data, classLoader ); 63 type = (String)data.get( INITIAL_FACTORY ); 64 } 65 final LoggerStoreFactory factory = 66 createLoggerStoreFactory( type, classLoader ); 67 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 private ClassLoader getClassLoader( final Map data ) 79 { 80 ClassLoader loader = (ClassLoader)data.get( 81 ClassLoader.class.getName() ); 82 if( null == loader ) 83 { 84 loader = Thread.currentThread().getContextClassLoader(); 85 if( null == loader ) 86 { 87 loader = InitialLoggerStoreFactory.class.getClassLoader(); 88 } 89 } 90 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 private Map loadDefaultConfig( final Map initial, 102 final ClassLoader classLoader ) 103 throws Exception 104 { 105 final HashMap map = new HashMap(); 106 107 final Enumeration resources = 108 classLoader.getResources( DEFAULT_PROPERTIES ); 109 while( resources.hasMoreElements() ) 110 { 111 final URL url = (URL)resources.nextElement(); 112 final InputStream stream = url.openStream(); 113 final Properties properties = new Properties(); 114 properties.load( stream ); 115 map.putAll( properties ); 116 } 117 118 map.putAll( initial ); 119 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 private LoggerStoreFactory createLoggerStoreFactory( final String type, 129 final ClassLoader classLoader ) 130 { 131 if( null == type ) 132 { 133 final String message = "No LoggerStoreFactory type specified."; 134 throw new IllegalStateException( message ); 135 } 136 137 try 138 { 139 final Class clazz = classLoader.loadClass( type ); 140 return (LoggerStoreFactory)clazz.newInstance(); 141 } 142 catch( final Exception e ) 143 { 144 final String message = 145 "Failed to created LoggerStoreFactory " + type; 146 throw new IllegalArgumentException( message ); 147 } 148 } 149 }

This page was automatically generated by Maven