blob: 999e45ed8c7d1b475568053cf5f70f3d79dbbd8d [file] [log] [blame]
package org.eclipse.epp.installer.internal.core.operations;
import java.io.File;
import java.text.MessageFormat;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.epp.installer.core.InstallOptions;
import org.eclipse.epp.installer.core.model.Context;
import org.eclipse.epp.installer.core.model.InstallOperation;
import org.eclipse.epp.installer.internal.core.IBackupManager;
import org.eclipse.epp.installer.internal.core.InstallLog;
import org.eclipse.epp.installer.internal.core.InstallLogEntry;
/**
* <p>
* Copyright (c) 2006, Instantiations, Inc.<br>
* All Rights Reserved
*/
public class BackupFileOperation extends InstallOperation {
private File file;
private boolean temporary;
private IBackupManager manager;
private InstallOptions options;
public BackupFileOperation(InstallOptions options, File file, IBackupManager manager, boolean temporary) {
if (options == null || manager == null || file == null) {
throw new IllegalArgumentException("file can't be null");
}
this.file = file;
this.manager = manager;
this.temporary = temporary;
this.options = options;
}
/**
* Method getErrorMessage.
*
* @return String
*/
private String getErrorMessage() {
String message = "Failed to backup file: " + file.getAbsolutePath();
return message;
}
private String getLogMessage(File storedFile) {
StringBuffer result = new StringBuffer("");
result.append(file.getAbsolutePath());
result.append(InstallLogEntry.BACKUP_ENTRY_TOKEN);
result.append(storedFile.getAbsolutePath());
result.append(InstallLogEntry.BACKUP_ENTRY_TOKEN);
result.append(Boolean.toString(temporary));
return result.toString();
}
/**
* Method run.
*
* @param installer
* Context
* @return IStatus
*/
protected IStatus run(Context installer) {
if (!manager.isInited()) {
File installDir = new File(options
.getString(InstallOptions.OPTION_INSTALL_DIR));
manager.setInstallDir(installDir);
manager.setRestoreTemporary(options.isInstall());
manager.setInited(true);
}
if (file.exists()) {
File storedFile = manager.storeFile(file, temporary);
if (storedFile != null) {
installer.getInstallLog().addEntry(
new InstallLogEntry(InstallLog.FILE_BACKUPED_ENTRY,
getLogMessage(storedFile)));
} else {
return new Status(Status.WARNING, Context.PLUGIN_ID, 0,
getErrorMessage(), null);
}
} else {
return new Status(Status.WARNING, Context.PLUGIN_ID, 0,
getErrorMessage(), null);
}
return Status.OK_STATUS;
}
/**
* Method toString.
*
* @return String
*/
public String toString() {
return MessageFormat.format("Backing up file {0}.",
new String[] { file.getAbsolutePath() });
}
public boolean equals(Object o) {
if (o instanceof BackupFileOperation) {
BackupFileOperation bfo = (BackupFileOperation) o;
return file.equals(bfo.file);
}
return false;
}
public int hashCode() {
return file.hashCode();
}
}