KickJava   Java API By Example, From Geeks To Geeks.

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


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: DataSourceMCFImpl.java,v 1.5 2005/04/28 08:43:24 benoitf 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.DataSource JavaDoc;
38
39 import org.objectweb.util.monolog.api.BasicLevel;
40
41 public class DataSourceMCFImpl
42         extends ManagedConnectionFactoryImpl {
43
44     DataSource JavaDoc ds = null;
45
46     public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject,
47                                                       ConnectionRequestInfo JavaDoc cxReq)
48               throws ResourceException JavaDoc {
49
50       PasswordCredential JavaDoc pc = Utility.getPasswordCredential(this, subject, cxReq, pw);
51       if(ds == null) {
52           try {
53               ds = (DataSource JavaDoc) Utility.getDataSource(this, pc, trace);
54           } catch(Exception JavaDoc ex) {
55               throw new ResourceException JavaDoc(ex.getMessage());
56           }
57       }
58       java.sql.Connection JavaDoc connection = null;
59       try {
60           if(cxReq != null) {
61               ConnectionRequestInfoImpl cx = (ConnectionRequestInfoImpl) cxReq;
62               connection = ds.getConnection(cx.getUser(), cx.getPassword());
63           } else if (pc != null){
64               connection = ds.getConnection(pc.getUserName(), new String JavaDoc(pc.getPassword()));
65           } else if (mcfData.getMCFData(MCFData.USER).length() > 0){
66               connection = ds.getConnection(mcfData.getMCFData(MCFData.USER),
67                                             mcfData.getMCFData(MCFData.PASSWORD));
68           } else {
69               connection = ds.getConnection();
70           }
71           if (trace.isLoggable(BasicLevel.DEBUG)) {
72               trace.log(BasicLevel.DEBUG, "Connection object returned is "+connection);
73           }
74       }
75       catch(SQLException JavaDoc sqle)
76       {
77           throw new ResourceAllocationException JavaDoc("The connection could not be allocated: " + sqle.getMessage());
78       }
79       return new ManagedConnectionImpl(this, pc, connection, null, null, null);
80     }
81
82     /* Determine if the factories are equal
83      */

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