KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > ManagedFactoryImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
36 import javax.resource.spi.ConnectionManager JavaDoc;
37 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
38 import javax.resource.spi.ManagedConnection JavaDoc;
39 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
40 import javax.resource.spi.ResourceAdapter JavaDoc;
41 import javax.resource.spi.ValidatingManagedConnectionFactory JavaDoc;
42 import javax.security.auth.Subject JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.util.HashSet JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Set JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * The managed factory implementation.
52  */

53 public class ManagedFactoryImpl
54   implements ManagedConnectionFactory JavaDoc, ValidatingManagedConnectionFactory JavaDoc {
55   protected static final Logger JavaDoc 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   /**
74    * Returns the DB pool.
75    */

76   public DBPoolImpl getDBPool()
77   {
78     return _dbPool;
79   }
80
81   /**
82    * Returns the connection config.
83    */

84   ConnectionConfig getConnectionConfig()
85   {
86     return _dbPool.getConnectionConfig();
87   }
88
89   /**
90    * Creates the data source the user sees.
91    */

92   public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc connManager)
93     throws ResourceException JavaDoc
94   {
95     return new DataSourceImpl(this, connManager);
96   }
97
98   /**
99    * Creates the data source the user sees. Not needed in this case,
100    * since ManagedFactoryImpl is only allowed in Resin.
101    */

102   public Object JavaDoc createConnectionFactory()
103     throws ResourceException JavaDoc
104   {
105     throw new UnsupportedOperationException JavaDoc();
106   }
107
108   /**
109    * Creates the underlying managed connection.
110    */

111   public ManagedConnection JavaDoc
112     createManagedConnection(Subject JavaDoc subject,
113                 ConnectionRequestInfo JavaDoc requestInfo)
114     throws ResourceException JavaDoc
115   {
116     Credential credential = (Credential) requestInfo;
117
118     SQLException JavaDoc 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 JavaDoc 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 JavaDoc e) {
146     exn = e;
147       }
148     }
149
150     if (exn != null)
151       throw new ResourceException JavaDoc(exn);
152     else
153       throw new ResourceException JavaDoc(L.l("Can't open database"));
154   }
155
156   /**
157    * Creates the underlying managed connection.
158    */

159   public ManagedConnection JavaDoc
160     matchManagedConnections(Set JavaDoc connSet,
161                 Subject JavaDoc subject,
162                 ConnectionRequestInfo JavaDoc requestInfo)
163     throws ResourceException JavaDoc
164   {
165     Iterator JavaDoc 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   /**
179    * Returns any invalid connections.
180    */

181   public Set JavaDoc getInvalidConnections(Set JavaDoc connSet)
182     throws ResourceException JavaDoc
183   {
184     Iterator JavaDoc iter = connSet.iterator();
185     HashSet JavaDoc 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 JavaDoc();
193
194     invalidSet.add(mConn);
195       }
196     }
197
198     return invalidSet;
199   }
200
201   public void setLogWriter(PrintWriter JavaDoc out)
202     throws ResourceException JavaDoc
203   {
204   }
205
206   public PrintWriter JavaDoc getLogWriter()
207     throws ResourceException JavaDoc
208   {
209     return null;
210   }
211
212   public ResourceAdapter JavaDoc getResourceAdapter()
213   {
214     return null;
215   }
216
217   public void setResourceAdapter(ResourceAdapter JavaDoc adapter)
218   {
219   }
220 }
221
222
Popular Tags