KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > FileCommitUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: FileCommitUtils.java,v 1.7 2007/01/07 06:14:01 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34 import org.opensubsystems.core.error.OSSException;
35 import org.opensubsystems.core.error.OSSInternalErrorException;
36
37 /**
38  * Class used for safe file manipulation. It simulates database transaction for
39  * file operations such as copy, move, replace or delete. The client should
40  * prepare any new or modified file to temporary location. Then the client should
41  * call prepareToCommit() method which will prepare the file fot the safe
42  * manipulation. After everything is ready, commit() method should be called,
43  * otherwise rollback() method should by called.
44  *
45  * @version $Id: FileCommitUtils.java,v 1.7 2007/01/07 06:14:01 bastafidli Exp $
46  * @author Julo Legeny
47  * @code.reviewer Miro Halas
48  * @code.reviewed Initial revision
49  */

50 public class FileCommitUtils
51 {
52    // Configuration settings ///////////////////////////////////////////////////
53

54    /**
55     * Configuration setting for how many times to retry file commit or rollback
56     * operations in case of error before the commit or rollback of file
57     * manipulation operation is aborted.
58     */

59    public static final String JavaDoc FILECOMMIT_RETRY_COUNT = "oss.filecommit.retrycount";
60    
61    // Constants ////////////////////////////////////////////////////////////////
62

63    /**
64     * Default value for configuration setting how many times to retry file commit
65     * operations in case or error before we give up.
66     */

67    public static final int FILECOMMIT_RETRY_COUNT_DEFAULT = 3;
68
69    /**
70     * Constant for sleeping time duration
71     */

72    public static final int SLEEP_TIME = 3000;
73
74    /**
75     * Constant for ready to copy directory name
76     */

77    public static final String JavaDoc FILE_PATH_READY_TO_COPY = "ready_to_copy";
78
79    /**
80     * Constant for ready to move directory name
81     */

82    public static final String JavaDoc FILE_PATH_READY_TO_MOVE = "ready_to_move";
83
84    /**
85     * Constant for ready to replace directory name
86     */

87    public static final String JavaDoc FILE_PATH_READY_TO_REPLACE = "ready_to_replace";
88
89    /**
90     * Constant for ready to replace directory name
91     */

92    public static final String JavaDoc FILE_PATH_READY_TO_BE_REPLACED = "ready_to_be_replaced";
93
94    /**
95     * Constant for ready to delete directory name
96     */

97    public static final String JavaDoc FILE_PATH_READY_TO_DELETE = "ready_to_delete";
98
99    /**
100     * Return code for successfull status
101     */

102    public static final int RC_OK = 0;
103
104    /**
105     * Object code for for successfull status
106     */

107    public static final Integer JavaDoc RC_OK_OBJ = new Integer JavaDoc(RC_OK);
108
109    /**
110     * Return code for DB exception status
111     */

112    protected static final int RC_DB_ERR = 1;
113
114    /**
115     * Object code for for DB exception status
116     */

117    public static final Integer JavaDoc RC_DB_ERR_OBJ = new Integer JavaDoc(RC_DB_ERR);
118
119    /**
120     * Return code for file exception status
121     */

122    protected static final int RC_FILE_ERR = 2;
123
124    /**
125     * Object code for for file exception status
126     */

127    public static final Integer JavaDoc RC_FILE_ERR_OBJ = new Integer JavaDoc(RC_FILE_ERR);
128
129    /**
130     * Constant for initial status: no action yet
131     */

132    public static final int STATUS_INITIAL = 0;
133
134    /**
135     * Constant for status: prepare to commit
136     */

137    public static final int STATUS_PREPARE_TO_COMMIT = 1;
138
139    /**
140     * Constant for status: commit
141     */

142    public static final int STATUS_COMMIT = 2;
143
144    /**
145     * Constant for status: rollback
146     */

147    public static final int STATUS_ROLLBACK = 3;
148
149    /**
150     * Constant for sender list identificator: copy
151     */

152    public static final int LIST_IDENTIFICATOR_COPY = 1;
153
154    /**
155     * Constant for sender list identificator: replace
156     */

157    public static final int LIST_IDENTIFICATOR_REPLACE = 2;
158
159    /**
160     * Constant for sender list identificator: to be replaced
161     */

162    public static final int LIST_IDENTIFICATOR_TO_BE_REPLACED = 3;
163
164    /**
165     * Constant for sender list identificator: replace
166     */

167    public static final int LIST_IDENTIFICATOR_MOVE = 4;
168
169    /**
170     * Constant for sender list identificator: replace
171     */

172    public static final int LIST_IDENTIFICATOR_DELETE = 5;
173
174    /**
175     * Constant for sender identificator: commit
176     */

177    public static final int SENDER_COMMIT = 1;
178
179    /**
180     * Constant for sender identificator: rollback
181     */

182    public static final int SENDER_ROLLBACK = 2;
183    
184    // Attributes ///////////////////////////////////////////////////////////////
185

186    /**
187     * Error returning code
188     */

189    protected Integer JavaDoc m_iReturnCode;
190
191    /**
192     * List of file names and path names that will be copied FROM (sources)
193     */

194    protected List JavaDoc m_lstCopyFromFiles;
195
196    /**
197     * List of file names and path names that will be copied TO (destinations)
198     */

199    protected List JavaDoc m_lstCopyToFiles;
200
201    /**
202     * List of file names and path names that will be moved FROM (sources)
203     */

204    protected List JavaDoc m_lstMoveFromFiles;
205
206    /**
207     * List of file names and path names that will be moved TO (destinations)
208     */

209    protected List JavaDoc m_lstMoveToFiles;
210
211    /**
212     * List of file names and path names that will be replaced FROM (sources)
213     */

214    protected List JavaDoc m_lstReplaceFromFiles;
215
216    /**
217     * List of file names and path names that will be replaced TO (destinations)
218     */

219    protected List JavaDoc m_lstReplaceToFiles;
220
221    /**
222     * List of file names and path names that will be deleted
223     */

224    protected List JavaDoc m_lstDeleteFiles;
225
226    /**
227     * Current status of the utils (prepare commit, commit or rollback).
228     * Status will be used for checking if there will be not called prepare
229     * to commit, commit or rollback more than once one after another.
230     *
231     * Status values:
232     * STATUS_PREPARE_TO_COMMIT = 1
233     * STATUS_COMMIT = 2
234     * STATUS_ROLLBACK = 3
235     */

236    protected int m_iStatus;
237
238    /**
239     * Flag signaling if moved files will be deleted from ready_to_move directory
240     * during rollback process. This flag will be TRUE if there will be moved files
241     * from temporary directory to the repository. If error will occured, there is
242     * not important move files back from ready_to_move directory to the temporary one.
243     */

244    protected boolean m_bMovedFilesDeleteForRollback;
245
246    // Class variables ///////////////////////////////////////////////////////////////
247

248    // === Schema for ready to copy process ==========================================
249
// LIST DIRECTORY COMMIT ROLLBACK
250
//--------------------------------------------------------------------------------
251
// m_lstReadyToCopyFrom = /ready_to_copy/ ---->>>| delete
252
// |
253
// m_lstReadyToCopyTo = /<final_destination>/ <<<----|
254
// ===============================================================================
255
/**
256     * List of files that are ready to copy FROM (sources).
257     * a.) This list will be used for commit if everything will be ok. Files will be renamed
258     * from current 'ready_to_copy' subdirectory into their final destination.
259     * b.) This list will be used for rollback if something fails. Files will be deleted
260     * from current 'ready_to_copy' subdirectory.
261     */

262    protected List JavaDoc m_lstReadyToCopyFrom;
263
264    /**
265     * List of files that are ready to copy TO (destinations).
266     * This list will be used for commit if everything will be ok. Files will be
267     * renamed from current 'ready_to_copy' subdirectory into final destination. This
268     * list will stored these final destinations.
269     */

270    protected List JavaDoc m_lstReadyToCopyTo;
271
272
273    // === Schema for ready to move process ==========================================
274
// LIST DIRECTORY COMMIT ROLLBACK
275
//--------------------------------------------------------------------------------
276
// m_lstReadyToMoveFrom = /ready_to_move/ ---->>>| ---->>>|
277
// | |
278
// m_lstReadyToMoveTo = /<final_destination>/ <<<----| |
279
// /<original_destination>/ <<<----|
280
// ===============================================================================
281
/**
282     * List of files that are ready to move FROM (sources).
283     * a.) This list will be used for commit if everything will be ok. Files will be moved
284     * from current 'ready_to_move' subdirectory into their final destination.
285     * b.) This list will be used for rollback if something fails. Files will be moved
286     * back from current 'ready_to_move' subdirectory to the original directory
287     * where they were stored before.
288     */

289    protected List JavaDoc m_lstReadyToMoveFrom;
290
291    /**
292     * List of files that are ready to move TO (destinations).
293     * a.) This list will be used for commit if everything will be ok. Files will be moved
294     * from current 'ready_to_move' subdirectory into their final destination. This
295     * list will stored these final destinations.
296     * b.) This list will be used for rollback if something fails. Files will be moved
297     * back from current 'ready_to_move' subdirectory to the original directory
298     * where they were stored before. List will stored original destinations.
299     */

300    protected List JavaDoc m_lstReadyToMoveTo;
301
302
303    // === Schema for ready to be replaced process ===================================
304
// LIST DIRECTORY COMMIT ROLLBACK
305
//--------------------------------------------------------------------------------
306
// m_lstReadyToBeReplacedFrom = /ready_to_be_replaced/ delete ---->>>|
307
// |
308
// m_lstReadyToBeReplacedTo = /<final_destination>/ <<<----|
309
// ===============================================================================
310
/**
311     * List of files that are ready to be replaced FROM (sources).
312     * a.) This list will be used for commit if everything will be ok. Files will be deleted
313     * from current 'ready_to_be_replaced' subdirectory.
314     * b.) This list will be used for rollback if something fails. Files will be renamed
315     * back from current 'ready_to_be_replaced' subdirectory to their original directory
316     * where they were stored before (their destinations are stored in the list
317     * m_lstReadyToBeReplacedTo).
318     */

319    protected List JavaDoc m_lstReadyToBeReplacedFrom;
320    
321    /**
322     * List of files that are ready to be replaced TO (destinations).
323     * This list will be used for rollback if something fails. Files will be renamed
324     * back from current 'ready_to_be_replaced' subdirectory to the original directory
325     * stored in this list.
326     */

327    protected List JavaDoc m_lstReadyToBeReplacedTo;
328
329
330    // === Schema for ready to replace process =======================================
331
// LIST DIRECTORY COMMIT ROLLBACK
332
//--------------------------------------------------------------------------------
333
// m_lstReadyToReplaceFrom = /ready_to_replace/ ---->>>| delete
334
// |
335
// m_lstReadyToReplaceTo = /<final_destination>/ <<<----|
336
// ===============================================================================
337
/**
338     * List of files that are ready to replace FROM (sources).
339     * a.) This list will be used for commit if everything will be ok. Files will be renamed
340     * from current 'ready_to_replace' subdirectory into their final destination.
341     * b.) This list will be used for rollback if something fails. Files will be deleted
342     * from current 'ready_to_replace' subdirectory.
343     */

344    protected List JavaDoc m_lstReadyToReplaceFrom;
345
346    /**
347     * List of files that are ready to replace TO (destinations).
348     * This list will be used for commit if everything will be ok. Files will be renamed
349     * from current 'ready_to_replace' subdirectory into their final destination. This
350     * list will stored these final destinations.
351     */

352    protected List JavaDoc m_lstReadyToReplaceTo;
353
354
355    // === Schema for ready to delete process ========================================
356
// LIST DIRECTORY COMMIT ROLLBACK
357
//--------------------------------------------------------------------------------
358
// m_lstReadyToDeleteFrom = /ready_to_delete/ delete ---->>>|
359
// |
360
// m_lstReadyToDeleteTo = /<final_destination>/ <<<----|
361
// ===============================================================================
362
/**
363     * List of files that are ready to delete FROM (sources).
364     * a.) This list will be used for commit if everything will be ok. Files will be deleted
365     * from current 'ready_to_delete' subdirectory.
366     * b.) This list will be used for rollback if something fails. Files will be renamed
367     * back from current 'ready_to_delete' subdirectory to the original directory
368     * where they were stored before.
369     */

370    protected List JavaDoc m_lstReadyToDeleteFrom;
371
372    /**
373     * List of files that are ready to delete TO (destinations).
374     * This list will be used for rollback if something fails. Files will be renamed
375     * back from current 'ready_to_delete' subdirectory to the original directory
376     * stored in this list.
377     */

378    protected List JavaDoc m_lstReadyToDeleteTo;
379
380    // Cached values ////////////////////////////////////////////////////////////
381

382    /**
383     * Logger for this class
384     */

385    private static Logger JavaDoc s_logger = Log.getInstance(FileCommitUtils.class);
386
387    /**
388     * How many times to retry file commit operations in case or error before we
389     * give up.
390     */

391    protected static int s_iFileCommitRetryCount;
392    
393    // Constructors /////////////////////////////////////////////////////////////
394

395    /**
396     * Static initializer.
397     */

398    static
399    {
400       // Read configuration parameters
401
Properties JavaDoc prpSettings;
402
403       prpSettings = Config.getInstance().getPropertiesSafely();
404       s_iFileCommitRetryCount = Config.getIntPropertyInRange(
405                                           prpSettings,
406                                           FILECOMMIT_RETRY_COUNT,
407                                           FILECOMMIT_RETRY_COUNT_DEFAULT,
408                                           "File commit retry count value",
409                                           0, // 0 is allowed since we try once regardless
410
Integer.MAX_VALUE);
411    }
412    
413    /**
414     * Empty constructor
415     */

416    public FileCommitUtils()
417    {
418       this(new Integer JavaDoc(STATUS_INITIAL), null, null, null, null, null, null, null, false);
419    }
420
421    /**
422     * Full constructor
423     *
424     * @param iReturnCode - error returning code
425     * @param lstCopyFromFiles - list of file and path names that will be copied FROM (sources)
426     * @param lstCopyToFiles - list of file and path names that will be copied TO (destination)
427     * @param lstMoveFromFiles - list of file and path names that will be moved FROM (sources)
428     * @param lstMoveToFiles - list of file and path names that will be moved TO (destination)
429     * @param lstReplaceFromFiles - list of file and path names that will be replaced FROM (sources)
430     * @param lstReplaceToFiles list of file and path names that will be replaced TO (destinations)
431     * @param lstDeleteFiles - list of file and path names that will be deleted
432     * @param bMovedFilesDeleteForRollback - flag signaling if moved files will be deleted from
433     * ready_to_move directory during rollback process.
434     * This flag will be TRUE if there will be moved files
435     * from temporary directory to the repository. If error
436     * will occured, there is not important move files back
437     * from ready_to_move directory to the temporary one.
438     */

439    public FileCommitUtils(
440       Integer JavaDoc iReturnCode,
441       List JavaDoc lstCopyFromFiles,
442       List JavaDoc lstCopyToFiles,
443       List JavaDoc lstMoveFromFiles,
444       List JavaDoc lstMoveToFiles,
445       List JavaDoc lstReplaceFromFiles,
446       List JavaDoc lstReplaceToFiles,
447       List JavaDoc lstDeleteFiles,
448       boolean bMovedFilesDeleteForRollback
449    )
450    {
451       super();
452       initGlobalLists();
453
454       this.m_iReturnCode = iReturnCode;
455       this.m_lstCopyFromFiles = lstCopyFromFiles != null ? lstCopyFromFiles
456                                                          : Collections.EMPTY_LIST;
457       this.m_lstCopyToFiles = lstCopyToFiles != null ? lstCopyToFiles
458                                                           : Collections.EMPTY_LIST;
459       this.m_lstMoveFromFiles = lstMoveFromFiles != null ? lstMoveFromFiles
460                                                             : Collections.EMPTY_LIST;
461       this.m_lstMoveToFiles = lstMoveToFiles != null ? lstMoveToFiles
462                                                           : Collections.EMPTY_LIST;
463       this.m_lstReplaceFromFiles = lstReplaceFromFiles != null ? lstReplaceFromFiles
464                                                                : Collections.EMPTY_LIST;
465       this.m_lstReplaceToFiles = lstReplaceToFiles != null ? lstReplaceToFiles
466                                                              : Collections.EMPTY_LIST;
467       this.m_lstDeleteFiles = lstDeleteFiles != null ? lstDeleteFiles
468                                                           : Collections.EMPTY_LIST;
469       this.m_bMovedFilesDeleteForRollback = bMovedFilesDeleteForRollback;
470       this.m_iStatus = FileCommitUtils.STATUS_INITIAL;
471    }
472
473    // main methods ////////////////////////////////////////////////
474

475    /**
476     * Method will prepare files to commit. It will copy, move, replace and delete files that
477     * were send form the controller into the ready_to_XXX directory. If something fails
478     * during this operation, there will be set up list for restoring changes back.
479     *
480     * @throws IOException - error occured during file moving
481     */

482    public void prepareToCommit(
483    ) throws IOException JavaDoc
484    {
485       // don't process this method when ReturnCode is null
486
if (this.m_iReturnCode != null)
487       {
488          StringBuffer JavaDoc sbTemp = new StringBuffer JavaDoc();
489          StringBuffer JavaDoc sbTemp1 = new StringBuffer JavaDoc();
490          StringBuffer JavaDoc sbTemp2 = new StringBuffer JavaDoc();
491    
492          TwoObjectStruct tosSource = null;
493          TwoObjectStruct tosDestination = null;
494          File JavaDoc fSourceFile = null;
495          File JavaDoc fSourceFileBackUp = null;
496          File JavaDoc fDestinationFile = null;
497          File JavaDoc fFinalDestinationFile = null;
498          File JavaDoc fDestinationFileBackUp = null;
499          File JavaDoc fPathTemp = null;
500    
501          // Initialize lists that will stored already prepared files to commit. These lists
502
// will be used for rollback when something will fail.
503
List JavaDoc lstReadyToCopyFilesFrom = new ArrayList JavaDoc(this.m_lstCopyToFiles.size());
504          List JavaDoc lstReadyToCopyFilesTo = new ArrayList JavaDoc(this.m_lstCopyToFiles.size());
505          List JavaDoc lstReadyToMoveFilesFrom = new ArrayList JavaDoc(this.m_lstMoveToFiles.size());
506          List JavaDoc lstReadyToMoveFilesTo = new ArrayList JavaDoc(this.m_lstMoveToFiles.size());
507          List JavaDoc lstReadyToMoveFilesToOriginal = new ArrayList JavaDoc(this.m_lstMoveToFiles.size());
508          List JavaDoc lstReadyToReplaceFilesFrom = new ArrayList JavaDoc(this.m_lstReplaceToFiles.size());
509          List JavaDoc lstReadyToReplaceFilesTo = new ArrayList JavaDoc(this.m_lstReplaceToFiles.size());
510          List JavaDoc lstReadyToBeReplacedFilesFrom = new ArrayList JavaDoc(this.m_lstReplaceToFiles.size());
511          List JavaDoc lstReadyToBeReplacedFilesTo = new ArrayList JavaDoc(this.m_lstReplaceToFiles.size());
512          List JavaDoc lstReadyToDeleteFilesFrom = new ArrayList JavaDoc(this.m_lstDeleteFiles.size());
513          List JavaDoc lstReadyToDeleteFilesTo = new ArrayList JavaDoc(this.m_lstDeleteFiles.size());
514          
515          Iterator JavaDoc itHelp1;
516          Iterator JavaDoc itHelp2;
517          
518          assert this.m_iStatus == FileCommitUtils.STATUS_INITIAL
519                 : "Cannot prepare file transaction to commit while " +
520                   "there is not initial status defined (file " +
521                   "transaction has been already started).";
522    
523          if (this.m_iStatus == FileCommitUtils.STATUS_INITIAL)
524          {
525             this.m_iStatus = FileCommitUtils.STATUS_PREPARE_TO_COMMIT;
526          }
527    
528          try
529          {
530             // copy processed files only if there was no error generated from the controller
531
if (this.m_iReturnCode == FileCommitUtils.RC_OK_OBJ)
532             {
533                // initialize list of global variables
534
initGlobalLists();
535       
536                // =========================================================================
537
// 1. COPY FILES
538
// -------------------------------------------------------------------------
539
// If there are files to be copied (from old version to the new one), create
540
// 'ready_to_copy' directory the files will be stored temporary in. If this
541
// will pass, we will known there is enough disk storage for new files
542
// =========================================================================
543
itHelp1 = this.m_lstCopyFromFiles.iterator();
544                itHelp2 = this.m_lstCopyToFiles.iterator();
545                while (itHelp1.hasNext() && itHelp2.hasNext())
546                {
547                   // get path name and file name of source and destination files
548
tosSource = (TwoObjectStruct) itHelp1.next();
549                   tosDestination = (TwoObjectStruct) itHelp2.next();
550                   
551                   // construct source file
552
sbTemp.delete(0, sbTemp.length());
553                   // check if there is path name present and if yes, append it
554
if (tosSource.getFirst() != null)
555                   {
556                      sbTemp.append((String JavaDoc) tosSource.getFirst());
557                   }
558                   sbTemp.append((String JavaDoc) tosSource.getSecond());
559                   fSourceFile = new File JavaDoc(sbTemp.toString());
560       
561                   // check if the source file already exists
562
if (fSourceFile.exists())
563                   {
564                      // construct destination ready_to_copy full path and than copy
565
// source file to this place
566
// also construct the final destination path
567
sbTemp.delete(0, sbTemp.length());
568                      sbTemp1.delete(0, sbTemp1.length());
569                      // check if there is path name presents and if yes, append it
570
if (tosDestination.getFirst() != null)
571                      {
572                         sbTemp.append((String JavaDoc) tosDestination.getFirst());
573                         sbTemp1.append((String JavaDoc) tosDestination.getFirst());
574                      }
575                      sbTemp.append(FILE_PATH_READY_TO_COPY);
576                      sbTemp.append(File.separator);
577                      
578                      // create destination 'ready_to_copy' directory
579
fPathTemp = new File JavaDoc(sbTemp.toString());
580                      
581                      try
582                      {
583                         // check if exists destination 'ready_to_copy' directory
584
if (!fPathTemp.exists())
585                         {
586                            // directory does not exist, so create new one
587
if (!fPathTemp.mkdirs())
588                            {
589                               // set global ready to copy variable that will be used
590
// for rollback method
591
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
592    
593                               // directory creation failed
594
throw new IOException JavaDoc("Error occured during creating of '"
595                                        + FILE_PATH_READY_TO_COPY + "' directory.");
596                            }
597                         }
598                      }
599                      catch (Throwable JavaDoc thr)
600                      {
601                         // set global ready to copy variable that will be used for rollback method
602
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
603    
604                         // directory creation failed
605
throw new IOException JavaDoc("Error occured during creating of '"
606                                   + FILE_PATH_READY_TO_COPY + "' directory. ");
607                      }
608       
609                      sbTemp.append((String JavaDoc) tosDestination.getSecond());
610                      // create full 'ready_to_copy' destination path
611
fDestinationFile = new File JavaDoc(sbTemp.toString());
612       
613                      sbTemp1.append((String JavaDoc) tosDestination.getSecond());
614                      // create full final destination path
615
fFinalDestinationFile = new File JavaDoc(sbTemp1.toString());
616       
617                      // Add 'ready_to_copy' source file to the list.
618
// a.) This list will be used for moving of already copied files to their
619
// destination directory if everything will be ok.This list stores sources.
620
// b.) This list will be used for deleting of already copied
621
// files if something will fail.
622
lstReadyToCopyFilesFrom.add(fDestinationFile);
623       
624                      // Add 'ready_to_copy' destination file to the list.
625
// This list will be used for moving of already copied files to their
626
// destination directory. This list stores final destinations.
627
lstReadyToCopyFilesTo.add(fFinalDestinationFile);
628       
629                      // !!! if next operation (FileUtils.moveFile) fail, it is possible we will have
630
// stored corrupted file in the ready_to_copy directory !!!
631

632                      try
633                      {
634                         // copy source file from the repository directory to
635
// 'ready_to_copy' destination
636
FileUtils.copyFile(fSourceFile, fDestinationFile);
637       
638                         // set global ready to copy variable that will be used for commit method
639
// commit will copy source files from 'ready_to_copy' directory to their
640
// final destinations
641
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
642                         this.m_lstReadyToCopyTo = lstReadyToCopyFilesTo;
643                      }
644                      catch (IOException JavaDoc ioExc)
645                      {
646                         // set global ready to copy variable that will be used for rollback method
647
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
648    
649                         throw new IOException JavaDoc("Error occured during copying of the file. ");
650                      }
651                      catch (OSSException bfExc)
652                      {
653                         // set global ready to copy variable that will be used for rollback method
654
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
655    
656                         throw new IOException JavaDoc("Error occured during copying of the file. ");
657                      }
658                   }
659                   else
660                   {
661                      // set global ready to copy variable that will be used for rollback method
662
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
663    
664                      // source file does not exist
665
throw new IOException JavaDoc("Source file that has to be copied does not exist.");
666                   }
667                }
668    
669                // =========================================================================
670
// 2. MOVE FILES
671
// -------------------------------------------------------------------------
672
// If there are files to be moved, create 'ready_to_move' directory the files
673
// will be stored temporary in. If this will pass, we will known there is
674
// enough disk storage for these files.
675
// =========================================================================
676
itHelp1 = this.m_lstMoveFromFiles.iterator();
677                itHelp2 = this.m_lstMoveToFiles.iterator();
678                while (itHelp1.hasNext() && itHelp2.hasNext())
679                {
680                   // get path name and file name of source and destination files
681
tosSource = (TwoObjectStruct) itHelp1.next();
682                   tosDestination = (TwoObjectStruct) itHelp2.next();
683                   
684                   // construct source file
685
sbTemp.delete(0, sbTemp.length());
686                   // check if there is path name present and if yes, append it
687
if (tosSource.getFirst() != null)
688                   {
689                      sbTemp.append((String JavaDoc) tosSource.getFirst());
690                   }
691                   sbTemp.append((String JavaDoc) tosSource.getSecond());
692                   fSourceFile = new File JavaDoc(sbTemp.toString());
693    
694                   // check if the source file already exists
695
if (fSourceFile.exists())
696                   {
697                      // construct destination ready_to_move full path and than move
698
// source file to this place
699
// also construct the final destination path
700
sbTemp.delete(0, sbTemp.length());
701                      sbTemp1.delete(0, sbTemp1.length());
702                      // check if there is path name presents and if yes, append it
703
if (tosDestination.getFirst() != null)
704                      {
705                         sbTemp.append((String JavaDoc) tosDestination.getFirst());
706                         sbTemp1.append((String JavaDoc) tosDestination.getFirst());
707                      }
708                      sbTemp.append(FILE_PATH_READY_TO_MOVE);
709                      sbTemp.append(File.separator);
710                      
711                      // create destination 'ready_to_move' directory
712
fPathTemp = new File JavaDoc(sbTemp.toString());
713                      
714                      try
715                      {
716                         // check if exists destination 'ready_to_move' directory
717
if (!fPathTemp.exists())
718                         {
719                            // directory does not exist, so create new one
720
if (!fPathTemp.mkdirs())
721                            {
722                               // set global ready to copy variable that will be used for rollback
723
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
724
725                               // set global ready to copy variable that will be used
726
// for rollback method
727
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
728    
729                               // directory creation failed
730
throw new IOException JavaDoc("Error occured during creating of '"
731                                        + FILE_PATH_READY_TO_MOVE + "' directory.");
732                            }
733                         }
734                      }
735                      catch (Throwable JavaDoc thr)
736                      {
737                         // set global ready to copy variable that will be used for rollback method
738
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
739
740                         // set global ready to move variable that will be used for rollback method
741
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
742    
743                         // directory creation failed
744
throw new IOException JavaDoc("Error occured during creating of '"
745                                   + FILE_PATH_READY_TO_MOVE + "' directory. ");
746                      }
747       
748                      sbTemp.append((String JavaDoc) tosDestination.getSecond());
749                      // create full 'ready_to_move' destination path
750
fDestinationFile = new File JavaDoc(sbTemp.toString());
751       
752                      sbTemp1.append((String JavaDoc) tosDestination.getSecond());
753                      // create full final destination path
754
fFinalDestinationFile = new File JavaDoc(sbTemp1.toString());
755    
756                      // test if there exists file with the same name in the final destination dir.
757
if (fFinalDestinationFile.exists())
758                      {
759                         // set global ready to copy variable that will be used for rollback method
760
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
761
762                         // set global ready to move variable that will be used for rollback method
763
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
764                         this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
765       
766                         // destination file with the same name already exist
767
throw new IOException JavaDoc("File that has to be moved already exist in the "
768                                                 + "destination directory.");
769                      }
770    
771                      // Add 'ready_to_move' source file to the list.
772
// a.) This list will be used for moving of already moved files to their
773
// destination directory if everything will be ok.This list stores sources.
774
// b.) This list will be used for moving back of already moved
775
// files if something will fail.
776
lstReadyToMoveFilesFrom.add(fDestinationFile);
777       
778                      // Add 'ready_to_move' destination file to the list.
779
// This list will be used for moving of already moved files to their
780
// destination directory if everything will be ok.
781
// This list stores final destinations.
782
lstReadyToMoveFilesTo.add(fFinalDestinationFile);
783                      
784                      // Add 'ready_to_move' destination file to the list.
785
// This list will be used for moving of already moved files to their
786
// original directory if something will fail.
787
// This list stores final destinations.
788
lstReadyToMoveFilesToOriginal.add(fSourceFile);
789       
790                      // !!! if next operation (FileUtils.moveFile) fail, it is possible we will have
791
// stored corrupted file in the ready_to_move directory !!!
792

793                      try
794                      {
795                         // move source file from the original dir. to 'ready_to_move' destination
796
FileUtils.moveFile(fSourceFile, fDestinationFile);
797       
798                         // set global ready to copy variable that will be used for commit method
799
// commit will move source files from 'ready_to_move' directory to their
800
// final destinations
801
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
802                         this.m_lstReadyToMoveTo = lstReadyToMoveFilesTo;
803                      }
804                      catch (IOException JavaDoc ioExc)
805                      {
806                         // set global ready to copy variable that will be used for rollback method
807
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
808
809                         // set global ready to move variable that will be used for rollback method
810
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
811                         this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
812    
813                         throw new IOException JavaDoc("Error occured during moving of the file. ");
814                      }
815                      catch (OSSException bfExc)
816                      {
817                         // set global ready to copy variable that will be used for rollback method
818
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
819
820                         // set global ready to move variable that will be used for rollback method
821
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
822                         this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
823    
824                         throw new IOException JavaDoc("Error occured during moving of the file. ");
825                      }
826                   }
827                   else
828                   {
829                      // set global ready to copy variable that will be used for rollback method
830
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
831
832                      // set global ready to move variable that will be used for rollback method
833
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
834                      this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
835    
836                      // source file does not exist
837
throw new IOException JavaDoc("Source file that has to be moved does not exist.");
838                   }
839                }
840    
841                // =========================================================================
842
// 3. REPLACE FILES
843
// -------------------------------------------------------------------------
844
// If there are files to be replaced (from source to destination), create
845
// 'ready_to_replace' directory the files will be stored temporary in. If this
846
// will pass, we will known there is enough disk storage for new files
847
// =========================================================================
848
itHelp1 = this.m_lstReplaceFromFiles.iterator();
849                itHelp2 = this.m_lstReplaceToFiles.iterator();
850                while (itHelp1.hasNext() && itHelp2.hasNext())
851                {
852                   // get path name and file name of source and destination files
853
tosSource = (TwoObjectStruct) itHelp1.next();
854                   tosDestination = (TwoObjectStruct) itHelp2.next();
855                   
856                   // construct source file
857
sbTemp.delete(0, sbTemp.length());
858                   // check if there is path name present and if yes, append it
859
if (tosSource.getFirst() != null)
860                   {
861                      sbTemp.append((String JavaDoc) tosSource.getFirst());
862                   }
863                   sbTemp.append((String JavaDoc) tosSource.getSecond());
864                   fSourceFile = new File JavaDoc(sbTemp.toString());
865       
866                   // construct source file that will be backed up
867
sbTemp.delete(0, sbTemp.length());
868                   // check if there is path name present and if yes, append it
869
if (tosDestination.getFirst() != null)
870                   {
871                      sbTemp.append((String JavaDoc) tosDestination.getFirst());
872                   }
873                   sbTemp.append((String JavaDoc) tosDestination.getSecond());
874                   fSourceFileBackUp = new File JavaDoc(sbTemp.toString());
875                   
876                   // check if the source files exist
877
if (fSourceFile.exists() && fSourceFileBackUp.exists())
878                   {
879                      // construct destination ready_to_replace full path and than copy
880
// source file to this place
881
// also construct destination ready_to_be_replaced full path and than rename
882
// file that will be replaced to this place (this will be used as backup of
883
// this file if something will fail)
884

885                      sbTemp.delete(0, sbTemp.length());
886                      sbTemp1.delete(0, sbTemp1.length());
887                      sbTemp2.delete(0, sbTemp2.length());
888                      // check if there is path name presents and if yes, append it
889
if (tosDestination.getFirst() != null)
890                      {
891                         sbTemp.append((String JavaDoc) tosDestination.getFirst());
892                         sbTemp1.append((String JavaDoc) tosDestination.getFirst());
893                         sbTemp2.append((String JavaDoc) tosDestination.getFirst());
894                      }
895                      sbTemp.append(FILE_PATH_READY_TO_REPLACE);
896                      sbTemp.append(File.separator);
897                      sbTemp1.append(FILE_PATH_READY_TO_BE_REPLACED);
898                      sbTemp1.append(File.separator);
899                      
900                      // create destination 'ready_to_replace' directory
901
fPathTemp = new File JavaDoc(sbTemp.toString());
902                      
903                      try
904                      {
905                         // check if exists destination 'ready_to_replace' directory
906
if (!fPathTemp.exists())
907                         {
908                            // directory does not exist, so create new one
909
if (!fPathTemp.mkdirs())
910                            {
911                               // set global ready to copy variable that will be used for rollback
912
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
913
914                               // set global ready to move variable that will be used for rollback
915
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
916                               this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
917
918                               // set global ready to replace and ready to be replaced variables
919
// that will be used for rollback method
920
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
921                               this.m_lstReadyToReplaceTo = lstReadyToReplaceFilesTo;
922                               this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
923                               this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
924    
925                               // directory creation failed
926
throw new IOException JavaDoc(
927                                     "Error occured during creating of '"
928                                     + FILE_PATH_READY_TO_REPLACE + "' directory.");
929                            }
930                         }
931                      }
932                      catch (Throwable JavaDoc thr)
933                      {
934                         // set global ready to copy variable that will be used for rollback method
935
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
936
937                         // set global ready to move variable that will be used for rollback method
938
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
939                         this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
940
941                         // set global ready to replace and ready to be replaced variables that
942
// will be used for rollback method
943
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
944                         this.m_lstReadyToReplaceTo = lstReadyToReplaceFilesTo;
945                         this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
946                         this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
947    
948                         // directory creation failed
949
IOException JavaDoc ioeExc = new IOException JavaDoc(
950                               "Error occured during creating of '"
951                               + FILE_PATH_READY_TO_REPLACE + "' directory.");
952                         ioeExc.initCause(thr);
953                         throw ioeExc;
954                      }
955       
956                      // create destination 'ready_to_be_replaced' directory
957
fPathTemp = new File JavaDoc(sbTemp1.toString());
958                      
959                      try
960                      {
961                         // check if exists destination 'ready_to_be_replaced' directory
962
if (!fPathTemp.exists())
963                         {
964                            // directory does not exist, so create new one
965
if (!fPathTemp.mkdirs())
966                            {
967                               // set global ready to copy variable that will be used for rollback
968
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
969
970                               // set global ready to move variable that will be used for rollback
971
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
972                               this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
973
974                               // set global ready to replace and ready to be replaced variables
975
// that will be used for rollback method
976
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
977                               this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
978                               this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
979    
980                               // directory creation failed
981
throw new IOException JavaDoc(
982                                     "Error occured during creating of '"
983                                     + FILE_PATH_READY_TO_BE_REPLACED + "' directory.");
984                            }
985                         }
986                      }
987                      catch (Throwable JavaDoc thr)
988                      {
989                         // set global ready to copy variable that will be used for rollback method
990
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
991
992                         // set global ready to move variable that will be used for rollback method
993
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
994                         this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
995
996                         // set global ready to replace and ready to be replaced variables that
997
// will be used for rollback method
998
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
999                         this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1000                        this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1001   
1002                        // directory creation failed
1003
IOException JavaDoc ioeExc = new IOException JavaDoc(
1004                                 "Error occured during creating of '"
1005                                 + FILE_PATH_READY_TO_BE_REPLACED + "' directory.");
1006                        ioeExc.initCause(thr);
1007                        throw ioeExc;
1008                     }
1009      
1010                     sbTemp.append((String JavaDoc) tosSource.getSecond());
1011                     sbTemp1.append((String JavaDoc) tosDestination.getSecond());
1012                     sbTemp2.append((String JavaDoc) tosSource.getSecond());
1013      
1014                     // create full 'ready_to_replace' destination path
1015
fDestinationFile = new File JavaDoc(sbTemp.toString());
1016      
1017                     // create final destination path of the replace file
1018
fFinalDestinationFile = new File JavaDoc(sbTemp2.toString());
1019      
1020                     // create full 'ready_to_be_replaced' destination path
1021
fDestinationFileBackUp = new File JavaDoc(sbTemp1.toString());
1022      
1023                     // Add 'ready_to_replace' source file to the list.
1024
// a.) This list will be used for renaming already renamed files to their
1025
// destination directory if everything will be ok.This list stores sources.
1026
// b.) This list will be used for deleting of already renamed
1027
// files if something will fail.
1028
lstReadyToReplaceFilesFrom.add(fDestinationFile);
1029      
1030                     // Add 'ready_to_replace' destination file to the list.
1031
// This list will be used for renaming already renamed files to their
1032
// destination directory if everything will be ok.
1033
// This list stores destinations.
1034
lstReadyToReplaceFilesTo.add(fFinalDestinationFile);
1035      
1036                     // !!! if next operation (FileUtils.moveFile) fail, it is possible we will have
1037
// stored corrupted file in the ready_to_replace directory !!!
1038

1039                     try
1040                     {
1041                        // move source file from the repository directory
1042
// to 'ready_to_replace' destination
1043
FileUtils.moveFile(fSourceFile, fDestinationFile);
1044                     }
1045                     catch (IOException JavaDoc ioExc)
1046                     {
1047                        // set global ready to copy variable that will be used for rollback method
1048
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1049
1050                        // set global ready to move variable that will be used for rollback method
1051
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1052                        this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1053
1054                        // set global ready to replace and ready to be replaced variables that
1055
// will be used for rollback method
1056
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1057                        this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1058                        this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1059   
1060                        IOException JavaDoc ioeExc = new IOException JavaDoc(
1061                                 "Error occured during copying of the file.");
1062                        ioeExc.initCause(ioExc);
1063                        throw ioeExc;
1064                     }
1065                     catch (OSSException bfExc)
1066                     {
1067                        // set global ready to copy variable that will be used for rollback method
1068
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1069
1070                        // set global ready to move variable that will be used for rollback method
1071
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1072                        this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1073
1074                        // set global ready to replace and ready to be replaced variables that
1075
// will be used for rollback method
1076
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1077                        this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1078                        this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1079   
1080                        IOException JavaDoc ioeExc = new IOException JavaDoc(
1081                                 "Error occured during copying of the file.");
1082                        ioeExc.initCause(bfExc);
1083                        throw ioeExc;
1084                     }
1085      
1086                     try
1087                     {
1088                        // rename file that will be replaced into the 'ready_to_be_replaced' dir.
1089
// we know that we are at the same filesystem and we can use renameTo method
1090
if (fSourceFileBackUp.renameTo(fDestinationFileBackUp))
1091                        {
1092                           // If renameTo will pass, there will be added just renamed file to the
1093
// list of TO BE RENAMED files that will be used for rollback method if
1094
// it will fail later. If it will fail now, it is not neccessary to add
1095
// this file to the particular list, bacause there will be not file
1096
// renamed and it will stay on the same place.
1097

1098                           // Add 'ready_to_be_replaced' file to the list.
1099
// a.) This list will be used for deleting of files
1100
// if everything will be ok.
1101
// b.) This list will be used for undo mode
1102
// (rollback method) - renaming back
1103
// files if something will fail. In this list will be stored sources
1104
// for undo mode.
1105
lstReadyToBeReplacedFilesFrom.add(fDestinationFileBackUp);
1106            
1107                           // Add 'ready_to_be_replaced' file to the list. This list will be used
1108
// for undo mode (rollback method) - renaming back files if something
1109
// will fail. In this list will be stored destinations for undo mode.
1110
lstReadyToBeReplacedFilesTo.add(fSourceFileBackUp);
1111      
1112                           // set global ready to replace and ready to be replaced variables that
1113
// will be used for commit method
1114
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1115                           this.m_lstReadyToReplaceTo = lstReadyToReplaceFilesTo;
1116                           this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1117                           this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1118                        }
1119                        else
1120                        {
1121                           // set global ready to copy variable that will be used for rollback
1122
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1123
1124                           // set global ready to move variable that will be used for rollback
1125
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1126                           this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1127
1128                           // set global ready to replace and ready to be replaced variables that
1129
// will be used for rollback method
1130
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1131                           this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1132                           this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1133   
1134                           throw new IOException JavaDoc("Error occured during file renaming.");
1135                        }
1136                     }
1137                     catch (Throwable JavaDoc thr)
1138                     {
1139                        // set global ready to copy variable that will be used for rollback method
1140
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1141
1142                        // set global ready to move variable that will be used for rollback method
1143
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1144                        this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1145
1146                        // set global ready to replace and ready to be replaced variables that
1147
// will be used for rollback method
1148
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1149                        this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1150                        this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1151   
1152                        IOException JavaDoc ioeExc = new IOException JavaDoc(
1153                                       "Error occured during file renaming.");
1154                        ioeExc.initCause(thr);
1155                        throw ioeExc;
1156                     }
1157                  }
1158                  else
1159                  {
1160                     // set global ready to copy variable that will be used for rollback method
1161
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1162
1163                     // set global ready to move variable that will be used for rollback method
1164
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1165                     this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1166
1167                     // set global ready to replace and ready to be replaced variables that
1168
// will be used for rollback method
1169
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1170                     this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1171                     this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1172   
1173                     // source file does not exist
1174
throw new IOException JavaDoc("Source file that has to be replaced does not exist.");
1175                  }
1176               }
1177      
1178               // =========================================================================
1179
// 4. DELETE FILES
1180
// -------------------------------------------------------------------------
1181
// If there are files to be deleted , create 'ready_to_delete' directory
1182
// the files will be stored temporary in. If this will pass, we can delete
1183
// these files from 'ready_to_delete' directory.
1184
// =========================================================================
1185
itHelp1 = this.m_lstDeleteFiles.iterator();
1186               while (itHelp1.hasNext())
1187               {
1188                  // get path name and file name of source and destination files
1189
tosSource = (TwoObjectStruct) itHelp1.next();
1190                  
1191                  // construct source file
1192
sbTemp.delete(0, sbTemp.length());
1193                  // check if there is path name present and if yes, append it
1194
if (tosSource.getFirst() != null)
1195                  {
1196                     sbTemp.append((String JavaDoc) tosSource.getFirst());
1197                  }
1198                  sbTemp.append((String JavaDoc) tosSource.getSecond());
1199                  fSourceFile = new File JavaDoc(sbTemp.toString());
1200      
1201                  // check if the source file already exists
1202
if (fSourceFile.exists())
1203                  {
1204                     // construct destination ready_to_delete full path and than rename
1205
// source file to this place. We can use renameTo method because we
1206
// are at the same file system.
1207

1208                     sbTemp.delete(0, sbTemp.length());
1209                     // check if there is path name presents and if yes, append it
1210
if (tosSource.getFirst() != null)
1211                     {
1212                        sbTemp.append((String JavaDoc) tosSource.getFirst());
1213                     }
1214                     sbTemp.append(FILE_PATH_READY_TO_DELETE);
1215                     sbTemp.append(File.separator);
1216                     
1217                     // create destination 'ready_to_delete' directory
1218
fPathTemp = new File JavaDoc(sbTemp.toString());
1219                     // check if exists destination 'ready_to_delete' directory
1220
if (!fPathTemp.exists())
1221                     {
1222                        try
1223                        {
1224                           // directory does not exist, so create new one
1225
if (!fPathTemp.mkdirs())
1226                           {
1227                              // set global ready to copy variable that will be used for rollback
1228
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1229
1230                              // set global ready to move variable that will be used for rollback
1231
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1232                              this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1233         
1234                              // set global ready to replace and ready to be replaced variables that
1235
// will be used for rollback method
1236
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1237                              this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1238                              this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1239
1240                              // set global ready to delete variables that will be used
1241
// for rollback method
1242
this.m_lstReadyToDeleteFrom = lstReadyToDeleteFilesFrom;
1243                              this.m_lstReadyToDeleteTo = lstReadyToDeleteFilesTo;
1244   
1245                              // directory creation failed
1246
throw new IOException JavaDoc(
1247                                    "Error occured during creating of '"
1248                                    + FILE_PATH_READY_TO_DELETE + "' directory.");
1249                           }
1250                        }
1251                        catch (Throwable JavaDoc thr)
1252                        {
1253                           // set global ready to copy variable that will be used for rollback
1254
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1255
1256                           // set global ready to move variable that will be used for rollback
1257
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1258                           this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1259      
1260                           // set global ready to replace and ready to be replaced variables that
1261
// will be used for rollback method
1262
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1263                           this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1264                           this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1265
1266                           // set global ready to delete variables that will be used
1267
// for rollback method
1268
this.m_lstReadyToDeleteFrom = lstReadyToDeleteFilesFrom;
1269                           this.m_lstReadyToDeleteTo = lstReadyToDeleteFilesTo;
1270   
1271                           // directory creation failed
1272
IOException JavaDoc ioeExc = new IOException JavaDoc(
1273                                    "Error occured during creating of '"
1274                                    + FILE_PATH_READY_TO_DELETE + "' directory.");
1275                           ioeExc.initCause(thr);
1276                           throw ioeExc;
1277                        }
1278                     }
1279   
1280                     sbTemp.append((String JavaDoc) tosSource.getSecond());
1281      
1282                     // create full 'ready_to_delete' destination path
1283
fSourceFileBackUp = new File JavaDoc(sbTemp.toString());
1284      
1285                     try
1286                     {
1287                        // rename file that will be replaced into the 'ready_to_delete' directory
1288
// we know that we are at the same filesystem and we can use renameTo method
1289
if (fSourceFile.renameTo(fSourceFileBackUp))
1290                        {
1291                           // Add 'ready_to_delete' source file to the list.
1292
// a.) This list of source files will be used for deleting of files if
1293
// everything will be ok.
1294
// b.) This list of source files will be used to rollback method - undo
1295
// delete mode if something will fail.
1296
lstReadyToDeleteFilesFrom.add(fSourceFileBackUp);
1297            
1298                           // Add 'ready_to_delete' destination file to the list.
1299
// This list of destination files will be used to rollback method - undo
1300
// delete mode if something will fail.
1301
lstReadyToDeleteFilesTo.add(fSourceFile);
1302      
1303                           // set global ready to delete variables that will be used for commit
1304
// method
1305
this.m_lstReadyToDeleteFrom = lstReadyToDeleteFilesFrom;
1306                        }
1307                        else
1308                        {
1309                           // set global ready to copy variable that will be used for rollback
1310
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1311
1312                           // set global ready to move variable that will be used for rollback
1313
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1314                           this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1315      
1316                           // set global ready to replace and ready to be replaced variables that
1317
// will be used for rollback method
1318
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1319                           this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1320                           this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1321
1322                           // set global ready to delete variables that will be used
1323
// for rollback method
1324
this.m_lstReadyToDeleteFrom = lstReadyToDeleteFilesFrom;
1325                           this.m_lstReadyToDeleteTo = lstReadyToDeleteFilesTo;
1326   
1327                           throw new IOException JavaDoc("Error occured during file renaming.");
1328                        }
1329                     }
1330                     catch (Throwable JavaDoc thr)
1331                     {
1332                        // set global ready to copy variable that will be used for rollback method
1333
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1334
1335                        // set global ready to move variable that will be used for rollback method
1336
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1337                        this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1338   
1339                        // set global ready to replace and ready to be replaced variables that
1340
// will be used for rollback method
1341
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1342                        this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1343                        this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1344
1345                        // set global ready to delete variables that will be used for rollback
1346
// method
1347
this.m_lstReadyToDeleteFrom = lstReadyToDeleteFilesFrom;
1348                        this.m_lstReadyToDeleteTo = lstReadyToDeleteFilesTo;
1349      
1350                        IOException JavaDoc ioeExc = new IOException JavaDoc(
1351                                 "Error occured during file renaming.");
1352                        ioeExc.initCause(thr);
1353                        throw ioeExc;
1354                     }
1355                  }
1356                  else
1357                  {
1358                     // if file does not exist nothing will happend and delete process
1359
// will continue. There will be not thrown exception because delete
1360
// process should be successfull also in case if files don't exist.
1361
/*
1362                     // set global ready to copy variable that will be used for rollback method
1363                     this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1364
1365                     // set global ready to move variable that will be used for rollback method
1366                     this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1367                     this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1368
1369                     // set global ready to replace and ready to be replaced variables that
1370                     // will be used for rollback method
1371                     this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1372                     this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1373                     this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1374
1375                     // set global ready to delete variables that will be used for rollback method
1376                     this.m_lstReadyToDeleteFrom = lstReadyToDeleteFilesFrom;
1377                     this.m_lstReadyToDeleteTo = lstReadyToDeleteFilesTo;
1378   
1379                     // source file does not exist
1380                     throw new IOException("Source file that has to be deleted does not exist.");
1381*/

1382                  }
1383               }
1384            }
1385            else
1386            {
1387               // there was send error code here so get error message
1388
if (this.m_iReturnCode == FileCommitUtils.RC_FILE_ERR_OBJ)
1389               {
1390                  throw new IOException JavaDoc("There was an error occured during image processing.");
1391               }
1392            }
1393         }
1394         catch (IOException JavaDoc ioExc)
1395         {
1396            // throw internal IOException (this was already occured by our internal try/catch)
1397
throw ioExc;
1398         }
1399         catch (Throwable JavaDoc thrExc)
1400         {
1401            // There was occured another exception as our internal IOException. We have to
1402
// set up lists of already processed files that will be send to the rollback method.
1403

1404            // set global ready to copy variable that will be used for rollback method
1405
this.m_lstReadyToCopyFrom = lstReadyToCopyFilesFrom;
1406   
1407            // set global ready to copy variable that will be used for commit method
1408
// commit will move source files from 'ready_to_move' directory to their
1409
// final destinations
1410
this.m_lstReadyToMoveFrom = lstReadyToMoveFilesFrom;
1411            this.m_lstReadyToMoveTo = lstReadyToMoveFilesToOriginal;
1412   
1413            // set global ready to replace and ready to be replaced variables that
1414
// will be used for rollback method
1415
this.m_lstReadyToReplaceFrom = lstReadyToReplaceFilesFrom;
1416            this.m_lstReadyToReplaceTo = lstReadyToReplaceFilesTo;
1417            this.m_lstReadyToBeReplacedFrom = lstReadyToBeReplacedFilesFrom;
1418            this.m_lstReadyToBeReplacedTo = lstReadyToBeReplacedFilesTo;
1419   
1420            // set global ready to delete variables that will be used for rollback method
1421
this.m_lstReadyToDeleteFrom = lstReadyToDeleteFilesFrom;
1422            this.m_lstReadyToDeleteTo = lstReadyToDeleteFilesTo;
1423   
1424            IOException JavaDoc ioeExc = new IOException JavaDoc(
1425               "Unspecified error has occured while preparing files to commit.");
1426            ioeExc.initCause(thrExc);
1427            throw ioeExc;
1428         }
1429      }
1430   }
1431
1432   /**
1433    * Method will commit files. It will renameTo files stored in the ready_to_XXX
1434    * directory into the final destination. If something fails during this operation,
1435    * it will repeat iRetryCounter times (after a sleep).
1436    *
1437    * @throws OSSException - error occured during files commiting
1438    */

1439   public void commit(
1440   ) throws OSSException
1441   {
1442      // don't process this method when ReturnCode is null
1443
if (this.m_iReturnCode != null)
1444      {
1445         int iRetryCounter = 0;
1446   
1447         if (this.m_iStatus == FileCommitUtils.STATUS_PREPARE_TO_COMMIT)
1448         {
1449            this.m_iStatus = FileCommitUtils.STATUS_COMMIT;
1450         }
1451         else
1452         {
1453            throw new OSSInternalErrorException("Cannot commit file transaction while " +
1454                                               "there is not prepare to commit status defined.");
1455         }
1456   
1457         // Process commit method while there are any not processed requested
1458
// files but repeat this process max. (iRetryCounter + 1 (for the first
1459
// time)) times, thats why we use <=
1460
while (((this.m_lstReadyToCopyFrom.size() > 0)
1461                 || (this.m_lstReadyToMoveFrom.size() > 0)
1462                 || (this.m_lstReadyToReplaceFrom.size() > 0)
1463                 || (this.m_lstReadyToBeReplacedFrom.size() > 0)
1464                 || (this.m_lstReadyToDeleteFrom.size() > 0))
1465                && (iRetryCounter <= s_iFileCommitRetryCount))
1466         {
1467            if (iRetryCounter > 0)
1468            {
1469               // There was an error occured. Wait for a few seconds and try again
1470
sleep();
1471            }
1472            
1473            try
1474            {
1475               if (GlobalConstants.ERROR_CHECKING)
1476               {
1477                  assert this.m_lstReadyToCopyFrom.size() == this.m_lstReadyToCopyTo.size()
1478                           : "Size of the source and destination list of files that " +
1479                             "will be copied has to be the same.";
1480                  assert this.m_lstReadyToMoveFrom.size() == this.m_lstReadyToMoveTo.size()
1481                           : "Size of the source and destination list of files that " +
1482                             "will be moved has to be the same.";
1483                  assert this.m_lstReadyToReplaceFrom.size() == this.m_lstReadyToReplaceTo.size()
1484                           : "Size of the source and destination list of files that " +
1485                             "will be replaced has to be the same.";
1486                  assert this.m_lstReadyToBeReplacedFrom.size()
1487                              == this.m_lstReadyToBeReplacedTo.size()
1488                           : "Size of the source and destination list of files that " +
1489                             "will be replaced has to be the same.";
1490               }
1491         
1492               // At this point we known that we heave prepared files for final commiting. We are
1493
// in the same file system so we can use renameTo method for all files.
1494

1495               // =========================================================================
1496
// 1. COPY FILES (rename files that have to be copied)
1497
// -------------------------------------------------------------------------
1498
// If there are prepared files to be copied (from old version to the new one),
1499
// rename them from the 'ready_to_copy' directory to their final destination. If
1500
// this will pass, list m_lstReadyToCopyFrom will be empty. If this will fail,
1501
// list m_lstReadyToCopyFrom will be not empty and whole process will repeat
1502
// later (after sleeping for a time).
1503
// =========================================================================
1504
doRenameFiles(this.m_lstReadyToCopyFrom,
1505                             this.m_lstReadyToCopyTo,
1506                             SENDER_COMMIT,
1507                             LIST_IDENTIFICATOR_COPY);
1508   
1509               // =========================================================================
1510
// 2. MOVE FILES (rename files that have to be moved)
1511
// -------------------------------------------------------------------------
1512
// If there are prepared files to be moved, rename them from the 'ready_to_move'
1513
// directory to their final destination. If this will pass, list
1514
// m_lstReadyToMoveFrom will be empty. If this will fail, list
1515
// m_lstReadyToMoveFrom will be not empty and whole process will repeat
1516
// later (after sleeping for a time).
1517
// =========================================================================
1518
doRenameFiles(this.m_lstReadyToMoveFrom,
1519                             this.m_lstReadyToMoveTo,
1520                             SENDER_COMMIT,
1521                             LIST_IDENTIFICATOR_MOVE);
1522         
1523               // =========================================================================
1524
// 3. REPLACE FILES (replace files that have to be replaced)
1525
// -------------------------------------------------------------------------
1526
// If there are prepared files to be replaced, rename them from 'ready_to_replace'
1527
// directory to their final destination. If this will pass, list
1528
// m_lstReadyToReplaceFrom will be empty. If this will fail, list
1529
// m_lstReadyToReplaceFrom will be not empty and whole process will repeat
1530
// later (after sleeping for a time).
1531
// =========================================================================
1532
doRenameFiles(this.m_lstReadyToReplaceFrom,
1533                             this.m_lstReadyToReplaceTo,
1534                             SENDER_COMMIT,
1535                             LIST_IDENTIFICATOR_REPLACE);
1536   
1537               // =========================================================================
1538
// 4. DELETE FILES
1539
// -------------------------------------------------------------------------
1540
// Delete files from 'ready_to_delete' and from 'ready_to_be_replaced'
1541
// directories. If this will pass, lists m_lstReadyToDeleteFrom and
1542
// m_lstReadyToBeReplacedFrom will be empty. If this will fail, one or both
1543
// specified list will be not empty and whole process will repeat later
1544
// (after sleeping for a time).
1545
// =========================================================================
1546
doDeleteFiles(this.m_lstReadyToBeReplacedFrom,
1547                             SENDER_COMMIT,
1548                             LIST_IDENTIFICATOR_TO_BE_REPLACED);
1549   
1550               doDeleteFiles(this.m_lstReadyToDeleteFrom,
1551                             SENDER_COMMIT,
1552                             LIST_IDENTIFICATOR_DELETE);
1553
1554            }
1555            catch (Throwable JavaDoc thrExc)
1556            {
1557               s_logger.log(Level.WARNING,
1558                            "An error has occured during file commit", thrExc);
1559            }
1560            finally
1561            {
1562               iRetryCounter++;
1563            }
1564         }
1565      }
1566   }
1567
1568   /**
1569    * Method to rollback files. It will rename back already collocated files.
1570    * (renameTo) files stored in the ready_to_XXX directory into the final
1571    * destination. If something fails during this operation, it will be
1572    * continue and there will be memoried files that failed. RenameTo process
1573    * for failed files will repeat later (after a sleep).
1574    *
1575    * @throws OSSException - error occured during files commiting
1576    */

1577   public void rollback(
1578   ) throws OSSException
1579   {
1580      // don't process this method when ReturnCode is null
1581
if (this.m_iReturnCode != null)
1582      {
1583         int iRetryCounter = 0;
1584   
1585         if (this.m_iStatus == FileCommitUtils.STATUS_PREPARE_TO_COMMIT)
1586         {
1587            this.m_iStatus = FileCommitUtils.STATUS_ROLLBACK;
1588         }
1589         else
1590         {
1591            throw new OSSInternalErrorException(
1592                         "Cannot prepare file transaction to rollback while "
1593                         + "there is not prepare to commit status defined.");
1594         }
1595   
1596         // Process rollback method while there are any not processed requested
1597
// files but repeat this process max. (iRetryCounter + 1 (for the first
1598
// time)) times, thats why we use <=
1599
while (((this.m_lstReadyToBeReplacedFrom.size() > 0)
1600                 || (this.m_lstReadyToMoveFrom.size() > 0)
1601                 || (this.m_lstReadyToDeleteFrom.size() > 0)
1602                 || (this.m_lstReadyToCopyFrom.size() > 0)
1603                 || (this.m_lstReadyToReplaceFrom.size() > 0))
1604                && (iRetryCounter <= s_iFileCommitRetryCount))
1605         {
1606            if (iRetryCounter > 0)
1607            {
1608               // There was an error occured. Wait for a few seconds and try again
1609
sleep();
1610            }
1611            
1612            try
1613            {
1614               // At this point we known that preparing of files generated exception.
1615
// Files already copied or renamed should be deleted. There was no file
1616
// renamed to the final destination directory, so we have to delete just
1617
// files already renamed to the 'ready_to_XXX' subdirectory. We don't
1618
// care about files that are still in the repository (temp) directory.
1619

1620               // =========================================================================
1621
// 1. ROLLBACK READY TO BE REPLACED FILES
1622
// =========================================================================
1623
// rename files back from the 'ready_to_be_replaced' to the original destination
1624
doRenameFiles(this.m_lstReadyToBeReplacedFrom,
1625                             this.m_lstReadyToBeReplacedTo,
1626                             SENDER_ROLLBACK,
1627                             LIST_IDENTIFICATOR_TO_BE_REPLACED);
1628         
1629               // =========================================================================
1630
// 2. ROLLBACK READY TO DELETE FILES
1631
// =========================================================================
1632
// rename files back from the 'ready_to_delete' to the original destination
1633
doRenameFiles(this.m_lstReadyToDeleteFrom,
1634                             this.m_lstReadyToDeleteTo,
1635                             SENDER_ROLLBACK,
1636                             LIST_IDENTIFICATOR_DELETE);
1637         
1638               // =========================================================================
1639
// 3. ROLLBACK READY TO MOVE FILES
1640
// =========================================================================
1641
if (this.m_bMovedFilesDeleteForRollback)
1642               {
1643                  // if there is not important to move files from 'ready_to_move' directory
1644
// to the destination one, just delete moved files (for example there is not
1645
// important to move back files from 'ready_to_move' directory to the temporary
1646
// directory).
1647
doDeleteFiles(this.m_lstReadyToMoveFrom,
1648                                SENDER_ROLLBACK,
1649                                LIST_IDENTIFICATOR_MOVE);
1650               }
1651               else
1652               {
1653                  // move files back from the 'ready_to_move' to the original destination
1654
// We have to move these since they might be at different file system
1655
doMoveFiles(this.m_lstReadyToMoveFrom,
1656                              this.m_lstReadyToMoveTo,
1657                              SENDER_ROLLBACK,
1658                              LIST_IDENTIFICATOR_MOVE);
1659               }
1660   
1661               // =========================================================================
1662
// 4. ROLLBACK READY TO COPY FILES
1663
// =========================================================================
1664
// delete files from the 'ready_to_copy' directory
1665
doDeleteFiles(this.m_lstReadyToCopyFrom,
1666                             SENDER_ROLLBACK,
1667                             LIST_IDENTIFICATOR_COPY);
1668   
1669               // =========================================================================
1670
// 5. ROLLBACK READY TO REPLACE FILES
1671
// =========================================================================
1672
// delete files from the 'ready_to_replace' directory
1673
doDeleteFiles(this.m_lstReadyToReplaceFrom,
1674                             SENDER_ROLLBACK,
1675                             LIST_IDENTIFICATOR_REPLACE);
1676            }
1677            catch (Throwable JavaDoc thrExc)
1678            {
1679               s_logger.log(Level.WARNING,
1680                                   "An error has occured during file rollback", thrExc);
1681            }
1682            iRetryCounter++;
1683         }
1684      }
1685   }
1686
1687   /**
1688    *
1689    * @return int
1690    */

1691   public int getStatus()
1692   {
1693      return m_iStatus;
1694   }
1695
1696   // helper methods ////////////////////////////////////////////////
1697

1698   /**
1699    * Method that will rename source files to destination ones. This uses
1700    * renameTo() method - we are sure that the source and destination files
1701    * will be on the same file system.
1702    *
1703    * @param lstSources - list of source files
1704    * @param lstDestinations - list of destination files
1705    * @param iSenderIdentificator - identificator of the sender - commit/rollback.
1706    * @param iListIdentificator - identificator of the list. By this identificator we can decide
1707    * deleting processed item from the particular global list.
1708    */

1709   private void doRenameFiles(
1710      List JavaDoc lstSources,
1711      List JavaDoc lstDestinations,
1712      int iSenderIdentificator,
1713      int iListIdentificator
1714   )
1715   {
1716      File JavaDoc fSourceFile;
1717      File JavaDoc fDestinationFile;
1718      
1719      // create copy of the source and destination list
1720
List JavaDoc lstSourcesCopy = new ArrayList JavaDoc(lstSources);
1721      List JavaDoc lstDestinationsCopy = new ArrayList JavaDoc(lstDestinations);
1722
1723      Iterator JavaDoc itHelp1;
1724      Iterator JavaDoc itHelp2;
1725      
1726      int iErrorIndexPosition = 0;
1727
1728      if (GlobalConstants.ERROR_CHECKING)
1729      {
1730         assert lstSourcesCopy.size() == lstDestinationsCopy.size()
1731                  : "Size of the source and destination list of files that " +
1732                    "will be renamed has to be the same.";
1733      }
1734
1735      if (lstSourcesCopy.size() > 0)
1736      {
1737         
1738         itHelp1 = lstSourcesCopy.iterator();
1739         itHelp2 = lstDestinationsCopy.iterator();
1740         while (itHelp1.hasNext() && itHelp2.hasNext())
1741         {
1742            // get source and destination file to be renamed back
1743
fSourceFile = (File JavaDoc) itHelp1.next();
1744            fDestinationFile = (File JavaDoc) itHelp2.next();
1745   
1746            if (fSourceFile.exists())
1747            {
1748               if (fDestinationFile.exists())
1749               {
1750                  // Since the destination file already exists (e.g. it couldn't
1751
// be removed because of permissions), remove the source
1752
// This should never happen during commit (maybe we should check
1753
// that this is really rollback) since for commit the flag
1754
// should be replace and then the file to be replaced is moved
1755
// away. If this is rollback then the destination (which is the
1756
// real original) may be there and in that case we want to remove
1757
// the copy
1758
s_logger.finest("Destination file "
1759                                           + fDestinationFile.getAbsolutePath() +
1760                                           " already exists, removing the source file " +
1761                                           fSourceFile.getAbsolutePath());
1762                  if (!fSourceFile.delete())
1763                  {
1764                     // set position of the file in the list the error was occured on
1765
iErrorIndexPosition++;
1766                     // log this failure
1767
s_logger.log(Level.WARNING,
1768                                         "Cannot delete file " + fSourceFile.getAbsolutePath() +
1769                                         " since the destination file already exists.");
1770                  }
1771                  else
1772                  {
1773                     // file was successfully deleted, so remove it from the global lists
1774
removeDeletedFileFromGlobalLists(iSenderIdentificator,
1775                                                      iListIdentificator,
1776                                                      iErrorIndexPosition);
1777                  }
1778               }
1779               else
1780               {
1781                  try
1782                  {
1783                     if (!fSourceFile.renameTo(fDestinationFile))
1784                     {
1785                        // set position of the file in the list the error was occured on
1786
iErrorIndexPosition++;
1787                        // log this failure
1788
s_logger.log(Level.WARNING,
1789                                 "Cannot rename file from " + fSourceFile.getAbsolutePath()
1790                                 + " to " + fDestinationFile.getAbsolutePath());
1791                     }
1792                     else
1793                     {
1794                        // file was successfully renamed, so remove it from the global lists
1795
removeRenamedFileFromGlobalLists(iSenderIdentificator,
1796                                                         iListIdentificator,
1797                                                         iErrorIndexPosition);
1798                     }
1799                  }
1800                  catch (Throwable JavaDoc thr)
1801                  {
1802                     // set position of the file in the list the error was occured on
1803
iErrorIndexPosition++;
1804                     // log this failure
1805
s_logger.log(Level.WARNING,
1806                                         "Cannot rename file from " + fSourceFile.getAbsolutePath()
1807                                         + " to " + fDestinationFile.getAbsolutePath(), thr);
1808                  }
1809               }
1810            }
1811            else
1812            {
1813               // Source file does not exist. This case should never became, because we have
1814
// already prepared files in private ready_to_XXX directory. Now we cannot
1815
// do anything because we are already doing commit or rollback. We shold just
1816
// remove this file from the global lists.
1817
removeRenamedFileFromGlobalLists(iSenderIdentificator,
1818                                                iListIdentificator,
1819                                                iErrorIndexPosition);
1820            }
1821         }
1822      }
1823   }
1824
1825   /**
1826    * Method that will move source files to destination ones. This uses
1827    * move() method since we can be going across file systems. For example during
1828    * rollback we are returning files back to their origin which can be at
1829    * different file system.
1830    *
1831    * @param lstSources - list of source files
1832    * @param lstDestinations - list of destination files
1833    * @param iSenderIdentificator - identificator of the sender - commit/rollback.
1834    * @param iListIdentificator - identificator of the list. By this identificator we can decide
1835    * deleting processed item from the particular global list.
1836    */

1837   private void doMoveFiles(
1838      List JavaDoc lstSources,
1839      List JavaDoc lstDestinations,
1840      int iSenderIdentificator,
1841      int iListIdentificator
1842   )
1843   {
1844      File JavaDoc fSourceFile;
1845      File JavaDoc fDestinationFile;
1846      
1847      // create copy of the source and destination list
1848
List JavaDoc lstSourcesCopy = new ArrayList JavaDoc(lstSources);
1849      List JavaDoc lstDestinationsCopy = new ArrayList JavaDoc(lstDestinations);
1850
1851      Iterator JavaDoc itHelp1;
1852      Iterator JavaDoc itHelp2;
1853      
1854      int iErrorIndexPosition = 0;
1855
1856      if (GlobalConstants.ERROR_CHECKING)
1857      {
1858         assert lstSourcesCopy.size() == lstDestinationsCopy.size()
1859                  : "Size of the source and destination list of files that " +
1860                    "will be moved has to be the same.";
1861      }
1862
1863      if (lstSourcesCopy.size() > 0)
1864      {
1865         
1866         itHelp1 = lstSourcesCopy.iterator();
1867         itHelp2 = lstDestinationsCopy.iterator();
1868         while (itHelp1.hasNext() && itHelp2.hasNext())
1869         {
1870            // get source and destination file to be move back
1871
fSourceFile = (File JavaDoc) itHelp1.next();
1872            fDestinationFile = (File JavaDoc) itHelp2.next();
1873   
1874            if (fSourceFile.exists())
1875            {
1876               if (fDestinationFile.exists())
1877               {
1878                  // Since the destination file already exists (e.g. it couldn't
1879
// be removed because of permissions), remove the source
1880
// This should never happen during commit (maybe we should check
1881
// that this is really rollback) since for commit the flag
1882
// should be replace and then the file to be replaced is moved
1883
// away. If this is rollback then the destination (which is the
1884
// real original) may be there and in that case we want to remove
1885
// the copy
1886
s_logger.finest("Destination file "
1887                                           + fDestinationFile.getAbsolutePath() +
1888                                           " already exists, removing the source file " +
1889                                           fSourceFile.getAbsolutePath());
1890                  if (!fSourceFile.delete())
1891                  {
1892                     // set position of the file in the list the error was occured on
1893
iErrorIndexPosition++;
1894                     // log this failure
1895
s_logger.log(Level.WARNING,
1896                                         "Cannot delete file " + fSourceFile.getAbsolutePath() +
1897                                         " since the destination file already exists.");
1898                  }
1899                  else
1900                  {
1901                     // file was successfully deleted, so remove it from the global lists
1902
removeDeletedFileFromGlobalLists(iSenderIdentificator,
1903                                                      iListIdentificator,
1904                                                      iErrorIndexPosition);
1905                  }
1906               }
1907               else
1908               {
1909                  try
1910                  {
1911                     FileUtils.moveFile(fSourceFile, fDestinationFile);
1912                     // file was successfully moved, so remove it from the global lists
1913
removeRenamedFileFromGlobalLists(iSenderIdentificator,
1914                                                      iListIdentificator,
1915                                                      iErrorIndexPosition);
1916                  }
1917                  catch (Throwable JavaDoc thr)
1918                  {
1919                     // set position of the file in the list the error was occured on
1920
iErrorIndexPosition++;
1921                     // log this failure
1922
s_logger.log(Level.WARNING,
1923                                         "Cannot move file from " + fSourceFile.getAbsolutePath()
1924                                         + " to " + fDestinationFile.getAbsolutePath(), thr);
1925                  }
1926               }
1927            }
1928            else
1929            {
1930               // Source file does not exist. This case should never became, because we have
1931
// already prepared files in private ready_to_XXX directory. Now we cannot
1932
// do anything because we are already doing commit or rollback. We shold just
1933
// remove this file from the global lists.
1934
removeRenamedFileFromGlobalLists(iSenderIdentificator,
1935                                                iListIdentificator,
1936                                                iErrorIndexPosition);
1937            }
1938         }
1939      }
1940   }
1941
1942   /**
1943    * Method for removing just renamed file from the global lstReadyToXXX list.
1944    *
1945    * @param iSenderIdentificator - identificator of the sender - commit/rollback.
1946    * @param iListIdentificator - identificator of the list. By this identificator we can decide
1947    * deleting processed item from the particular global list.
1948    * @param iIndexPosition - actual position in the particular global list. Items from the list
1949    * are removed from the start of this list. If error has occured, index
1950    * position is increased and this item will be not deleted from the list
1951    * but whole process will repeat.
1952    */

1953   private void removeRenamedFileFromGlobalLists(
1954      int iSenderIdentificator,
1955      int iListIdentificator,
1956      int iIndexPosition
1957   )
1958   {
1959      switch (iSenderIdentificator)
1960      {
1961         // if the sender is commit() method
1962
case SENDER_COMMIT:
1963         {
1964            switch (iListIdentificator)
1965            {
1966               case LIST_IDENTIFICATOR_COPY:
1967               {
1968                  // remove processed item from ready to copy list
1969
this.m_lstReadyToCopyFrom.remove(iIndexPosition);
1970                  this.m_lstReadyToCopyTo.remove(iIndexPosition);
1971                  break;
1972               }
1973               case LIST_IDENTIFICATOR_MOVE:
1974               {
1975                  this.m_lstReadyToMoveFrom.remove(iIndexPosition);
1976                  this.m_lstReadyToMoveTo.remove(iIndexPosition);
1977                  break;
1978               }
1979               case LIST_IDENTIFICATOR_REPLACE:
1980               {
1981                  // remove processed item from ready to replace list
1982
this.m_lstReadyToReplaceFrom.remove(iIndexPosition);
1983                  this.m_lstReadyToReplaceTo.remove(iIndexPosition);
1984                  break;
1985               }
1986               default:
1987               {
1988                  assert true : "List identificator is incorrectly specified.";
1989               }
1990            }
1991            break;
1992         }
1993         // if the sender is rollback() method
1994
case SENDER_ROLLBACK:
1995         {
1996            switch (iListIdentificator)
1997            {
1998               case LIST_IDENTIFICATOR_TO_BE_REPLACED:
1999               {
2000                  // remove processed item from ready to be replaced list
2001
this.m_lstReadyToBeReplacedFrom.remove(iIndexPosition);
2002                  this.m_lstReadyToBeReplacedTo.remove(iIndexPosition);
2003                  break;
2004               }
2005               case LIST_IDENTIFICATOR_MOVE:
2006               {
2007                  this.m_lstReadyToMoveFrom.remove(iIndexPosition);
2008                  this.m_lstReadyToMoveTo.remove(iIndexPosition);
2009                  break;
2010               }
2011               case LIST_IDENTIFICATOR_DELETE:
2012               {
2013                  // remove processed item from ready to delete list
2014
this.m_lstReadyToDeleteFrom.remove(iIndexPosition);
2015                  this.m_lstReadyToDeleteTo.remove(iIndexPosition);
2016                  break;
2017               }
2018               default:
2019               {
2020                  assert true : "List identificator is incorrectly specified.";
2021               }
2022            }
2023            break;
2024         }
2025         default:
2026         {
2027            assert true : "Sender is incorrectly specified.";
2028         }
2029      }
2030   }
2031
2032   /**
2033    * Method that will delete source files in the list.
2034    *
2035    * @param lstSources - list of source files
2036    * @param iSenderIdentificator - identificator of the sender - commit/rollback.
2037    * @param iListIdentificator - identificator of the list. By this identificator we can decide
2038    * deleting processed item from the particular global list.
2039    */

2040   private void doDeleteFiles(
2041      List JavaDoc lstSources,
2042      int iSenderIdentificator,
2043      int iListIdentificator
2044   )
2045   {
2046      File JavaDoc fSourceFile;
2047
2048      // create copy of the source and destination list
2049
List JavaDoc lstSourcesCopy = new ArrayList JavaDoc(lstSources);
2050
2051      Iterator JavaDoc itHelp1;
2052      int iErrorIndexPosition = 0;
2053
2054      if (lstSourcesCopy.size() > 0)
2055      {
2056         itHelp1 = lstSourcesCopy.iterator();
2057         while (itHelp1.hasNext())
2058         {
2059            // get source file to be deleted
2060
fSourceFile = (File JavaDoc) itHelp1.next();
2061   
2062            if (fSourceFile.exists())
2063            {
2064               try
2065               {
2066                  if (!fSourceFile.delete())
2067                  {
2068                     // set position of the file in the list the error was occured on
2069
iErrorIndexPosition++;
2070                     // log this failure
2071
s_logger.log(Level.WARNING,
2072                                         "Cannot delete file " + fSourceFile.getAbsolutePath());
2073                  }
2074                  else
2075                  {
2076                     // file was successfully deleted, so remove it from the global lists
2077
removeDeletedFileFromGlobalLists(iSenderIdentificator,
2078                                                      iListIdentificator,
2079                                                      iErrorIndexPosition);
2080                  }
2081               }
2082               catch (Throwable JavaDoc thr)
2083               {
2084                  // set position of the file in the list the error was occured on
2085
iErrorIndexPosition++;
2086                  // log this failure
2087
s_logger.log(Level.WARNING,
2088                                      "Cannot delete file " + fSourceFile.getAbsolutePath(), thr);
2089               }
2090            }
2091            else
2092            {
2093               // Source file does not exist. This case should never became, because we have
2094
// already prepared files in private ready_to_XXX directory. Now we cannot
2095
// do anything because we are already doing commit or rollback. We shold just
2096
// remove this file from the global lists.
2097
removeDeletedFileFromGlobalLists(iSenderIdentificator,
2098                                                iListIdentificator,
2099                                                iErrorIndexPosition);
2100            }
2101         }
2102      }
2103   }
2104
2105   /**
2106    * Method for removing just deleted file from the global lstReadyToXXX list.
2107    *
2108    * @param iSenderIdentificator - identificator of the sender - commit/rollback.
2109    * @param iListIdentificator - identificator of the list. By this identificator we can decide
2110    * deleting processed item from the particular global list.
2111    * @param iIndexPosition - actual position in the particular global list. Items from the list
2112    * are removed from the start of this list. If error has occured, index
2113    * position is increased and this item will be not deleted from the list
2114    * but whole process will repeat.
2115    */

2116   private void removeDeletedFileFromGlobalLists(
2117      int iSenderIdentificator,
2118      int iListIdentificator,
2119      int iIndexPosition
2120   )
2121   {
2122      switch (iSenderIdentificator)
2123      {
2124         // if the sender is commit() method
2125
case SENDER_COMMIT:
2126         {
2127            switch (iListIdentificator)
2128            {
2129               case LIST_IDENTIFICATOR_TO_BE_REPLACED:
2130               {
2131                  // remove processed item from ready to be replaced list
2132
this.m_lstReadyToBeReplacedFrom.remove(iIndexPosition);
2133                  break;
2134               }
2135               case LIST_IDENTIFICATOR_DELETE:
2136               {
2137                  // remove processed item from ready to delete list
2138
this.m_lstReadyToDeleteFrom.remove(iIndexPosition);
2139                  break;
2140               }
2141               default:
2142               {
2143                  assert true : "List identificator is incorrectly specified.";
2144               }
2145            }
2146            break;
2147         }
2148         // if the sender is rollback() method
2149
case SENDER_ROLLBACK:
2150         {
2151            switch (iListIdentificator)
2152            {
2153               case LIST_IDENTIFICATOR_COPY:
2154               {
2155                  // remove processed item from ready to copy list
2156
this.m_lstReadyToCopyFrom.remove(iIndexPosition);
2157                  break;
2158               }
2159               case LIST_IDENTIFICATOR_MOVE:
2160               {
2161                  this.m_lstReadyToMoveFrom.remove(iIndexPosition);
2162                  break;
2163               }
2164               case LIST_IDENTIFICATOR_REPLACE:
2165               {
2166                  // remove processed item from ready to replace list
2167
this.m_lstReadyToReplaceFrom.remove(iIndexPosition);
2168                  break;
2169               }
2170               default:
2171               {
2172                  assert true : "List identificator is incorrectly specified.";
2173               }
2174            }
2175            break;
2176         }
2177         default:
2178         {
2179            assert true : "Sender is incorrectly specified.";
2180         }
2181      }
2182   }
2183
2184   /**
2185    * Method for initializing of global lists.
2186    */

2187   private void initGlobalLists()
2188   {
2189      m_lstReadyToCopyFrom = Collections.EMPTY_LIST;
2190      m_lstReadyToCopyTo = Collections.EMPTY_LIST;
2191      m_lstReadyToMoveFrom = Collections.EMPTY_LIST;
2192      m_lstReadyToMoveTo = Collections.EMPTY_LIST;
2193      m_lstReadyToBeReplacedFrom = Collections.EMPTY_LIST;
2194      m_lstReadyToBeReplacedTo = Collections.EMPTY_LIST;
2195      m_lstReadyToReplaceFrom = Collections.EMPTY_LIST;
2196      m_lstReadyToReplaceTo = Collections.EMPTY_LIST;
2197      m_lstReadyToDeleteFrom = Collections.EMPTY_LIST;
2198      m_lstReadyToDeleteTo = Collections.EMPTY_LIST;
2199   }
2200
2201   /**
2202    * Method to sleep process.
2203    */

2204   private void sleep()
2205   {
2206      try
2207      {
2208         Thread.sleep(SLEEP_TIME);
2209      }
2210      catch (InterruptedException JavaDoc iExc)
2211      {
2212         s_logger.log(Level.WARNING, "Sleep was interrupted.");
2213      }
2214   }
2215}
2216
Popular Tags