View Javadoc

1   package fr.in2p3.jsaga.adaptor.data;
2   
3   import fr.in2p3.jsaga.adaptor.data.permission.PermissionBytes;
4   import fr.in2p3.jsaga.adaptor.data.read.FileAttributes;
5   import org.globus.ftp.MlsxEntry;
6   import org.ogf.saga.error.DoesNotExistException;
7   
8   import java.text.ParseException;
9   import java.text.SimpleDateFormat;
10  import java.util.Date;
11  import java.util.Locale;
12  
13  /* ***************************************************
14  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
15  * ***             http://cc.in2p3.fr/             ***
16  * ***************************************************
17  * File:   Gsiftp2FileAttributes
18  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
19  * Date:   28 aout 2007
20  * ***************************************************
21  * Description:                                      */
22  /**
23   *
24   */
25  public class Gsiftp2FileAttributes extends FileAttributes {
26      private static final int POSITION_USER = 1;
27      private static final int POSITION_GROUP = 2;
28      private static final int POSITION_ANY = 3;
29  
30      private static final int UNIX_READ = 4;
31      private static final int UNIX_WRITE = 2;
32      private static final int UNIX_EXEC = 1;
33  
34      private MlsxEntry m_entry;
35  
36      public Gsiftp2FileAttributes(MlsxEntry entry) throws DoesNotExistException {
37          // check if entry must be ignored
38          String name = entry.getFileName();
39          if (name ==null || name.equals(".") || name.equals("..")) {
40              throw new DoesNotExistException("Ignore this entry");
41          }
42  
43          // set entry
44          m_entry = entry;
45      }
46  
47      public String getName() {
48          return m_entry.getFileName();
49      }
50  
51      public int getType() {
52          String _type = m_entry.get("type");
53          if (_type==null) return TYPE_UNKNOWN;
54          if (_type.equals("file")) {
55              return TYPE_FILE;
56          } else if (_type.endsWith("dir")) {
57              return TYPE_DIRECTORY;
58          } else {
59              return TYPE_UNKNOWN;
60          }
61      }
62  
63      public long getSize() {
64          String _size = m_entry.get("size");
65          if (_size==null) return SIZE_UNKNOWN;
66          try {
67              return Long.parseLong(m_entry.get("size"));
68          } catch(NumberFormatException e) {
69              return SIZE_UNKNOWN;
70          }
71      }
72  
73      public PermissionBytes getUserPermission() {
74          return this.getPermission(POSITION_USER);
75      }
76  
77      public PermissionBytes getGroupPermission() {
78          return this.getPermission(POSITION_GROUP);
79      }
80  
81      public PermissionBytes getAnyPermission() {
82          return this.getPermission(POSITION_ANY);
83      }
84  
85      private PermissionBytes getPermission(int position) {
86          String _perm = m_entry.get("unix.mode");
87          if (_perm==null) return PERMISSION_UNKNOWN;
88          PermissionBytes perms = PermissionBytes.NONE;
89          int userPerm = _perm.charAt(position) - '0';
90          if ((userPerm & UNIX_READ) != 0) {
91              perms = perms.or(PermissionBytes.READ);
92          }
93          if ((userPerm & UNIX_WRITE) != 0) {
94              perms = perms.or(PermissionBytes.WRITE);
95          }
96          if ((userPerm & UNIX_EXEC) != 0) {
97              perms = perms.or(PermissionBytes.EXEC);
98          }
99          return perms;
100     }
101 
102     public String getOwner() {
103         return ID_UNKNOWN;
104     }
105 
106     public String getGroup() {
107         return ID_UNKNOWN;
108     }
109 
110     public long getLastModified() {
111         String _date = m_entry.get("modify");
112         if (_date==null) return DATE_UNKNOWN;
113         try {
114             Date date = new SimpleDateFormat("yyyyMMddhhmmss", Locale.ENGLISH).parse(_date);
115             return date.getTime()+7200000;
116         } catch (ParseException e) {
117             return DATE_UNKNOWN;
118         }
119     }
120 }