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;
9
10 import java.io.InputStream;
11 import java.util.HashMap;
12 import org.codehaus.spice.loggerstore.factories.DOMLog4JLoggerStoreFactory;
13 import org.codehaus.spice.loggerstore.factories.ExcaliburLogKitLoggerStoreFactory;
14 import org.codehaus.spice.loggerstore.factories.InitialLoggerStoreFactory;
15 import org.codehaus.spice.loggerstore.factories.Jdk14LoggerStoreFactory;
16 import org.codehaus.spice.loggerstore.factories.PropertyLog4JLoggerStoreFactory;
17 import org.codehaus.spice.loggerstore.factories.SimpleLogKitLoggerStoreFactory;
18
19 /***
20 * Configurator is a collection of utility methods to create and configure
21 * LoggerStore objects of different types using configuration resources. LogKit,
22 * Log4J and JDK14 Loggers are supported. In the case of Log4J, both DOM and
23 * Property configuration types are supported.
24 *
25 * @author <a href="mailto:mauro.talevi at aquilonia.org">Mauro Talevi</a>
26 */
27 public class Configurator
28 {
29 /*** Constant used to define Log4J type with DOMConfigurator */
30 public static final String LOG4J_DOM = "log4j-dom";
31
32 /*** Constant used to define Log4J type with PropertyConfigurator */
33 public static final String LOG4J_PROPERTY = "log4j-property";
34
35 /*** Constant used to define LogKit type with Excalibur configuration */
36 public static final String LOGKIT_EXCALIBUR = "logkit-excalibur";
37
38 /*** Constant used to define LogKit type with Simple configuration */
39 public static final String LOGKIT_SIMPLE = "logkit-simple";
40
41 /*** Constant used to define JDK14 type */
42 public static final String JDK14 = "jdk14";
43
44 /***
45 * Create and configure a {@link LoggerStore} from a specified configuration
46 * resource.
47 *
48 * @param configuratorType the type of the configurator
49 * @param resource the String encoding the path of the configuration
50 * resource
51 * @return the configured LoggerStore
52 * @throws Exception if unable to create the LoggerStore
53 */
54 public static LoggerStore createLoggerStore( final String configuratorType,
55 final String resource )
56 throws Exception
57 {
58 final InitialLoggerStoreFactory factory = new InitialLoggerStoreFactory();
59 final HashMap data = new HashMap();
60 data.put( InitialLoggerStoreFactory.INITIAL_FACTORY,
61 getFactoryClassName( configuratorType ) );
62 data.put( LoggerStoreFactory.FILE_LOCATION, resource );
63 return factory.createLoggerStore( data );
64 }
65
66 /***
67 * Create and configure a {@link LoggerStore} from a specified configuration
68 * resource.
69 *
70 * @param configuratorType the type of the configurator
71 * @param resource the InputStream of the configuration resource
72 * @return the configured LoggerStore
73 * @throws Exception if unable to create the LoggerStore
74 */
75 public static LoggerStore createLoggerStore( final String configuratorType,
76 final InputStream resource )
77 throws Exception
78 {
79 final InitialLoggerStoreFactory factory = new InitialLoggerStoreFactory();
80 final HashMap data = new HashMap();
81 data.put( InitialLoggerStoreFactory.INITIAL_FACTORY,
82 getFactoryClassName( configuratorType ) );
83 data.put( InputStream.class.getName(), resource );
84 return factory.createLoggerStore( data );
85 }
86
87 /***
88 * Get the Factory class name of the LoggerStoreFactory that corresponds to
89 * specified type of Logger.
90 *
91 * @param type the type of Configurator
92 */
93 private static String getFactoryClassName( final String type )
94 {
95 if( LOG4J_DOM.equals( type ) )
96 {
97 return DOMLog4JLoggerStoreFactory.class.getName();
98 }
99 else if( LOG4J_PROPERTY.equals( type ) )
100 {
101 return PropertyLog4JLoggerStoreFactory.class.getName();
102 }
103 else if( LOGKIT_EXCALIBUR.equals( type ) )
104 {
105 return ExcaliburLogKitLoggerStoreFactory.class.getName();
106 }
107 else if( LOGKIT_SIMPLE.equals( type ) )
108 {
109 return SimpleLogKitLoggerStoreFactory.class.getName();
110 }
111 else if( JDK14.equals( type ) )
112 {
113 return Jdk14LoggerStoreFactory.class.getName();
114 }
115 else
116 {
117 final String message = "Unknown type " + type;
118 throw new IllegalArgumentException( message );
119 }
120 }
121 }
This page was automatically generated by Maven