KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > lib > MapperJCA


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.lib;
25
26 import org.objectweb.jorm.api.PException;
27 import org.objectweb.jorm.api.PExceptionProtocol;
28 import org.objectweb.jorm.api.JormConfigurator;
29
30 import javax.resource.cci.ConnectionFactory JavaDoc;
31 import javax.resource.cci.Connection JavaDoc;
32 import javax.resource.cci.ConnectionSpec JavaDoc;
33 import javax.resource.ResourceException JavaDoc;
34
35 /**
36  * This mapper supports JCA CCI connection management.
37  * @author P. Dechamboux
38  */

39 public class MapperJCA extends Mapper {
40     private ConnectionFactory JavaDoc connectionFactory;
41
42     public MapperJCA() throws PException {
43         super();
44     }
45
46     public MapperJCA(JormConfigurator jc) {
47         super(jc);
48     }
49
50     /**
51      * Closes the given connection which has been allocated previously using
52      * getConnection.
53      * @param conn The concerned connection.
54      */

55     public void closeConnection(Object JavaDoc conn) throws PException {
56         try {
57             ((Connection JavaDoc) conn).close();
58         } catch (ResourceException JavaDoc re) {
59             throw new PException(re, "Problem while closing connection.");
60         }
61     }
62
63     /**
64      * Allocates a connection for accessing the underlying DS.
65      */

66     public Object JavaDoc getConnection() throws PException {
67         try {
68             return connectionFactory.getConnection();
69         } catch (ResourceException JavaDoc re) {
70             throw new PException(re, "Problem while allocating connection.");
71         }
72     }
73
74     /**
75      * Allocates a connection for accessing the underlying DS knowing additional
76      * context information.
77      * @param ctxt Context information used to allocate the connection.
78      */

79     public Object JavaDoc getConnection(Object JavaDoc ctxt) throws PException {
80         try {
81             return connectionFactory.getConnection((ConnectionSpec JavaDoc) ctxt);
82         } catch (ResourceException JavaDoc re) {
83             throw new PException(re, "Problem while allocating connection with SPEC.");
84         }
85     }
86
87     public Object JavaDoc getConnection(Object JavaDoc connectionContext, Object JavaDoc user) throws PException {
88         try {
89             return connectionFactory.getConnection((ConnectionSpec JavaDoc) connectionContext);
90         } catch (ResourceException JavaDoc re) {
91             throw new PException(re, "Problem while allocating connection with SPEC.");
92         }
93     }
94
95     /**
96      * Assigns a connection factory to this mapper.
97      * @exception PExceptionProtocol It is raised if a connection factory
98      * has already been assigned.
99      */

100     public void setConnectionFactory(Object JavaDoc cf) throws PException {
101         if (!(cf instanceof ConnectionFactory JavaDoc))
102             throw new PException("Requires a JCA connection factory.");
103         if (connectionFactory != null)
104             throw new PExceptionProtocol("Connection factory assigned once.");
105         connectionFactory = (ConnectionFactory JavaDoc) cf;
106     }
107
108     /**
109      * Returns the connection factory associated to this mapper.
110      */

111     public Object JavaDoc getConnectionFactory() {
112         return connectionFactory;
113     }
114 }
115
Popular Tags