KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > connector > ra > fos > FosManagedConnection


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-2002 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: pascal.dechamboux@rd.francetelecom.com
21  *
22  */

23
24 package org.objectweb.perseus.connector.ra.fos;
25
26 import org.objectweb.perseus.fos.api.FosAccess;
27 import org.objectweb.perseus.fos.api.FosException;
28 import org.objectweb.perseus.fos.api.FosLoggerFactory;
29 import org.objectweb.perseus.fos.api.FosStructure;
30 import org.objectweb.perseus.fos.api.FosTransaction;
31 import org.objectweb.perseus.fos.lib.FosTxContext;
32 import org.objectweb.util.monolog.api.BasicLevel;
33 import org.objectweb.util.monolog.api.Logger;
34
35 import java.io.PrintWriter JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import javax.resource.ResourceException JavaDoc;
39 import javax.resource.spi.ConnectionEvent JavaDoc;
40 import javax.resource.spi.ConnectionEventListener JavaDoc;
41 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
42 import javax.resource.spi.LocalTransaction JavaDoc;
43 import javax.resource.spi.ManagedConnection JavaDoc;
44 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
45 import javax.security.auth.Subject JavaDoc;
46 import javax.transaction.xa.XAResource JavaDoc;
47
48 /**
49  * @author S. Chassande-Barrioz, P. Dechamboux
50  */

51 public class FosManagedConnection
52     implements ManagedConnection JavaDoc, LocalTransaction JavaDoc,
53     javax.resource.cci.LocalTransaction JavaDoc {
54     private static final ArrayList JavaDoc NOLISTENERS = new ArrayList JavaDoc();
55     /**
56      * The logger into which traces about FosManagedConnection are produced.
57      */

58     private Logger logger;
59     /**
60      * The ManagedConnectionFactory that have requested the allocation of this
61      * ManagedConnection.
62      */

63     private FosManagedConnectionFactory managedConnectionFactory;
64     /**
65      * The Connections associated with this ManagedConnection. Always at least
66      * one connection.
67      */

68     private ArrayList JavaDoc associatedConnections = new ArrayList JavaDoc(1);
69     /**
70      * The ConnectionEventListener list associated with this ManagedConnection.
71      */

72     private ArrayList JavaDoc associatedListeners;
73     /**
74      * It is set if the ManagedConnection execute within a LocalTransaction.
75      */

76     private FosTxContext localTransactionTxContext;
77     /**
78      * It is set if the ManagedConnection has been requested for a XAResource.
79      */

80     private FosXAResource xaResource;
81     /**
82      * It defines the auto-commit mode of this connection. It defaults to true
83      * at creation. It is ignored while running within a LocalTransaction or
84      * a DTP context (XA mode).
85      */

86     private boolean autoCommit;
87
88     /**
89      * Constructs a FosManagedConnection.
90      * @param el The logger into which to produce ManagedConnection-related
91      * traces.
92      * @param fmcf The ManagedConnectionFactory that has requested the
93      * ManagedConnection creation.
94      */

95     FosManagedConnection(Logger el, FosManagedConnectionFactory fmcf) {
96         logger = el;
97         if (FosLoggerFactory.DEBUG)
98             logger.log(BasicLevel.DEBUG,
99                        "Constructs a new FosManagedConnection");
100         managedConnectionFactory = fmcf;
101         associatedListeners = NOLISTENERS;
102         try {
103             cleanup();
104         } catch (ResourceException JavaDoc re) {
105             // Can never happen
106
}
107     }
108
109     /**
110      * Dissociates a Connection from the ones that are associated to this
111      * ManagedConnection.
112      * @param conn The Connection to be dissociated.
113      */

114     synchronized void dissociateConnection(Object JavaDoc conn) throws ResourceException JavaDoc {
115         if (FosLoggerFactory.DEBUG)
116             logger.log(BasicLevel.DEBUG, "Dissociates Connection: " + conn);
117         int ind = associatedConnections.indexOf(conn);
118         if (FosLoggerFactory.DEBUG)
119             logger.log(BasicLevel.DEBUG, "Index within the associatedConnections: " + ind);
120         if (ind == -1)
121             throw new ResourceException JavaDoc("FOS: cannot dissociate an unknown Connection.");
122         ConnectionEvent JavaDoc ce = new ConnectionEvent JavaDoc(this,
123             ConnectionEvent.CONNECTION_CLOSED);
124         ce.setConnectionHandle(conn);
125         for (Iterator JavaDoc it = associatedListeners.iterator(); it.hasNext();)
126             ((ConnectionEventListener JavaDoc) it.next()).connectionClosed(ce);
127         associatedConnections.remove(ind);
128         if (associatedConnections.size() == 0)
129             cleanup();
130     }
131
132     /**
133      * Sets the connection to the relevant auto-commit mode.
134      */

135     public synchronized void setAutoCommit(boolean b) throws ResourceException JavaDoc {
136         autoCommit = b;
137     }
138
139     /**
140      * Gets the connection auto-commit mode.
141      */

142     public boolean getAutoCommit() throws ResourceException JavaDoc {
143         return autoCommit;
144     }
145
146     // IMPLEMENTATION OF METHODS FROM THE (cci)ManagedConnection INTERFACE
147

148     /**
149      * Delegates the creation of a Connection to the ConnectionFactory.
150      * "subject" and "info" parameters are ignored.
151      */

152     public Object JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc info)
153         throws ResourceException JavaDoc {
154         FosConnectionImpl rac = ((FosConnectionFactory) managedConnectionFactory.getConnectionFactory()).createConnection();
155         rac.initialize(this);
156         return rac;
157     }
158
159     /**
160      * Cleans up the connection.
161      */

