KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > lib > MapperJDBC


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
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 of the License, or (at your option) 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 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.mapper.rdb.lib;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import javax.sql.DataSource JavaDoc;
30
31 import org.objectweb.jorm.api.JormConfigurator;
32 import org.objectweb.jorm.api.PException;
33 import org.objectweb.jorm.api.PExceptionProtocol;
34 import org.objectweb.jorm.lib.Mapper;
35 import org.objectweb.jorm.mapper.rdb.adapter.RdbAdapterFactory;
36 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter;
37
38 /**
39  * This mapper supports JDBC connection management.
40  * @author P. Dechamboux
41  */

42 public class MapperJDBC extends Mapper implements PMapperRdb {
43
44     private DataSource JavaDoc source = null;
45
46     private RdbAdapter adapter;
47
48     public MapperJDBC() throws PException {
49         super();
50     }
51
52     public MapperJDBC(JormConfigurator jc) {
53         super(jc);
54     }
55
56     /**
57      * Closes the given connection which has been allocated previously using
58      * getConnection.
59      * @param conn The concerned JDBC connection.
60      */

61     public void closeConnection(Object JavaDoc conn) throws PException {
62         try {
63             ((Connection JavaDoc) conn).close();
64         } catch (SQLException JavaDoc se) {
65             throw new PException(se, "Problem while closing connection.");
66         }
67     }
68
69     /**
70      * Allocates a connection for accessing the underlying relational DB.
71      */

72     public Object JavaDoc getConnection() throws PException {
73         if (source == null) {
74             throw new PException("Cannot get an SQL connection: no datasource specified.");
75         }
76         try {
77             return source.getConnection();
78         } catch (SQLException JavaDoc se) {
79             throw new PException(se, "Problem while allocating connection.");
80         }
81     }
82
83     /**
84      * Allocates a connection for accessing the underlying relational DB.
85      * @param ctxt The connection specification for the connection to be allocated.
86      */

87     public Object JavaDoc getConnection(Object JavaDoc ctxt) throws PException {
88         if (!(ctxt instanceof ConnectionSpecJDBC))
89             throw new PException("Requires a JDBC connection specification.");
90         ConnectionSpecJDBC cs = (ConnectionSpecJDBC) ctxt;
91         try {
92             if (cs.user != null && cs.passwd != null) {
93                 return source.getConnection(cs.user, cs.passwd);
94             }
95             return source.getConnection();
96         } catch (SQLException JavaDoc se) {
97             throw new PException(se,
98                                  "Problem while allocating connection with SPEC.");
99         }
100     }
101
102     public Object JavaDoc getConnection(Object JavaDoc connectionContext, Object JavaDoc user) throws PException {
103         return getConnection(connectionContext);
104     }
105
106     /**
107      * Assigns the information about the JDBC connection to be allocated
108      * through this mapper.
109      * @exception PExceptionProtocol It is raised if a connection factory
110      * has already been assigned.
111      */

112     public void setConnectionFactory(Object JavaDoc cf) throws PException {
113         if (cf == null)
114             return;
115         if (cf instanceof DataSource JavaDoc) {
116             source = (DataSource JavaDoc) cf;
117             return;
118         }
119         throw new PException("Requires a JDBC connection specification.");
120     }
121
122     /**
123      * Returns the connection factory associated to this mapper.
124      */

125     public Object JavaDoc getConnectionFactory() {
126         return source;
127     }
128
129     public void start() throws PException {
130         String JavaDoc mn = getMapperName();
131         //calculate the adapter from the mapper name
132
if (mn == null || "rdb.automatic".equalsIgnoreCase(mn)) {
133             // No mapper name specified ==> try to find the adapter through the
134
// DataSource (database meta data)
135
try {
136                 adapter = RdbAdapterFactory.getTypeConverter(source);
137             } catch (org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapterException e) {
138                 throw new PException(e, "Impossible to find a RdbAdapter in automatic mode");
139             }
140             //assign the mapper name with the adapter name
141
setMapperName("rdb." + adapter.getName());
142         } else {
143             int idx = mn.indexOf('.');
144             if (idx == -1) {
145                 mn = RdbAdapterFactory.DATABASE_NAME_JDBC;
146             } else {
147                 mn = mn.substring(idx + 1);
148             }
149             try {
150                 adapter = RdbAdapterFactory.getTypeConverter(mn);
151             } catch (org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapterException e) {
152                 throw new PException(e, "" +
153                     "Impossible to find a RdbAdapter for the specified mapper name: "
154                     + getMapperName());
155             }
156         }
157         adapter.setLogger(logger);
158         super.start();
159     }
160     
161     // IMPLEMENTATIOIN OF THE PMapperRdb INTERFACE //
162
//---------------------------------------------//
163

164     public RdbAdapter getRdbAdapter() {
165         return adapter;
166     }
167 }
168
Popular Tags