Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 eXo Platform SAS.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.exoplatform.services.jcr.ext.replication.recovery;
 
 
 import java.io.File;
 import java.util.List;

Created by The eXo Platform SAS.

Author(s):
Alex Reshetnyak
Version:
$Id: RecoveryWriter.java 34445 2009-07-24 07:51:18Z dkatayev $
 
 
 public class RecoveryWriter extends AbstractFSAccess
 {

   
The apache logger.
 
    private static Log log = ExoLogger.getLogger("exo.jcr.component.ext.RecoveryWriter");

   
Definition the timeout to FileRemover.
 
    private static final long REMOVER_TIMEOUT = 5 * 60 * 1000;

   
The FileCleaner will delete the temporary files.
 
    private final FileCleaner fileCleaner;

   
The FileNameFactory will be created file names.
 
    private FileNameFactory fileNameFactory;

   
Definition the folder to ChangesLog.
 
    private File recoveryDir;

   
Definition the folder to data.
 
    private File recoveryDirDate;

   
The FileRemover will deleted the already delivered ChangesLogs.
 
    private FileRemover fileRemover;

   
RecoveryWriter constructor.

Parameters:
recoveryDir the recovery directory
fileNameFactory the FileNameFactory
fileCleaner the File Cleaner
ownName the own name
Throws:
java.io.IOException will be generated the IOException
   public RecoveryWriter(File recoveryDirFileNameFactory fileNameFactoryFileCleaner fileCleanerString ownName)
      throws IOException
   {
      this. = fileCleaner;
      this. = recoveryDir;
      this. = fileNameFactory;
       = new File(PrivilegedFileHelper.getCanonicalPath(this.) + . + );
      if (!PrivilegedFileHelper.exists())
      {
         PrivilegedFileHelper.mkdirs();
      }
       = new FileRemover(recoveryDirfileCleanerownName);
      .start();
   }

   
save.

Parameters:
confirmationChengesLog the PendingConfirmationChengesLog
Returns:
String return the name of file
Throws:
java.io.IOException will be generated the IOException
   public String saveDataInfo(PendingConfirmationChengesLog confirmationChengesLogthrows IOException
   {
      if (confirmationChengesLog.getNotConfirmationList().size() > 0)
      {
         File f = new File(confirmationChengesLog.getDataFilePath());
         // save info
         if (.isDebugEnabled())
            .debug("Write info : " + PrivilegedFileHelper.getAbsolutePath(f));
         writeNotConfirmationInfo(fconfirmationChengesLog.getNotConfirmationList());
         return f.getName();
      }
      return null;
   }

   
save.

Parameters:
timeStamp the time stamp to ChangesLog
identifier the identifier to ChangesLog
tcLog the ChangersLog
Returns:
String the canonical path to serialized ChangrsLog
Throws:
java.io.IOException will be generated the IOException
   public String saveDataFile(Calendar timeStampString identifierTransactionChangesLog tcLogthrows IOException
   {
      String fileName = .getTimeStampName(timeStamp) + "_" + identifier;
      // create dir
      File dir =
         new File(PrivilegedFileHelper.getCanonicalPath() + .
            + .getRandomSubPath());
      PrivilegedFileHelper.mkdirs(dir);
      File f = new File(PrivilegedFileHelper.getCanonicalPath(dir) + . + . + fileName);
      // save data
      this.save(ftcLog);
      return PrivilegedFileHelper.getCanonicalPath(f);
   }

   
save.

Parameters:
f the file to binary data
changesLog the ChangesLog
Returns:
String return the name of file
Throws:
java.io.IOException will be generated the IOException
   public String save(File fTransactionChangesLog changesLogthrows IOException
   {
      // save data
      ObjectWriterImpl out = new ObjectWriterImpl(PrivilegedFileHelper.fileOutputStream(f));
      wr.write(outchangesLog);
      out.flush();
      out.close();
      return f.getName();
   }

   
writeNotConfirmationInfo.

Parameters:
dataFile the file to binary ChangesLog
participantsClusterList the list of cluster participants who was not saved the ChangesLog
Throws:
java.io.IOException will be generated the IOException
   private synchronized void writeNotConfirmationInfo(File dataFileList<StringparticipantsClusterList)
      throws IOException
   {
      for (String name : participantsClusterList)
      {
         File metaDataFile = new File(PrivilegedFileHelper.getCanonicalPath() + . + name);
         if (!PrivilegedFileHelper.exists(metaDataFile))
         {
            PrivilegedFileHelper.createNewFile(metaDataFile);
         }
         RandomAccessFile raf = PrivilegedFileHelper.randomAccessFile(metaDataFile"rw");
         raf.seek(PrivilegedFileHelper.length(metaDataFile));
         raf.write((PrivilegedFileHelper.getCanonicalPath(dataFile) + "\n").getBytes());
         raf.close();
      }
   }

   
removeChangesLog.

Parameters:
identifier the identification string to ChangesLog
ownerName the owner name
Throws:
java.io.IOException will be generated the IOException
   public synchronized void removeChangesLog(String identifierString ownerNamethrows IOException
   {
      File metaDataFile = new File(PrivilegedFileHelper.getAbsolutePath() + . + ownerName);
      RandomAccessFile raf = PrivilegedFileHelper.randomAccessFile(metaDataFile"rw");
      String fileName;
      while ((fileName = raf.readLine()) != null)
         if (fileName.indexOf(identifier) != -1)
         {
            raf.seek(raf.getFilePointer() - (fileName.length() + 1));
            String s = new String(fileName);
            s = s.replaceAll(".");
            raf.writeBytes(s);
            if (.isDebugEnabled())
            {
               .debug("remove metadata : " + fileName);
               .debug("remove changes log form fs : " + identifier);
            }
            saveRemoveChangesLog(PrivilegedFileHelper.getCanonicalPath((new File(fileName))));
            break;
         }
      raf.close();
   }

   
removeChangesLog.

Parameters:
fileNameList the list of file name
ownerName the owner name
Returns:
long return the how many files was deleted
Throws:
java.io.IOException will be generated the IOException
   public long removeChangesLog(List<StringfileNameListString ownerNamethrows IOException
   {
      long removeCounter = 0;
      File metaDataFile = new File(PrivilegedFileHelper.getAbsolutePath() + . + ownerName);
      RandomAccessFile raf = PrivilegedFileHelper.randomAccessFile(metaDataFile"rw");
      HashMap<StringStringfileNameMap = new HashMap<StringString>();
      for (String fileName : fileNameList)
         fileNameMap.put(fileNamefileName);
      String fName;
      while ((fName = raf.readLine()) != null)
         if (fName.startsWith() == false)
         {
            File f = new File(fName);
            if (fileNameMap.containsKey(f.getName()))
            {
               raf.seek(raf.getFilePointer() - (fName.length() + 1));
               String s = new String(fName);
               s = s.replaceAll(".");
               raf.writeBytes(s);
               removeCounter++;
               if (.isDebugEnabled())
               {
                  .debug("remove metadata : " + fName);
                  .debug("remove changeslogs form fs : " + fileNameList.size());
               }
            }
         }
      raf.close();
      saveRemoveChangesLog(fileNameListownerName);
      return removeCounter;
   }

   
saveRemoveChangesLog. Save the file name for deleting.

Parameters:
filePath the file name
Throws:
java.io.IOException will be generated the IOException
   public void saveRemoveChangesLog(String filePaththrows IOException
   {
      if (.isDebugEnabled())
         .debug("Seve removable changeslogs form fs : " + filePath);
      File removeDataFile =
         new File(PrivilegedFileHelper.getAbsolutePath() + . +  + .
            + IdGenerator.generate() + );
      PrivilegedFileHelper.createNewFile(removeDataFile);
      BufferedWriter bw = new BufferedWriter(new FileWriter(removeDataFile));
      if (.isDebugEnabled())
         .debug("remove  : " + filePath);
      bw.write(filePath + "\n");
      bw.flush();
      bw.close();
   }

   
saveRemoveChangesLog. Save the file name to deleting for a specific members.

Parameters:
fileNameList the lost of file names
ownerName the owner name
Throws:
java.io.IOException will be generated the IOException
   private void saveRemoveChangesLog(List<StringfileNameListString ownerNamethrows IOException
   {
      if (.isDebugEnabled())
         .debug("Seve removable changeslogs form fs : " + fileNameList.size());
      File removeDataFile =
         new File(PrivilegedFileHelper.getAbsolutePath() + . +  + .
            + IdGenerator.generate() + );
      PrivilegedFileHelper.createNewFile(removeDataFile);
      BufferedWriter bw = new BufferedWriter(new FileWriter(removeDataFile));
      for (String fileName : fileNameList)
      {
         if (.isDebugEnabled())
            .debug("remove  : " + fileName);
         bw.write(fileName + "\n");
      }
      bw.flush();
      bw.close();
   }

   
removeDataFile.

Parameters:
f File, data file.
   public void removeDataFile(File f)
   {
      if (!PrivilegedFileHelper.delete(f))
         .addFile(f);
   }
FileRemover. The thread will be remove ChangesLog, saved as binary file.
class FileRemover extends Thread
{
   
The apache logger.
   private static Log log = ExoLogger.getLogger("exo.jcr.component.ext.FileRemover");

   
Definition the constants to one second.
   private static final double ONE_SECOND = 1000.0;

   
Sleep period.
   private long period;

   
Definition the folder to ChangesLog.
   private File recoveryDir;

   
The FileCleaner will delete the temporary files.
   private FileCleaner fileCleaner;

   
The FilesFilter to removable files.
   class RemoveFilesFilter implements FileFilter
   {
      
      public boolean accept(File pathname)
      {
         return pathname.getName().endsWith(.);
      }
   }

   
FileRemover constructor.

Parameters:
period the sleep period
recoveryDir the recovery directory
fileCleaner the FileCleaner
ownName the own name
   public FileRemover(long periodFile recoveryDirFileCleaner fileCleanerString ownName)
   {
      super("FileRemover@" + ownName);
      this. = period;
      this. = recoveryDir;
      this. = fileCleaner;
      .info(getName() + " has been inited");
   }

   
   public void run()
   {
      while (true)
      {
         try
         {
            Thread.yield();
            Thread.sleep();
            File recoveryDataDir =
               new File(PrivilegedFileHelper.getCanonicalPath() + .
                  + .);
            File[] fArray = PrivilegedFileHelper.listFiles(recoveryDataDirnew RemoveFilesFilter());
            if (fArray.length > 0)
            {
               ArrayList<StringneedRemoveFilesName = getAllRemoveFileName(fArray);
               HashMap<StringStringmap = getAllPendingBinaryFilePath();
               for (String fPath : needRemoveFilesName)
               {
                  File f = new File(fPath);
                  if (PrivilegedFileHelper.exists(f) && !map.containsKey(f.getName()))
                  {
                     .addFile(f);
                     if (.isDebugEnabled())
                        .debug("Remove file :" + PrivilegedFileHelper.getCanonicalPath(f));
                  }
               }
               // remove *.remove files
               if (map.size() == 0)
                  for (File ff : fArray)
                  {
                     .addFile(ff);
                     if (.isDebugEnabled())
                        .debug("Remove file :" + PrivilegedFileHelper.getCanonicalPath(ff));
                  }
            }
         }
         catch (IOException e)
         {
            .error("FileRemover error :"e);
         }
         catch (InterruptedException e)
         {
            .error("FileRemover error :"e);
         }
      }
   }

   
getAllRemoveFileName.

Parameters:
array the array of file names to deleting
Returns:
ArrayList the list of deleting files
Throws:
java.io.IOException will be generated the IOException
   private ArrayList<StringgetAllRemoveFileName(File[] arraythrows IOException
   {
      ArrayList<StringfileNameList = new ArrayList<String>();
      for (File file : array)
         if (file.isFile())
         {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String sFileName;
            while ((sFileName = br.readLine()) != null)
            {
               fileNameList.add(sFileName);
               if (.isDebugEnabled())
                  .debug("Need remove file : " + sFileName);
            }
         }
      return fileNameList;
   }

   
getAllPendingBinaryFilePath.

Returns:
HashMap return HashMap of file names per owner.
Throws:
java.io.IOException will be generated the IOException
   {
      HashMap<StringStringmap = new HashMap<StringString>();
      for (File f : PrivilegedFileHelper.listFiles())
         if (f.isFile())
            for (String filePath : getFilePathList(f))
               map.put(new File(filePath).getName(), filePath);
      return map;
   }

   
getFilePathList.

Parameters:
f the File with removable files
Returns:
List return the list of file path
Throws:
java.io.IOException will be generated the IOException
   private List<StringgetFilePathList(File fthrows IOException
   {
      List<Stringlist = new ArrayList<String>();
      BufferedReader reader = new BufferedReader(new FileReader(f));
      String str;
      while ((str = reader.readLine()) != null)
         if (str.startsWith(.) == false)
            list.add(str);
      return list;
   }
New to GrepCode? Check out our FAQ X