162     public void cleanup() throws ResourceException JavaDoc {
163         if (FosLoggerFactory.DEBUG)
164             logger.log(BasicLevel.DEBUG, "Cleans up ManagedConnection");
165         int clsize = associatedConnections.size();
166         if (clsize != 0) {
167             while (clsize-- > 0)
168                 associatedConnections.remove(0);
169             logger.log(BasicLevel.WARN, "Fos ManagedConnection still has associated Fos Connections (" + clsize + ")!");
170         }
171         if (associatedListeners != NOLISTENERS) {
172             clsize = associatedListeners.size();
173             if (clsize != 0) {
174                 while (clsize-- > 0)
175                     associatedListeners.remove(0);
176                 logger.log(BasicLevel.WARN, "Fos ManagedConnection still has associated Listeners (" + clsize + ")!");
177             }
178         }
179         localTransactionTxContext = null;
180         if (xaResource != null) {
181             managedConnectionFactory.getXAResourceFactory().releaseXAResource(xaResource);
182             xaResource = null;
183         }
184         autoCommit = true;
185     }
186
187     /**
188      * Called when ManagedConnection is removed.
189      */

190     public void destroy() throws ResourceException JavaDoc {
191         cleanup();
192         logger = null;
193         managedConnectionFactory = null;
194         associatedConnections = null;
195         associatedListeners = null;
196         localTransactionTxContext = null;
197         xaResource = null;
198     }
199
200     /**
201      * Associates a new Connection to this ManagedConnection. Nothing is done
202      * if it has already been associated (enforces no duplicate).
203      */

204     public synchronized void associateConnection(Object JavaDoc o) throws ResourceException JavaDoc {
205         if (FosLoggerFactory.DEBUG)
206             logger.log(BasicLevel.DEBUG, "Associates Connection: " + o);
207         if (!associatedConnections.contains(o))
208             associatedConnections.add(o);
209         else
210             logger.log(BasicLevel.WARN, "Connection: " + o
211                 + "has already been associated: ignored!");
212     }
213
214     /**
215      * Adds a listener to the associatedListeners list if it has not
216      * already been done. This means that duplicates are ignored.
217      * It first creates this list if needed (creation at first add).
218      */

219     public synchronized void addConnectionEventListener(
220         ConnectionEventListener JavaDoc listener) {
221         if (associatedListeners == NOLISTENERS)
222             associatedListeners = new ArrayList JavaDoc();
223         if (FosLoggerFactory.DEBUG)
224             logger.log(BasicLevel.DEBUG, "Associates ConnectionEventListener: "
225                 + listener);
226         if (!associatedListeners.contains(listener))
227             associatedListeners.add(listener);
228         else
229             logger.log(BasicLevel.WARN, "ConnectionEventListener: " + listener
230                 + "has already been associated: ignored!");
231     }
232
233     /**
234      * Removes a listener from the associatedListeners list. If the list does
235      * not eist (null), or if this listener does not belong to this list,
236      * nothing is done.
237      */

