KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > filesys > NetworkFile


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.server.filesys;
18
19 import java.io.IOException JavaDoc;
20
21 import org.alfresco.filesys.locking.FileLock;
22 import org.alfresco.filesys.locking.FileLockList;
23
24 /**
25  * <p>
26  * The network file represents a file or directory on a filesystem. The server keeps track of the
27  * open files on a per session basis.
28  * <p>
29  * This class may be extended as required by your own disk driver class.
30  */

31 public abstract class NetworkFile
32 {
33
34     // Granted file access types
35

36     public static final int READONLY = 0;
37     public static final int WRITEONLY = 1;
38     public static final int READWRITE = 2;
39
40     // File status flags
41

42     public static final int IOPending = 0x0001;
43     public static final int DeleteOnClose = 0x0002;
44
45     // File identifier and parent directory identifier
46

47     protected int m_fid;
48     protected int m_dirId;
49
50     // Unique file identifier
51

52     protected long m_uniqueId;
53
54     // File/directory name
55

56     protected String JavaDoc m_name;
57
58     // Stream name and id
59

60     protected String JavaDoc m_streamName;
61     protected int m_streamId;
62
63     // Full name, relative to the share
64

65     protected String JavaDoc m_fullName;
66
67     // File attributes
68

69     protected int m_attrib;
70
71     // File size
72

73     protected long m_fileSize;
74
75     // File creation/modify/last access date/time
76

77     protected long m_createDate;
78     protected long m_modifyDate;
79     protected long m_accessDate;
80
81     // Granted file access type
82

83     protected int m_grantedAccess;
84
85     // Flag to indicate that the file has been closed
86

87     protected boolean m_closed = true;
88
89     // Count of write requests to the file, used to determine if the file size may have changed
90

91     protected int m_writeCount;
92
93     // List of locks on this file by this session. The lock object will almost certainly be
94
// referenced elsewhere depending upon the LockManager implementation used. If locking support is not
95
// enabled for the DiskInterface implementation the lock list will not be allocated.
96
//
97
// This lock list is used to release locks on the file if the session abnormally terminates or
98
// closes the file without releasing all locks.
99

100     private FileLockList m_lockList;
101
102     // File status flags
103

104     private int m_flags;
105
106     /**
107      * Create a network file object with the specified file identifier.
108      *
109      * @param fid int
110      */

111     public NetworkFile(int fid)
112     {
113         m_fid = fid;
114     }
115
116     /**
117      * Create a network file with the specified file and parent directory ids
118      *
119      * @param fid int
120      * @param did int
121      */

122     public NetworkFile(int fid, int did)
123     {
124         m_fid = fid;
125         m_dirId = did;
126     }
127
128     /**
129      * Create a network file with the specified file id, stream id and parent directory id
130      *
131      * @param fid int
132      * @param stid int
133      * @param did int
134      */

135     public NetworkFile(int fid, int stid, int did)
136     {
137         m_fid = fid;
138         m_streamId = stid;
139         m_dirId = did;
140     }
141
142     /**
143      * Create a network file object with the specified file/directory name.
144      *
145      * @param name File name string.
146      */

147     public NetworkFile(String JavaDoc name)
148     {
149         m_name = name;
150     }
151
152     /**
153      * Return the parent directory identifier
154      *
155      * @return int
156      */

157     public final int getDirectoryId()
158     {
159         return m_dirId;
160     }
161
162     /**
163      * Return the file attributes.
164      *
165      * @return int
166      */

167     public final int getFileAttributes()
168     {
169         return m_attrib;
170     }
171
172     /**
173      * Return the file identifier.
174      *
175      * @return int
176      */

177     public final int getFileId()
178     {
179         return m_fid;
180     }
181
182     /**
183      * Get the file size, in bytes.
184      *
185      * @return long
186      */

187     public final long getFileSize()
188     {
189         return m_fileSize;
190     }
191
192     /**
193      * Get the file size, in bytes.
194      *
195      * @return int
196      */

197     public final int getFileSizeInt()
198     {
199         return (int) (m_fileSize & 0x0FFFFFFFFL);
200     }
201
202     /**
203      * Return the full name, relative to the share.
204      *
205      * @return java.lang.String
206      */

207     public final String JavaDoc getFullName()
208     {
209         return m_fullName;
210     }
211
212     /**
213      * Return the full name including the stream name, relative to the share.
214      *
215      * @return java.lang.String
216      */

217     public final String JavaDoc getFullNameStream()
218     {
219         if (isStream())
220             return m_fullName + m_streamName;
221         else
222             return m_fullName;
223     }
224
225     /**
226      * Return the granted file access mode.
227      */

228     public final int getGrantedAccess()
229     {
230         return m_grantedAccess;
231     }
232
233     /**
234      * Return the file/directory name.
235      *
236      * @return java.lang.String
237      */

238     public String JavaDoc getName()
239     {
240         return m_name;
241     }
242
243     /**
244      * Return the stream id, zero indicates the main file stream
245      *
246      * @return int
247      */

248     public final int getStreamId()
249     {
250         return m_streamId;
251     }
252
253     /**
254      * Return the stream name, if this is a stream
255      *
256      * @return String
257      */

258     public final String JavaDoc getStreamName()
259     {
260         return m_streamName;
261     }
262
263     /**
264      * Return the unique file identifier
265      *
266      * @return long
267      */

268     public final long getUniqueId()
269     {
270         return m_uniqueId;
271     }
272
273     /**
274      * Determine if the file has been closed.
275      *
276      * @return boolean
277      */

278     public final boolean isClosed()
279     {
280         return m_closed;
281     }
282
283     /**
284      * Return the directory file attribute status.
285      *
286      * @return true if the file is a directory, else false.
287      */

288
289     public final boolean isDirectory()
290     {
291         return (m_attrib & FileAttribute.Directory) != 0 ? true : false;
292     }
293
294     /**
295      * Return the hidden file attribute status.
296      *
297      * @return true if the file is hidden, else false.
298      */

299
300     public final boolean isHidden()
301     {
302         return (m_attrib & FileAttribute.Hidden) != 0 ? true : false;
303     }
304
305     /**
306      * Return the read-only file attribute status.
307      *
308      * @return true if the file is read-only, else false.
309      */

310
311     public final boolean isReadOnly()
312     {
313         return (m_attrib & FileAttribute.ReadOnly) != 0 ? true : false;
314     }
315
316     /**
317      * Return the system file attribute status.
318      *
319      * @return true if the file is a system file, else false.
320      */

321
322     public final boolean isSystem()
323     {
324         return (m_attrib & FileAttribute.System) != 0 ? true : false;
325     }
326
327     /**
328      * Return the archived attribute status
329      *
330      * @return boolean
331      */

332     public final boolean isArchived()
333     {
334         return (m_attrib & FileAttribute.Archive) != 0 ? true : false;
335     }
336
337     /**
338      * Check if this is a stream file
339      *
340      * @return boolean
341      */

342     public final boolean isStream()
343     {
344         return m_streamName != null ? true : false;
345     }
346
347     /**
348      * Check if there are active locks on this file by this session
349      *
350      * @return boolean
351      */

352     public final boolean hasLocks()
353     {
354         if (m_lockList != null && m_lockList.numberOfLocks() > 0)
355             return true;
356         return false;
357     }
358
359     /**
360      * Check for NT attributes
361      *
362      * @param attr int
363      * @return boolean
364      */

365     public final boolean hasNTAttribute(int attr)
366     {
367         return (m_attrib & attr) == attr ? true : false;
368     }
369
370     /**
371      * Determine if the file access date/time is valid
372      *
373      * @return boolean
374      */

375     public final boolean hasAccessDate()
376     {
377         return m_accessDate != 0L ? true : false;
378     }
379
380     /**
381      * Return the file access date/time
382      *
383      * @return long
384      */

385     public final long getAccessDate()
386     {
387         return m_accessDate;
388     }
389
390     /**
391      * Determine if the file creation date/time is valid
392      *
393      * @return boolean
394      */

395     public final boolean hasCreationDate()
396     {
397         return m_createDate != 0L ? true : false;
398     }
399
400     /**
401      * Return the file creation date/time
402      *
403      * @return long
404      */

405     public final long getCreationDate()
406     {
407         return m_createDate;
408     }
409
410     /**
411      * Check if the delete on close flag has been set for this file
412      *
413      * @return boolean
414      */

415     public final boolean hasDeleteOnClose()
416     {
417         return (m_flags & DeleteOnClose) != 0 ? true : false;
418     }
419
420     /**
421      * Check if the file has an I/O request pending
422      *
423      * @return boolean
424      */

425     public final boolean hasIOPending()
426     {
427         return (m_flags & IOPending) != 0 ? true : false;
428     }
429
430     /**
431      * Determine if the file modification date/time is valid
432      *
433      * @return boolean
434      */

435     public boolean hasModifyDate()
436     {
437         return m_modifyDate != 0L ? true : false;
438     }
439
440     /**
441      * Return the file modify date/time
442      *
443      * @return long
444      */

445     public final long getModifyDate()
446     {
447         return m_modifyDate;
448     }
449
450     /**
451      * Get the write count for the file
452      *
453      * @return int
454      */

455     public final int getWriteCount()
456     {
457         return m_writeCount;
458     }
459
460     /**
461      * Increment the write count
462      */

463     public final void incrementWriteCount()
464     {
465         m_writeCount++;
466     }
467
468     /**
469      * Set the file attributes, as specified by the SMBFileAttribute class.
470      *
471      * @param attrib int
472      */

473     public final void setAttributes(int attrib)
474     {
475         m_attrib = attrib;
476     }
477
478     /**
479      * Set, or clear, the delete on close flag
480      *
481      * @param del boolean
482      */

483     public final void setDeleteOnClose(boolean del)
484     {
485         setStatusFlag(DeleteOnClose, del);
486     }
487
488     /**
489      * Set the parent directory identifier
490      *
491      * @param dirId int
492      */

493     public final void setDirectoryId(int dirId)
494     {
495         m_dirId = dirId;
496     }
497
498     /**
499      * Set the file identifier.
500      *
501      * @param fid int
502      */

503     public final void setFileId(int fid)
504     {
505         m_fid = fid;
506     }
507
508     /**
509      * Set the file size.
510      *
511      * @param siz long
512      */

513     public final void setFileSize(long siz)
514     {
515         m_fileSize = siz;
516     }
517
518     /**
519      * Set the file size.
520      *
521      * @param siz int
522      */

523     public final void setFileSize(int siz)
524     {
525         m_fileSize = (long) siz;
526     }
527
528     /**
529      * Set the full file name, relative to the share.
530      *
531      * @param name java.lang.String
532      */

533     public final void setFullName(String JavaDoc name)
534     {
535         m_fullName = name;
536     }
537
538     /**
539      * Set the granted file access mode.
540      *
541      * @param mode int
542      */

543     public final void setGrantedAccess(int mode)
544     {
545         m_grantedAccess = mode;
546     }
547
548     /**
549      * Set the file name.
550      *
551      * @param name String
552      */

553     public final void setName(String JavaDoc name)
554     {
555         m_name = name;
556     }
557
558     /**
559      * set/clear the I/O pending flag
560      *
561      * @param pending boolean
562      */

563     public final void setIOPending(boolean pending)
564     {
565         setStatusFlag(IOPending, pending);
566     }
567
568     /**
569      * Set the stream id
570      *
571      * @param id int
572      */

573     public final void setStreamId(int id)
574     {
575         m_streamId = id;
576     }
577
578     /**
579      * Set the stream name
580      *
581      * @param name String
582      */

583     public final void setStreamName(String JavaDoc name)
584     {
585         m_streamName = name;
586     }
587
588     /**
589      * Set the file closed state.
590      *
591      * @param b boolean
592      */

593     public final synchronized void setClosed(boolean b)
594     {
595         m_closed = b;
596     }
597
598     /**
599      * Set the file access date/time
600      *
601      * @param dattim long
602      */

603     public final void setAccessDate(long dattim)
604     {
605         m_accessDate = dattim;
606     }
607
608     /**
609      * Set the file creation date/time
610      *
611      * @param dattim long
612      */

613     public final void setCreationDate(long dattim)
614     {
615         m_createDate = dattim;
616     }
617
618     /**
619      * Set the file modification date/time
620      *
621      * @param dattim long
622      */

623     public final void setModifyDate(long dattim)
624     {
625         m_modifyDate = dattim;
626     }
627
628     /**
629      * Set/clear a file status flag
630      *
631      * @param flag int
632      * @param sts boolean
633      */

634     protected final synchronized void setStatusFlag(int flag, boolean sts)
635     {
636         boolean state = (m_flags & flag) != 0;
637         if (sts == true && state == false)
638             m_flags += flag;
639         else if (sts == false && state == true)
640             m_flags -= flag;
641     }
642
643     /**
644      * Add a lock to the active lock list
645      *
646      * @param lock FileLock
647      */

648     public final synchronized void addLock(FileLock lock)
649     {
650
651         // Check if the lock list has been allocated
652

653         if (m_lockList == null)
654             m_lockList = new FileLockList();
655
656         // Add the lock
657

658         m_lockList.addLock(lock);
659     }
660
661     /**
662      * Remove a lock from the active lock list
663      *
664      * @param lock FileLock
665      */

666     public final synchronized void removeLock(FileLock lock)
667     {
668
669         // Check if the lock list is allocated
670

671         if (m_lockList == null)
672             return;
673
674         // Remove the lock
675

676         m_lockList.removeLock(lock);
677     }
678
679     /**
680      * Remove all locks from the lock list
681      */

682     public final synchronized void removeAllLocks()
683     {
684
685         // Check if the lock list is valid
686

687         if (m_lockList != null)
688             m_lockList.removeAllLocks();
689     }
690
691     /**
692      * Return the count of active locks
693      *
694      * @return int
695      */

696     public final int numberOfLocks()
697     {
698
699         // Check if the lock list is allocated
700

701         if (m_lockList == null)
702             return 0;
703         return m_lockList.numberOfLocks();
704     }
705
706     /**
707      * Get the details of an active lock from the list
708      *
709      * @param idx int
710      * @return FileLock
711      */

712     public final FileLock getLockAt(int idx)
713     {
714
715         // Check if the lock list is allocated and the index is valid
716

717         if (m_lockList != null)
718             return m_lockList.getLockAt(idx);
719
720         // Invalid index or lock list not valid
721

722         return null;
723     }
724
725     /**
726      * Return the lock list
727      *
728      * @return FileLockList
729      */

730     public final FileLockList getLockList()
731     {
732         return m_lockList;
733     }
734
735     /**
736      * Set the unique file identifier
737      *
738      * @param id long
739      */

740     protected final void setUniqueId(long id)
741     {
742         m_uniqueId = id;
743     }
744
745     /**
746      * Set the unique id using the file and directory id
747      *
748      * @param fid int
749      * @param did int
750      */

751     protected final void setUniqueId(int fid, int did)
752     {
753         long ldid = (long) did;
754         long lfid = (long) fid;
755         m_uniqueId = (ldid << 32) + lfid;
756     }
757
758     /**
759      * Set the unique id using the full path string
760      *
761      * @param path String
762      */

763     protected final void setUniqueId(String JavaDoc path)
764     {
765         m_uniqueId = (long) path.toUpperCase().hashCode();
766     }
767
768     /**
769      * Open the file
770      *
771      * @param createFlag boolean
772      * @exception IOException
773      */

774     public abstract void openFile(boolean createFlag) throws IOException JavaDoc;
775
776     /**
777      * Read from the file.
778      *
779      * @param buf byte[]
780      * @param len int
781      * @param pos int
782      * @param fileOff long
783      * @return Length of data read.
784      * @exception IOException
785      */

786     public abstract int readFile(byte[] buf, int len, int pos, long fileOff) throws java.io.IOException JavaDoc;
787
788     /**
789      * Write a block of data to the file.
790      *
791      * @param buf byte[]
792      * @param len int
793      * @param pos int
794      * @param fileOff long
795      * @exception IOException
796      */

797     public abstract void writeFile(byte[] buf, int len, int pos, long fileOff) throws java.io.IOException JavaDoc;
798
799     /**
800      * Seek to the specified file position.
801      *
802      * @param pos long
803      * @param typ int
804      * @return int
805      * @exception IOException
806      */

807     public abstract long seekFile(long pos, int typ) throws IOException JavaDoc;
808
809     /**
810      * Flush any buffered output to the file
811      *
812      * @throws IOException
813      */

814     public abstract void flushFile() throws IOException JavaDoc;
815
816     /**
817      * Truncate the file to the specified file size
818      *
819      * @param siz long
820      * @exception IOException
821      */

822     public abstract void truncateFile(long siz) throws IOException JavaDoc;
823
824     /**
825      * Close the database file
826      */

827     public abstract void closeFile() throws IOException JavaDoc;
828
829     /**
830      * Temporary method
831      */

832     public void close() throws IOException JavaDoc
833     {
834         closeFile();
835     }
836 }
Popular Tags