KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.configuration.Configuration;
15 import org.apache.avalon.framework.configuration.ConfigurationException;
16 import org.apache.avalon.framework.logger.AbstractLogEnabled;
17 import org.apache.avalon.framework.logger.LogKitLogger;
18 import org.apache.avalon.framework.logger.Loggable;
19 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
20 import com.informix.jdbcx.IfxConnectionPoolDataSource;
21 import com.informix.jdbcx.IfxDataSource;
22
23 /**
24  * The Informix implementation for DataSources in Excalibur. This uses the
25  * <code>com.informix.jdbcx.IfxConnectionPoolDataSource</code> object. It uses
26  * the following format for configuration (italics mark information you change):
27  *
28  * <pre>
29  * &lt;informix&gt;
30  * &lt;pool-controller init="<i>5</i>" min="<i>5</i>" max="<i>10</i>"/&gt;
31  * &lt;dbname&gt;<i>dbname</i>&lt;/dbname&gt;
32  * &lt;servername&gt;<i>servername</i>&lt;/servername&gt;
33  * &lt;host port="<i>2000</i>"&gt;<i>host</i>&lt;/host&gt;
34  * &lt;user&gt;<i>user</i>&lt;/user&gt;
35  * &lt;password&gt;<i>user</i>&lt;/password&gt;
36  * &lt;informix&gt;
37  * </pre>
38  *
39  * Informix doesn't like the JdbcDataSource Component, so we gave it it's own.
40  * Do not use this datasource if you are planning on using your J2EE server's
41  * connection pooling.
42  *
43  * You must have Informix's JDBC 2.2 or higher jar file, as well as the extensions
44  * jar file (<code>ifxjdbc.jar</code> and <code>ifxjdbcx.jar</code>). Also, this
45  * DataSource requires the Avalon Cadastre package because it uses the MemoryContext.
46  *
47  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
48  * @version CVS $Revision: 1.6 $ $Date: 2001/12/11 09:53:28 $
49  * @since 4.0
50  */

51 public class InformixDataSource
52     extends AbstractLogEnabled
53     implements DataSourceComponent, Loggable
54 {
55     private IfxDataSource m_dataSource;
56     private boolean m_autocommit;
57     private static boolean INIT_FACTORY = false;
58
59     /**
60      * Set up the system property for the context factory if it hasn't been
61      * done already. This is not done in a static initializer due to the
62      * existence of the J2eeDataSource.
63      */

64     public InformixDataSource()
65     {
66         if ( ! InformixDataSource.INIT_FACTORY )
67         {
68             System.setProperty( Context.INITIAL_CONTEXT_FACTORY,
69                  "org.apache.avalon.excalibur.naming.memory.MemoryInitialContextFactory" );
70         }
71     }
72
73     public void setLogger( final org.apache.log.Logger logger )
74     {
75         enableLogging( new LogKitLogger( logger ) );
76     }
77
78     /**
79      * Return an Informix Connection object
80      */

81     public Connection JavaDoc getConnection() throws SQLException JavaDoc
82     {
83         Connection JavaDoc conn = m_dataSource.getConnection();
84
85         if ( conn.getAutoCommit() != m_autocommit )
86         {
87             conn.setAutoCommit(m_autocommit);
88         }
89
90         return conn;
91     }
92
93     /**
94      * Set up the Informix driver for direct use.
95      */

96     public void configure(Configuration conf) throws ConfigurationException
97     {
98         Configuration poolController = conf.getChild("pool-controller");
99         String JavaDoc dbname = conf.getChild("dbname").getValue("ifx");
100         IfxConnectionPoolDataSource pooledDataSource = new IfxConnectionPoolDataSource();
101         m_autocommit = conf.getChild("autocommit").getValueAsBoolean(true);
102
103         pooledDataSource.setIfxCPMInitPoolSize(poolController.getAttributeAsInteger("init", 5));
104         pooledDataSource.setIfxCPMMinPoolSize(poolController.getAttributeAsInteger("min", 5));
105         pooledDataSource.setIfxCPMMaxPoolSize(poolController.getAttributeAsInteger("max", 10));
106         pooledDataSource.setIfxCPMServiceInterval(100);
107         pooledDataSource.setServerName(conf.getChild("servername").getValue());
108         pooledDataSource.setDatabaseName(conf.getChild("dbname").getValue());
109         pooledDataSource.setIfxIFXHOST(conf.getChild("host").getValue());
110         pooledDataSource.setPortNumber(conf.getChild("host").getAttributeAsInteger("port"));
111         pooledDataSource.setUser(conf.getChild("user").getValue());
112         pooledDataSource.setPassword(conf.getChild("password").getValue());
113
114         try
115         {
116             Context JavaDoc context = new InitialContext JavaDoc();
117
118             context.bind(dbname + "pool", pooledDataSource);
119
120             m_dataSource = new IfxDataSource();
121             m_dataSource.setDataSourceName(dbname + "pool");
122             m_dataSource.setServerName(conf.getChild("servername").getValue());
123             m_dataSource.setDatabaseName(conf.getChild("dbname").getValue());
124             m_dataSource.setIfxIFXHOST(conf.getChild("host").getValue());
125             m_dataSource.setPortNumber(conf.getChild("host").getAttributeAsInteger("port"));
126             m_dataSource.setUser(conf.getChild("user").getValue());
127             m_dataSource.setPassword(conf.getChild("password").getValue());
128
129             context.bind(dbname, m_dataSource);
130         }
131         catch (Exception JavaDoc e)
132         {
133             if (getLogger().isErrorEnabled())
134             {
135                 getLogger().error("There was an error trying to bind the connection pool", e);
136             }
137             throw new ConfigurationException("There was an error trying to bind the connection pool", e);
138         }
139     }
140 }
141
Popular Tags