KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.sql;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import javax.resource.ResourceException JavaDoc;
35 import javax.resource.spi.ConnectionManager JavaDoc;
36 import javax.sql.DataSource JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.sql.Connection JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * The wrapped data source.
44  */

45 public class DataSourceImpl implements DataSource JavaDoc {
46   protected static final Logger JavaDoc log = Log.open(DataSourceImpl.class);
47   private static final L10N L = new L10N(DataSourceImpl.class);
48
49   private ManagedFactoryImpl _managedFactory;
50   private ConnectionManager JavaDoc _connManager;
51
52   DataSourceImpl(ManagedFactoryImpl factory, ConnectionManager JavaDoc cm)
53   {
54     _managedFactory = factory;
55     _connManager = cm;
56   }
57
58   /**
59    * Returns a connection.
60    */

61   public Connection JavaDoc getConnection()
62     throws SQLException JavaDoc
63   {
64     try {
65       return (Connection JavaDoc) _connManager.allocateConnection(_managedFactory,
66                               null);
67     } catch (ResourceException JavaDoc e) {
68       Throwable JavaDoc cause;
69
70       for (cause = e; cause != null; cause = cause.getCause()) {
71     if (cause instanceof SQLException JavaDoc)
72       throw (SQLException JavaDoc) cause;
73       }
74
75       throw new SQLExceptionWrapper(e);
76     }
77   }
78
79   /**
80    * Returns a connection.
81    */

82   public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password)
83     throws SQLException JavaDoc
84   {
85     try {
86       Credential credential = null;
87
88       if (username != null && password != null)
89     credential = new Credential(username, password);
90
91       return (Connection JavaDoc) _connManager.allocateConnection(_managedFactory,
92                                credential);
93     } catch (ResourceException JavaDoc e) {
94       Throwable JavaDoc cause;
95
96       for (cause = e; cause != null; cause = cause.getCause()) {
97     if (cause instanceof SQLException JavaDoc)
98       throw (SQLException JavaDoc) cause;
99       }
100
101       throw new SQLExceptionWrapper(e);
102     }
103   }
104
105   /**
106    * Returns the login timeout.
107    */

108   public int getLoginTimeout()
109   {
110     return 0;
111   }
112
113   /**
114    * Returns the login timeout.
115    */

116   public void setLoginTimeout(int seconds)
117   {
118   }
119
120   /**
121    * Returns the log writer.
122    */

123   public PrintWriter JavaDoc getLogWriter()
124   {
125     return null;
126   }
127
128   /**
129    * Sets the log writer.
130    */

131   public void setLogWriter(PrintWriter JavaDoc out)
132   {
133   }
134
135   /**
136    * Returns true if the impl has closed.
137    */

138   boolean isClosed()
139   {
140     return false;
141   }
142 }
143
144
Popular Tags