KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > db > NormalizedJDBCTarget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.output.db;
18
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Statement JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import javax.sql.DataSource JavaDoc;
25 import org.apache.log.LogEvent;
26
27 /**
28  * JDBC target that writes to normalized tables.
29  * This reduces overhead and cost of querying/storing logs.
30  *
31  * <p>Parts based on JDBC logger from prottomatter by
32  * <a HREF="mailto:nate@protomatter.com">Nate Sammons</a></p>
33  *
34  * @author Peter Donald
35  */

36 public class NormalizedJDBCTarget
37     extends DefaultJDBCTarget
38 {
39     private HashMap JavaDoc m_categoryIDs = new HashMap JavaDoc();
40     private HashMap JavaDoc m_priorityIDs = new HashMap JavaDoc();
41
42     public NormalizedJDBCTarget( final DataSource JavaDoc dataSource,
43                                  final String JavaDoc table,
44                                  final ColumnInfo[] columns )
45     {
46         super( dataSource, table, columns );
47     }
48
49     /**
50      * Adds a single object into statement.
51      */

52     protected void specifyColumn( final PreparedStatement JavaDoc statement,
53                                   final int index,
54                                   final LogEvent event )
55         throws SQLException JavaDoc
56     {
57         final ColumnInfo info = getColumn( index );
58         int id = 0;
59         String JavaDoc tableName = null;
60
61         switch( info.getType() )
62         {
63             case ColumnType.CATEGORY:
64                 tableName = getTable() + "_" + ColumnType.CATEGORY_STR + "_SET";
65                 id = getID( tableName, m_categoryIDs, event.getCategory() );
66                 statement.setInt( index + 1, id );
67                 break;
68
69             case ColumnType.PRIORITY:
70                 tableName = getTable() + "_" + ColumnType.PRIORITY_STR + "_SET";
71                 id = getID( tableName, m_priorityIDs, event.getPriority().getName() );
72                 statement.setInt( index + 1, id );
73                 break;
74
75             default:
76                 super.specifyColumn( statement, index, event );
77         }
78     }
79
80     protected synchronized int getID( final String JavaDoc tableName, final HashMap JavaDoc idMap, final String JavaDoc instance )
81         throws SQLException JavaDoc
82     {
83         final Integer JavaDoc id = (Integer JavaDoc)idMap.get( instance );
84         if( null != id ) return id.intValue();
85
86         // see if it's been put in before.
87
Statement JavaDoc statement = null;
88         ResultSet JavaDoc resultSet = null;
89
90         try
91         {
92             statement = getConnection().createStatement();
93
94             final String JavaDoc querySql = "SELECT ID FROM " + tableName + " WHERE NAME='" + instance + "'";
95             resultSet = statement.executeQuery( querySql );
96
97             if( resultSet.next() )
98             {
99                 final Integer JavaDoc newID = new Integer JavaDoc( resultSet.getInt( 1 ) );
100                 idMap.put( instance, newID );
101                 return newID.intValue();
102             }
103
104             resultSet.close();
105
106             //Note that the next part should be a transaction but
107
//it is not mega vital so ...
108

109             //Find the max id in table and set
110
//max to it's value if any items are present in table
111
final String JavaDoc maxQuerySql = "SELECT MAX(ID) FROM " + tableName;
112             resultSet = statement.executeQuery( maxQuerySql );
113             int max = 0;
114             if( resultSet.next() ) max = resultSet.getInt( 1 );
115             resultSet.close();
116
117             final int newID = max + 1;
118             final String JavaDoc insertSQL = "INSERT INTO " + tableName +
119                 " (ID, NAME) VALUES ( " + newID + ", '" + instance + "')";
120             statement.executeUpdate( insertSQL );
121
122             idMap.put( instance, new Integer JavaDoc( newID ) );
123             return newID;
124         }
125         finally
126         {
127             // close up shop
128
if( null != resultSet )
129             {
130                 try
131                 {
132                     resultSet.close();
133                 }
134                 catch( final Exception JavaDoc e )
135                 {
136                 }
137             }
138             if( null != statement )
139             {
140                 try
141                 {
142                     statement.close();
143                 }
144                 catch( final Exception JavaDoc e )
145                 {
146                 }
147             }
148         }
149     }
150 }
151
Popular Tags