1 4 package com.zerog.ia.customcode.util.fileutils; 5 6 import java.io.*; 7 8 import com.zerog.ia.api.pub.*; 9 10 17 public class DeleteDirectory extends CustomCodeAction { 18 private static final String INSTALL_MESSAGE = "Deleting directories"; 19 private static final String UNINSTALL_MESSAGE = ""; 20 private static final String ERR_MSG = "DeleteDirectory: no path specified."; 21 private static final String SOURCE_VAR_NAME = "$DeleteDirectory_Path$"; 22 private boolean isLoaded = false; 23 private static final String SUCCESS = "SUCCESS"; 24 private static final String ERROR = "ERROR"; 25 26 39 public void install(InstallerProxy ip) throws InstallException { 40 if (isLoaded == true) return; 41 isLoaded = true; 42 43 46 String path = ip.substitute(SOURCE_VAR_NAME); 47 48 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 76 public void uninstall(UninstallerProxy up) throws InstallException { 77 } 78 79 84 public String getInstallStatusMessage() { 85 return INSTALL_MESSAGE; 86 } 87 88 93 public String getUninstallStatusMessage() { 94 return UNINSTALL_MESSAGE; 95 } 96 97 101 public static void deleteDirectory(String directoryToDelete) throws IOException { 102 deleteDirectory(new File(directoryToDelete)); 103 } 104 105 109 public static boolean deleteDirectory(File directoryToDelete) throws IOException { 110 if (directoryToDelete.isDirectory()) { 112 113 String fileSep = System.getProperty("file.separator"); 114 115 String [] 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 (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 146 private void error(String path) { 147 System.err.println(ERR_MSG); 148 System.err.println("Path: " + path); 149 } 150 } 151 | Popular Tags |