KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > jca > JdoConnectionFactory


1 /**
2  * perseus/connector: this is an implementation of some JCA-related technologies
3  * (resource adapters and managers) for the ObjectWeb consortium.
4  * Copyright (C) 2001-2004 France Telecom R&D
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: speedo@objectweb.org
21  *
22  */

23
24 package org.objectweb.speedo.jca;
25
26 import org.objectweb.util.monolog.api.BasicLevel;
27 import org.objectweb.util.monolog.api.Logger;
28 import org.objectweb.speedo.api.Debug;
29 import org.objectweb.speedo.api.SpeedoProperties;
30
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.Reference JavaDoc;
33 import javax.resource.NotSupportedException JavaDoc;
34 import javax.resource.ResourceException JavaDoc;
35 import javax.resource.cci.Connection JavaDoc;
36 import javax.resource.cci.ConnectionFactory JavaDoc;
37 import javax.resource.cci.ConnectionSpec JavaDoc;
38 import javax.resource.cci.RecordFactory JavaDoc;
39 import javax.resource.cci.ResourceAdapterMetaData JavaDoc;
40 import javax.resource.spi.ConnectionManager JavaDoc;
41 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
42 import javax.jdo.datastore.DataStoreCache;
43 import javax.jdo.listener.InstanceLifecycleListener;
44 import javax.jdo.PersistenceManagerFactory;
45 import javax.jdo.PersistenceManager;
46 import javax.jdo.JDOException;
47 import java.util.Properties JavaDoc;
48 import java.util.Collection JavaDoc;
49
50 /**
51  * @author P. Dechamboux
52  */

