1 22 23 24 package com.mchange.v2.c3p0; 25 26 import java.beans.PropertyChangeEvent ; 27 import java.beans.PropertyChangeListener ; 28 import java.beans.VetoableChangeListener ; 29 import java.beans.PropertyVetoException ; 30 import java.io.IOException ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutputStream ; 33 import java.io.PrintWriter ; 34 import java.sql.Connection ; 35 import java.sql.SQLException ; 36 import java.util.Hashtable ; 37 import javax.naming.Name ; 38 import javax.naming.NamingException ; 39 import javax.naming.InitialContext ; 40 import javax.sql.DataSource ; 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 48 { 49 final static MLogger logger = MLog.getLogger( JndiRefForwardingDataSource.class ); 50 51 transient DataSource 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 l = new VetoableChangeListener () 66 { 67 public void vetoableChange( PropertyChangeEvent evt ) throws PropertyVetoException 68 { 69 Object val = evt.getNewValue(); 70 if ( "jndiName".equals( evt.getPropertyName() ) ) 71 { 72 if (! (val instanceof Name || val instanceof String ) ) 73 throw new PropertyVetoException ("jndiName must be a String or a javax.naming.Name", evt); 74 } 75 } 76 }; 77 this.addVetoableChangeListener( l ); 78 79 PropertyChangeListener pcl = new PropertyChangeListener () 80 { 81 public void propertyChange( PropertyChangeEvent evt ) 82 { cachedInner = null; } 83 }; 84 this.addPropertyChangeListener( pcl ); 85 } 86 87 private DataSource dereference() throws SQLException 89 { 90 Object jndiName = this.getJndiName(); 91 Hashtable jndiEnv = this.getJndiEnv(); 92 try 93 { 94 InitialContext ctx; 95 if (jndiEnv != null) 96 ctx = new InitialContext ( jndiEnv ); 97 else 98 ctx = new InitialContext (); 99 if (jndiName instanceof String ) 100 return (DataSource ) ctx.lookup( (String ) jndiName ); 101 else if (jndiName instanceof Name ) 102 return (DataSource ) ctx.lookup( (Name ) jndiName ); 103 else 104 throw new SQLException ("Could not find ConnectionPoolDataSource with " + 105 "JNDI name: " + jndiName); 106 } 107 catch( NamingException e ) 108 { 109 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 inner() throws SQLException 117 { 118 if (cachedInner != null) 119 return cachedInner; 120 else 121 { 122 DataSource out = dereference(); 123 if (this.isCaching()) 124 cachedInner = out; 125 return out; 126 } 127 } 128 129 public Connection getConnection() throws SQLException 130 { return inner().getConnection(); } 131 132 public Connection getConnection(String username, String password) throws SQLException 133 { return inner().getConnection( username, password ); } 134 135 public PrintWriter getLogWriter() throws SQLException 136 { return inner().getLogWriter(); } 137 138 public void setLogWriter(PrintWriter out) throws SQLException 139 { inner().setLogWriter( out ); } 140 141 public int getLoginTimeout() throws SQLException 142 { return inner().getLoginTimeout(); } 143 144 public void setLoginTimeout(int seconds) throws SQLException 145 { inner().setLoginTimeout( seconds ); } 146 147 private static final long serialVersionUID = 1; 149 private static final short VERSION = 0x0001; 150 151 private void writeObject( ObjectOutputStream oos ) throws IOException 152 { 153 oos.writeShort( VERSION ); 154 } 155 156 private void readObject( ObjectInputStream ois ) throws IOException , ClassNotFoundException 157 { 158 short version = ois.readShort(); 159 switch (version) 160 { 161 case VERSION: 162 setUpPropertyListeners(); 163 break; 164 default: 165 throw new IOException ("Unsupported Serialized Version: " + version); 166 } 167 } 168 } 169 170 | Popular Tags |