Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
85 | luk | 1 | /* |
2 | * InotifyEvent.java - inotify event class |
||
3 | * |
||
4 | * Copyright (c) 2006 Lukas Jelinek, http://www.aiken.cz |
||
5 | * |
||
6 | * ========================================================================== |
||
7 | * |
||
8 | * This program is free software; you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License Version 2 as |
||
10 | * published by the Free Software Foundation. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | * GNU General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU General Public License |
||
18 | * along with this program; if not, write to the Free Software |
||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
20 | * |
||
21 | * ========================================================================== |
||
22 | */ |
||
23 | |||
24 | package cz.aiken.util.jincron; |
||
25 | |||
26 | import java.util.*; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * @author luk |
||
31 | */ |
||
32 | public class InotifyEvent { |
||
33 | |||
34 | private int mask = 0; |
||
35 | private int flags = 0; |
||
36 | |||
37 | public static final int IN_ACCESS = 0x00000001; |
||
38 | public static final int IN_MODIFY = 0x00000002; |
||
39 | public static final int IN_ATTRIB = 0x00000004; |
||
40 | public static final int IN_CLOSE_WRITE = 0x00000008; |
||
41 | public static final int IN_CLOSE_NOWRITE = 0x00000010; |
||
42 | public static final int IN_OPEN = 0x00000020; |
||
43 | public static final int IN_MOVED_FROM = 0x00000040; |
||
44 | public static final int IN_MOVED_TO = 0x00000080; |
||
45 | public static final int IN_CREATE = 0x00000100; |
||
46 | public static final int IN_DELETE = 0x00000200; |
||
47 | public static final int IN_DELETE_SELF = 0x00000400; |
||
48 | public static final int IN_MOVE_SELF = 0x00000800; |
||
49 | |||
50 | public static final int IN_ONLYDIR = 0x01000000; |
||
51 | public static final int IN_DONT_FOLLOW = 0x02000000; |
||
52 | |||
53 | public static final int IN_ONESHOT = 0x80000000; |
||
54 | |||
55 | public static final int IN_CLOSE = IN_CLOSE_WRITE | IN_CLOSE_NOWRITE; |
||
56 | public static final int IN_MOVE = IN_MOVED_FROM | IN_MOVED_TO; |
||
57 | |||
58 | public static final int IN_ALL_EVENTS = IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE |
||
59 | | IN_OPEN | IN_MOVE | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF; |
||
60 | |||
61 | public static final int IN_NO_LOOP = 0x00000001; |
||
62 | |||
63 | private static HashMap<String, Integer> nameToMask = new HashMap<String, Integer>(); |
||
64 | private static HashMap<String, Integer> flagsToMask = new HashMap<String, Integer>(); |
||
65 | |||
66 | static { |
||
67 | nameToMask.put("IN_ACCESS", IN_ACCESS); |
||
68 | nameToMask.put("IN_MODIFY", IN_MODIFY); |
||
69 | nameToMask.put("IN_ATTRIB", IN_ATTRIB); |
||
70 | nameToMask.put("IN_CLOSE_WRITE", IN_CLOSE_WRITE); |
||
71 | nameToMask.put("IN_CLOSE_NOWRITE", IN_CLOSE_NOWRITE); |
||
72 | nameToMask.put("IN_OPEN", IN_OPEN); |
||
73 | nameToMask.put("IN_MOVED_FROM", IN_MOVED_FROM); |
||
74 | nameToMask.put("IN_MOVED_TO", IN_MOVED_TO); |
||
75 | nameToMask.put("IN_CREATE", IN_CREATE); |
||
76 | nameToMask.put("IN_DELETE", IN_DELETE); |
||
77 | nameToMask.put("IN_DELETE_SELF", IN_DELETE_SELF); |
||
78 | nameToMask.put("IN_MOVE_SELF", IN_MOVE_SELF); |
||
79 | nameToMask.put("IN_ONLYDIR", IN_ONLYDIR); |
||
80 | nameToMask.put("IN_DONT_FOLLOW", IN_DONT_FOLLOW); |
||
81 | nameToMask.put("IN_ONESHOT", IN_ONESHOT); |
||
82 | nameToMask.put("IN_CLOSE", IN_CLOSE); |
||
83 | nameToMask.put("IN_MOVE", IN_MOVE); |
||
84 | nameToMask.put("IN_ALL_EVENTS", IN_ALL_EVENTS); |
||
85 | |||
86 | flagsToMask.put("IN_NO_LOOP", IN_NO_LOOP); |
||
87 | } |
||
88 | |||
89 | /** Creates a new instance of InotifyEvent */ |
||
90 | public InotifyEvent(int mask, int flags) { |
||
91 | this.mask = mask; |
||
92 | this.flags = flags; |
||
93 | } |
||
94 | |||
95 | public InotifyEvent(InotifyEvent src) { |
||
96 | this(src.getMask(), src.getFlags()); |
||
97 | } |
||
98 | |||
99 | public static InotifyEvent parse(String text) { |
||
100 | int mask = 0; |
||
101 | int flags = 0; |
||
102 | |||
103 | try { |
||
104 | mask = Integer.parseInt(text); |
||
105 | } catch (NumberFormatException e) { |
||
106 | String sa[] = text.split(","); |
||
107 | for (int i=0; i<sa.length; i++) { |
||
108 | String s = sa[i].trim(); |
||
109 | mask |= getMaskByName(s); |
||
110 | flags |= getFlagsByName(s); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | return new InotifyEvent(mask, flags); |
||
115 | } |
||
116 | |||
117 | public int getMask() { |
||
118 | return mask; |
||
119 | } |
||
120 | |||
121 | public void setMask(int mask) { |
||
122 | this.mask = mask; |
||
123 | } |
||
124 | |||
125 | public boolean hasMask(int mask) { |
||
126 | return (mask & this.mask) == mask; |
||
127 | } |
||
128 | |||
129 | public void changeMask(int mask, boolean add) { |
||
130 | if (add) { |
||
131 | this.mask |= mask; |
||
132 | } |
||
133 | else { |
||
134 | this.mask &= ~mask; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | public int getFlags() { |
||
139 | return flags; |
||
140 | } |
||
141 | |||
142 | public void setFlags(int flags) { |
||
143 | this.flags = flags; |
||
144 | } |
||
145 | |||
146 | public boolean hasFlags(int flags) { |
||
147 | return (flags & this.flags) == flags; |
||
148 | } |
||
149 | |||
150 | public void changeFlags(int flags, boolean add) { |
||
151 | if (add) { |
||
152 | this.flags |= flags; |
||
153 | } |
||
154 | else { |
||
155 | this.flags &= ~flags; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | public String toString() { |
||
160 | StringBuilder sb = new StringBuilder(); |
||
161 | |||
162 | if ((mask & IN_ALL_EVENTS) == IN_ALL_EVENTS) { |
||
163 | if (sb.length() > 0) |
||
164 | sb.append(','); |
||
165 | sb.append("IN_ALL_EVENTS"); |
||
166 | } |
||
167 | else { |
||
168 | if ((mask & IN_MOVE) == IN_MOVE) { |
||
169 | if (sb.length() > 0) |
||
170 | sb.append(','); |
||
171 | sb.append("IN_MOVE"); |
||
172 | } |
||
173 | else { |
||
174 | if ((mask & IN_MOVED_FROM) == IN_MOVED_FROM) { |
||
175 | if (sb.length() > 0) |
||
176 | sb.append(','); |
||
177 | sb.append("IN_MOVED_FROM"); |
||
178 | } |
||
179 | else if ((mask & IN_MOVED_TO) == IN_MOVED_TO) { |
||
180 | if (sb.length() > 0) |
||
181 | sb.append(','); |
||
182 | sb.append("IN_MOVED_TO"); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | if ((mask & IN_CLOSE) == IN_CLOSE) { |
||
187 | if (sb.length() > 0) |
||
188 | sb.append(','); |
||
189 | sb.append("IN_CLOSE"); |
||
190 | } |
||
191 | else { |
||
192 | if ((mask & IN_CLOSE_WRITE) == IN_CLOSE_WRITE) { |
||
193 | if (sb.length() > 0) |
||
194 | sb.append(','); |
||
195 | sb.append("IN_CLOSE_WRITE"); |
||
196 | } |
||
197 | else if ((mask & IN_CLOSE_NOWRITE) == IN_CLOSE_NOWRITE) { |
||
198 | if (sb.length() > 0) |
||
199 | sb.append(','); |
||
200 | sb.append("IN_CLOSE_NOWRITE"); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | if ((mask & IN_ACCESS) == IN_ACCESS) { |
||
205 | if (sb.length() > 0) |
||
206 | sb.append(','); |
||
207 | sb.append("IN_ACCESS"); |
||
208 | } |
||
209 | |||
210 | if ((mask & IN_MODIFY) == IN_MODIFY) { |
||
211 | if (sb.length() > 0) |
||
212 | sb.append(','); |
||
213 | sb.append("IN_MODIFY"); |
||
214 | } |
||
215 | |||
216 | if ((mask & IN_ATTRIB) == IN_ATTRIB) { |
||
217 | if (sb.length() > 0) |
||
218 | sb.append(','); |
||
219 | sb.append("IN_ATTRIB"); |
||
220 | } |
||
221 | |||
222 | if ((mask & IN_OPEN) == IN_OPEN) { |
||
223 | if (sb.length() > 0) |
||
224 | sb.append(','); |
||
225 | sb.append("IN_OPEN"); |
||
226 | } |
||
227 | |||
228 | if ((mask & IN_CREATE) == IN_CREATE) { |
||
229 | if (sb.length() > 0) |
||
230 | sb.append(','); |
||
231 | sb.append("IN_CREATE"); |
||
232 | } |
||
233 | |||
234 | if ((mask & IN_DELETE) == IN_DELETE) { |
||
235 | if (sb.length() > 0) |
||
236 | sb.append(','); |
||
237 | sb.append("IN_DELETE"); |
||
238 | } |
||
239 | |||
240 | if ((mask & IN_DELETE_SELF) == IN_DELETE_SELF) { |
||
241 | if (sb.length() > 0) |
||
242 | sb.append(','); |
||
243 | sb.append("IN_DELETE_SELF"); |
||
244 | } |
||
245 | |||
246 | if ((mask & IN_MOVE_SELF) == IN_MOVE_SELF) { |
||
247 | if (sb.length() > 0) |
||
248 | sb.append(','); |
||
249 | sb.append("IN_MOVE_SELF"); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | if ((mask & IN_ONLYDIR) == IN_ONLYDIR) { |
||
254 | if (sb.length() > 0) |
||
255 | sb.append(','); |
||
256 | sb.append("IN_ONLYDIR"); |
||
257 | } |
||
258 | |||
259 | if ((mask & IN_DONT_FOLLOW) == IN_DONT_FOLLOW) { |
||
260 | if (sb.length() > 0) |
||
261 | sb.append(','); |
||
262 | sb.append("IN_DONT_FOLLOW"); |
||
263 | } |
||
264 | |||
265 | if ((mask & IN_ONESHOT) == IN_ONESHOT) { |
||
266 | if (sb.length() > 0) |
||
267 | sb.append(','); |
||
268 | sb.append("IN_ONESHOT"); |
||
269 | } |
||
270 | |||
271 | if ((flags & IN_NO_LOOP) == IN_NO_LOOP) { |
||
272 | if (sb.length() > 0) |
||
273 | sb.append(','); |
||
274 | sb.append("IN_NO_LOOP"); |
||
275 | } |
||
276 | |||
277 | return sb.toString(); |
||
278 | } |
||
279 | |||
280 | public static int getMaskByName(String name) { |
||
281 | Integer i = nameToMask.get(name); |
||
282 | return i != null ? i.intValue() : 0; |
||
283 | } |
||
284 | |||
285 | public static int getFlagsByName(String name) { |
||
286 | Integer i = flagsToMask.get(name); |
||
287 | return i != null ? i.intValue() : 0; |
||
288 | } |
||
289 | } |
||
290 | |||
291 |