KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > jca > JalistoConnection


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.jca;
25
26 import org.objectweb.jalisto.se.api.*;
27 import org.objectweb.jalisto.se.api.query.QueryManager;
28 import org.objectweb.jalisto.se.api.internal.SessionInternal;
29 import org.objectweb.jalisto.se.exception.JcaException;
30 import org.objectweb.jalisto.se.impl.trace.Trace;
31 import org.objectweb.jalisto.se.impl.util.JalistoUtils;
32
33 import javax.resource.ResourceException JavaDoc;
34 import javax.resource.cci.*;
35 import javax.resource.spi.ManagedConnection JavaDoc;
36 import javax.transaction.xa.XAException JavaDoc;
37 import java.util.Collection JavaDoc;
38
39 public class JalistoConnection implements javax.resource.cci.Connection JavaDoc, Session {
40
41     public JalistoConnection(JalistoManagedConnection jalistoMc, Trace trace) {
42         this.trace = trace;
43         this.jalistoMc = jalistoMc;
44     }
45
46     public void setMc(ManagedConnection JavaDoc mc) {
47         if (mc instanceof JalistoManagedConnection) {
48             this.jalistoMc = (JalistoManagedConnection) mc;
49         } else {
50             throw new JcaException("Cannot manage connection of this class : " + mc.getClass().toString());
51         }
52     }
53
54     public void open(JalistoManagedConnection jalistoMc) throws ResourceException JavaDoc {
55         this.jalistoMc = jalistoMc;
56         isValide = true;
57         isClosed = false;
58     }
59
60     public void invalidate() throws ResourceException JavaDoc {
61         jalistoMc = null;
62         isValide = false;
63     }
64
65     public boolean isClosed() {
66         return isClosed;
67     }
68
69     private final Session getSession() {
70         try {
71             checkClosedOrNotValid();
72         } catch (ResourceException JavaDoc e) {
73             new JcaException(CLOSED_EXCEPTION_MSG);
74         }
75         Session result = jalistoMc.getSession();
76         if (result == null) {
77             throw new JcaException("PM null in connection... You may be outside of a transaction");
78         }
79         return result;
80     }
81
82     private void checkClosedOrNotValid() throws ResourceException JavaDoc {
83         if ((isClosed || (!isValide))) {
84             throw new ResourceException JavaDoc(CLOSED_EXCEPTION_MSG);
85         }
86     }
87
88     private static String JavaDoc addressOf(Object JavaDoc o) {
89         String JavaDoc s = String.valueOf(o);
90         int index = s.indexOf('@');
91         if (index == -1)
92             return s;
93         return JalistoUtils.substring(s, index);
94     }
95
96
97     public String JavaDoc toString() {
98         return "JalistoConnection" + addressOf(super.toString()) +
99                " ManagedConnection" + addressOf(jalistoMc) + " ManagedPM" +
100                addressOf(jalistoMc == null ? null : jalistoMc.getSession());
101     }
102
103     /*************************** Connection implementation **************************************/
104
105     public LocalTransaction getLocalTransaction() throws ResourceException JavaDoc {
106         checkClosedOrNotValid();
107         return (javax.resource.cci.LocalTransaction JavaDoc) jalistoMc.getLocalTransaction();
108     }
109
110     public synchronized void close() {
111         trace.println(Trace.JCA, TRACE_ID + " closing connection");
112         try {
113             checkClosedOrNotValid();
114         } catch (ResourceException JavaDoc e) {
115             new JcaException(CLOSED_EXCEPTION_MSG);
116         }
117         try {
118             jalistoMc.close(this);
119         } catch (XAException JavaDoc e) {
120             e.printStackTrace();
121         }
122         isClosed = true;
123         trace.println(Trace.JCA, TRACE_ID + " connection closed...");
124     }
125
126     public Interaction createInteraction() throws ResourceException JavaDoc {
127         checkClosedOrNotValid();
128         return null;
129     }
130
131     public ConnectionMetaData getMetaData() throws ResourceException JavaDoc {
132         checkClosedOrNotValid();
133         return null;
134     }
135
136     public ResultSetInfo getResultSetInfo() throws ResourceException JavaDoc {
137         checkClosedOrNotValid();
138         return null;
139     }
140
141     public void setAutoCommit(boolean b) throws ResourceException JavaDoc {
142         checkClosedOrNotValid();
143     }
144
145     public boolean getAutoCommit() throws ResourceException JavaDoc {
146         checkClosedOrNotValid();
147         return false;
148     }
149
150     /*************************** Session Implementation ***************************************/
151
152     public void closeSession() {
153         getSession().closeSession();
154     }
155
156     public void openSession() {
157         getSession().openSession();
158     }
159
160     public boolean isOpen() {
161         return getSession().isOpen();
162     }
163
164     public boolean contains(Object JavaDoc oid) {
165         return getSession().contains(oid);
166     }
167
168     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, Class JavaDoc objectClass) {
169         return getSession().createObject(objectToCreate, objectClass);
170     }
171
172     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, String JavaDoc objectClassName) {
173         return getSession().createObject(objectToCreate, objectClassName);
174     }
175
176     public Object JavaDoc createObject(Object JavaDoc oid, Object JavaDoc[] objectToCreate) {
177         return getSession().createObject(oid, objectToCreate);
178     }
179
180     public Transaction currentTransaction() {
181         return getSession().currentTransaction();
182     }
183
184     public void defineClass(ClassDescription classDescription) {
185         getSession().defineClass(classDescription);
186     }
187
188     public void deleteObjectByOid(Object JavaDoc oid) {
189         getSession().deleteObjectByOid(oid);
190     }
191
192     public String JavaDoc getClassNameFor(Object JavaDoc oid) {
193         return null;
194     }
195
196     public Extent getExtent(String JavaDoc fullCLassName) {
197         return getSession().getExtent(fullCLassName);
198     }
199
200     public Extent getExtent(Class JavaDoc theClass) {
201         return getSession().getExtent(theClass);
202     }
203
204     public SessionInternal getInternalSession() {
205         return getSession().getInternalSession();
206     }
207
208     public QueryManager getQueryManager() {
209         return getSession().getQueryManager();
210     }
211
212     public MetaRepository getMetaRepository() {
213         return getSession().getMetaRepository();
214     }
215
216     public boolean isClassDefined(String JavaDoc fullQualifiedClassName) {
217         return getSession().isClassDefined(fullQualifiedClassName);
218     }
219
220     public boolean isNewBase() {
221         return getSession().isNewBase();
222     }
223
224     public Object JavaDoc makeNewFileOid(Class JavaDoc objectClass) {
225         return getSession().makeNewFileOid(objectClass);
226     }
227
228     public Object JavaDoc makeNewFileOid(String JavaDoc objectClassName) {
229         return getSession().makeNewFileOid(objectClassName);
230     }
231
232     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid) {
233         return getSession().readObjectByOid(oid);
234     }
235
236     public void removeClass(String JavaDoc fullClassName) {
237         getSession().removeClass(fullClassName);
238     }
239
240     public void reorganize() {
241         getSession().reorganize();
242     }
243
244     public Object JavaDoc updateObjectByOid(Object JavaDoc oid, Object JavaDoc[] objectToUpdate) {
245         return getSession().updateObjectByOid(oid, objectToUpdate);
246     }
247
248     public Collection JavaDoc readObjectsByOids(Collection JavaDoc oids) {
249         return getSession().readObjectsByOids(oids);
250     }
251
252     public Object JavaDoc[] refreshObjectByOid(Object JavaDoc oid) {
253         return getSession().refreshObjectByOid(oid);
254     }
255
256     public void eraseStorage() {
257         getSession().eraseStorage();
258     }
259
260
261     private static final String JavaDoc TRACE_ID = "[JalistoConnection]";
262     private static final String JavaDoc CLOSED_EXCEPTION_MSG = "Connection already closed or invalidated...";
263     private JalistoManagedConnection jalistoMc;
264     private Trace trace;
265     private boolean isClosed = false;
266     private boolean isValide = true;
267 }
268
Popular Tags