KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > datasource > J2eeDataSource


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.datasource;
9
10 import java.sql.Connection JavaDoc;
11 import java.sql.SQLException JavaDoc;
12 import javax.naming.Context JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import javax.naming.NamingException JavaDoc;
15 import javax.sql.DataSource JavaDoc;
16 import org.apache.avalon.framework.logger.AbstractLogEnabled;
17 import org.apache.avalon.framework.logger.Loggable;
18 import org.apache.avalon.framework.logger.LogKitLogger;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21
22 /**
23  * The J2EE implementation for DataSources in Cocoon. This uses the
24  * <code>javax.sql.DataSource</code> object and assumes that the
25  * J2EE container pools the datasources properly.
26  *
27  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
28  * @version CVS $Revision: 1.5 $ $Date: 2001/12/11 09:53:28 $
29  * @since 4.0
30  */

31 public class J2eeDataSource
32     extends AbstractLogEnabled
33     implements DataSourceComponent, Loggable
34 {
35     public static final String JavaDoc JDBC_NAME = "java:comp/env/jdbc/";
36     protected DataSource JavaDoc m_dataSource = null;
37
38     public void setLogger( org.apache.log.Logger logger )
39     {
40         enableLogging( new LogKitLogger( logger ) );
41     }
42
43     /**
44      * Configure and set up DB connection. Here we set the connection
45      * information needed to create the Connection objects. It must
46      * be called only once.
47      *
48      * @param conf The Configuration object needed to describe the
49      * connection.
50      *
51      * @throws ConfigurationException
52      */

53     public void configure( final Configuration configuration )
54         throws ConfigurationException
55     {
56         if( null == m_dataSource )
57         {
58             final String JavaDoc databaseName = configuration.getChild("dbname").getValue();
59
60             try
61             {
62                 final Context JavaDoc initialContext = new InitialContext JavaDoc();
63                 m_dataSource = (DataSource JavaDoc)initialContext.lookup( JDBC_NAME + databaseName );
64             }
65             catch( final NamingException JavaDoc ne )
66             {
67                 if (getLogger().isErrorEnabled())
68                 {
69                     getLogger().error( "Problem with JNDI lookup of datasource", ne );
70                 }
71
72                 throw new ConfigurationException( "Could not use JNDI to find datasource", ne );
73             }
74         }
75     }
76
77     /** Get the database connection */
78     public Connection JavaDoc getConnection()
79         throws SQLException JavaDoc
80     {
81         if( null == m_dataSource )
82         {
83             throw new SQLException JavaDoc( "Can not access DataSource object" );
84         }
85
86         return m_dataSource.getConnection();
87     }
88 }
89
Popular Tags