KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > local > LocalTxDataSource


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
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 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 software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.jdbc.local;
23
24 import java.util.Hashtable JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.Name JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.resource.ResourceException JavaDoc;
29 import javax.resource.spi.ConnectionManager JavaDoc;
30 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
31 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
32 import javax.transaction.TransactionManager JavaDoc;
33 import org.jboss.deployment.DeploymentException;
34 import org.jboss.logging.Logger;
35 import org.jboss.resource.connectionmanager.CachedConnectionManager;
36 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
37 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
38 import org.jboss.resource.connectionmanager.TxConnectionManager;
39 import org.jboss.resource.connectionmanager.CachedConnectionManagerReference;
40 import org.jboss.util.naming.NonSerializableFactory;
41 import org.jboss.util.naming.Util;
42
43 /**
44  * This is a pojo that instantiates a Local tx connection pool.
45  * It provides same functionality as ds.xml files
46  *
47  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
48  * @version $Revision: 37459 $
49  */

50 public class LocalTxDataSource
51 {
52    protected static Logger log = Logger.getLogger(LocalTxDataSource.class.getName());
53
54
55    public LocalTxDataSource()
56    {
57    }
58
59    private CachedConnectionManager cachedConnectionManager;
60    private TransactionManager JavaDoc transactionManager;
61    private String JavaDoc jndiName;
62
63    private InternalManagedConnectionPool.PoolParams poolParams = new InternalManagedConnectionPool.PoolParams();
64    private LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory();
65
66    private JBossManagedConnectionPool.OnePool pool = new JBossManagedConnectionPool.OnePool(mcf, poolParams, false, log);
67    private TxConnectionManager connectionManager;
68    private Object JavaDoc datasource;
69    protected Hashtable JavaDoc initialContextProperties;
70    protected InitialContext JavaDoc initialContext;
71
72    public class ConnectionManagerDelegate implements ConnectionManager JavaDoc
73    {
74       private static final long serialVersionUID = 1L;
75
76       public Object JavaDoc allocateConnection(ManagedConnectionFactory JavaDoc mcf, ConnectionRequestInfo JavaDoc cxRequestInfo) throws ResourceException JavaDoc
77       {
78          return connectionManager.allocateConnection(mcf, cxRequestInfo);
79       }
80    }
81
82    public void setInitialContextProperties(Hashtable JavaDoc initialContextProperties)
83    {
84       this.initialContextProperties = initialContextProperties;
85    }
86
87    public void start() throws Exception JavaDoc
88    {
89       if (initialContextProperties == null) initialContext = new InitialContext JavaDoc();
90       else initialContext = new InitialContext JavaDoc(initialContextProperties);
91       
92       connectionManager = new TxConnectionManager(cachedConnectionManager, pool, transactionManager);
93       connectionManager.setLocalTransactions(true);
94       connectionManager.setTrackConnectionByTx(true);
95       pool.setConnectionListenerFactory(connectionManager);
96       datasource = connectionManager.getPoolingStrategy().getManagedConnectionFactory().createConnectionFactory(new ConnectionManagerDelegate());
97       bindConnectionFactory();
98    }
99
100
101    /**
102     * Bind the connection factory into jndi
103     */

104    protected void bindConnectionFactory() throws Exception JavaDoc
105    {
106       InitialContext JavaDoc ctx = initialContext;
107       try
108       {
109          Name JavaDoc name = ctx.getNameParser("").parse(jndiName);
110          String JavaDoc key = name.toString();
111          if( true == true && name.size() > 1 )
112          {
113             int size = name.size() - 1;
114             Util.createSubcontext(initialContext, name.getPrefix(size));
115          }
116          NonSerializableFactory.rebind(initialContext, key, datasource);
117          log.info("Bound datasource to JNDI name '" + jndiName + "'");
118       }
119       catch (NamingException JavaDoc ne)
120       {
121          throw new DeploymentException("Could not bind ConnectionFactory into jndi: " + jndiName, ne);
122       }
123       finally
124       {
125          ctx.close();
126       }
127    }
128
129    protected void unbindConnectionFactory() throws Exception JavaDoc
130    {
131       InitialContext JavaDoc ctx = initialContext;
132       try
133       {
134          ctx.unbind(jndiName);
135          NonSerializableFactory.unbind(jndiName);
136          log.info("Unbound datasource for JNDI name '" + jndiName + "'");
137       }
138       catch (NamingException JavaDoc ne)
139       {
140          log.error("Could not unbind datasource from jndi: " + jndiName, ne);
141       }
142       finally
143       {
144          ctx.close();
145       }
146    }
147
148    public Object JavaDoc getDatasource()
149    {
150       return datasource;
151    }
152
153    public void setCachedConnectionManager(CachedConnectionManagerReference cachedConnectionManager)
154    {
155       this.cachedConnectionManager = cachedConnectionManager.getCachedConnectionManager();
156    }
157
158    public TransactionManager JavaDoc getTransactionManager()
159    {
160       return transactionManager;
161    }
162
163    public void setTransactionManager(TransactionManager JavaDoc transactionManager)
164    {
165       this.transactionManager = transactionManager;
166    }
167
168    public String JavaDoc getJndiName()
169    {
170       return jndiName;
171    }
172
173    public void setJndiName(String JavaDoc jndiName)
174    {
175       this.jndiName = jndiName;
176    }
177
178    public int getMinSize()
179    {
180       return poolParams.minSize;
181    }
182
183    public void setMinSize(int minSize)
184    {
185       poolParams.minSize = minSize;
186    }
187
188    public int getMaxSize()
189    {
190       return poolParams.maxSize;
191    }
192
193    public void setMaxSize(int maxSize)
194    {
195       poolParams.maxSize = maxSize;
196    }
197
198    public int getBlockingTimeout()
199    {
200       return poolParams.blockingTimeout;
201    }
202
203    public void setBlockingTimeout(int blockingTimeout)
204    {
205       poolParams.blockingTimeout = blockingTimeout;
206    }
207
208    public long getIdleTimeout()
209    {
210       return poolParams.idleTimeout;
211    }
212
213    public void setIdleTimeout(long idleTimeout)
214    {
215       poolParams.idleTimeout = idleTimeout;
216    }
217
218    public String JavaDoc getDriverClass()
219    {
220       return mcf.getDriverClass();
221    }
222
223    public void setDriverClass(final String JavaDoc driverClass)
224    {
225       mcf.setDriverClass(driverClass);
226    }
227
228    public String JavaDoc getConnectionURL()
229    {
230       return mcf.getConnectionURL();
231    }
232
233    public void setConnectionURL(final String JavaDoc connectionURL)
234    {
235       mcf.setConnectionURL(connectionURL);
236    }
237
238    public void setUserName(final String JavaDoc userName)
239    {
240       mcf.setUserName(userName);
241    }
242
243    public void setPassword(final String JavaDoc password)
244    {
245       mcf.setPassword(password);
246    }
247
248    public void setPreparedStatementCacheSize(int size)
249    {
250       mcf.setPreparedStatementCacheSize(size);
251    }
252
253    public int getPreparedStatementCacheSize()
254    {
255       return mcf.getPreparedStatementCacheSize();
256    }
257
258    public boolean getSharePreparedStatements()
259    {
260       return mcf.getSharePreparedStatements();
261    }
262
263    public void setSharePreparedStatements(boolean sharePS)
264    {
265       mcf.setSharePreparedStatements(sharePS);
266    }
267
268    public boolean getTxQueryTimeout()
269    {
270       return mcf.isTransactionQueryTimeout();
271    }
272
273    public void setTxQueryTimeout(boolean qt)
274    {
275       mcf.setTransactionQueryTimeout(qt);
276    }
277
278    public String JavaDoc getTransactionIsolation()
279    {
280       return mcf.getTransactionIsolation();
281    }
282
283    public void setTransactionIsolation(String JavaDoc transactionIsolation)
284    {
285       mcf.setTransactionIsolation(transactionIsolation);
286    }
287
288    public String JavaDoc getNewConnectionSQL()
289    {
290       return mcf.getNewConnectionSQL();
291    }
292
293    public void setNewConnectionSQL(String JavaDoc newConnectionSQL)
294    {
295       mcf.setNewConnectionSQL(newConnectionSQL);
296    }
297
298    public String JavaDoc getCheckValidConnectionSQL()
299    {
300       return mcf.getCheckValidConnectionSQL();
301    }
302
303    public void setCheckValidConnectionSQL(String JavaDoc checkValidConnectionSQL)
304    {
305       mcf.setCheckValidConnectionSQL(checkValidConnectionSQL);
306    }
307
308    public String JavaDoc getTrackStatements()
309    {
310       return mcf.getTrackStatements();
311    }
312
313    public void setTrackStatements(String JavaDoc value)
314    {
315       mcf.setTrackStatements(value);
316    }
317
318    public String JavaDoc getExceptionSorterClassName()
319    {
320       return mcf.getExceptionSorterClassName();
321    }
322
323    public void setExceptionSorterClassName(String JavaDoc exceptionSorterClassName)
324    {
325       mcf.setExceptionSorterClassName(exceptionSorterClassName);
326    }
327
328    public String JavaDoc getValidConnectionCheckerClassName()
329    {
330       return mcf.getValidConnectionCheckerClassName();
331    }
332
333    public void setValidConnectionCheckerClassName(String JavaDoc value)
334    {
335       mcf.setValidConnectionCheckerClassName(value);
336    }
337
338 }
339
Popular Tags