FileManager.java revision ca669ae54f86dbeea277280690584d9f591c7571
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2008 Sun Microsystems, Inc.
* Portions Copyright 2012-2015 ForgeRock AS.
*/
/**
* Utility class for use by applications containing methods for managing
* file system files. This class handles application notifications for
* interesting events.
*/
public class FileManager {
/**
* Describes the approach taken to deleting a file or directory.
*/
public enum DeletionPolicy {
/**
* Delete the file or directory immediately.
*/
/**
* Mark the file or directory for deletion after the JVM has exited.
*/
/**
* First try to delete the file immediately. If the deletion was
* unsuccessful mark the file for deleteion when the JVM has
* existed.
*/
}
private Application application;
/**
* Creates a new file manager.
*/
public FileManager() {
// do nothing;
}
/**
* Creates a new file manager.
* @param app Application managing files to which progress notifications
* will be sent
*/
this.application = app;
}
/**
* Recursively copies any files or directories appearing in
* <code>source</code> or a subdirectory of <code>source</code>
* to the corresponding directory under <code>target</code>. Files
* in under <code>source</code> are not copied to <code>target</code>
* if a file by the same name already exists in <code>target</code>.
*
* @param source source directory
* @param target target directory
* @throws ApplicationException if there is a problem copying files
*/
throws ApplicationException
{
if (sourceFileNames != null) {
}
}
}
}
/**
* Renames the source file to the target file. If the target file exists
* it is first deleted. The rename and delete operation return values
* are checked for success and if unsuccessful, this method throws an
* exception.
*
* @param fileToRename The file to rename.
* @param target The file to which <code>fileToRename</code> will be
* moved.
* @throws ApplicationException If a problem occurs while attempting to rename
* the file. On the Windows platform, this typically
* indicates that the file is in use by this or another
* application.
*/
throws ApplicationException {
synchronized (target) {
{
throw new ApplicationException(
}
}
throw new ApplicationException(
}
}
}
/**
* Move a file.
* @param object File to move
* @param newParent File representing new parent directory
* @throws ApplicationException if something goes wrong
*/
throws ApplicationException
{
}
/**
* Move a file.
* @param object File to move
* @param newParent File representing new parent directory
* @param filter that will be asked whether or not the operation should be
* performed
* @throws ApplicationException if something goes wrong
*/
throws ApplicationException
{
// TODO: application notification
}
}
/**
* Deletes a single file or directory.
* @param object File to delete
* @throws ApplicationException if something goes wrong
*/
throws ApplicationException
{
}
/**
* Deletes a single file or directory.
* @param object File to delete
* @param filter that will be asked whether or not the operation should be
* performed
* @throws ApplicationException if something goes wrong
*/
throws ApplicationException
{
}
}
/**
* Deletes the children of a directory.
*
* @param parentDir the directory whose children is deleted
* @throws ApplicationException if there is a problem deleting children
*/
}
}
}
}
/**
* Deletes everything below the specified file.
*
* @param file the path to be deleted.
* @throws org.opends.quicksetup.ApplicationException if something goes wrong.
*/
}
/**
* Deletes everything below the specified file.
*
* @param file the path to be deleted.
* @param filter the filter of the files to know if the file can be deleted
* directly or not.
* @param deletePolicy describes how deletions are to be made
* JVM exits rather than deleting the files immediately.
* @throws ApplicationException if something goes wrong.
*/
throws ApplicationException {
}
/**
* Copies everything below the specified file.
*
* @param objectFile the file to be copied.
* @param destDir the directory to copy the file to
* @return File representing the destination
* @throws ApplicationException if something goes wrong.
*/
throws ApplicationException
{
return co.getDestination();
}
/**
* Copies everything below the specified file.
*
* @param objectFile the file to be copied.
* @param destDir the directory to copy the file to
* @param overwrite overwrite destination files.
* @return File representing the destination
* @throws ApplicationException if something goes wrong.
*/
throws ApplicationException
{
return co.getDestination();
}
/**
* Copies everything below the specified file.
*
* @param objectFile the file to be copied.
* @param destDir the directory to copy the file to
* @throws ApplicationException if something goes wrong.
*/
throws ApplicationException
{
}
/**
* Copies everything below the specified file.
*
* @param objectFile the file to be copied.
* @param destDir the directory to copy the file to
* @param filter the filter of the files to know if the file can be copied
* directly or not.
* @throws ApplicationException if something goes wrong.
*/
throws ApplicationException {
}
/**
* Copies everything below the specified file.
*
* @param objectFile the file to be copied.
* @param destDir the directory to copy the file to
* @param filter the filter of the files to know if the file can be copied
* directly or not.
* @param overwrite overwrite destination files.
* @throws ApplicationException if something goes wrong.
*/
throws ApplicationException {
filter);
}
/**
* Determines whether or not two files differ in content.
*
* @param f1 file to compare
* @param f2 file to compare
* @return boolean where true indicates that two files differ
* @throws IOException if there is a problem reading the files' conents
*/
boolean differ = false;
try {
boolean done = false;
}
} finally {
}
return differ;
}
throws ApplicationException {
}
} else {
}
} else {
}
}
}
} else {
}
}
} else {
if (application != null) {
}
}
}
/**
* A file operation.
*/
private abstract class FileOperation {
private File objectFile;
/**
* Creates a new file operation.
* @param objectFile to be operated on
*/
this.objectFile = objectFile;
}
/**
* Gets the file to be operated on.
* @return File to be operated on
*/
protected File getObjectFile() {
return objectFile;
}
/**
* Make a copy of this class for the child file.
* @param child to act as the new file object
* @return FileOperation as the same type as this class
*/
/**
* Execute this operation.
* @throws ApplicationException if there is a problem.
*/
public abstract void apply() throws ApplicationException;
}
/**
* A copy operation.
*/
private class CopyOperation extends FileOperation {
private File destination;
private boolean overwrite;
/**
* Create a new copy operation.
* @param objectFile to copy
* @param destDir to copy to
* @param overwrite if true copy should overwrite any existing file
*/
super(objectFile);
}
/**
* {@inheritDoc}
*/
}
/**
* Returns the destination file that is the result of copying
* <code>objectFile</code> to <code>destDir</code>.
* @return The destination file.
*/
public File getDestination() {
return this.destination;
}
/**
* {@inheritDoc}
*/
public void apply() throws ApplicationException {
if (objectFile.isDirectory()) {
if (!destination.exists()) {
}
} else {
// If overwriting and the destination exists then kill it
}
if (!destination.exists()) {
destination.getAbsolutePath())));
}
try {
byte[] buf = new byte[1024];
int i;
}
// TODO: set the file's permissions. This is made easier in
// Java 1.6 but until then use the TestUtilities methods
}
}
} catch (Exception e) {
throw new ApplicationException(
} finally {
}
} else {
throw new ApplicationException(
}
} else {
}
}
}
}
}
/**
* A delete operation.
*/
private class DeleteOperation extends FileOperation {
private DeletionPolicy deletionPolicy;
/**
* Creates a delete operation.
* @param objectFile to delete
* @param deletionPolicy describing how files will be deleted
* is to take place after this program exists. This is useful
* for cleaning up files that are currently in use.
*/
super(objectFile);
this.deletionPolicy = deletionPolicy;
}
/**
* {@inheritDoc}
*/
}
/**
* {@inheritDoc}
*/
public void apply() throws ApplicationException {
if (isFile) {
} else {
file.getAbsolutePath())));
}
}
file.getAbsolutePath()));
boolean delete = false;
/*
* Sometimes the server keeps some locks on the files.
* TODO: remove this code once stop-ds returns properly when server
* is stopped.
*/
int nTries = 5;
file.deleteOnExit();
delete = true;
} else {
equals(deletionPolicy)) {
file.deleteOnExit();
delete = true;
}
}
if (!delete) {
try {
}
// do nothing;
}
}
}
if (!delete) {
if (isFile) {
} else {
}
throw new ApplicationException(
}
}
}
}
/**
* A delete operation.
*/
private class MoveOperation extends FileOperation {
/**
* Creates a delete operation.
* @param objectFile to delete
* @param newParent Filr where <code>objectFile</code> will be copied.
*/
super(objectFile);
}
/**
* {@inheritDoc}
*/
}
/**
* {@inheritDoc}
*/
public void apply() throws ApplicationException {
if (destination.exists()) {
}
null);
}
}
}
}