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