53 public class JdoConnectionFactory
54         implements ConnectionFactory JavaDoc, ResourceAdapterMetaData JavaDoc,
55         PersistenceManagerFactory {
56     /**
57      * The logger into which traces about JdoConnectionFactory are produced.
58      */

59     private Logger logger;
60     /**
61      * The JDO Adapter Version.
62      */

63     private final static String JavaDoc JDOADAPTERVERSION = "1.00";
64     /**
65      * The JDO Adapter Vendor Name (i.e. ObjectWeb !!).
66      */

67     private final static String JavaDoc JDOADAPTERVENDORNAME = "The ObjectWeb Consortium";
68     /**
69      * The JDO Adapter Name.
70      */

71     private final static String JavaDoc JDOADAPTERNAME = "OW Java Data Object (JDO) Adapter";
72     /**
73      * A short description of the JDO Adapter.
74      */

75     private final static String JavaDoc JDOADAPTERSHORTDESCRIPTION = "It provides a JCA wrapper to any JDO compliant implementation.";
76     /**
77      * The JDO Spec Version (none yet).
78      */

79     private final static String JavaDoc JDOSPECVERSION = "1.00";
80
81     /**
82      * The JNDI Reference associated to this adapter.
83      */

84     private Reference JavaDoc jndiReference = null;
85     /**
86      * The ConnectionManager used to actually get Connection.
87      */

88     private ConnectionManager JavaDoc connectionManager = null;
89     /**
90      * The ManagedConnectionFactory associated to this ConnectionFactory.
91      */

92     private JdoManagedConnectionFactory mcf;
93
94     /**
95      * The transaction mode within a j2ee context.
96      */

97     private byte transactionMode = SpeedoProperties.TRANSACTION_BMODE_NORMAL;
98     
99     /**
100      * Constructs a JdoConnectionFactory. Associates a ConnectionManager with
101      * it, or creates a default one (if null).
102      * @param fmcf The ManagedConnectionFactory to be associated with this
103      * ConnectionFactory.
104      * @param cm The ConnectionManager to be associated with this
105      * ConnectionFactory. It may be null.
106      * @param transactionMode the mode of transaction chosen for the application.
107      */

108     JdoConnectionFactory(Logger logger,
109                          JdoManagedConnectionFactory fmcf,
110                          ConnectionManager JavaDoc cm,
111                          byte transactionMode) throws ResourceException JavaDoc {
112         this.logger = logger;
113         if (Debug.ON)
114             logger.log(BasicLevel.DEBUG,
115                        "Constructs a new JdoConnectionFactory.");
116         mcf = fmcf;
117         setConnectionManager(cm);
118         this.transactionMode = transactionMode;
119     }
120
121     /**
122      * Creates a new JdoConnection.
123      */

124     JdoConnection createConnection() throws ResourceException JavaDoc {
125         JdoConnection res = new JdoConnection(logger, this);
126         if (Debug.ON)
127             logger.log(BasicLevel.DEBUG, "New JdoConnection created: " + res);
128         return res;
129     }
130
131     private void checkMCFStarted() {
132         if (!mcf.started) {
133             try {
134                 mcf.start();
135             } catch (ResourceException JavaDoc e) {
136                 throw new JDOException("JDO Connector: cannot initialize the ManagedConnectionFactory.", e);
137             }
138         }
139     }
140
141     public void setConnectionManager(ConnectionManager JavaDoc connectionManager) {
142         if (connectionManager != null) {
143             logger.log(BasicLevel.DEBUG,
144                     "Linked the ConnectionFactory to the ConnectionManager: "
145                     + connectionManager);
146         }
147         this.connectionManager = connectionManager;
148     }
149
150     // IMPLEMENTATION OF METHODS FROM THE (cci)ConnectionFactory INTERFACE
151

152     /**
153      * Connection allocation is delegated to the ConnectionManager assigned to
154      * this factory.
155      */

156     public Connection JavaDoc getConnection() throws ResourceException JavaDoc {
157         if (!mcf.started) {
158             mcf.start();
159         }
160         PersistenceManager pm = (PersistenceManager)
161                 connectionManager.allocateConnection(mcf, null);
162         pm.currentTransaction();
163         return (Connection JavaDoc) pm;
164     }
165
166     /**
167      * Connection allocation is delegated to the ConnectionManager assigned to
168      * this factory.
169      * @param spec The connection is not taken into account within JDO.
170      */

171     public Connection JavaDoc getConnection(ConnectionSpec JavaDoc spec) throws ResourceException JavaDoc {
172         if (!mcf.started) {
173             mcf.start();
174         }
175         ConnectionRequestInfo JavaDoc cri = null;
176         if (spec instanceof ConnectionRequestInfo JavaDoc) {
177             cri = (ConnectionRequestInfo JavaDoc) spec;
178         }
179         PersistenceManager pm = (PersistenceManager)
180                 connectionManager.allocateConnection(mcf, cri);
181         pm.currentTransaction();
182         return (Connection JavaDoc) pm;
183     }
184
185     /**
186      * Assigns a JNDI Reference to the adapter.
187      * @param reference The JNDI Reference to be assigned.
188      */

189     public void setReference(Reference JavaDoc reference) {
190         jndiReference = reference;
191     }
192
193     /**
194      * Returns the JNDI Reference.
195      * @return The JNDI Reference.
196      */

197     public Reference JavaDoc getReference() throws NamingException JavaDoc {
198         return jndiReference;
199     }
200
201     /**
202      * No support for record.
203      */

204     public RecordFactory JavaDoc getRecordFactory() throws ResourceException JavaDoc {
205         throw new NotSupportedException JavaDoc("JDO Connector: no support for record.");
206     }
207
208     /**
209      * The JdoConnectionFactory manages its metadata on its own.
210      */

211     public ResourceAdapterMetaData JavaDoc getMetaData() throws ResourceException JavaDoc {
212         return this;
213     }
214
215     // IMPLEMENTATION OF METHODS FROM THE (cci)ResourceAdapaterMetaData INTERFACE
216

217     /**
218      * Gives access to the adapter version metadata for the JDO.
219      * @return The JDO adapter version.
220      */

221     public String JavaDoc getAdapterVersion() {
222         return JDOADAPTERVERSION;
223     }
224
225     /**
226      * Gives access to the adapter vendor name metadata for the JDO.
227      * @return The JDO adapter vendor name.
228      */

229     public String JavaDoc getAdapterVendorName() {
230         return JDOADAPTERVENDORNAME;
231     }
232
233     /**
234      * Gives access to the adapter name metadata for the JDO.
235      * @return The JDO adapter name.
236      */

237     public String JavaDoc getAdapterName() {
238         return JDOADAPTERNAME;
239     }
240
241     /**
242      * Gives access to a short description of the JDO adapter.
243      * @return The JDO short description.
244      */

245     public String JavaDoc getAdapterShortDescription() {
246         return JDOADAPTERSHORTDESCRIPTION;
247     }
248
249     /**
250      * Gives access to the specification version of the JDO.
251      * @return The JDO specification version.
252      */

253     public String JavaDoc getSpecVersion() {
254         return JDOSPECVERSION;
255     }
256
257     /**
258      * No interaction is supported by the JDO adapter.
259      */

260     public String JavaDoc[] getInteractionSpecsSupported() {
261         return new String JavaDoc[0];
262     }
263
264     /**
265      * No support for record.
266      */

267     public boolean supportsExecuteWithInputAndOutputRecord() {
268         return false;
269     }
270
271     /**
272      * No support for record.
273      */

274     public boolean supportsExecuteWithInputRecordOnly() {
275         return false;
276     }
277
278     /**
279      * Yes, it does support local transaction demarcation.
280      */

281     public boolean supportsLocalTransactionDemarcation() {
282         return true;
283     }
284
285     // IMPLEMENTATION OF METHODS FROM THE PersistenceManagerFactory INTERFACE
286

287     public PersistenceManager getPersistenceManager() {
288         try {
289             return (PersistenceManager) getConnection();
290         } catch (ResourceException JavaDoc e) {
291             throw new JDOException("JDO Connector: problem while getting a PersistenceManager.", e);
292         }
293     }
294
295     public PersistenceManager getPersistenceManager(String JavaDoc s, String JavaDoc s1) {
296         try {
297             PersistenceManager pm = (PersistenceManager)
298                     getConnection(new JDOConnectionSpec(s, s1));
299             pm.currentTransaction();
300             return pm;
301         } catch (ResourceException JavaDoc e) {
302             throw new JDOException("JDO Connector: problem while getting a PersistenceManager.", e);
303         }
304     }
305
306     public void setConnectionUserName(String JavaDoc s) {
307         checkMCFStarted();
308         mcf.pmf.setConnectionUserName(s);
309     }
310
311     public String JavaDoc getConnectionUserName() {
312         checkMCFStarted();
313         return mcf.pmf.getConnectionUserName();
314     }
315
316     public void setConnectionPassword(String JavaDoc s) {
317         checkMCFStarted();
318         mcf.pmf.setConnectionPassword(s);
319     }
320
321     public void setConnectionURL(String JavaDoc s) {
322         checkMCFStarted();
323         mcf.pmf.setConnectionURL(s);
324     }
325
326     public String JavaDoc getConnectionURL() {
327         checkMCFStarted();
328         return mcf.pmf.getConnectionURL();
329     }
330
331     public void setConnectionDriverName(String JavaDoc s) {
332         checkMCFStarted();
333         mcf.pmf.setConnectionDriverName(s);
334     }
335
336     public String JavaDoc getConnectionDriverName() {
337         checkMCFStarted();
338         return mcf.pmf.getConnectionDriverName();
339     }
340
341     public void setConnectionFactoryName(String JavaDoc s) {
342         checkMCFStarted();
343         mcf.pmf.setConnectionFactoryName(s);
344     }
345
346     public String JavaDoc getConnectionFactoryName() {
347         checkMCFStarted();
348         return mcf.pmf.getConnectionFactoryName();
349     }
350
351     public void setConnectionFactory(Object JavaDoc o) {
352         checkMCFStarted();
353         mcf.pmf.setConnectionFactory(o);
354     }
355
356     public Object JavaDoc getConnectionFactory() {
357         checkMCFStarted();
358         return mcf.pmf.getConnectionFactory();
359     }
360
361     public void setConnectionFactory2Name(String JavaDoc s) {
362         checkMCFStarted();
363         mcf.pmf.setConnectionFactory2Name(s);
364     }
365
366     public String JavaDoc getConnectionFactory2Name() {
367         checkMCFStarted();
368         return mcf.pmf.getConnectionFactory2Name();
369     }
370
371     public void setConnectionFactory2(Object JavaDoc o) {
372         checkMCFStarted();
373         mcf.pmf.setConnectionFactory2(o);
374     }
375
376     public Object JavaDoc getConnectionFactory2() {
377         checkMCFStarted();
378         return mcf.pmf.getConnectionFactory2();
379     }
380
381     public void setMultithreaded(boolean b) {
382         checkMCFStarted();
383         mcf.pmf.setMultithreaded(b);
384     }
385
386     public boolean getMultithreaded() {
387         checkMCFStarted();
388         return mcf.pmf.getMultithreaded();
389     }
390
391     public void setOptimistic(boolean b) {
392         checkMCFStarted();
393         mcf.pmf.setOptimistic(b);
394     }
395
396     public boolean getOptimistic() {
397         checkMCFStarted();
398         return mcf.pmf.getOptimistic();
399     }
400
401     public void setRetainValues(boolean b) {
402         checkMCFStarted();
403         mcf.pmf.setRetainValues(b);
404     }
405
406     public boolean getRetainValues() {
407         checkMCFStarted();
408         return mcf.pmf.getRetainValues();
409     }
410
411     public void setRestoreValues(boolean b) {
412         checkMCFStarted();
413         mcf.pmf.setRestoreValues(b);
414     }
415
416     public boolean getRestoreValues() {
417         checkMCFStarted();
418         return mcf.pmf.getRestoreValues();
419     }
420
421     public void setNontransactionalRead(boolean b) {
422         checkMCFStarted();
423         mcf.pmf.setNontransactionalRead(b);
424     }
425
426     public boolean getNontransactionalRead() {
427         checkMCFStarted();
428         return mcf.pmf.getNontransactionalRead();
429     }
430
431     public void setNontransactionalWrite(boolean b) {
432         checkMCFStarted();
433         mcf.pmf.setNontransactionalWrite(b);
434     }
435
436     public boolean getNontransactionalWrite() {
437         checkMCFStarted();
438         return mcf.pmf.getNontransactionalWrite();
439     }
440
441     public void setIgnoreCache(boolean b) {
442         checkMCFStarted();
443         mcf.pmf.setIgnoreCache(b);
444     }
445
446     public boolean getIgnoreCache() {
447         checkMCFStarted();
448         return mcf.pmf.getIgnoreCache();
449     }
450
451     public Properties getProperties() {
452         checkMCFStarted();
453         return mcf.pmf.getProperties();
454     }
455
456     public Collection JavaDoc supportedOptions() {
457         checkMCFStarted();
458         return mcf.pmf.supportedOptions();
459     }
460
461     public void close() {
462         checkMCFStarted();
463         mcf.pmf.close();
464     }
465     
466     public DataStoreCache getDataStoreCache() {
467         checkMCFStarted();
468         return mcf.pmf.getDataStoreCache();
469     }
470     public String JavaDoc getMapping() {
471         checkMCFStarted();
472         return mcf.pmf.getMapping();
473     }
474     public boolean isClosed() {
475         checkMCFStarted();
476         return mcf.pmf.isClosed();
477     }
478     public void setMapping(String JavaDoc arg0) {
479         checkMCFStarted();
480         mcf.pmf.setMapping(arg0);
481     }
482     public void addInstanceLifecycleListener(InstanceLifecycleListener arg0,
483             Class JavaDoc[] arg1) {
484         checkMCFStarted();
485         mcf.pmf.addInstanceLifecycleListener(arg0, arg1);
486     }
487     public void removeInstanceLifecycleListener(InstanceLifecycleListener arg0) {
488         checkMCFStarted();
489         mcf.pmf.removeInstanceLifecycleListener(arg0);
490     }
491     
492     public byte getTransactionMode() {
493         return transactionMode;
494     }
495 }
496
Popular Tags