KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.Session;
29 import org.objectweb.jalisto.se.jca.data.JalistoConnectionRequestInfo;
30
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.Reference JavaDoc;
33 import javax.naming.StringRefAddr JavaDoc;
34 import javax.resource.NotSupportedException JavaDoc;
35 import javax.resource.ResourceException JavaDoc;
36 import javax.resource.cci.*;
37 import javax.resource.spi.ConnectionManager JavaDoc;
38 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
39 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
40 import java.io.ObjectStreamException JavaDoc;
41
42 public class JalistoConnectionFactory implements ResourceAdapterMetaData, ConnectionFactory JavaDoc {
43
44     public JalistoConnectionFactory(ConnectionManager JavaDoc connectionManager, JalistoManagedConnectionFactory mcFactory)
45             throws Exception JavaDoc {
46         super();
47         this.trace = mcFactory.getTrace();
48         trace.println(Trace.JCA, TRACE_ID + " constructor");
49         this.connectionManager = connectionManager;
50         this.mcf = mcFactory;
51     }
52
53     public JalistoConnectionFactory(ManagedConnectionFactory JavaDoc mcf, ConnectionManager JavaDoc cm) throws Exception JavaDoc {
54         this(cm, (JalistoManagedConnectionFactory) mcf);
55     }
56
57
58     public Session getSession(String JavaDoc userid, String JavaDoc password) {
59         synchronized (this) {
60             Session result;
61             try {
62                 JalistoConnectionSpec spec = new JalistoConnectionSpec();
63                 spec.setUserName(userid);
64                 spec.setPassword(password);
65                 result = (Session) getConnection(spec);
66             } catch (ResourceException JavaDoc re) {
67                 throw new JcaException(PMGET_EXCEPTION_MSG, re);
68             }
69             return result;
70         }
71     }
72
73     public synchronized Session getSession() {
74         synchronized (this) {
75             Session result;
76             try {
77                 result = (Session) getConnection();
78             } catch (ResourceException JavaDoc re) {
79                 throw new JcaException(PMGET_EXCEPTION_MSG, re);
80             }
81             return result;
82         }
83     }
84
85
86     private Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc {
87         trace.println(Trace.JCA, TRACE_ID + " writeReplace");
88         JalistoCfSerializationWrapper wrapper = new JalistoCfSerializationWrapper();
89         try {
90             wrapper.setReference(this.getReference());
91             wrapper.setManagedConnectionFactory(mcf);
92             wrapper.setConnectionManager(connectionManager);
93         } catch (NamingException JavaDoc e) {
94             throw new JcaException("Cannot save reference before serialization", e);
95         }
96         return wrapper;
97     }
98
99
100     /**************************** properties methods *****************************************/
101
102     public void setConnectionDriverName(String JavaDoc connectionDriverName) {
103         this.connectionDriverName = connectionDriverName;
104     }
105
106     public void setConnectionPassword(String JavaDoc connectionPassword) {
107         this.connectionPassword = connectionPassword;
108     }
109
110     public void setConnectionURL(String JavaDoc connectionURL) {
111         this.connectionURL = connectionURL;
112     }
113
114     public void setConnectionUserName(String JavaDoc connectionUserName) {
115         this.connectionUserName = connectionUserName;
116     }
117
118     public String JavaDoc getConnectionDriverName() {
119         return connectionDriverName;
120     }
121
122     public String JavaDoc getConnectionPassword() {
123         return connectionPassword;
124     }
125
126     public String JavaDoc getConnectionURL() {
127         return connectionURL;
128     }
129
130     public String JavaDoc getConnectionUserName() {
131         return connectionUserName;
132     }
133
134     /***************************** ConnectionFactory implementation ***************************/
135
136     public javax.resource.cci.Connection JavaDoc getConnection() throws ResourceException JavaDoc {
137         synchronized (this) {
138             return getConnection(null);
139         }
140     }
141
142     public javax.resource.cci.Connection JavaDoc getConnection(ConnectionSpec spec) throws ResourceException JavaDoc {
143         synchronized (this) {
144             trace.println(Trace.JCA, TRACE_ID + " getConnection({0})", spec);
145             ConnectionRequestInfo infos = new JalistoConnectionRequestInfo(spec);
146             return (javax.resource.cci.Connection JavaDoc) connectionManager.allocateConnection(mcf, infos);
147         }
148     }
149
150     public RecordFactory getRecordFactory() throws ResourceException JavaDoc {
151         synchronized (this) {
152             try {
153                 throw new NotSupportedException JavaDoc("getRecordFactory()");
154             } catch (Exception JavaDoc e) {
155                 e.printStackTrace();
156             }
157             return null;
158         }
159     }
160
161     public ResourceAdapterMetaData getMetaData() throws ResourceException JavaDoc {
162         synchronized (this) {
163             trace.println(Trace.JCA, TRACE_ID + " getMetaData()");
164             return this;
165         }
166     }
167
168
169     /***************************** ResourceAdapterMetaData implementation ***************************/
170
171     public String JavaDoc getAdapterName() {
172         synchronized (this) {
173             return null;
174         }
175     }
176
177     public String JavaDoc getAdapterShortDescription() {
178         synchronized (this) {
179             return RESDESC;
180         }
181     }
182
183     public String JavaDoc getAdapterVendorName() {
184         synchronized (this) {
185             return null;
186         }
187     }
188
189     public String JavaDoc getAdapterVersion() {
190         synchronized (this) {
191             return null;
192         }
193     }
194
195     public String JavaDoc getSpecVersion() {
196         return null;
197     }
198
199     public boolean supportsExecuteWithInputAndOutputRecord() {
200         return false;
201     }
202
203     public boolean supportsExecuteWithInputRecordOnly() {
204         return false;
205     }
206
207     public boolean supportsLocalTransactionDemarcation() {
208         return false;
209     }
210
211     public String JavaDoc[] getInteractionSpecsSupported() {
212         synchronized (this) {
213             return null;
214         }
215     }
216
217     /****************************** Referenceable implementation ************************************/
218
219     public void setReference(Reference JavaDoc reference) {
220         synchronized (this) {
221             trace.println(Trace.JCA, TRACE_ID + " set new reference = {0}", reference);
222         }
223     }
224
225     public Reference JavaDoc getReference() throws NamingException JavaDoc {
226         synchronized (this) {
227             JalistoObjectFactory.setTrace(trace);
228             trace.println(Trace.JCA, TRACE_ID + " get reference");
229
230             Reference JavaDoc ref = new Reference JavaDoc(this.getClass().getName(), DRIVER_NAMING_FACTORY_NAME, null);
231
232             // These values are used by ObjectFactory
233
ref.add(new StringRefAddr JavaDoc(this.NAMINGPROPNAME, this.RESNAME));
234             ref.add(new StringRefAddr JavaDoc(JalistoObjectFactory.JALISTOID, this.toString()));
235             JalistoObjectFactory.addConnectionFactory(this.toString(), this);
236             trace.println(Trace.JCA, TRACE_ID + " the reference is \n{0}", ref);
237
238             return ref;
239         }
240     }
241
242
243     private ConnectionManager JavaDoc connectionManager = null;
244     private JalistoManagedConnectionFactory mcf = null;
245     private transient Trace trace;
246
247     private String JavaDoc connectionDriverName;
248     private String JavaDoc connectionURL; // properties file name
249
private String JavaDoc connectionUserName;
250     private String JavaDoc connectionPassword;
251
252     private static final String JavaDoc PMGET_EXCEPTION_MSG = "Error while getting a JalistoSession";
253     private static final String JavaDoc TRACE_ID = "[JalistoConnectionFactory]";
254     private static final String JavaDoc DRIVER_NAMING_FACTORY_NAME = "org.objectweb.jalisto.se.jca.JalistoObjectFactory";
255     private static final String JavaDoc NAMINGPROPNAME = "xcalia.jalisto";
256     private static final String JavaDoc RESNAME = "Xcalia Jalisto Connector Adapter 1.0";
257     private static final String JavaDoc RESDESC = "This is a generic Xcalia Connector adapter for Jalisto";
258 }
259
Popular Tags