KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > factory > JDBCTargetFactory


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.logger.factory;
9
10
11
12
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.sql.DataSource JavaDoc;
17 import org.apache.avalon.excalibur.logger.LogTargetFactory;
18 import org.apache.avalon.framework.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.log.LogTarget;
22 import org.apache.log.output.db.ColumnInfo;
23 import org.apache.log.output.db.ColumnType;
24 import org.apache.log.output.db.DefaultJDBCTarget;
25 import org.apache.log.output.db.NormalizedJDBCTarget;
26
27 /**
28  * Factory for JDBCLogTarget-s. The configuration looks like this:
29  *
30  * <pre>
31  * &lt;jdbc id="database"&gt;
32  * &lt;datasource&gt;java:/LogTargetDataSource&lt;/datasource&gt;
33  * &lt;normalized&gt;true&lt;/normalized&gt;
34  * &lt;table name="LOG"&gt;
35  * &lt;sequence&gt;ID&lt;/sequence&gt;
36  * &lt;category&gt;CATEGORY&lt;/category&gt;
37  * &lt;priority&gt;PRIORITY&lt;/priority&gt;
38  * &lt;message&gt;MESSAGE&lt;/message&gt;
39  * &lt;time&gt;TIME&lt;/time&gt;
40  * &lt;rtime&gt;RTIME&lt;/rtime&gt;
41  * &lt;throwable&gt;THROWABLE&lt;/throwable&gt;
42  * &lt;hostname&gt;HOSTNAME&lt;/hostname&gt;
43  * &lt;static aux="-"&gt;STATIC&lt;/static&gt;
44  * &lt;context aux="principal"&gt;PRINCIPAL&lt;/context&gt;
45  * &lt;context aux="ipaddress"&gt;IPADDRESS&lt;/context&gt;
46  * &lt;context aux="username"&gt;USERNAME&lt;/context&gt;
47  * &lt;/table&gt;
48  * &lt;/jdbc&gt;
49  * </pre>
50  *
51  * @author <a HREF="mailto:mirceatoma@home.com">Mircea Toma</a>;
52  * @version CVS $Revision: 1.5 $ $Date: 2001/12/11 09:53:30 $
53  */

54 public class JDBCTargetFactory implements LogTargetFactory
55 {
56     public LogTarget createTarget(Configuration configuration)
57         throws ConfigurationException
58     {
59         final String JavaDoc dataSourceName =
60             configuration.getChild( "datasource", true ).getValue();
61
62         final boolean normalized =
63             configuration.getChild( "normalized", true ).getValueAsBoolean( false );
64
65         final Configuration tableConfiguration =
66             configuration.getChild( "table" );
67
68         final String JavaDoc table = tableConfiguration.getAttribute( "name" );
69
70         final Configuration[] conf = tableConfiguration.getChildren();
71         final ColumnInfo[] columns = new ColumnInfo[ conf.length ];
72
73         for (int i = 0; i < conf.length; i++)
74         {
75             final String JavaDoc name = conf[i].getValue();
76             final int type = ColumnType.getTypeIdFor( conf[i].getName() );
77             final String JavaDoc aux = conf[i].getAttribute( "aux", null );
78
79             columns[i] = new ColumnInfo( name, type, aux );
80         }
81
82         final DataSource JavaDoc dataSource;
83
84         try
85         {
86             Context JavaDoc ctx = new InitialContext JavaDoc();
87             dataSource = (DataSource JavaDoc)ctx.lookup( dataSourceName );
88         }
89         catch( final NamingException JavaDoc ne )
90         {
91             throw new ConfigurationException( "Cannot lookup data source", ne );
92         }
93
94         final LogTarget logTarget;
95         if( normalized )
96         {
97             logTarget = new NormalizedJDBCTarget(dataSource, table, columns);
98         }
99         else
100         {
101             logTarget = new DefaultJDBCTarget( dataSource, table, columns );
102         }
103
104         return logTarget;
105     }
106 }
107
Popular Tags