KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > zerog > ia > customcode > util > fileutils > DeleteDirectory


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.zerog.ia.customcode.util.fileutils;
5
6 import java.io.*;
7
8 import com.zerog.ia.api.pub.*;
9
10 /**
11  * DeleteDirectory deletes the specified directory. If the directory is not empty, its contents are deleted recursively
12  * before it is removed.
13  *
14  * @see com.acme.dialogs.CustomCodeAction
15  * @version 3.0.0
16  */

17 public class DeleteDirectory extends CustomCodeAction {
18   private static final String JavaDoc INSTALL_MESSAGE = "Deleting directories";
19   private static final String JavaDoc UNINSTALL_MESSAGE = "";
20   private static final String JavaDoc ERR_MSG = "DeleteDirectory: no path specified.";
21   private static final String JavaDoc SOURCE_VAR_NAME = "$DeleteDirectory_Path$";
22   private boolean isLoaded = false;
23   private static final String JavaDoc SUCCESS = "SUCCESS";
24   private static final String JavaDoc ERROR = "ERROR";
25
26   /**
27    * This is the method that is called at install-time. The InstallerProxy instance provides methods to access
28    * information in the installer, set status, and control flow.
29    * <p>
30    * For the purposes of the this action (DeleteDirectory), this method
31    * <ol>
32    * <li>gets its parameters from InstallAnywhere Variables,</li>
33    * <li>checks the parameters' validity,</li>
34    * <li>and recursively deletes the directories.</li>
35    * </ol>
36    *
37    * @see com.zerog.ia.api.pub.CustomCodeAction#install
38    */

39   public void install(InstallerProxy ip) throws InstallException {
40     if (isLoaded == true) return;
41     isLoaded = true;
42
43     /*
44      * Get input from InstallAnywhere Variables. The literal contents of the Variables are retieved into the Strings.
45      */

46     String JavaDoc path = ip.substitute(SOURCE_VAR_NAME);
47
48     /*
49      * substitute() will return an empty string for any InstallAnywhere Variable that hasn't been assigned yet.
50      */

51
52     if (path.equals("")) {
53       error(path);
54     } else {
55       try {
56         deleteDirectory(path);
57         ip.setVariable("DELETE_DIRECTORY_SUCCESS", DeleteDirectory.SUCCESS);
58         System.out.println("DeleteDirectory: succeeded");
59       } catch (IOException ioe) {
60         System.out.println("DeleteDirectory: Exception = " + ioe.getMessage());
61         ip.setVariable("DELETE_DIRECTORY_SUCCESS", DeleteDirectory.ERROR);
62         System.out.println("DeleteDirectory: DELETE_DIRECTORY_SUCCESS=" + DeleteDirectory.ERROR);
63
64         throw new NonfatalInstallException(ioe.getMessage());
65       }
66     }
67   }
68
69   /**
70    * This is the method that is called at uninstall-time. For an example of how to effect the uninstallation of
71    * something like this, please see com.acme.fileutils.CopyFile.
72    *
73    * @see com.acme.fileutils.CopyFile
74    * @see com.zerog.ia.api.pub.CustomCodeAction#uninstall
75    */

76   public void uninstall(UninstallerProxy up) throws InstallException {
77   }
78
79   /**
80    * This method will be called to display a status message during the installation.
81    *
82    * @see com.zerog.ia.api.pub.CustomCodeAction#getInstallStatusMessage
83    */

84   public String JavaDoc getInstallStatusMessage() {
85     return INSTALL_MESSAGE;
86   }
87
88   /**
89    * This method will be called to display a status message during the uninstall.
90    *
91    * @see com.zerog.ia.api.pub.CustomCodeAction#getUninstallStatusMessage
92    */

93   public String JavaDoc getUninstallStatusMessage() {
94     return UNINSTALL_MESSAGE;
95   }
96
97   /**
98    * Delete the specified directory represented by directoryToDelete. If the directory is not empty, its contents are
99    * deleted recursively before it is removed.
100    */

101   public static void deleteDirectory(String JavaDoc directoryToDelete) throws IOException {
102     deleteDirectory(new File(directoryToDelete));
103   }
104
105   /**
106    * Delete the specified directory represented by directoryToDelete. If the directory is not empty, its contents are
107    * deleted recursively before it is removed.
108    */

109   public static boolean deleteDirectory(File directoryToDelete) throws IOException {
110     // make sure it's a directory
111
if (directoryToDelete.isDirectory()) {
112
113       String JavaDoc fileSep = System.getProperty("file.separator");
114
115       String JavaDoc[] filesAndDirs = directoryToDelete.list();
116       int numberFiles = filesAndDirs.length;
117
118       for (int i = 0; i < numberFiles; i++) {
119
120         File currentFile = new File(directoryToDelete.getPath() + fileSep + filesAndDirs[i]);
121
122         // If it's a dir, empty it first
123
if (currentFile.isDirectory()) {
124
125           if (!deleteDirectory(currentFile)) return false;
126           continue;
127         }
128
129         if (!currentFile.delete()) {
130           System.err.println("DELETING");
131           throw new IOException("Can't delete file or directory: " + currentFile.getAbsolutePath());
132         }
133
134       }
135
136       return directoryToDelete.delete();
137     } else {
138       throw new IOException("Couldn't delete directory because directory is a file: "
139                             + directoryToDelete.getAbsolutePath());
140     }
141   }
142
143   /**
144    * Print something to indicate that the parameters were not acceptable.
145    */

146   private void error(String JavaDoc path) {
147     System.err.println(ERR_MSG);
148     System.err.println("Path: " + path);
149   }
150 }
151
Popular Tags