238     public synchronized void removeConnectionEventListener(
239         ConnectionEventListener JavaDoc listener) {
240         if (associatedListeners == NOLISTENERS)
241             logger.log(BasicLevel.WARN,
242                 "No associated ConnectionEventListener: ignored!");
243         int ind = associatedListeners.indexOf(listener);
244         if (FosLoggerFactory.DEBUG)
245             logger.log(BasicLevel.DEBUG,
246                 "Index within the associatedListeners: " + ind);
247         if (ind == -1)
248             logger.log(BasicLevel.WARN, "ConnectionEventListener" + listener
249                 + "not associated here: ignored!");
250         else
251             associatedListeners.remove(ind);
252     }
253
254     /**
255      * Retrieves an XA resource. Switches running mode to XA mode.
256      */

257     public synchronized XAResource JavaDoc getXAResource() throws ResourceException JavaDoc {
258         try {
259             if (localTransactionTxContext != null)
260                 throw new ResourceException JavaDoc(
261                     "Try switching to XA running mode while a LocalTransaction has been begun!");
262             if (xaResource == null) {
263                 xaResource = managedConnectionFactory.getXAResourceFactory().createXAResource();
264                 if (FosLoggerFactory.DEBUG)
265                     logger.log(BasicLevel.DEBUG, "Creates a new XAResource: " + xaResource);
266             }
267             return xaResource;
268         } catch (FosException fe) {
269             ResourceException JavaDoc re = new ResourceException JavaDoc("FOS: [nested exception].");
270             re.setLinkedException(fe);
271             throw re;
272         }
273     }
274
275     public LocalTransaction JavaDoc getLocalTransaction() throws ResourceException JavaDoc {
276         return this;
277     }
278
279     public ManagedConnectionMetaData JavaDoc getMetaData() throws ResourceException JavaDoc {
280         return null;
281     }
282
283     /**
284      * Not supported yet.
285      */

286     public void setLogWriter(PrintWriter JavaDoc writer) throws ResourceException JavaDoc {
287         throw new ResourceException JavaDoc("FOS: logging to PrintWriter not supported yet.");
288     }
289
290     /**
291      * Not supported yet.
292      */

293     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
294         throw new ResourceException JavaDoc("FOS: logging to PrintWriter not supported yet.");
295     }
296
297     // IMPLEMENTATION OF METHODS FROM THE (cci)LocalTransaction INTERFACE
298

299     /**
300      * Begins the LocalTransaction if it has not already been started.
301      */

302     public synchronized void begin() throws ResourceException JavaDoc {
303         if (localTransactionTxContext != null)
304             throw new ResourceException JavaDoc("FOS: a LocalTransaction has already begun.");
305         try {
306             localTransactionTxContext
307                 = (FosTxContext) managedConnectionFactory.getTxContextFactory()
308                 .createTxContext();
309             localTransactionTxContext.begin();
310             if (FosLoggerFactory.DEBUG)
311                 logger.log(BasicLevel.DEBUG, "LocalTransaction begin - txContext: "
312                     + localTransactionTxContext);
313             if (xaResource != null)
314                 logger.log(BasicLevel.WARN,
315                     "Begins a LocalTransaction on a ManagedConnection that runs in XA mode!");
316             ConnectionEvent JavaDoc ce = new ConnectionEvent JavaDoc(this,
317                 ConnectionEvent.LOCAL_TRANSACTION_STARTED);
318             for (Iterator JavaDoc it = associatedListeners.iterator(); it.hasNext();)
319                 ((ConnectionEventListener JavaDoc) it.next()).localTransactionStarted(ce);
320         } catch (FosException fe) {
321             ResourceException JavaDoc re = new ResourceException JavaDoc(
322                 "FOS: cannot begin LocalTransaction [nested exception].");
323             re.setLinkedException(fe);
324             throw re;
325         }
326     }
327
328     /**
329      * Commits the LocalTransaction if it is active.
330      */

