KickJava   Java API By Example, From Geeks To Geeks.

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


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.exception.JcaException;
27 import org.objectweb.jalisto.se.impl.trace.Trace;
28 import org.objectweb.jalisto.se.impl.trace.TraceNull;
29 import org.objectweb.jalisto.se.api.Session;
30 import org.objectweb.jalisto.se.jca.data.JalistoManagedConnectionMetaData;
31 import org.objectweb.jalisto.se.jca.session.ManagedSession;
32
33 import javax.resource.ResourceException JavaDoc;
34 import javax.resource.spi.*;
35 import javax.security.auth.Subject JavaDoc;
36 import javax.transaction.xa.XAException JavaDoc;
37 import javax.transaction.xa.XAResource JavaDoc;
38 import java.io.PrintWriter JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.List JavaDoc;
41
42 public class JalistoManagedConnection implements ManagedConnection {
43
44     public JalistoManagedConnection(String JavaDoc connectionURL, ConnectionRequestInfo info, Trace trace) {
45         this.connectionURL = connectionURL;
46         this.trace = trace;
47         this.info = info;
48         metadata = new JalistoManagedConnectionMetaData();
49         try {
50             getXAResource();
51         } catch (ResourceException JavaDoc e) {
52             throw new JcaException("Can't initialize the JalistoManagedConnection", e);
53         }
54     }
55
56     public void setManagedJalistoSession(ManagedSession managedJalistoSession) {
57         this.managedJalistoSession = managedJalistoSession;
58     }
59
60     public boolean isAvalaible() {
61         return (isAvalaible && (managedJalistoSession == null) && jdoxarWrapper.isAvailable());
62     }
63
64     public void setNotAvalaible() {
65         isAvalaible = false;
66     }
67
68     public void setCfIdentification(String JavaDoc id) {
69         cfIdentification = id;
70     }
71
72     public String JavaDoc getCfIdentification() {
73         return cfIdentification;
74     }
75
76     public String JavaDoc getConnectionURL() {
77         return connectionURL;
78     }
79
80
81     public void close(JalistoConnection c) throws XAException JavaDoc {
82         trace("close(c)");
83         if (!c.equals(activeJalistoConnection)) {
84             throw new XAException JavaDoc("Cannot close a not active connection...");
85         }
86         for (int i = 0; i < eventListeners.size(); i++) {
87             ConnectionEventListener cel = (ConnectionEventListener) eventListeners.get(i);
88             ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED);
89             ce.setConnectionHandle(c);
90             cel.connectionClosed(ce);
91         }
92     }
93
94     public Session getSession() {
95         return managedJalistoSession;
96     }
97
98     public boolean matchConnectionRequestInfo(ConnectionRequestInfo requestInfo) {
99         if (info == null) {
100             return (requestInfo == null);
101         }
102         // info != null
103
return info.equals(requestInfo);
104     }
105
106     private void trace(Object JavaDoc o) {
107         String JavaDoc message = TRACE_ID + " " + String.valueOf(o);
108         trace.println(Trace.JCA, message);
109     }
110
111
112     public boolean equals(Object JavaDoc o) {
113         boolean result = false;
114         if (o instanceof JalistoManagedConnection) {
115             JalistoManagedConnection candidat = (JalistoManagedConnection) o;
116             result = (this.hashCode() == candidat.hashCode());
117         }
118         return result;
119     }
120
121     public int hashCode() {
122         if (hashCode == 0) {
123             hashCode = super.hashCode();
124         }
125         return hashCode;
126     }
127
128
129     /***************************** ManagedConnection implementation ****************************/
130
131     // pas de connection sharing pour l'instant (p40 et 71 de la spec)
132
public Object JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo info) throws ResourceException JavaDoc {
133         trace("getConnection");
134         if (activeJalistoConnection == null) {
135             activeJalistoConnection = new JalistoConnection(this, trace);
136         } else {
137             if (activeJalistoConnection.isClosed()) {
138                 activeJalistoConnection.open(this);
139             }
140             //throw new JDOUserException("not the first call to getConnection, but don't support connection sharing");
141
}
142         return activeJalistoConnection;
143     }
144
145     public void associateConnection(Object JavaDoc c) throws ResourceException JavaDoc {
146         trace("associateConnection " + c.getClass().toString());
147
148         if (c == null) {
149             throw new ResourceException JavaDoc("Cannot associate a null connection");
150         }
151
152         if (activeJalistoConnection == null) {
153             activeJalistoConnection = (JalistoConnection) c;
154             return;
155         }
156
157         if (activeJalistoConnection.equals(c)) {
158             return;
159         }
160
161         if (c instanceof JalistoConnection) {
162             JalistoConnection jdoJalistoConnection = (JalistoConnection) c;
163             jdoJalistoConnection.setMc(this);
164             activeJalistoConnection.invalidate();
165             activeJalistoConnection = jdoJalistoConnection;
166         } else {
167             throw new ResourceException JavaDoc("The managedConnection cannot manage this connection : not a JDOConnection");
168         }
169     }
170
171     public XAResource JavaDoc getXAResource() throws ResourceException JavaDoc {
172         trace("getXAResource");
173         if (jdoxarWrapper == null) {
174             trace("getXAResource : new ressource");
175             jdoxarWrapper = new JalistoXAResourceWrapper(this, trace);
176         }
177         return jdoxarWrapper;
178     }
179
180     public void addConnectionEventListener(ConnectionEventListener listener) {
181         trace("addConnectionEventListener");
182         eventListeners.add(listener);
183     }
184
185     public void removeConnectionEventListener(ConnectionEventListener listener) {
186         trace("removeConnectionEventListener");
187         eventListeners.remove(listener);
188     }
189
190     public void cleanup() throws ResourceException JavaDoc {
191         trace("cleanup");
192         if (activeJalistoConnection != null) {
193             activeJalistoConnection.invalidate();
194             activeJalistoConnection = null;
195         }
196         jdoxarWrapper.cleanUp();
197         managedJalistoSession = null;
198         isAvalaible = true;
199     }
200
201     public void destroy() throws ResourceException JavaDoc {
202         trace("destroy");
203         jdoxarWrapper = null;
204         isAvalaible = false;
205         eventListeners.clear();
206         logger = null;
207     }
208
209     // must be spi.LocalTransaction
210
public LocalTransaction getLocalTransaction() throws ResourceException JavaDoc {
211         throw new RuntimeException JavaDoc("not implemented yet...");
212     }
213
214     public ManagedConnectionMetaData getMetaData() throws ResourceException JavaDoc {
215         trace("getMetaData : " + metadata.toString());
216         return metadata;
217     }
218
219     public void setLogWriter(PrintWriter JavaDoc writer) throws ResourceException JavaDoc {
220         trace("setLogWriter");
221         logger = writer;
222     }
223
224     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
225         trace("getLogWriter");
226         return logger;
227     }
228
229
230     private ManagedSession managedJalistoSession;
231     private JalistoConnection activeJalistoConnection = null;
232     private JalistoXAResourceWrapper jdoxarWrapper;
233
234     private String JavaDoc connectionURL;
235     private String JavaDoc cfIdentification;
236     private ConnectionRequestInfo info;
237     private JalistoManagedConnectionMetaData metadata;
238
239     private List JavaDoc eventListeners = new ArrayList JavaDoc();
240
241     private PrintWriter JavaDoc logger;
242     private Trace trace = new TraceNull();
243
244
245     private boolean isAvalaible = true;
246     private int hashCode = 0;
247
248     private static final String JavaDoc TRACE_ID = "[JalistoManagedConnection]";
249 }
250
Popular Tags