Package net.sourceforge.javautil.common

Source Code of net.sourceforge.javautil.common.BackupUtil

package net.sourceforge.javautil.common;

import net.sourceforge.javautil.common.io.IVirtualDirectory;
import net.sourceforge.javautil.common.io.IVirtualFile;
import net.sourceforge.javautil.common.io.IVirtualPath;

/**
* This will provide various utility methods that help out in most general
* backup solutions.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: BackupUtil.java 2297 2010-06-16 00:13:14Z ponderator $
*/
public class BackupUtil {
 
  /**
   * @param directory The directory that holds the rotatable backup {@link IVirtualFile}
   * @param prefix The prefix for the backup file
   * @param extension The extension
   * @param maxRotations The maximum rotations that should be made before removing the oldest file
   *
   * @return The file that can be used for the backup
   */
  public static IVirtualFile rotate (IVirtualDirectory directory, String prefix, String extension, int maxRotations) {
    for (int i=maxRotations; i>0; i--) {
      IVirtualFile rotation = directory.getFile(getRotatedFileName(prefix, extension, i));
      if (rotation != null) {
        if (i == maxRotations) rotation.remove();
        else rotation.rename(getRotatedFileName(prefix, extension, i+1));
      }
    }
    return directory.getFile(getRotatedFileName(prefix, extension, 1), true);
  }
 
  /**
   * @param directory The directory containing the backup files
   * @param prefix The prefix for the backups
   * @param extension The extension for the backups
   * @param maxRotations The maximum rotations for the rotated backups
   * @return The most recent file available for recovery
   */
  public static IVirtualFile reverseRotation (IVirtualDirectory directory, String prefix, String extension, int maxRotations) {
    int minimum = -1;
    for (int i=1; i<=maxRotations; i++) {
      IVirtualFile rotation = directory.getFile(getRotatedFileName(prefix, extension, i));
      if (rotation != null) {
        if (i == 1) {
          IVirtualFile reverse = rotate(directory, prefix + "-reverse", extension, maxRotations);
          rotation.copy(reverse);
          if (!rotation.remove())
            throw new IllegalStateException("Could not remove rotation file: " + rotation);
        }
        else {
          minimum = i-1;
          rotation.rename(getRotatedFileName(prefix, extension, i-1));
        }
      }
    }
    return minimum == -1 ? null : directory.getFile(getRotatedFileName(prefix, extension, minimum));
  }

 
  /**
   * @param prefix The prefix to use for the rotation
   * @param extension The extension
   * @param rotation The maximum rotations that should be made before removing the oldest file
   * @return A filename for referencing the oldest rotation expected file name
   */
  public static String getOldestRotatedFileName (String prefix, String extension, int maxRotations) {
    return getRotatedFileName(prefix, extension, maxRotations);
  }

  /**
   * @param prefix The prefix to use for the rotation
   * @param extension The extension
   * @return A filename for referencing the most recent rotated expected file name
   */
  public static String getMostRecentRotatedFileName (String prefix, String extension) {
    return getRotatedFileName(prefix, extension, 1);
  }
 
  /**
   * @param prefix The prefix to use for the rotation
   * @param extension The extension
   * @param rotation The rotation in particular
   * @return A filename for referencing the rotated file
   */
  public static String getRotatedFileName (String prefix, String extension, int rotation) {
    return prefix + "-" + rotation + "." + extension;
  }

}
TOP

Related Classes of net.sourceforge.javautil.common.BackupUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.