KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > vendor > jboss > jndi > FakeSerializableDataSource


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.vendor.jboss.jndi;
23
24 import java.io.Serializable JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import javax.sql.DataSource JavaDoc;
28 import org.jboss.naming.NonSerializableFactory;
29
30 /**
31 * This comment is on all of the vendor.jboss package classes, because the classes are
32 * so interlinked. It documents how we bind primrose under JBoss.<p>
33 * JBoss has two JNDI trees.
34 * <ul>
35 * <li>A serializable object tree.
36 * <li>A non-serializable object tree.
37 * </ul>
38 * Primrose's core jmx objects (Queue, PoolController) cannot be bound under the serializable tree
39 * because JBoss checks <i>on binding</i> whether the objects can be serialized. Because the Queue objects
40 * contain Connection objects, this will always fail.<p>
41 * So how do we get around this ?!<br>
42 * First, the MasterPoolDataSourceFactory (deployed via the SAR) initializes the pool, and binds
43 * the Queue objects, and the PoolController object under the non-serializable tree.<p>
44 * Then all the pool DataSource objects, created via the PoolDataSourceFactory's (via BindPrimrose)
45 * are all bound under the non-serializable tree.<br>
46 * Get a list of the pools bound by PoolController,
47 * and then for each one, let the PoolDataSourceFactory object
48 * create our DatSource objects, which we will bind under the non-serializable tree.
49 * Also bind our 'fake' DataSource objects under the normal JNDI tree
50 * so they can be bound, and access the real non-serializable objects at runtime
51 * for extracting client connections.<p>
52 *
53 * When client code looks up our fake DataSource object (unbeknown to them obviously), and calls
54 * the getConnection() method, our fake object gets a ref to the real DataSource from the non-serializable
55 * JNDI tree, and extracts the Connection for the client.<p>
56 *
57 * Why does this work ? <br>
58 * The serializable JNDI tree only checks for serializablity at the point of binding - not when you
59 * do lookup() calls - so as long as we can bind our fake DataSources at the primrose initialization
60 * point, it works.
61 */

62 public class FakeSerializableDataSource implements Serializable JavaDoc, DataSource JavaDoc {
63     private String JavaDoc name = "";
64
65     /**
66     * Default constructor to conform with Serialization protocol. Not used.
67     */

68     public FakeSerializableDataSource() {}
69
70     /**
71     * Construct the object with the name that the real JNDI resource is bound under.
72     */

73     public FakeSerializableDataSource(String JavaDoc name) {
74         this.name = name;
75     }
76
77     /**
78     * Attempts to establish a connection with the data source that this DataSource object represents.<br>
79     * Looks up the object in the non-serializable JNDI tree, and extracts the connection.
80     */

81     public Connection JavaDoc getConnection() {
82         try {
83             DataSource JavaDoc ds = (DataSource JavaDoc)NonSerializableFactory.lookup(name);
84             return ds.getConnection();
85         } catch (Exception JavaDoc e) {
86             System.err.println("[FakeSerializableDataSource] ERRROR ! Cannot find real datasource in non-serializable JNDI tree");
87             e.printStackTrace(System.err);
88             return null;
89         }
90
91     }
92
93     /**
94     * Attempts to establish a connection with the data source that this DataSource object represents.<br>
95     * Defaults to use the no-args method - usernames should be set in poolConfig.properties file.
96     */

97     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) {
98         return getConnection();
99     }
100
101     /**
102     * Gets the maximum time in seconds that this data source can wait while attempting to connect to a database.
103     * <br> Not used - only there for DataSource compatibilty.
104     */

105     public int getLoginTimeout() {
106         return -1;
107     }
108
109     /**
110     * Retrieves the log writer for this DataSource object.
111     */

112     public PrintWriter JavaDoc getLogWriter() {
113         return null;
114     }
115
116     /**
117     * Sets the maximum time in seconds that this data source will wait while attempting to connect to a database.
118     * <br> Not used - only there for DataSource compatibilty.
119     */

120     public void setLoginTimeout(int seconds) {
121
122     }
123
124     /**
125     * Sets the log writer for this DataSource object to the given java.io.PrintWriter object.
126     * <br> Not used - only there for DataSource compatibilty.
127     */

128     public void setLogWriter(PrintWriter JavaDoc out) {
129
130     }
131 }
Popular Tags