1 29 30 package com.caucho.sql; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 35 import javax.resource.ResourceException ; 36 import javax.resource.spi.ConnectionManager ; 37 import javax.resource.spi.ConnectionRequestInfo ; 38 import javax.resource.spi.ManagedConnection ; 39 import javax.resource.spi.ManagedConnectionFactory ; 40 import javax.resource.spi.ResourceAdapter ; 41 import javax.resource.spi.ValidatingManagedConnectionFactory ; 42 import javax.security.auth.Subject ; 43 import java.io.PrintWriter ; 44 import java.sql.SQLException ; 45 import java.util.HashSet ; 46 import java.util.Iterator ; 47 import java.util.Set ; 48 import java.util.logging.Logger ; 49 50 53 public class ManagedFactoryImpl 54 implements ManagedConnectionFactory , ValidatingManagedConnectionFactory { 55 protected static final Logger log = Log.open(ManagedFactoryImpl.class); 56 private static final L10N L = new L10N(ManagedFactoryImpl.class); 57 58 private DBPoolImpl _dbPool; 59 private DriverConfig []_drivers; 60 private DriverConfig []_backupDrivers; 61 62 private long _roundRobin; 63 64 ManagedFactoryImpl(DBPoolImpl dbPool, 65 DriverConfig []drivers, 66 DriverConfig []backupDrivers) 67 { 68 _dbPool = dbPool; 69 _drivers = drivers; 70 _backupDrivers = backupDrivers; 71 } 72 73 76 public DBPoolImpl getDBPool() 77 { 78 return _dbPool; 79 } 80 81 84 ConnectionConfig getConnectionConfig() 85 { 86 return _dbPool.getConnectionConfig(); 87 } 88 89 92 public Object createConnectionFactory(ConnectionManager connManager) 93 throws ResourceException 94 { 95 return new DataSourceImpl(this, connManager); 96 } 97 98 102 public Object createConnectionFactory() 103 throws ResourceException 104 { 105 throw new UnsupportedOperationException (); 106 } 107 108 111 public ManagedConnection 112 createManagedConnection(Subject subject, 113 ConnectionRequestInfo requestInfo) 114 throws ResourceException 115 { 116 Credential credential = (Credential) requestInfo; 117 118 SQLException exn = null; 119 120 for (int i = 0; i < _drivers.length; i++) { 121 int index = (int) (_roundRobin++ % _drivers.length); 122 123 DriverConfig driver = _drivers[index]; 124 125 try { 126 return new ManagedConnectionImpl(this, 127 driver, 128 _dbPool.getConnectionConfig(), 129 credential); 130 } catch (SQLException e) { 131 exn = e; 132 } 133 } 134 135 for (int i = 0; i < _backupDrivers.length; i++) { 136 int index = (int) (_roundRobin++ % _backupDrivers.length); 137 138 DriverConfig driver = _backupDrivers[index]; 139 140 try { 141 return new ManagedConnectionImpl(this, 142 driver, 143 _dbPool.getConnectionConfig(), 144 credential); 145 } catch (SQLException e) { 146 exn = e; 147 } 148 } 149 150 if (exn != null) 151 throw new ResourceException (exn); 152 else 153 throw new ResourceException (L.l("Can't open database")); 154 } 155 156 159 public ManagedConnection 160 matchManagedConnections(Set connSet, 161 Subject subject, 162 ConnectionRequestInfo requestInfo) 163 throws ResourceException 164 { 165 Iterator iter = connSet.iterator(); 166 167 while (iter.hasNext()) { 168 ManagedConnectionImpl mConn = (ManagedConnectionImpl) iter.next(); 169 170 if (requestInfo == mConn.getCredentials() || 171 requestInfo != null && requestInfo.equals(mConn.getCredentials())) 172 return mConn; 173 } 174 175 return null; 176 } 177 178 181 public Set getInvalidConnections(Set connSet) 182 throws ResourceException 183 { 184 Iterator iter = connSet.iterator(); 185 HashSet invalidSet = null; 186 187 while (iter.hasNext()) { 188 ManagedConnectionImpl mConn = (ManagedConnectionImpl) iter.next(); 189 190 if (! mConn.isValid()) { 191 if (invalidSet == null) 192 invalidSet = new HashSet (); 193 194 invalidSet.add(mConn); 195 } 196 } 197 198 return invalidSet; 199 } 200 201 public void setLogWriter(PrintWriter out) 202 throws ResourceException 203 { 204 } 205 206 public PrintWriter getLogWriter() 207 throws ResourceException 208 { 209 return null; 210 } 211 212 public ResourceAdapter getResourceAdapter() 213 { 214 return null; 215 } 216 217 public void setResourceAdapter(ResourceAdapter adapter) 218 { 219 } 220 } 221 222 | Popular Tags |