1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util;
18
19 import java.io.File;
20
21
22 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
23 import org.apache.jetspeed.services.logging.JetspeedLogger;
24
25
26
27
28
29
30
31
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
47
48
49
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
78
79
80
81
82
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();
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
108 for(int ix = 0; list != null && ix < list.length; ix++)
109 deleteTraversal(path + list[ix]);
110
111
112 try
113 {
114 file.delete();
115 }
116 catch (Exception e)
117 {
118 logger.error("Failed to Delete directory: " + path + " : " , e);
119 file.deleteOnExit();
120 }
121
122 }
123 }
124 }
125
126
127