KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.jorm.api.PException;
27 import org.objectweb.jorm.api.PExceptionIO;
28 import org.objectweb.jorm.api.PExceptionProtocol;
29 import org.objectweb.perseus.persistence.api.ConnectionHolder;
30
31 import java.sql.Connection JavaDoc;
32
33 /**
34  * Defines a connection wrapper for all kind of connection that can be used
35  * with the RDB mapper.
36  * @author P. Dechamboux
37  */

38 public class RdbConnectionWrapper {
39     /**
40      * Narrows a connection requested to a mapper into an SQL connection.
41      * @param conn A mapper connection.
42      * @return An SQL connection.
43      * @throws PException Thrown when narrowing is impossible.
44      */

45     public static Connection JavaDoc narrow2SQL(Object JavaDoc conn) throws PException {
46         if (conn == null) {
47             throw new PExceptionProtocol("Impossible to narrow a null connection");
48         }
49         if (conn instanceof Connection JavaDoc)
50             return (Connection JavaDoc) conn;
51         if (conn instanceof ConnectionHolder) {
52             try {
53                 return (Connection JavaDoc) ((org.objectweb.perseus.persistence.api.ConnectionHolder) conn).getCHConnectionForWrite();
54             } catch (org.objectweb.perseus.persistence.api.PersistenceException e) {
55                 throw new PExceptionIO(e, "Impossible to fetch a connection to access to the RDB.");
56             }
57         }
58         throw new PExceptionProtocol("Unknow type of connection to be narrowed to a SQL connection.");
59     }
60 }
61
Popular Tags