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 at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * 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 and
14 * limitations under the License.
15 */
16
17 package org.apache.jetspeed.util;
18
19 import java.io.File;
20
21 // Jetspeed classes
22 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
23 import org.apache.jetspeed.services.logging.JetspeedLogger;
24
25
26 /*
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 */
33 public class DirectoryUtils
34 {
35 /***
36 * Static initialization of the logger for this class
37 */
38 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(DirectoryUtils.class.getName());
39
40 public static void main(String[] args)
41 {
42 DirectoryUtils.rmdir(args[0]);
43 }
44
45 /*
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 */
52 public static final boolean rmdir(String directory)
53 {
54 try
55 {
56 File dir = new File(directory);
57 if (!dir.isDirectory())
58 {
59 dir.delete();
60 return true;
61 }
62
63 deleteTraversal(directory);
64
65 dir.delete();
66 return true;
67
68 }
69 catch (Exception e)
70 {
71 logger.error("Error in rmdir utility:" , e);
72 return false;
73 }
74 }
75
76 /*
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 */
85 private static void deleteTraversal(String path)
86 {
87 File file = new File(path);
88 if (file.isFile())
89 {
90 try
91 {
92 file.delete();
93 }
94 catch (Exception e)
95 {
96 logger.error("Failed to Delete file: " + path + " : " , e);
97 file.deleteOnExit(); // try to get it later...
98 }
99 }
100 else if (file.isDirectory())
101 {
102 if (!path.endsWith(File.separator))
103 path += File.separator;
104
105 String list[] = file.list();
106
107 // Process all files recursivly
108 for(int ix = 0; list != null && ix < list.length; ix++)
109 deleteTraversal(path + list[ix]);
110
111 // now try to delete the directory
112 try
113 {
114 file.delete();
115 }
116 catch (Exception e)
117 {
118 logger.error("Failed to Delete directory: " + path + " : " , e);
119 file.deleteOnExit(); // try to get it later...
120 }
121
122 }
123 }
124 }
125
126
127