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: * DeleteDirectory deletes the specified directory. If the directory is not empty, its contents are deleted recursively
012: * before it is removed.
013: *
014: * @see com.acme.dialogs.CustomCodeAction
015: * @version 3.0.0
016: */
017: public class DeleteDirectory extends CustomCodeAction {
018: private static final String INSTALL_MESSAGE = "Deleting directories";
019: private static final String UNINSTALL_MESSAGE = "";
020: private static final String ERR_MSG = "DeleteDirectory: no path specified.";
021: private static final String SOURCE_VAR_NAME = "$DeleteDirectory_Path$";
022: private boolean isLoaded = false;
023: private static final String SUCCESS = "SUCCESS";
024: private static final String ERROR = "ERROR";
025:
026: /**
027: * This is the method that is called at install-time. The InstallerProxy instance provides methods to access
028: * information in the installer, set status, and control flow.
029: * <p>
030: * For the purposes of the this action (DeleteDirectory), this method
031: * <ol>
032: * <li>gets its parameters from InstallAnywhere Variables,</li>
033: * <li>checks the parameters' validity,</li>
034: * <li>and recursively deletes the directories.</li>
035: * </ol>
036: *
037: * @see com.zerog.ia.api.pub.CustomCodeAction#install
038: */
039: public void install(InstallerProxy ip) throws InstallException {
040: if (isLoaded == true)
041: return;
042: isLoaded = true;
043:
044: /*
045: * Get input from InstallAnywhere Variables. The literal contents of the Variables are retieved into the Strings.
046: */
047: String path = ip.substitute(SOURCE_VAR_NAME);
048:
049: /*
050: * substitute() will return an empty string for any InstallAnywhere Variable that hasn't been assigned yet.
051: */
052:
053: if (path.equals("")) {
054: error(path);
055: } else {
056: try {
057: deleteDirectory(path);
058: ip.setVariable("DELETE_DIRECTORY_SUCCESS",
059: DeleteDirectory.SUCCESS);
060: System.out.println("DeleteDirectory: succeeded");
061: } catch (IOException ioe) {
062: System.out.println("DeleteDirectory: Exception = "
063: + ioe.getMessage());
064: ip.setVariable("DELETE_DIRECTORY_SUCCESS",
065: DeleteDirectory.ERROR);
066: System.out
067: .println("DeleteDirectory: DELETE_DIRECTORY_SUCCESS="
068: + DeleteDirectory.ERROR);
069:
070: throw new NonfatalInstallException(ioe.getMessage());
071: }
072: }
073: }
074:
075: /**
076: * This is the method that is called at uninstall-time. For an example of how to effect the uninstallation of
077: * something like this, please see com.acme.fileutils.CopyFile.
078: *
079: * @see com.acme.fileutils.CopyFile
080: * @see com.zerog.ia.api.pub.CustomCodeAction#uninstall
081: */
082: public void uninstall(UninstallerProxy up) throws InstallException {
083: //
084: }
085:
086: /**
087: * This method will be called to display a status message during the installation.
088: *
089: * @see com.zerog.ia.api.pub.CustomCodeAction#getInstallStatusMessage
090: */
091: public String getInstallStatusMessage() {
092: return INSTALL_MESSAGE;
093: }
094:
095: /**
096: * This method will be called to display a status message during the uninstall.
097: *
098: * @see com.zerog.ia.api.pub.CustomCodeAction#getUninstallStatusMessage
099: */
100: public String getUninstallStatusMessage() {
101: return UNINSTALL_MESSAGE;
102: }
103:
104: /**
105: * Delete the specified directory represented by directoryToDelete. If the directory is not empty, its contents are
106: * deleted recursively before it is removed.
107: */
108: public static void deleteDirectory(String directoryToDelete)
109: throws IOException {
110: deleteDirectory(new File(directoryToDelete));
111: }
112:
113: /**
114: * Delete the specified directory represented by directoryToDelete. If the directory is not empty, its contents are
115: * deleted recursively before it is removed.
116: */
117: public static boolean deleteDirectory(File directoryToDelete)
118: throws IOException {
119: // make sure it's a directory
120: if (directoryToDelete.isDirectory()) {
121:
122: String fileSep = System.getProperty("file.separator");
123:
124: String[] filesAndDirs = directoryToDelete.list();
125: int numberFiles = filesAndDirs.length;
126:
127: for (int i = 0; i < numberFiles; i++) {
128:
129: File currentFile = new File(directoryToDelete.getPath()
130: + fileSep + filesAndDirs[i]);
131:
132: // If it's a dir, empty it first
133: if (currentFile.isDirectory()) {
134:
135: if (!deleteDirectory(currentFile))
136: return false;
137: continue;
138: }
139:
140: if (!currentFile.delete()) {
141: System.err.println("DELETING");
142: throw new IOException(
143: "Can't delete file or directory: "
144: + currentFile.getAbsolutePath());
145: }
146:
147: }
148:
149: return directoryToDelete.delete();
150: } else {
151: throw new IOException(
152: "Couldn't delete directory because directory is a file: "
153: + directoryToDelete.getAbsolutePath());
154: }
155: }
156:
157: /**
158: * Print something to indicate that the parameters were not acceptable.
159: */
160: private void error(String path) {
161: System.err.println(ERR_MSG);
162: System.err.println("Path: " + path);
163: }
164: }
|