KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > WrapperDataSource


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;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import javax.naming.Reference JavaDoc;
30 import javax.resource.Referenceable JavaDoc;
31 import javax.resource.ResourceException JavaDoc;
32 import javax.resource.spi.ConnectionManager JavaDoc;
33 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
34 import javax.sql.DataSource JavaDoc;
35 import javax.transaction.RollbackException JavaDoc;
36
37 import org.jboss.tm.TransactionTimeoutConfiguration;
38 import org.jboss.util.NestedSQLException;
39
40 /**
41  * WrapperDataSource
42  *
43  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
44  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
45  * @version $Revision: 37459 $
46  */

47 public class WrapperDataSource implements Referenceable JavaDoc, DataSource JavaDoc, Serializable JavaDoc
48 {
49    static final long serialVersionUID = 3570285419164793501L;
50    
51    private final BaseWrapperManagedConnectionFactory mcf;
52    private final ConnectionManager JavaDoc cm;
53
54    private Reference JavaDoc reference;
55
56    public WrapperDataSource (final BaseWrapperManagedConnectionFactory mcf, final ConnectionManager JavaDoc cm)
57    {
58       this.mcf = mcf;
59       this.cm = cm;
60    }
61
62    public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc
63    {
64       // TODO: implement this javax.sql.DataSource method
65
return null;
66    }
67
68    public void setLogWriter(PrintWriter JavaDoc param1) throws SQLException JavaDoc
69    {
70       // TODO: implement this javax.sql.DataSource method
71
}
72
73    public int getLoginTimeout() throws SQLException JavaDoc
74    {
75       // TODO: implement this javax.sql.DataSource method
76
return 0;
77    }
78
79    public void setLoginTimeout(int param1) throws SQLException JavaDoc
80    {
81       // TODO: implement this javax.sql.DataSource method
82
}
83    
84    public Connection JavaDoc getConnection() throws SQLException JavaDoc
85    {
86       try
87       {
88          WrappedConnection wc = (WrappedConnection) cm.allocateConnection(mcf, null);
89          wc.setDataSource(this);
90          return wc;
91       }
92       catch (ResourceException JavaDoc re)
93       {
94          throw new NestedSQLException(re);
95       }
96    }
97
98    public Connection JavaDoc getConnection(String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc
99    {
100       ConnectionRequestInfo JavaDoc cri = new WrappedConnectionRequestInfo(user, password);
101       try
102       {
103          WrappedConnection wc = (WrappedConnection) cm.allocateConnection(mcf, cri);
104          wc.setDataSource(this);
105          return wc;
106       }
107       catch (ResourceException JavaDoc re)
108       {
109          throw new NestedSQLException(re);
110       }
111    }
112    
113    public void setReference(final Reference JavaDoc reference)
114    {
115       this.reference = reference;
116    }
117
118    public Reference JavaDoc getReference()
119    {
120       return reference;
121    }
122    
123    protected int getTimeLeftBeforeTransactionTimeout() throws SQLException JavaDoc
124    {
125       try
126       {
127          if (cm instanceof TransactionTimeoutConfiguration)
128          {
129             long timeout = ((TransactionTimeoutConfiguration) cm).getTimeLeftBeforeTransactionTimeout(true);
130             // No timeout
131
if (timeout == -1)
132                return -1;
133             // Round up to the nearest second
134
long result = timeout / 1000;
135             if ((result % 1000) != 0)
136                ++result;
137             return (int) result;
138          }
139          else
140             return -1;
141       }
142       catch (RollbackException JavaDoc e)
143       {
144          throw new NestedSQLException(e);
145       }
146    }
147 }
148
Popular Tags