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 protected LoggerStore doCreateLoggerStore( final Map config )
50 throws Exception
51 {
52 LoggerManager loggerManager =
53 (LoggerManager)config.get( LoggerManager.class.getName() );
54 if( null == loggerManager )
55 {
56 String type = (String)config.get( LOGGER_MANAGER );
57 if( null == type )
58 {
59 type = DEFAULT_LOGGER_MANAGER;
60 }
61 final ClassLoader classLoader = getClassLoader( config );
62 loggerManager = createLoggerManager( type, classLoader );
63 }
64
65 Logger logger =
66 (Logger)config.get( Logger.class.getName() );
67 if( null == logger )
68 {
69 logger = loggerManager.getDefaultLogger();
70 }
71
72 final Context context =
73 (Context)config.get( Context.class.getName() );
74
75 final Configuration configuration =
76 (Configuration)config.get( Configuration.class.getName() );
77 if( null != configuration )
78 {
79 return new LogKitLoggerStore( loggerManager,
80 logger,
81 context,
82 configuration );
83 }
84
85 final InputStream resource = getInputStream( config );
86 if( null != resource )
87 {
88 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
89 return new LogKitLoggerStore( loggerManager,
90 logger,
91 context,
92 builder.build( resource ) );
93 }
94
95 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 protected ClassLoader getClassLoader( final Map data )
107 {
108 ClassLoader loader = (ClassLoader)data.get(
109 ClassLoader.class.getName() );
110 if( null == loader )
111 {
112 loader = Thread.currentThread().getContextClassLoader();
113 if( null == loader )
114 {
115 loader = LogKitLoggerStoreFactory.class.getClassLoader();
116 }
117 }
118 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 private LoggerManager createLoggerManager( final String type,
128 final ClassLoader classLoader )
129 {
130 try
131 {
132 final Class clazz = classLoader.loadClass( type );
133 return (LoggerManager)clazz.newInstance();
134 }
135 catch( final Exception e )
136 {
137 final String message =
138 "Failed to created LoggerManager: " + type;
139 throw new IllegalArgumentException( message );
140 }
141 }
142 }
This page was automatically generated by Maven