1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util;
1819import java.io.File;
2021// Jetspeed classes22import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
23import org.apache.jetspeed.services.logging.JetspeedLogger;
242526/*27 * File System Directory Utilities. Some utilities that java.io doesn't give us.28 *29 * rmdir() - removes a directory and all subdirectories and files underneath.30 *31 * @author David S. Taylor <a href="mailto:taylor@apache.org">David Sean Taylor</a>32 */33publicclassDirectoryUtils34 {
35/***36 * Static initialization of the logger for this class37 */38privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(DirectoryUtils.class.getName());
3940publicstaticvoid main(String[] args)
41 {
42 DirectoryUtils.rmdir(args[0]);
43 }
4445/*46 * Removes a directory and all subdirectories and files beneath it.47 *48 * @param directory The name of the root directory to be deleted.49 * @return boolean If all went successful, returns true, otherwise false.50 * 51 */52publicstaticfinalboolean rmdir(String directory)
53 {
54try55 {
56 File dir = new File(directory);
57if (!dir.isDirectory())
58 {
59 dir.delete();
60returntrue;
61 }
6263 deleteTraversal(directory);
6465 dir.delete();
66returntrue;
6768 }
69catch (Exception e)
70 {
71 logger.error("Error in rmdir utility:" , e);
72return false;
73 }
74 }
7576/*77 * Recursive deletion engine, traverses through all subdirectories, 78 * attempting to delete every file and directory it comes across.79 * NOTE: this version doesn't do any security checks, nor does it 80 * check for file modes and attempt to change them.81 *82 * @param path The directory path to be traversed.83 * 84 */85privatestaticvoid deleteTraversal(String path)
86 {
87 File file = new File(path);
88if (file.isFile())
89 {
90try91 {
92 file.delete();
93 }
94catch (Exception e)
95 {
96 logger.error("Failed to Delete file: " + path + " : " , e);
97 file.deleteOnExit(); // try to get it later...98 }
99 }
100elseif (file.isDirectory())
101 {
102if (!path.endsWith(File.separator))
103 path += File.separator;
104105 String list[] = file.list();
106107// Process all files recursivly108for(int ix = 0; list != null && ix < list.length; ix++)
109 deleteTraversal(path + list[ix]);
110111// now try to delete the directory112try113 {
114 file.delete();
115 }
116catch (Exception e)
117 {
118 logger.error("Failed to Delete directory: " + path + " : " , e);
119 file.deleteOnExit(); // try to get it later...120 }
121122 }
123 }
124 }
125126127