KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import javax.sql.DataSource JavaDoc;
22 import org.apache.log.LogEvent;
23 import org.apache.log.output.AbstractTarget;
24
25 /**
26  * Abstract JDBC target.
27  *
28  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
29  * @author Peter Donald
30  */

31 public abstract class AbstractJDBCTarget
32     extends AbstractTarget
33 {
34     ///Datasource to extract connections from
35
private DataSource JavaDoc m_dataSource;
36
37     ///Database connection
38
private Connection JavaDoc m_connection;
39
40     /**
41      * Creation of a new instance of the AbstractJDBCTarget.
42      * @param dataSource the JDBC datasource
43      */

44     protected AbstractJDBCTarget( final DataSource JavaDoc dataSource )
45     {
46         m_dataSource = dataSource;
47     }
48
49     /**
50      * Process a log event, via formatting and outputting it.
51      *
52      * @param event the log event
53      * @exception Exception if an event processing error occurs
54      */

55     protected synchronized void doProcessEvent( final LogEvent event )
56         throws Exception JavaDoc
57     {
58         checkConnection();
59
60         if( isOpen() )
61         {
62             output( event );
63         }
64     }
65
66     /**
67      * Output a log event to DB.
68      * This must be implemented by subclasses.
69      *
70      * @param event the log event.
71      */

72     protected abstract void output( LogEvent event );
73
74     /**
75      * Startup log session.
76      *
77      */

78     protected synchronized void open()
79     {
80         if( !isOpen() )
81         {
82             super.open();
83             openConnection();
84         }
85     }
86
87     /**
88      * Open connection to underlying database.
89      *
90      */

91     protected synchronized void openConnection()
92     {
93         try
94         {
95             m_connection = m_dataSource.getConnection();
96         }
97         catch( final Throwable JavaDoc throwable )
98         {
99             getErrorHandler().error( "Unable to open connection", throwable, null );
100         }
101     }
102
103     /**
104      * Utility method for subclasses to access connection.
105      *
106      * @return the Connection
107      */

108     protected final synchronized Connection JavaDoc getConnection()
109     {
110         return m_connection;
111     }
112
113     /**
114      * Utility method to check connection and bring it back up if necessary.
115      */

116     protected final synchronized void checkConnection()
117     {
118         if( isStale() )
119         {
120             closeConnection();
121             openConnection();
122         }
123     }
124
125     /**
126      * Detect if connection is stale and should be reopened.
127      *
128      * @return true if connection is stale, false otherwise
129      */

130     protected synchronized boolean isStale()
131     {
132         if( null == m_connection )
133         {
134             return true;
135         }
136
137         try
138         {
139             if( m_connection.isClosed() )
140             {
141                 return true;
142             }
143         }
144         catch( final SQLException JavaDoc se )
145         {
146             return true;
147         }
148
149         return false;
150     }
151
152     /**
153      * Shutdown target.
154      * Attempting to write to target after close() will cause errors to be logged.
155      *
156      */

157     public synchronized void close()
158     {
159         if( isOpen() )
160         {
161             closeConnection();
162             super.close();
163         }
164     }
165
166     /**
167      * Close connection to underlying database.
168      *
169      */

170     protected synchronized void closeConnection()
171     {
172         if( null != m_connection )
173         {
174             try
175             {
176                 m_connection.close();
177             }
178             catch( final SQLException JavaDoc se )
179             {
180                 getErrorHandler().error( "Error shutting down JDBC connection", se, null );
181             }
182
183             m_connection = null;
184         }
185     }
186 }
187
Popular Tags