| package org.eclipse.epp.installer.internal.core; |
| |
| import java.io.BufferedReader; |
| import java.io.BufferedWriter; |
| import java.io.IOException; |
| import java.io.Writer; |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.eclipse.epp.installer.core.model.IRollbackAction; |
| |
| |
| /** |
| * Class InstallLog used to save installation log information in file. Also used |
| * to rollback installation then in uninstall action. |
| */ |
| public class InstallLog { |
| |
| // TODO Log Entry codes - temporarily defined here |
| public static final int DIRECTORY_CREATED_ENTRY = (int) 'C'; |
| |
| public static final int FILE_CREATED_ENTRY = (int) 'F'; |
| |
| public static final int LINK_CREATED_ENTRY = (int) 'L'; |
| |
| public static final int FILE_BACKUPED_ENTRY = (int) 'B'; |
| |
| public static final int ECLIPSE_CONFIG_CLEANED_ENTRY = (int) 'E'; |
| |
| /** |
| * List of log entrys. |
| */ |
| private List log = new ArrayList(); |
| |
| /** |
| * map stores Integer -> IRollbackAction entries of contributed rollback |
| * actions |
| */ |
| private static Map rollbackActions = new HashMap(); |
| |
| /** |
| * Line separator used to write entrys into file. |
| */ |
| private static String lineSeparator; |
| |
| /** |
| * Register default actions. |
| */ |
| static { |
| // register code actions (note: other actions may be contributed by |
| // extensions) |
| registerAction(DIRECTORY_CREATED_ENTRY, new RemoveFileAction()); |
| registerAction(FILE_CREATED_ENTRY, new RemoveFileAction()); |
| registerAction(LINK_CREATED_ENTRY, new RemoveLinkAction()); |
| registerAction(FILE_BACKUPED_ENTRY, new RestoreFileAction()); |
| // registerAction(ECLIPSE_CONFIG_CLEANED_ENTRY, new CleanEclipseConfigurationAction()); |
| lineSeparator = System.getProperty("line.separator"); |
| } |
| |
| /** |
| * Construct new instance of InstallLog. |
| * |
| * Check for Platform are initialized. |
| * |
| */ |
| public InstallLog() { |
| } |
| |
| /** |
| * Append enntry to list of entrys. |
| * |
| * @param entry |
| * InstallLogEntry to append. |
| */ |
| public void addEntry(InstallLogEntry entry) { |
| log.add(entry); |
| } |
| |
| /** |
| * Return rollback action for specified type of action. |
| * |
| * @param type |
| * int action type. |
| * @return IRollbackAction action to return. <code>null</code> if no |
| * action are pressent. |
| */ |
| public static IRollbackAction getRollbackAction(int type) { |
| return (IRollbackAction) rollbackActions.get(new Integer(type)); |
| } |
| |
| /** |
| * Write installation log. |
| * |
| * @param out |
| * BufferedWriter to write into. |
| * @throws IOException |
| * thrown if could't complite write operation successful. |
| */ |
| public void write(BufferedWriter out) throws IOException { |
| for (int i = 0; i < log.size(); i++) { |
| writeEntry(out, (InstallLogEntry) log.get(i)); |
| } |
| } |
| |
| /** |
| * Read installation log. |
| * |
| * @param reader |
| * BufferedReader to read from. |
| * @throws IOException |
| * thrown if could't complire read operation successful. |
| */ |
| public void read(BufferedReader reader) throws IOException { |
| InstallLogEntry entry = null; |
| while ((entry = readEntry(reader)) != null) { |
| log.add(entry); |
| } |
| } |
| |
| /** |
| * Register selected rollback action. |
| * |
| * Same actions may be registere to more than one, code, but this is not |
| * recommended. |
| * |
| * @param code |
| * action code. |
| * @param action |
| * IRollbackAction action to register. |
| */ |
| public static void registerAction(int code, IRollbackAction action) { |
| rollbackActions.put(new Integer(code), action); |
| } |
| |
| /** |
| * Write one log entry to specified writer. |
| * |
| * @param writer |
| * Writer writer to write in. |
| * @param entry |
| * InstallLogEntry entry to write. |
| * @throws IOException |
| * thrown if write operation aren't successful. |
| */ |
| private static void writeEntry(Writer writer, InstallLogEntry entry) |
| throws IOException { |
| writer.write(entry.getOperationCode()); |
| writer.write(' '); |
| writer.write(entry.getArgument()); |
| writer.write(lineSeparator); |
| } |
| |
| /** |
| * Read entry from specified reader. |
| * |
| * @param reader |
| * BufferedReader reader to read from. |
| * @return InstallLogEntry readed log entry. <code>null</code> if nothing are |
| * readed. |
| * @throws IOException |
| * thrown if read operation aren't successful or of entry are in |
| * invalid format. |
| */ |
| private static InstallLogEntry readEntry(BufferedReader reader) throws IOException { |
| String line = reader.readLine(); |
| if (line == null) { |
| return null; |
| } |
| int i = line.indexOf(' '); |
| if (i != 1) { |
| throw new IOException("invalid install log."); |
| } |
| int opCode = line.charAt(0); |
| String arg = line.substring(2); |
| return new InstallLogEntry(opCode, arg); |
| } |
| |
| /** |
| * Return list of all entrys. |
| * |
| * @return List of entrys. |
| */ |
| public List getEntries() { |
| return log; |
| } |
| } |