KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jdbc > DataSourceImpl


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: DataSourceImpl.java,v 1.5 2005/05/04 14:09:00 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.jdbc;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30 import java.sql.Connection JavaDoc;
31 import java.sql.SQLException JavaDoc;
32
33 import javax.naming.Reference JavaDoc;
34
35 import javax.resource.Referenceable JavaDoc;
36 import javax.resource.ResourceException JavaDoc;
37 import javax.resource.spi.ConnectionManager JavaDoc;
38
39 import org.objectweb.util.monolog.api.BasicLevel;
40
41 /**
42  * Description of the DataSourceImpl. This is the dataSource for the JDBC RA.
43  * @author Eric Hardesty
44  * @created 22 May 2003
45  */

46 public class DataSourceImpl implements javax.sql.DataSource JavaDoc, Serializable JavaDoc, Referenceable JavaDoc {
47
48     private ConnectionManager JavaDoc cm;
49
50     private ManagedConnectionFactoryImpl mcf;
51
52     private PrintWriter JavaDoc pw;
53
54     private Reference JavaDoc reference;
55
56     int loginTimeout = 0;
57
58     /**
59      * Logging (debug enabled)
60      */

61     private final boolean isDebugOn;
62
63     public DataSourceImpl(ManagedConnectionFactoryImpl _mcf, ConnectionManager JavaDoc _cm) {
64         if (_cm == null) {
65             cm = new ConnectionManagerImpl();
66         } else {
67             cm = _cm;
68         }
69         mcf = _mcf;
70         isDebugOn = mcf.trace.isLoggable(BasicLevel.DEBUG);
71     }
72
73     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
74         if (isDebugOn) {
75             mcf.trace.log(BasicLevel.DEBUG, "");
76         }
77         try {
78             Connection JavaDoc con = (Connection JavaDoc) cm.allocateConnection(mcf, null);
79             if (con == null) {
80                 SQLException JavaDoc se = new SQLException JavaDoc("Null connection object returned");
81                 throw se;
82             }
83             return con;
84         } catch (ResourceException JavaDoc re) {
85             throw new SQLException JavaDoc(re.getMessage());
86         }
87     }
88
89     public Connection JavaDoc getConnection(String JavaDoc user, String JavaDoc pwd) throws SQLException JavaDoc {
90         if (isDebugOn) {
91             mcf.trace.log(BasicLevel.DEBUG, "" + user);
92         }
93         try {
94             ConnectionRequestInfoImpl info = new ConnectionRequestInfoImpl(user, pwd);
95             Connection JavaDoc con = (Connection JavaDoc) cm.allocateConnection(mcf, info);
96             if (con == null) {
97                 SQLException JavaDoc se = new SQLException JavaDoc("Null connection object returned");
98                 throw se;
99             }
100             return con;
101         } catch (ResourceException JavaDoc re) {
102             throw new SQLException JavaDoc(re.getMessage());
103         }
104     }
105
106     public int getLoginTimeout() throws SQLException JavaDoc {
107         return loginTimeout;
108     }
109
110     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
111         return pw;
112     }
113
114     public Reference JavaDoc getReference() {
115         return reference;
116     }
117
118     public void setLoginTimeout(int _loginTimeout) throws SQLException JavaDoc {
119         loginTimeout = _loginTimeout;
120     }
121
122     public void setLogWriter(PrintWriter JavaDoc _pw) throws SQLException JavaDoc {
123         pw = _pw;
124     }
125
126     public void setReference(Reference JavaDoc _ref) {
127         reference = _ref;
128     }
129
130     /* JOnAS JDBC implementation for CMP */
131
132     public String JavaDoc getMapperName() {
133         String JavaDoc mn = mcf.getMapperName();
134         if (isDebugOn) {
135             mcf.trace.log(BasicLevel.DEBUG, mn);
136         }
137         return mn;
138     }
139
140 }
141
Popular Tags