KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.alfresco.filesys.smb.SharingMode;
20 import org.alfresco.filesys.smb.WinNT;
21
22 /**
23  * File Open Parameters Class
24  * <p>
25  * Contains the details of a file open request.
26  */

27 public class FileOpenParams
28 {
29     // Constants
30

31     public final static String JavaDoc StreamSeparator = ":";
32
33     // Conversion array for Core/LanMan open actions to NT open action codes
34

35     private static int[] _NTToLMOpenCode = {
36             FileAction.TruncateExisting + FileAction.CreateNotExist,
37             FileAction.OpenIfExists,
38             FileAction.CreateNotExist,
39             FileAction.OpenIfExists + FileAction.CreateNotExist,
40             FileAction.TruncateExisting,
41             FileAction.TruncateExisting + FileAction.CreateNotExist };
42
43     // File open mode strings
44

45     private static String JavaDoc[] _openMode = { "Supersede", "Open", "Create", "OpenIf", "Overwrite", "OverwriteIf" };
46
47     // File/directory to be opened
48

49     private String JavaDoc m_path;
50
51     // Stream name
52

53     private String JavaDoc m_stream;
54
55     // File open action
56

57     private int m_openAction;
58
59     // Desired access mode
60

61     private int m_accessMode;
62
63     // File attributes
64

65     private int m_attr;
66
67     // Allocation size
68

69     private long m_allocSize;
70
71     // Shared access flags
72

73     private int m_sharedAccess = SharingMode.READWRITE;
74
75     // Creation date/time
76

77     private long m_createDate;
78
79     // Root directory file id, zero if not specified
80

81     private int m_rootFID;
82
83     // Create options
84

85     private int m_createOptions;
86
87     // Security impersonation level, -1 if not set
88

89     private int m_secLevel;
90
91     // Security flags
92

93     private int m_secFlags;
94
95     // Owner group and user id
96

97     private int m_gid = -1;
98     private int m_uid = -1;
99
100     // Unix mode
101

102     private int m_mode = -1;
103
104     /**
105      * Class constructor for Core SMB dialect Open SMB requests
106      *
107      * @param path String
108      * @param openAction int
109      * @param accessMode int
110      * @param fileAttr int
111      */

112     public FileOpenParams(String JavaDoc path, int openAction, int accessMode, int fileAttr)
113     {
114
115         // Parse the file path, split into file name and stream if specified
116

117         parseFileName(path);
118
119         m_openAction = convertToNTOpenAction(openAction);
120         m_accessMode = convertToNTAccessMode(accessMode);
121         m_attr = fileAttr;
122
123         // Check if the diectory attribute is set
124

125         if (FileAttribute.isDirectory(m_attr))
126             m_createOptions = WinNT.CreateDirectory;
127
128         // No security settings
129

130         m_secLevel = -1;
131     }
132
133     /**
134      * Class constructor for Core SMB dialect Open SMB requests
135      *
136      * @param path String
137      * @param openAction int
138      * @param accessMode int
139      * @param fileAttr int
140      * @param gid int
141      * @param uid int
142      * @param mode int
143      */

144     public FileOpenParams(String JavaDoc path, int openAction, int accessMode, int fileAttr, int gid, int uid, int mode)
145     {
146
147         // Parse the file path, split into file name and stream if specified
148

149         parseFileName(path);
150
151         m_openAction = convertToNTOpenAction(openAction);
152         m_accessMode = convertToNTAccessMode(accessMode);
153         m_attr = fileAttr;
154
155         // Check if the diectory attribute is set
156

157         if (FileAttribute.isDirectory(m_attr))
158             m_createOptions = WinNT.CreateDirectory;
159
160         // No security settings
161

162         m_secLevel = -1;
163
164         m_gid = gid;
165         m_uid = uid;
166         m_mode = mode;
167     }
168
169     /**
170      * Class constructor for LanMan SMB dialect OpenAndX requests
171      *
172      * @param path String
173      * @param openAction int
174      * @param accessMode int
175      * @param searchAttr int
176      * @param fileAttr int
177      * @param allocSize int
178      * @param createDate long
179      */

180     public FileOpenParams(String JavaDoc path, int openAction, int accessMode, int searchAttr, int fileAttr, int allocSize,
181             long createDate)
182     {
183
184         // Parse the file path, split into file name and stream if specified
185

186         parseFileName(path);
187
188         m_openAction = convertToNTOpenAction(openAction);
189         m_accessMode = convertToNTAccessMode(accessMode);
190         m_attr = fileAttr;
191         m_sharedAccess = convertToNTSharedMode(accessMode);
192         m_allocSize = (long) allocSize;
193         m_createDate = createDate;
194
195         // Check if the diectory attribute is set
196

197         if (FileAttribute.isDirectory(m_attr))
198             m_createOptions = WinNT.CreateDirectory;
199
200         // No security settings
201

202         m_secLevel = -1;
203     }
204
205     /**
206      * Class constructor for NT SMB dialect NTCreateAndX requests
207      *
208      * @param path String
209      * @param openAction int
210      * @param accessMode int
211      * @param attr int
212      * @param sharedAccess int
213      * @param allocSize long
214      * @param createOption int
215      * @param rootFID int
216      * @param secLevel int
217      * @param secFlags int
218      */

219     public FileOpenParams(String JavaDoc path, int openAction, int accessMode, int attr, int sharedAccess, long allocSize,
220             int createOption, int rootFID, int secLevel, int secFlags)
221     {
222
223         // Parse the file path, split into file name and stream if specified
224

225         parseFileName(path);
226
227         m_openAction = openAction;
228         m_accessMode = accessMode;
229         m_attr = attr;
230         m_sharedAccess = sharedAccess;
231         m_allocSize = allocSize;
232         m_createOptions = createOption;
233         m_rootFID = rootFID;
234         m_secLevel = secLevel;
235         m_secFlags = secFlags;
236
237         // Make sure the directory attribute is set if the create directory option is set
238

239         if ((createOption & WinNT.CreateDirectory) != 0 && (m_attr & FileAttribute.Directory) == 0)
240             m_attr += FileAttribute.Directory;
241     }
242
243     /**
244      * Return the path to be opened/created
245      *
246      * @return String
247      */

248     public final String JavaDoc getPath()
249     {
250         return m_path;
251     }
252
253     /**
254      * Return the full path to be opened/created, including the stream
255      *
256      * @return String
257      */

258     public final String JavaDoc getFullPath()
259     {
260         if (isStream())
261             return m_path + m_stream;
262         else
263             return m_path;
264     }
265
266     /**
267      * Return the file attributes
268      *
269      * @return int
270      */

271     public final int getAttributes()
272     {
273         return m_attr;
274     }
275
276     /**
277      * Return the allocation size, or zero if not specified
278      *
279      * @return long
280      */

281     public final long getAllocationSize()
282     {
283         return m_allocSize;
284     }
285
286     /**
287      * Determine if a creation date/time has been specified
288      *
289      * @return boolean
290      */

291     public final boolean hasCreationDateTime()
292     {
293         return m_createDate != 0L ? true : false;
294     }
295
296     /**
297      * Return the file creation date/time
298      *
299      * @return long
300      */

301     public final long getCreationDateTime()
302     {
303         return m_createDate;
304     }
305
306     /**
307      * Return the open/create file/directory action All actions are mapped to the FileAction.NTxxx
308      * action codes.
309      *
310      * @return int
311      */

312     public final int getOpenAction()
313     {
314         return m_openAction;
315     }
316
317     /**
318      * Return the root directory file id, or zero if not specified
319      *
320      * @return int
321      */

322     public final int getRootDirectoryFID()
323     {
324         return m_rootFID;
325     }
326
327     /**
328      * Return the stream name
329      *
330      * @return String
331      */

332     public final String JavaDoc getStreamName()
333     {
334         return m_stream;
335     }
336
337     /**
338      * Check if the specified create option is enabled, specified in the WinNT class.
339      *
340      * @param flag int
341      * @return boolean
342      */

343     public final boolean hasCreateOption(int flag)
344     {
345         return (m_createOptions & flag) != 0 ? true : false;
346     }
347
348     /**
349      * Check if a file stream has been specified in the path to be created/opened
350      *
351      * @return boolean
352      */

353     public final boolean isStream()
354     {
355         return m_stream != null ? true : false;
356     }
357
358     /**
359      * Determine if the file is to be opened read-only
360      *
361      * @return boolean
362      */

363     public final boolean isReadOnlyAccess()
364     {
365         // Check if read-only or execute access has been requested
366

367         if (( m_accessMode & AccessMode.NTReadWrite) == AccessMode.NTRead ||
368                 (m_accessMode & AccessMode.NTExecute) != 0)
369             return true;
370         return false;
371     }
372
373     /**
374      * Determine if the file is to be opened write-only
375      *
376      * @return boolean
377      */

378     public final boolean isWriteOnlyAccess()
379     {
380         return (m_accessMode & AccessMode.NTReadWrite) == AccessMode.NTWrite ? true : false;
381     }
382
383     /**
384      * Determine if the file is to be opened read/write
385      *
386      * @return boolean
387      */

388     public final boolean isReadWriteAccess()
389     {
390         return (m_accessMode & AccessMode.NTReadWrite) == AccessMode.NTReadWrite ? true : false;
391     }
392
393     /**
394      * Check for a particular access mode
395      *
396      * @param mode int
397      * @return boolean
398      */

399     public final boolean hasAccessMode(int mode)
400     {
401         return (m_accessMode & mode) == mode ? true : false;
402     }
403     
404     /**
405      * Determine if the target of the create/open is a directory
406      *
407      * @return boolean
408      */

409     public final boolean isDirectory()
410     {
411         return hasCreateOption(WinNT.CreateDirectory);
412     }
413
414     /**
415      * Determine if the file will be accessed sequentially only
416      *
417      * @return boolean
418      */

419     public final boolean isSequentialAccessOnly()
420     {
421         return hasCreateOption(WinNT.CreateSequential);
422     }
423
424     /**
425      * Determine if the file should be deleted when closed
426      *
427      * @return boolean
428      */

429     public final boolean isDeleteOnClose()
430     {
431         return hasCreateOption(WinNT.CreateDeleteOnClose);
432     }
433
434     /**
435      * Determine if write-through mode is enabled (buffering is not allowed if enabled)
436      *
437      * @return boolean
438      */

439     public final boolean isWriteThrough()
440     {
441         return hasCreateOption(WinNT.CreateWriteThrough);
442     }
443
444     /**
445      * Determine if the open mode should overwrite/truncate an existing file
446      *
447      * @return boolean
448      */

449     public final boolean isOverwrite()
450     {
451         if (getOpenAction() == FileAction.NTSupersede || getOpenAction() == FileAction.NTOverwrite
452                 || getOpenAction() == FileAction.NTOverwriteIf)
453             return true;
454         return false;
455     }
456
457     /**
458      * Return the shared access mode, zero equals allow any shared access
459      *
460      * @return int
461      */

462     public final int getSharedAccess()
463     {
464         return m_sharedAccess;
465     }
466
467     /**
468      * Determine if security impersonation is enabled
469      *
470      * @return boolean
471      */

472     public final boolean hasSecurityLevel()
473     {
474         return m_secLevel != -1 ? true : false;
475     }
476
477     /**
478      * Return the security impersonation level. Levels are defined in the WinNT class.
479      *
480      * @return int
481      */

482     public final int getSecurityLevel()
483     {
484         return m_secLevel;
485     }
486
487     /**
488      * Determine if the security context tracking flag is enabled
489      *
490      * @return boolean
491      */

492     public final boolean hasSecurityContextTracking()
493     {
494         return (m_secFlags & WinNT.SecurityContextTracking) != 0 ? true : false;
495     }
496
497     /**
498      * Determine if the security effective only flag is enabled
499      *
500      * @return boolean
501      */

502     public final boolean hasSecurityEffectiveOnly()
503     {
504         return (m_secFlags & WinNT.SecurityEffectiveOnly) != 0 ? true : false;
505     }
506
507     /**
508      * Determine if the group id has been set
509      *
510      * @return boolean
511      */

512     public final boolean hasGid()
513     {
514         return m_gid != -1 ? true : false;
515     }
516
517     /**
518      * Return the owner group id
519      *
520      * @return int
521      */

522     public final int getGid()
523     {
524         return m_gid;
525     }
526
527     /**
528      * Determine if the user id has been set
529      *
530      * @return boolean
531      */

532     public final boolean hasUid()
533     {
534         return m_uid != -1 ? true : false;
535     }
536
537     /**
538      * Return the owner user id
539      *
540      * @return int
541      */

542     public final int getUid()
543     {
544         return m_uid;
545     }
546
547     /**
548      * Determine if the mode has been set
549      *
550      * @return boolean
551      */

552     public final boolean hasMode()
553     {
554         return m_mode != -1 ? true : false;
555     }
556
557     /**
558      * Return the Unix mode
559      *
560      * @return int
561      */

562     public final int getMode()
563     {
564         return m_mode;
565     }
566
567     /**
568      * Set the Unix mode
569      *
570      * @param mode int
571      */

572     public final void setMode(int mode)
573     {
574         m_mode = mode;
575     }
576
577     /**
578      * Set a create option flag
579      *
580      * @param flag int
581      */

582     public final void setCreateOption(int flag)
583     {
584         m_createOptions = m_createOptions | flag;
585     }
586
587     /**
588      * Convert a Core/LanMan access mode to an NT access mode
589      *
590      * @param accessMode int
591      * @return int
592      */

593     private final int convertToNTAccessMode(int accessMode)
594     {
595
596         // Convert the Core/LanMan SMB dialect format access mode value to an NT access mode
597

598         int mode = 0;
599
600         switch (AccessMode.getAccessMode(accessMode))
601         {
602         case AccessMode.ReadOnly:
603             mode = AccessMode.NTRead;
604             break;
605         case AccessMode.WriteOnly:
606             mode = AccessMode.NTWrite;
607             break;
608         case AccessMode.ReadWrite:
609             mode = AccessMode.NTReadWrite;
610             break;
611         }
612         return mode;
613     }
614
615     /**
616      * Convert a Core/LanMan open action to an NT open action
617      *
618      * @param openAction int
619      * @return int
620      */

621     private final int convertToNTOpenAction(int openAction)
622     {
623
624         // Convert the Core/LanMan SMB dialect open action to an NT open action
625

626         int action = FileAction.NTOpen;
627
628         for (int i = 0; i < _NTToLMOpenCode.length; i++)
629         {
630             if (_NTToLMOpenCode[i] == openAction)
631                 action = i;
632         }
633         return action;
634     }
635
636     /**
637      * Convert a Core/LanMan shared access to NT sharing flags
638      *
639      * @param sharedAccess int
640      * @return int
641      */

642     private final int convertToNTSharedMode(int sharedAccess)
643     {
644
645         // Get the shared access value from the access mask
646

647         int shr = AccessMode.getSharingMode(sharedAccess);
648         int ret = SharingMode.READWRITE;
649
650         switch (shr)
651         {
652         case AccessMode.Exclusive:
653             ret = SharingMode.NOSHARING;
654             break;
655         case AccessMode.DenyRead:
656             ret = SharingMode.WRITE;
657             break;
658         case AccessMode.DenyWrite:
659             ret = SharingMode.READ;
660             break;
661         }
662         return ret;
663     }
664
665     /**
666      * Parse a file name to split the main file name/path and stream name
667      *
668      * @param fileName String
669      */

670     private final void parseFileName(String JavaDoc fileName)
671     {
672
673         // Check if the file name contains a stream name
674

675         int pos = fileName.indexOf(StreamSeparator);
676         if (pos == -1)
677         {
678             m_path = fileName;
679             return;
680         }
681
682         // Split the main file name and stream name
683

684         m_path = fileName.substring(0, pos);
685         m_stream = fileName.substring(pos);
686     }
687
688     /**
689      * Return the file open parameters as a string
690      *
691      * @return String
692      */

693     public String JavaDoc toString()
694     {
695         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
696         str.append("[");
697
698         str.append(getPath());
699
700         str.append(",");
701         str.append(_openMode[getOpenAction()]);
702         str.append(",acc=0x");
703         str.append(Integer.toHexString(m_accessMode));
704         str.append(",attr=0x");
705         str.append(Integer.toHexString(getAttributes()));
706         str.append(",alloc=");
707         str.append(getAllocationSize());
708         str.append(",share=0x");
709         str.append(Integer.toHexString(getSharedAccess()));
710
711         if (getRootDirectoryFID() != 0)
712         {
713             str.append(",fid=");
714             str.append(getRootDirectoryFID());
715         }
716
717         if (hasCreationDateTime())
718         {
719             str.append(",cdate=");
720             str.append(getCreationDateTime());
721         }
722
723         if (m_createOptions != 0)
724         {
725             str.append(",copt=0x");
726             str.append(Integer.toHexString(m_createOptions));
727         }
728
729         if (hasSecurityLevel())
730         {
731             str.append(",seclev=");
732             str.append(getSecurityLevel());
733             str.append(",secflg=0x");
734             str.append(Integer.toHexString(m_secFlags));
735         }
736         str.append("]");
737
738         if (hasGid() || hasUid())
739         {
740             str.append(",gid=");
741             str.append(getGid());
742             str.append(",uid=");
743             str.append(getUid());
744             str.append(",mode=");
745             str.append(getMode());
746         }
747         return str.toString();
748     }
749 }
750
Popular Tags