1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.vo; |
15 | |
|
16 | |
import java.io.File; |
17 | |
import java.lang.reflect.Method; |
18 | |
import java.text.NumberFormat; |
19 | |
import java.text.SimpleDateFormat; |
20 | |
import java.util.Date; |
21 | |
|
22 | |
import net.admin4j.deps.commons.lang3.JavaVersion; |
23 | |
import net.admin4j.deps.commons.lang3.SystemUtils; |
24 | |
import net.admin4j.deps.commons.lang3.Validate; |
25 | |
import net.admin4j.util.Admin4jRuntimeException; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 273 | public class FileWrapperVO extends BaseVO implements Comparable<FileWrapperVO> { |
33 | |
private static final long serialVersionUID = -3043572642440858281L; |
34 | 6 | private static final boolean IS_WINDOWS = SystemUtils.OS_NAME.toLowerCase().indexOf("windows") >= 0; |
35 | |
private File file; |
36 | |
|
37 | 120 | public FileWrapperVO(File file) { |
38 | |
Validate.notNull(file, "Null file not allowed."); |
39 | 120 | this.file = file; |
40 | 120 | } |
41 | |
|
42 | |
public String getName() { |
43 | 159 | if (this.file.getParent() == null) { |
44 | 3 | return this.file.getPath().replace('\\', '/'); |
45 | |
} |
46 | 156 | return this.file.getName(); |
47 | |
} |
48 | |
|
49 | |
public String getPath() { |
50 | 3 | return this.file.getPath().replace('\\', '/'); |
51 | |
} |
52 | |
|
53 | |
public String getFullName() { |
54 | 84 | return this.file.getAbsolutePath().replace('\\', '/'); |
55 | |
} |
56 | |
|
57 | |
public boolean isWritable() { |
58 | 78 | return this.file.canWrite(); |
59 | |
} |
60 | |
|
61 | |
public boolean isReadable() { |
62 | 78 | return this.file.canRead(); |
63 | |
} |
64 | |
|
65 | |
public boolean isExecutable() { |
66 | 42 | if (IS_WINDOWS && localCanExecute(this.file) && (this.file.getName().endsWith(".cmd") || this.file.getName().endsWith(".bat"))) { |
67 | 0 | return true; |
68 | |
} |
69 | 42 | else if (IS_WINDOWS) { |
70 | 42 | return false; |
71 | |
} |
72 | 0 | else return localCanExecute(this.file); |
73 | |
} |
74 | |
|
75 | |
|
76 | |
private boolean localCanExecute(File file) { |
77 | 0 | if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_6)) { |
78 | |
try { |
79 | 120 | Method canExecuteMethod = File.class.getMethod("canExecute", (Class[])null); |
80 | 120 | return (Boolean)canExecuteMethod.invoke(file,(Object[]) null); |
81 | 0 | } catch (Exception e) { |
82 | 0 | throw new Admin4jRuntimeException("Can't execute File.canExecute()", e); |
83 | |
} |
84 | |
} |
85 | |
|
86 | 0 | return false; |
87 | |
} |
88 | |
|
89 | |
public String getAccessAttributes() { |
90 | 78 | String fileAttrs = ""; |
91 | 78 | if (file.isDirectory()) { |
92 | 42 | fileAttrs = fileAttrs + "d"; |
93 | |
} |
94 | 36 | else fileAttrs = fileAttrs + "-"; |
95 | |
|
96 | 78 | if (this.localCanExecute(file) ) { |
97 | 78 | fileAttrs = fileAttrs + "x"; |
98 | |
} |
99 | 0 | else fileAttrs = fileAttrs + "-"; |
100 | |
|
101 | 78 | if (file.canRead()) { |
102 | 78 | fileAttrs = fileAttrs + "r"; |
103 | |
} |
104 | 0 | else fileAttrs = fileAttrs + "-"; |
105 | |
|
106 | 78 | if (file.canWrite()) { |
107 | 78 | fileAttrs = fileAttrs + "w"; |
108 | |
} |
109 | 0 | else fileAttrs = fileAttrs + "-"; |
110 | |
|
111 | 78 | return fileAttrs; |
112 | |
} |
113 | |
|
114 | |
public String getSize() { |
115 | 36 | if (file.length() > 0L && file.length() < 1024 ) |
116 | 9 | return "1 KB"; |
117 | 27 | return NumberFormat.getIntegerInstance().format(file.length() / 1024) + " KB"; |
118 | |
} |
119 | |
|
120 | |
public String getDateTime() { |
121 | 78 | SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy h:mm a"); |
122 | 78 | return format.format(new Date(file.lastModified())); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public int compareTo(FileWrapperVO o) { |
129 | 276 | return this.file.compareTo(o.file); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
@Override |
136 | |
public boolean equals(Object obj) { |
137 | 3 | if (obj == null) { |
138 | 0 | return false; |
139 | |
} |
140 | 3 | FileWrapperVO rhs = null; |
141 | 3 | if (obj instanceof FileWrapperVO) { |
142 | 3 | rhs = (FileWrapperVO)obj; |
143 | 3 | return this.file.equals(rhs.file); |
144 | |
} |
145 | 0 | return false; |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
@Override |
152 | |
public int hashCode() { |
153 | 30012 | return this.file.hashCode(); |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
@Override |
160 | |
public Object clone() throws CloneNotSupportedException { |
161 | 3 | return new FileWrapperVO(this.file); |
162 | |
} |
163 | |
|
164 | |
} |