KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > JndiRefForwardingDataSource


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software 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
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0;
25
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.VetoableChangeListener JavaDoc;
29 import java.beans.PropertyVetoException JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.sql.Connection JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.util.Hashtable JavaDoc;
37 import javax.naming.Name JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import javax.sql.DataSource JavaDoc;
41 import com.mchange.v2.log.MLevel;
42 import com.mchange.v2.log.MLog;
43 import com.mchange.v2.log.MLogger;
44 import com.mchange.v2.sql.SqlUtils;
45 import com.mchange.v2.c3p0.impl.JndiRefDataSourceBase;
46
47 final class JndiRefForwardingDataSource extends JndiRefDataSourceBase implements DataSource JavaDoc
48 {
49     final static MLogger logger = MLog.getLogger( JndiRefForwardingDataSource.class );
50
51     //MT: protected by this' lock in all cases
52
transient DataSource JavaDoc cachedInner;
53
54     public JndiRefForwardingDataSource()
55     { this( true ); }
56
57     public JndiRefForwardingDataSource( boolean autoregister )
58     {
59     super( autoregister );
60     setUpPropertyListeners();
61     }
62
63     private void setUpPropertyListeners()
64     {
65     VetoableChangeListener JavaDoc l = new VetoableChangeListener JavaDoc()
66         {
67         public void vetoableChange( PropertyChangeEvent JavaDoc evt ) throws PropertyVetoException JavaDoc
68         {
69             Object JavaDoc val = evt.getNewValue();
70             if ( "jndiName".equals( evt.getPropertyName() ) )
71             {
72                 if (! (val instanceof Name JavaDoc || val instanceof String JavaDoc) )
73                 throw new PropertyVetoException JavaDoc("jndiName must be a String or a javax.naming.Name", evt);
74             }
75         }
76         };
77     this.addVetoableChangeListener( l );
78
79     PropertyChangeListener JavaDoc pcl = new PropertyChangeListener JavaDoc()
80         {
81         public void propertyChange( PropertyChangeEvent JavaDoc evt )
82         { cachedInner = null; }
83         };
84     this.addPropertyChangeListener( pcl );
85     }
86
87     //MT: called only from inner(), effectively synchrtonized
88
private DataSource JavaDoc dereference() throws SQLException JavaDoc
89     {
90     Object JavaDoc jndiName = this.getJndiName();
91     Hashtable JavaDoc jndiEnv = this.getJndiEnv();
92     try
93         {
94         InitialContext JavaDoc ctx;
95         if (jndiEnv != null)
96             ctx = new InitialContext JavaDoc( jndiEnv );
97         else
98             ctx = new InitialContext JavaDoc();
99         if (jndiName instanceof String JavaDoc)
100             return (DataSource JavaDoc) ctx.lookup( (String JavaDoc) jndiName );
101         else if (jndiName instanceof Name JavaDoc)
102             return (DataSource JavaDoc) ctx.lookup( (Name JavaDoc) jndiName );
103         else
104             throw new SQLException JavaDoc("Could not find ConnectionPoolDataSource with " +
105                        "JNDI name: " + jndiName);
106         }
107     catch( NamingException JavaDoc e )
108         {
109         //e.printStackTrace();
110
if ( logger.isLoggable( MLevel.WARNING ) )
111             logger.log( MLevel.WARNING, "An Exception occurred while trying to look up a target DataSource via JNDI!", e );
112         throw SqlUtils.toSQLException( e );
113         }
114     }
115
116     private synchronized DataSource JavaDoc inner() throws SQLException JavaDoc
117     {
118     if (cachedInner != null)
119         return cachedInner;
120     else
121         {
122         DataSource JavaDoc out = dereference();
123         if (this.isCaching())
124             cachedInner = out;
125         return out;
126         }
127     }
128
129     public Connection JavaDoc getConnection() throws SQLException JavaDoc
130     { return inner().getConnection(); }
131
132     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc
133     { return inner().getConnection( username, password ); }
134
135     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc
136     { return inner().getLogWriter(); }
137
138     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc
139     { inner().setLogWriter( out ); }
140
141     public int getLoginTimeout() throws SQLException JavaDoc
142     { return inner().getLoginTimeout(); }
143
144     public void setLoginTimeout(int seconds) throws SQLException JavaDoc
145     { inner().setLoginTimeout( seconds ); }
146
147     // serialization stuff -- set up bound/constrained property event handlers on deserialization
148
private static final long serialVersionUID = 1;
149     private static final short VERSION = 0x0001;
150     
151     private void writeObject( ObjectOutputStream JavaDoc oos ) throws IOException JavaDoc
152     {
153     oos.writeShort( VERSION );
154     }
155     
156     private void readObject( ObjectInputStream JavaDoc ois ) throws IOException JavaDoc, ClassNotFoundException JavaDoc
157     {
158     short version = ois.readShort();
159     switch (version)
160         {
161         case VERSION:
162         setUpPropertyListeners();
163         break;
164         default:
165         throw new IOException JavaDoc("Unsupported Serialized Version: " + version);
166         }
167     }
168 }
169
170
Popular Tags