KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > datasource > PoolDataSource


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email: Use "Contact Us Form" on website
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */

21
22 package uk.org.primrose.pool.datasource;
23
24 import uk.org.primrose.pool.jmx.*;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import javax.sql.DataSource JavaDoc;
30 import javax.naming.*;
31 import java.util.ArrayList JavaDoc;
32 import uk.org.primrose.vendor.jboss.jndi.FakeSerializableArrayList;
33
34 public class PoolDataSource implements DataSource JavaDoc {
35
36     private String JavaDoc poolName = "";
37
38     public void setPoolName(String JavaDoc poolName) {
39         this.poolName = poolName;
40     }
41
42     public String JavaDoc getPoolName() {
43         return poolName;
44     }
45
46     /**
47     * Attempts to establish a connection with the data source that this DataSource object represents.
48     */

49     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
50     Connection JavaDoc conn = null;
51         try {
52             Context ctx = new InitialContext();
53             ArrayList JavaDoc al = null;
54             Object JavaDoc o = null;
55
56             try {
57                 o = ctx.lookup("java:comp/env/masterPool");
58             } catch (Exception JavaDoc e) {
59                 o = ctx.lookup("masterPool");
60             }
61
62             String JavaDoc s = o.getClass().getName();
63             if (o instanceof ArrayList JavaDoc) {
64                 al = (ArrayList JavaDoc)o;
65             } else if (s.indexOf("FakeSerializableArrayList") != -1) {
66                 FakeSerializableArrayList alf = (FakeSerializableArrayList)o;
67                 al = alf.initialize();
68             } else if (s.indexOf("BindPrimroseFactory") != -1) {
69                 if (s.indexOf("resin") != -1) {
70                     uk.org.primrose.vendor.resin.jndi.BindPrimroseFactory brf = (uk.org.primrose.vendor.resin.jndi.BindPrimroseFactory)o;
71                     al = brf.getPools();
72                 } else if (s.indexOf("jetty") != -1) {
73                     if (s.indexOf("BindPrimroseFactoryV6") != -1) {
74                         uk.org.primrose.vendor.jetty.BindPrimroseFactoryV6 brf = (uk.org.primrose.vendor.jetty.BindPrimroseFactoryV6)o;
75                         al = brf.getPools();
76                     } else {
77                         uk.org.primrose.vendor.jetty.BindPrimroseFactory brf = (uk.org.primrose.vendor.jetty.BindPrimroseFactory)o;
78                         al = brf.getPools();
79
80                     }
81                 }
82
83             } else {
84                 throw new SQLException JavaDoc("Cannot get list of pools available, so cannot return connection");
85             }
86
87             //if (o instanceof ArrayList) {
88
// al = (ArrayList)o;
89
//} else {
90
// FakeSerializableArrayList alf = (FakeSerializableArrayList)o;
91
// al = alf.initialize();
92
//}
93

94             // Loop the pools until we find the one that we're after
95
// and then return a connection from the Queue
96

97         if (al.size() == 0) {
98         throw new SQLException JavaDoc("No Pool Queues have been loaded - primrose initialisation problem ?");
99         }
100
101             for (int i = 0; i < al.size(); i++) {
102                 Queue q = (Queue)al.get(i);
103                 if (q.getName().equals(poolName)) {
104                     conn = q.getConnection();
105                 }
106             }
107
108             if (conn == null) {
109             throw new SQLException JavaDoc("Cannot find valid connection object from pools - perhaps db is down ?");
110
111             }
112         return conn;
113         } catch (Exception JavaDoc e) {
114             e.printStackTrace(System.err);
115         throw new SQLException JavaDoc("Error retrieving list of pools available, or extracting valid connection - so cannot return connection");
116         }
117
118     }
119
120     /**
121     * Attempts to establish a connection with the data source that this DataSource object represents.
122     */

123     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc {
124         return getConnection();
125     }
126
127     /**
128     * Gets the maximum time in seconds that this data source can wait while attempting to connect to a database.
129     */

130     public int getLoginTimeout() {
131         return -1;
132     }
133
134     /**
135     * Retrieves the log writer for this DataSource object.
136     */

137     public PrintWriter JavaDoc getLogWriter() {
138         return null;
139     }
140
141     /**
142     * Sets the maximum time in seconds that this data source will wait while attempting to connect to a database.
143     */

144     public void setLoginTimeout(int seconds) {
145
146     }
147
148     /**
149     * Sets the log writer for this DataSource object to the given java.io.PrintWriter object.
150     */

151     public void setLogWriter(PrintWriter JavaDoc out) {
152
153     }
154
155 }
156
Popular Tags