KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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  * Initial developer(s): Eric HARDESTY
22  * --------------------------------------------------------------------------
23  * $Id: ConnectionPoolMCFImpl.java,v 1.5 2004/12/14 16:13:51 ehardesty Exp $
24  * --------------------------------------------------------------------------
25  */

26 package org.objectweb.jonas.jdbc;
27
28
29 import java.sql.SQLException JavaDoc;
30
31 import javax.resource.ResourceException JavaDoc;
32 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
33 import javax.resource.spi.ManagedConnection JavaDoc;
34 import javax.resource.spi.ResourceAllocationException JavaDoc;
35 import javax.resource.spi.security.PasswordCredential JavaDoc;
36 import javax.security.auth.Subject JavaDoc;
37 import javax.sql.ConnectionPoolDataSource JavaDoc;
38
39 /**
40  * JDBC RA connection pool MCF implmentation
41  * @author Eric Hardesty
42  * Contributor(s):
43  *
44  */

45 public class ConnectionPoolMCFImpl
46         extends ManagedConnectionFactoryImpl {
47
48     
49     ConnectionPoolDataSource JavaDoc ds = null;
50
51     public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject,
52                                                       ConnectionRequestInfo JavaDoc cxReq)
53               throws ResourceException JavaDoc {
54                   
55       PasswordCredential JavaDoc pc = Utility.getPasswordCredential(this, subject, cxReq, pw);
56       if(ds == null) {
57           try {
58               ds = (ConnectionPoolDataSource JavaDoc) Utility.getDataSource(this, pc, trace);
59           } catch(Exception JavaDoc ex) {
60               throw new ResourceException JavaDoc(ex.getMessage());
61           }
62       }
63       
64       javax.sql.PooledConnection JavaDoc pConnection = null;
65       java.sql.Connection JavaDoc connection = null;
66       try {
67           if(cxReq != null) {
68               ConnectionRequestInfoImpl cx = (ConnectionRequestInfoImpl) cxReq;
69               pConnection = ds.getPooledConnection(cx.getUser(), cx.getPassword());
70           } else if (pc != null){
71               pConnection = ds.getPooledConnection(pc.getUserName(), new String JavaDoc(pc.getPassword()));
72           } else if (mcfData.getMCFData(MCFData.USER).length() > 0){
73               pConnection = ds.getPooledConnection(mcfData.getMCFData(MCFData.USER),
74                                                    mcfData.getMCFData(MCFData.PASSWORD));
75           } else {
76               pConnection = ds.getPooledConnection();
77           }
78           if (pConnection != null) {
79               connection = pConnection.getConnection();
80           }
81       }
82       catch(SQLException JavaDoc sqle)
83       {
84           throw new ResourceAllocationException JavaDoc("The connection could not be allocated: " + sqle.getMessage());
85       }
86       return new ManagedConnectionImpl(this, pc, connection, pConnection, null, null);
87     }
88
89     /* Determine if the factories are equal
90      */

91     public boolean equals(Object JavaDoc obj) {
92         if (obj instanceof ConnectionPoolMCFImpl) {
93             return mcfData.equals(((ConnectionPoolMCFImpl)obj).mcfData);
94         }
95         else {
96             return false;
97         }
98     }
99
100     // JOnAS JDBC RA ConnectionPool config properties
101
public String JavaDoc getDatabaseName()
102     {
103         return mcfData.getMCFData(MCFData.DATABASENAME);
104     }
105
106     public void setDatabaseName(String JavaDoc val)
107     {
108         mcfData.setMCFData(MCFData.DATABASENAME, val);
109     }
110
111     public String JavaDoc getDescription()
112     {
113         return mcfData.getMCFData(MCFData.DESCRIPTION);
114     }
115
116     public void setDescription(String JavaDoc val)
117     {
118         mcfData.setMCFData(MCFData.DESCRIPTION, val);
119     }
120
121     public String JavaDoc getPortNumber()
122     {
123         return mcfData.getMCFData(MCFData.PORTNUMBER);
124     }
125
126     public void setPortNumber(String JavaDoc val)
127     {
128         mcfData.setMCFData(MCFData.PORTNUMBER, val);
129     }
130
131     public String JavaDoc getServerName()
132     {
133         return mcfData.getMCFData(MCFData.SERVERNAME);
134     }
135
136     public void setServerName(String JavaDoc val)
137     {
138         mcfData.setMCFData(MCFData.SERVERNAME, val);
139     }
140
141 }
Popular Tags