331     public synchronized void commit() throws ResourceException JavaDoc {
332         if (localTransactionTxContext == null)
333             throw new ResourceException JavaDoc("FOS: no LocalTransaction has been begun.");
334         if (FosLoggerFactory.DEBUG)
335             logger.log(BasicLevel.DEBUG, "LocalTransaction commit - txContext: "
336                 + localTransactionTxContext);
337         try {
338             localTransactionTxContext.commit();
339             managedConnectionFactory.getTxContextFactory().releaseTxContext(localTransactionTxContext);
340             localTransactionTxContext = null;
341             ConnectionEvent JavaDoc ce = new ConnectionEvent JavaDoc(this,
342                 ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
343             for (Iterator JavaDoc it = associatedListeners.iterator(); it.hasNext();)
344                 ((ConnectionEventListener JavaDoc) it.next()).localTransactionCommitted(ce);
345         } catch (FosException fe) {
346             ResourceException JavaDoc re = new ResourceException JavaDoc(
347                 "FOS: cannot commit LocalTransaction [nested exception].");
348             re.setLinkedException(fe);
349             throw re;
350         }
351     }
352
353     /**
354      * Rollbacks the LocalTransaction if it is active.
355      */

356     public synchronized void rollback() throws ResourceException JavaDoc {
357         if (localTransactionTxContext == null)
358             throw new ResourceException JavaDoc("FOS: no LocalTransaction has been begun.");
359         if (FosLoggerFactory.DEBUG)
360             logger.log(BasicLevel.DEBUG, "LocalTransaction rollback - txContext: "
361                 + localTransactionTxContext);
362         try {
363             localTransactionTxContext.rollback();
364             managedConnectionFactory.getTxContextFactory().releaseTxContext(localTransactionTxContext);
365             localTransactionTxContext = null;
366             ConnectionEvent JavaDoc ce = new ConnectionEvent JavaDoc(this,
367                 ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
368             for (Iterator JavaDoc it = associatedListeners.iterator(); it.hasNext();)
369                 ((ConnectionEventListener JavaDoc) it.next()).localTransactionRolledback(ce);
370         } catch (FosException fe) {
371             ResourceException JavaDoc re = new ResourceException JavaDoc(
372                 "FOS: cannot rollback LocalTransaction [nested exception].");
373             re.setLinkedException(fe);
374             throw re;
375         }
376     }
377
378     // IMPLEMENTATION OF METHODS FROM THE FosAccess INTERFACE
379

380     /**
381      * Deletes the file associated with a persistent object. This file is
382      * specified by the directory under which it is stored under dbDir, and
383      * its name (or id) within this directory.
384      * @param dirof The directory under dbDir where the deleted file is
385      * located.
386      * @param id The name of the file to delete (corresponding to the object
387      * identifier).
388      */

389     public synchronized void delete(String JavaDoc dirof, String JavaDoc id) throws FosException {
390         if (localTransactionTxContext != null) {
391             if (FosLoggerFactory.DEBUG)
392                 logger.log(BasicLevel.DEBUG, "Performs DELETE under LocalTransaction mode.");
393             localTransactionTxContext.delete(dirof, id);
394             return;
395         }
396         if (xaResource != null) {
397             if (FosLoggerFactory.DEBUG)
398                 logger.log(BasicLevel.DEBUG, "Performs DELETE under XA mode.");
399             xaResource.getTxContext().delete(dirof, id);
400             return;
401         }
402         if (!autoCommit)
403             throw new FosException(
404                 "Auto-commit OFF: cannot perform FosAccess operation with no transaction context!");
405         // Executes in auto-commit mode.
406
if (FosLoggerFactory.DEBUG)
407             logger.log(BasicLevel.DEBUG, "Performs DELETE under AUTO-COMMIT mode.");
408         FosTransaction txContext
409             = managedConnectionFactory.getTxContextFactory().createTxContext();
410         txContext.begin();
411         try {
412             txContext.delete(dirof, id);
413             txContext.commit();
414             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
415         } catch (FosException fe) {
416             txContext.rollback();
417             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
418             throw new FosException("Problem while running in auto-commit mode.", fe);
419         }
420     }
421
422     /**
423      * Deletes a directory that stores persistent objects along with all object
424      * files stored under it.
425      * @param dirof The directory under dbDir to be deleted.
426      */

427     public void deleteDir(String JavaDoc dirof) throws FosException {
428         FosTransaction txContext
429             = managedConnectionFactory.getTxContextFactory().createTxContext();
430         try {
431             txContext.deleteDir(dirof);
432             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
433         } catch (FosException fe) {
434             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
435             throw fe;
436         }
437     }
438
439     /**
440      * Tests if the file associated to a persistent object exists. This file is
441      * specified by the directory under which it is stored under dbDir, and
442      * its name (or id) within this directory.
443      * @param dirof The directory under dbDir where the file should be located.
444      * @param id The name of the file to test (corresponding to the object
445      * identifier).
446      * @return true if the object file exists, else false.
447      */

448     public synchronized boolean exist(String JavaDoc dirof, String JavaDoc id) throws FosException {
449         if (localTransactionTxContext != null) {
450             if (FosLoggerFactory.DEBUG)
451                 logger.log(BasicLevel.DEBUG, "Performs EXIST under LocalTransaction mode.");
452             return localTransactionTxContext.exist(dirof, id);
453         }
454         if (xaResource != null) {
455             if (FosLoggerFactory.DEBUG)
456                 logger.log(BasicLevel.DEBUG, "Performs EXIST under XA mode.");
457             return xaResource.getTxContext().exist(dirof, id);
458         }
459         if (!autoCommit)
460             throw new FosException(
461                 "Auto-commit OFF: cannot perform FosAccess operation with no transaction context!");
462         // Executes in auto-commit mode.
463
if (FosLoggerFactory.DEBUG)
464             logger.log(BasicLevel.DEBUG, "Performs EXIST under AUTO-COMMIT mode.");
465         FosTransaction txContext
466             = managedConnectionFactory.getTxContextFactory().createTxContext();
467         txContext.begin();
468         try {
469             boolean res = txContext.exist(dirof, id);
470             txContext.commit();
471             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
472             return res;
473         } catch (FosException fe) {
474             txContext.rollback();
475             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
476             throw new FosException("Problem while running in auto-commit mode.", fe);
477         }
478     }
479
480     /**
481      * Tests if a directory that stores persistent objects exists.
482      * @param dirof The directory under dbDir to test the existence.
483      * @return true if the directory exists, else false.
484      */

485     public boolean existDir(String JavaDoc dirof) throws FosException {
486         FosTransaction txContext
487             = managedConnectionFactory.getTxContextFactory().createTxContext();
488         try {
489             boolean res = txContext.existDir(dirof);
490             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
491             return res;
492         } catch (FosException fe) {
493             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
494             throw fe;
495         }
496     }
497
498     /**
499      * Reads the content of a persistent object from a file. This file is
500      * specified by the directory under which it is stored under dbDir, and
501      * its name (or id) within this directory. The object that actually reads
502      * the file is also given by the user.
503      * @param dirof The directory under dbDir where the read file is located.
504      * @param id The name of the file to read (corresponding to the object
505      * identifier).
506      * @param fs The user object for actually reading the file.
507      */

508     public synchronized void read(String JavaDoc dirof, String JavaDoc id, FosStructure fs,
509                                   FosAccess conn, Object JavaDoc ctxt)
510         throws FosException {
511         if (localTransactionTxContext != null) {
512             if (FosLoggerFactory.DEBUG)
513                 logger.log(BasicLevel.DEBUG, "Performs READ under LocalTransaction mode.");
514             localTransactionTxContext.read(dirof, id, fs, conn, ctxt);
515             return;
516         }
517         if (xaResource != null) {
518             if (FosLoggerFactory.DEBUG)
519                 logger.log(BasicLevel.DEBUG, "Performs READ under XA mode.");
520             xaResource.getTxContext().read(dirof, id, fs, conn, ctxt);
521             return;
522         }
523         if (!autoCommit)
524             throw new FosException(
525                 "Auto-commit OFF: cannot perform FosAccess operation with no transaction context!");
526         // Executes in auto-commit mode.
527
if (FosLoggerFactory.DEBUG)
528             logger.log(BasicLevel.DEBUG, "Performs READ under AUTO-COMMIT mode.");
529         FosTxContext txContext
530             = (FosTxContext) managedConnectionFactory.getTxContextFactory()
531             .createTxContext();
532         txContext.begin();
533         try {
534             txContext.read(dirof, id, fs, conn, ctxt);
535             txContext.commit();
536             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
537         } catch (FosException fe) {
538             txContext.rollback();
539             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
540             throw new FosException("Problem while running in auto-commit mode.", fe);
541         }
542     }
543
544     /**
545      * Gets an iterator in order to iterate over the names of the data
546      * object files stored into that sub-directory.
547      * @param dirof The sub-directory from which to scan the data
548      * object file names.
549      * @return The iterator in order to iterate over these names.
550      */

551     public synchronized Iterator JavaDoc scan(String JavaDoc dirof) throws FosException {
552         if (localTransactionTxContext != null) {
553             if (FosLoggerFactory.DEBUG)
554                 logger.log(BasicLevel.DEBUG, "Performs SCAN under LocalTransaction mode.");
555             return localTransactionTxContext.scan(dirof);
556         }
557         if (xaResource != null) {
558             if (FosLoggerFactory.DEBUG)
559                 logger.log(BasicLevel.DEBUG, "Performs SCAN under XA mode.");
560             return xaResource.getTxContext().scan(dirof);
561         }
562         if (!autoCommit)
563             throw new FosException(
564                 "Auto-commit OFF: cannot perform FosAccess operation with no transaction context!");
565         // Executes in auto-commit mode.
566
if (FosLoggerFactory.DEBUG)
567             logger.log(BasicLevel.DEBUG, "Performs SCAN under AUTO-COMMIT mode.");
568         FosTransaction txContext
569             = managedConnectionFactory.getTxContextFactory().createTxContext();
570         txContext.begin();
571         try {
572             Iterator JavaDoc res = txContext.scan(dirof);
573             txContext.commit();
574             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
575             return res;
576         } catch (FosException fe) {
577             txContext.rollback();
578             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
579             throw new FosException("Problem while running in auto-commit mode.", fe);
580         }
581     }
582
583     /**
584      * Writes the content of a persistent object to a file. This file is
585      * specified by the directory under which it is stored under dbDir, and
586      * its name (or id) within this directory. The object that actually writes
587      * the file is also given by the user.
588      * @param dirof The directory under dbDir where the written file is
589      * located.
590      * @param id The name of the file to write (corresponding to the object
591      * identifier).
592      * @param fs The user object for actually writing the file.
593      */

594     public synchronized void write(String JavaDoc dirof, String JavaDoc id, FosStructure fs,
595                                    FosAccess conn, Object JavaDoc ctxt)
596         throws FosException {
597         if (localTransactionTxContext != null) {
598             if (FosLoggerFactory.DEBUG)
599                 logger.log(BasicLevel.DEBUG, "Performs WRITE under LocalTransaction mode.");
600             localTransactionTxContext.write(dirof, id, fs, conn, ctxt);
601             return;
602         }
603         if (xaResource != null) {
604             if (FosLoggerFactory.DEBUG)
605                 logger.log(BasicLevel.DEBUG, "Performs WRITE under XA mode.");
606             xaResource.getTxContext().write(dirof, id, fs, conn, ctxt);
607             return;
608         }
609         if (!autoCommit)
610             throw new FosException(
611                 "Auto-commit OFF: cannot perform FosAccess operation with no transaction context!");
612         // Executes in auto-commit mode.
613
if (FosLoggerFactory.DEBUG)
614             logger.log(BasicLevel.DEBUG, "Performs WRITE under AUTO-COMMIT mode.");
615         FosTxContext txContext
616             = (FosTxContext) managedConnectionFactory.getTxContextFactory()
617             .createTxContext();
618         txContext.begin();
619         try {
620             txContext.write(dirof, id, fs, conn, ctxt);
621             txContext.commit();
622             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
623         } catch (FosException fe) {
624             txContext.rollback();
625             managedConnectionFactory.getTxContextFactory().releaseTxContext(txContext);
626             throw new FosException("Problem while running in auto-commit mode.", fe);
627         }
628     }
629 }
Popular Tags