blob: 2335d409c908644b1545aa7336f5ffee430438ca [file] [log] [blame]
package org.eclipse.epp.installer.internal.core.operations;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.MessageFormat;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.epp.installer.core.IErrorConstants;
import org.eclipse.epp.installer.core.InstallOptions;
import org.eclipse.epp.installer.core.eclipse.EclipseInstallation;
import org.eclipse.epp.installer.core.model.Context;
import org.eclipse.epp.installer.core.model.SequentialOperation;
import org.eclipse.epp.installer.internal.core.InstallLog;
import org.eclipse.epp.installer.internal.core.InstallLogEntry;
/**
* Create link to extension in specified Eclipse installation.
*/
public class CreateLinkOperation extends SequentialOperation {
private File pathToEclipse;
private File pathToExtension;
private String extensionFeature;
private InstallOptions options;
private final int backupSettings = IBackupOptions.OPTION_DO_BACKUP | IBackupOptions.OPTION_TEMP_BACKUP;
/**
* Constructor for CreateLinkOperation.
*
* @param linkFile
* File link file.
* @param pathToExtension
* String absolute path to extension.
*/
public CreateLinkOperation(InstallOptions options, File pathToEclipse,
File pathToExtension, String extensionFeature) {
if (options == null || pathToEclipse == null || pathToExtension == null
|| extensionFeature == null)
throw new IllegalArgumentException();
this.options = options;
this.pathToEclipse = pathToEclipse;
this.pathToExtension = pathToExtension;
this.extensionFeature = extensionFeature;
}
/*
* Add dependend create link file operation
*/
protected void prepare() throws Exception {
addPrerequisite(new CreateDirectoryOperation(getLinksDirectory()));
File linkFile = getLinkFile();
if (!options.isNobackup())
if (linkFile.exists()) {
add(new DeleteFileOperation(options, linkFile, backupSettings));
}
// Do not prompt the user to overwrite a link file
// thus no need to create the file as it will be created
// by the FileWriter in the run() method below.
//addPrerequisite(new CreateFileOperation(linkFile, false));
}
private File getLinksDirectory() {
EclipseInstallation ei = new EclipseInstallation(pathToEclipse);
return ei.getLinksDir();
}
private File getLinkFile() {
return new File(getLinksDirectory(), extensionFeature + ".link");
}
/**
* Method getErrorStatus.
*
* @return IStatus
*/
private IStatus getErrorStatus() {
String message = MessageFormat.format(
"Can not specify link to extension: {0}",
new Object[] { pathToExtension });
return new Status(IStatus.ERROR, Context.PLUGIN_ID,
IErrorConstants.ERROR_CANNOT_CREATE_LINK, message, null);
}
/**
* Method run.
*
* @param installer
* Context
* @return IStatus
*/
protected IStatus run(Context installer) {
try {
File linkFile = getLinkFile();
FileWriter writer = new FileWriter(linkFile);
String pathToExtensionString = pathToExtension.getAbsolutePath();
pathToExtensionString = pathToExtensionString.replace('\\', '/');
// pathToExtensionString contains "/" as separator on all platforms,
// even on Windows there will be "C:/" instead of "C:\"
writer.write("path=" + pathToExtensionString);
writer.flush();
writer.close();
installer.getInstallLog().addEntry(
new InstallLogEntry(InstallLog.LINK_CREATED_ENTRY, linkFile
.getAbsolutePath()
+ InstallLogEntry.LINK_ENTRY_TOKEN
+ pathToExtensionString));
} catch (IOException e) {
return getErrorStatus();
}
return Status.OK_STATUS;
}
/**
* Method hashCode.
*
* @return int
*/
public int hashCode() {
return pathToExtension.hashCode() + pathToEclipse.hashCode()
+ extensionFeature.hashCode();
}
/**
* Method equals.
*
* @param o
* Object
* @return boolean
*/
public boolean equals(Object o) {
if (o instanceof CreateLinkOperation) {
CreateLinkOperation clo = (CreateLinkOperation) o;
return (pathToExtension.equals(clo.pathToExtension)
&& pathToEclipse.equals(clo.pathToEclipse) && extensionFeature
.equals(clo.extensionFeature));
}
return false;
}
/**
* Method toString.
*
* @return String
*/
public String toString() {
return MessageFormat.format("Linking to extension {0}.",
new String[] { pathToExtension.getAbsolutePath() });
}
}