001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.zerog.ia.customcode.util.fileutils;
005:
006: import java.io.*;
007:
008: import com.zerog.ia.api.pub.*;
009:
010: /**
011: * CopyDirectory takes a specified file and copies it
012: * to a new location.
013: *
014: * @see com.acme.dialogs.CustomCodeAction
015: *
016: * @version 3.0.0
017: */
018: public class DeleteFile extends CustomCodeAction {
019: private static final String INSTALL_MESSAGE = "Removing files";
020: private static final String UNINSTALL_MESSAGE = "";
021: private static final String ERR_MSG = "DeleteFile: no file specified.";
022: private static final String FILE_VAR_NAME = "$DeleteFile_File$";
023: private boolean isLoaded = false;
024:
025: /**
026: * This is the method that is called at install-time. The InstallerProxy
027: * instance provides methods to access information in the installer,
028: * set status, and control flow.<p>
029: *
030: * For the purposes of the this action (DeleteFile), this method
031: * <ol>
032: * <li>gets its parameters from InstallAnywhere Variables,</li>
033: * <li>checks the parameters' validity,</li>
034: * <li>and deletes the file.</li></ol>
035: *
036: * @see com.zerog.ia.api.pub.CustomCodeAction#install
037: */
038: public void install(InstallerProxy ip) throws InstallException {
039:
040: if (isLoaded == true)
041: return;
042: isLoaded = true;
043:
044: /* Get input from InstallAnywhere Variables. The literal contents of
045: the Variables are retieved into the Strings. */
046: String fileToDelete = ip.substitute(FILE_VAR_NAME);
047:
048: /* substitute() will return an empty string for any InstallAnywhere
049: Variable that hasn't been assigned yet.
050:
051: /* If there is both a source and a destination, copy the files. */
052: if (fileToDelete.equals("")) {
053: error(fileToDelete);
054: } else {
055: try {
056: deleteFile(fileToDelete);
057: } catch (IOException ioe) {
058: throw new NonfatalInstallException(ioe.getMessage());
059: }
060: }
061: }
062:
063: /**
064: * This is the method that is called at uninstall-time. For
065: * an example of how to effect the uninstallation of something
066: * like this, please see com.acme.fileutils.CopyFile.
067: *
068: * @see com.acme.fileutils.CopyFile
069: * @see com.zerog.ia.api.pub.CustomCodeAction#uninstall
070: */
071: public void uninstall(UninstallerProxy up) throws InstallException {
072: //
073: }
074:
075: /**
076: * This method will be called to display a status message during the
077: * installation.
078: *
079: * @see com.zerog.ia.api.pub.CustomCodeAction#getInstallStatusMessage
080: */
081: public String getInstallStatusMessage() {
082: return INSTALL_MESSAGE;
083: }
084:
085: /**
086: * This method will be called to display a status message during the
087: * uninstall.
088: *
089: * @see com.zerog.ia.api.pub.CustomCodeAction#getUninstallStatusMessage
090: */
091: public String getUninstallStatusMessage() {
092: return UNINSTALL_MESSAGE;
093: }
094:
095: /**
096: * Delete the file represented by fileToDelete.
097: */
098: public static void deleteFile(String fileToDelete)
099: throws IOException {
100: deleteFile(new File(fileToDelete));
101: }
102:
103: /**
104: * Delete the file represented by fileToDelete.
105: */
106: public static void deleteFile(File fileToDelete) throws IOException {
107: if (fileToDelete.isFile()) {
108: if (!fileToDelete.delete()) {
109: throw new IOException("Couldn't delete file: "
110: + fileToDelete.getAbsolutePath());
111: }
112: } else {
113: throw new IOException(
114: "Couldn't delete file because file is a directory: "
115: + fileToDelete.getAbsolutePath());
116: }
117: }
118:
119: /**
120: * Print something to indicate that the parameters were not acceptable.
121: */
122: private void error(String path) {
123: System.err.println(ERR_MSG);
124: System.err.println("Path: " + path);
125: }
126: }
|