KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > notify > NotifyRequest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.server.notify;
18
19 import java.util.Date JavaDoc;
20
21 import org.alfresco.filesys.server.filesys.DiskDeviceContext;
22 import org.alfresco.filesys.server.filesys.NetworkFile;
23 import org.alfresco.filesys.server.filesys.NotifyChange;
24 import org.alfresco.filesys.smb.server.SMBSrvSession;
25
26 /**
27  * Notify Change Request Details Class
28  */

29 public class NotifyRequest
30 {
31
32     // Constants
33

34     public final static long DefaultRequestTimeout = 10000L; // 10 seconds
35

36     // Notify change filter
37

38     private int m_filter;
39
40     // Flag to indicate if sub-directories of the directory being watched will also trigger
41
// notifications
42

43     private boolean m_watchTree;
44
45     // Session that posted the notify change request
46

47     private SMBSrvSession m_sess;
48
49     // Directory being watched
50

51     private NetworkFile m_watchDir;
52
53     // Root relative path, normalised to uppercase
54

55     private String JavaDoc m_watchPath;
56
57     // Unique client request id.
58
//
59
// If the multiplex id equals -1 the request has completed and we are waiting for the request to
60
// be reset with a
61
// new multiplex id.
62

63     private int m_mid;
64     private int m_tid;
65     private int m_pid;
66     private int m_uid;
67
68     // Notifications to buffer whilst waiting for request to be reset
69

70     private int m_maxQueueLen;
71
72     // Disk device context that the request is associated with
73

74     private DiskDeviceContext m_diskCtx;
75
76     // Buffered event list
77

78     private NotifyChangeEventList m_bufferedEvents;
79
80     // Notify request completed flag
81

82     private boolean m_completed;
83     private long m_expiresAt;
84
85     // Flag to indicate that many file changes have occurred and a notify enum status should be
86
// returned
87
// to the client
88

89     private boolean m_notifyEnum;
90
91     /**
92      * Class constructor
93      *
94      * @param filter int
95      * @param watchTree boolean
96      * @param sess SMBSrvSession
97      * @param dir NetworkFile
98      * @param mid int
99      * @param tid int
100      * @param pid int
101      * @param uid int
102      * @param qlen int
103      */

104     public NotifyRequest(int filter, boolean watchTree, SMBSrvSession sess, NetworkFile dir, int mid, int tid, int pid,
105             int uid, int qlen)
106     {
107         m_filter = filter;
108         m_watchTree = watchTree;
109         m_sess = sess;
110         m_watchDir = dir;
111
112         m_mid = mid;
113         m_tid = tid;
114         m_pid = pid;
115         m_uid = uid;
116
117         m_maxQueueLen = qlen;
118
119         // Set the normalised watch path
120

121         m_watchPath = m_watchDir.getFullName().toUpperCase();
122         if (m_watchPath.length() == 0)
123             m_watchPath = "\\";
124         else if (m_watchPath.indexOf('/') != -1)
125             m_watchPath.replace('/', '\\');
126     }
127
128     /**
129      * Get the notify change filter
130      *
131      * @return int
132      */

133     public final int getFilter()
134     {
135         return m_filter;
136     }
137
138     /**
139      * Determine if the request has completed
140      *
141      * @return boolean
142      */

143     public final boolean isCompleted()
144     {
145         return m_completed;
146     }
147
148     /**
149      * Determine if the request has expired
150      *
151      * @param curTime long
152      * @return boolean
153      */

154     public final boolean hasExpired(long curTime)
155     {
156         if (isCompleted() == false)
157             return false;
158         else if (m_expiresAt < curTime)
159             return true;
160         return false;
161     }
162
163     /**
164      * Determine if the filter has file name change notification, triggered if a file is created,
165      * renamed or deleted
166      *
167      * @return boolean
168      */

169     public final boolean hasFileNameChange()
170     {
171         return hasFilter(NotifyChange.FileName);
172     }
173
174     /**
175      * Determine if the filter has directory name change notification, triggered if a directory is
176      * created or deleted.
177      *
178      * @return boolean
179      */

180     public final boolean hasDirectoryNameChange()
181     {
182         return hasFilter(NotifyChange.DirectoryName);
183     }
184
185     /**
186      * Determine if the filter has attribute change notification
187      *
188      * @return boolean
189      */

190     public final boolean hasAttributeChange()
191     {
192         return hasFilter(NotifyChange.Attributes);
193     }
194
195     /**
196      * Determine if the filter has file size change notification
197      *
198      * @return boolean
199      */

200     public final boolean hasFileSizeChange()
201     {
202         return hasFilter(NotifyChange.Size);
203     }
204
205     /**
206      * Determine if the filter has last write time change notification
207      *
208      * @return boolean
209      */

210     public final boolean hasFileWriteTimeChange()
211     {
212         return hasFilter(NotifyChange.LastWrite);
213     }
214
215     /**
216      * Determine if the filter has last access time change notification
217      *
218      * @return boolean
219      */

220     public final boolean hasFileAccessTimeChange()
221     {
222         return hasFilter(NotifyChange.LastAccess);
223     }
224
225     /**
226      * Determine if the filter has creation time change notification
227      *
228      * @return boolean
229      */

230     public final boolean hasFileCreateTimeChange()
231     {
232         return hasFilter(NotifyChange.Creation);
233     }
234
235     /**
236      * Determine if the filter has the security descriptor change notification
237      *
238      * @return boolean
239      */

240     public final boolean hasSecurityDescriptorChange()
241     {
242         return hasFilter(NotifyChange.Security);
243     }
244
245     /**
246      * Check if the change filter has the specified flag enabled
247      *
248      * @param flag
249      * @return boolean
250      */

251     public final boolean hasFilter(int flag)
252     {
253         return (m_filter & flag) != 0 ? true : false;
254     }
255
256     /**
257      * Check if the notify enum flag is set
258      *
259      * @return boolean
260      */

261     public final boolean hasNotifyEnum()
262     {
263         return m_notifyEnum;
264     }
265
266     /**
267      * Determine if sub-directories of the directory being watched should also trigger notifications
268      *
269      * @return boolean
270      */

271     public final boolean hasWatchTree()
272     {
273         return m_watchTree;
274     }
275
276     /**
277      * Get the session that posted the notify request
278      *
279      * @return SMBSrvSession
280      */

281     public final SMBSrvSession getSession()
282     {
283         return m_sess;
284     }
285
286     /**
287      * Get the directory being watched
288      *
289      * @return NetworkFile
290      */

291     public final NetworkFile getDirectory()
292     {
293         return m_watchDir;
294     }
295
296     /**
297      * Get the normalised watch path
298      *
299      * @return String
300      */

301     public final String JavaDoc getWatchPath()
302     {
303         return m_watchPath;
304     }
305
306     /**
307      * Get the multiplex-id of the request
308      *
309      * @return int
310      */

311     public final int getMultiplexId()
312     {
313         return m_mid;
314     }
315
316     /**
317      * Get the tree id of the request
318      *
319      * @return int
320      */

321     public final int getTreeId()
322     {
323         return m_tid;
324     }
325
326     /**
327      * Get the process id of the request
328      *
329      * @return int
330      */

331     public final int getProcessId()
332     {
333         return m_pid;
334     }
335
336     /**
337      * Get the user id of the request
338      *
339      * @return int
340      */

341     public final int getUserId()
342     {
343         return m_uid;
344     }
345
346     /**
347      * Return the expiry time that a completed request must be reset by before being removed from
348      * the queue.
349      *
350      * @return long
351      */

352     public final long getExpiryTime()
353     {
354         return m_expiresAt;
355     }
356
357     /**
358      * Get the associated disk context
359      *
360      * @return DiskDeviceContext
361      */

362     public final DiskDeviceContext getDiskContext()
363     {
364         return m_diskCtx;
365     }
366
367     /**
368      * Return the maximum number of notifications to buffer whilst waiting for the request to be
369      * reset
370      *
371      * @return int
372      */

373     public final int getMaximumQueueLength()
374     {
375         return m_maxQueueLen;
376     }
377
378     /**
379      * Determine if there are buffered events
380      *
381      * @return boolean
382      */

383     public final boolean hasBufferedEvents()
384     {
385         if (m_bufferedEvents != null && m_bufferedEvents.numberOfEvents() > 0)
386             return true;
387         return false;
388     }
389
390     /**
391      * Return the buffered notification event list
392      *
393      * @return NotifyChangeEventList
394      */

395     public final NotifyChangeEventList getBufferedEventList()
396     {
397         return m_bufferedEvents;
398     }
399
400     /**
401      * Add a buffered notification event, to be sent when the notify request is reset by the client
402      *
403      * @param evt NotifyChangeEvent
404      */

405     public final void addEvent(NotifyChangeEvent evt)
406     {
407
408         // Check if the notify enum flag is set, if so then do not buffer any events
409

410         if (hasNotifyEnum())
411             return;
412
413         // Check if the buffered event list has been allocated
414

415         if (m_bufferedEvents == null)
416             m_bufferedEvents = new NotifyChangeEventList();
417
418         // Add the event if the list has not reached the maximum buffered event count
419

420         if (m_bufferedEvents.numberOfEvents() < getMaximumQueueLength())
421         {
422
423             // Buffer the event until the client resets the notify filter
424

425             m_bufferedEvents.addEvent(evt);
426         }
427         else
428         {
429
430             // Remove all buffered events and set the notify enum flag to indicate that there
431
// have been many file changes
432

433             removeAllEvents();
434             setNotifyEnum(true);
435         }
436     }
437
438     /**
439      * Remove all buffered events from the request
440      */

441     public final void removeAllEvents()
442     {
443         if (m_bufferedEvents != null)
444         {
445             m_bufferedEvents.removeAllEvents();
446             m_bufferedEvents = null;
447         }
448     }
449
450     /**
451      * Clear the buffered event list, do not destroy the list
452      */

453     public final void clearBufferedEvents()
454     {
455         m_bufferedEvents = null;
456     }
457
458     /**
459      * Set/clear the notify enum flag that indicates if there have been many file changes
460      *
461      * @param ena boolean
462      */

463     public final void setNotifyEnum(boolean ena)
464     {
465         m_notifyEnum = ena;
466     }
467
468     /**
469      * Set the associated disk device context
470      *
471      * @param ctx DiskDeviceContext
472      */

473     protected final void setDiskContext(DiskDeviceContext ctx)
474     {
475         m_diskCtx = ctx;
476     }
477
478     /**
479      * Set the multiplex id for the notification
480      *
481      * @param mid int
482      */

483     public final void setMultiplexId(int mid)
484     {
485         m_mid = mid;
486     }
487
488     /**
489      * Set the request completed flag
490      *
491      * @param comp boolean
492      */

493     public final void setCompleted(boolean comp)
494     {
495         m_completed = comp;
496
497         if (comp)
498             m_expiresAt = System.currentTimeMillis() + DefaultRequestTimeout;
499     }
500
501     /**
502      * Set the request completed flag and set an expiry time when the request expires
503      *
504      * @param comp boolean
505      * @param expire long
506      */

507     public final void setCompleted(boolean comp, long expires)
508     {
509         m_completed = comp;
510         m_expiresAt = expires;
511     }
512
513     /**
514      * Return the notify request as a string
515      *
516      * @return String
517      */

518     public String JavaDoc toString()
519     {
520         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
521
522         str.append("[");
523
524         str.append(getSession().getUniqueId());
525         str.append(":");
526
527         if (getWatchPath().length() == 0)
528             str.append("Root");
529         else
530             str.append(getWatchPath());
531         str.append(":");
532
533         if (hasFileNameChange())
534             str.append("File,");
535
536         if (hasDirectoryNameChange())
537             str.append("Dir,");
538
539         if (hasAttributeChange())
540             str.append("Attr,");
541
542         if (hasFileSizeChange())
543             str.append("Size,");
544
545         if (hasFileWriteTimeChange())
546             str.append("Write,");
547
548         if (hasFileAccessTimeChange())
549             str.append("Access,");
550
551         if (hasFileCreateTimeChange())
552             str.append("Create,");
553
554         if (hasSecurityDescriptorChange())
555             str.append("Security,");
556
557         if (hasWatchTree())
558             str.append("Tree");
559         else
560             str.append("NoTree");
561
562         str.append(" MID=");
563         str.append(getMultiplexId());
564
565         str.append(" PID=");
566         str.append(getProcessId());
567
568         str.append(" TID=");
569         str.append(getTreeId());
570
571         str.append(" UID=");
572         str.append(getUserId());
573
574         if (isCompleted())
575         {
576             str.append(",Completed,TMO=");
577             str.append(new Date JavaDoc(getExpiryTime()).toString());
578         }
579
580         str.append(",Queue=");
581         str.append(getMaximumQueueLength());
582         if (hasBufferedEvents())
583         {
584             str.append("/");
585             str.append(getBufferedEventList().numberOfEvents());
586         }
587
588         if (hasNotifyEnum())
589             str.append(",ENUM");
590
591         str.append("]");
592
593         return str.toString();
594     }
595 }
596
Popular Tags