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.FileInfo;
6   import org.ogf.saga.error.DoesNotExistException;
7   
8   import java.text.ParseException;
9   import java.text.SimpleDateFormat;
10  import java.util.*;
11  
12  /* ***************************************************
13  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
14  * ***             http://cc.in2p3.fr/             ***
15  * ***************************************************
16  * File:   Gsiftp1FileAttributes
17  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
18  * Date:   10 sept. 2007
19  * ***************************************************
20  * Description:                                      */
21  /**
22   *
23   */
24  public class Gsiftp1FileAttributes extends FileAttributes {
25      private FileInfo m_entry;
26  
27      public Gsiftp1FileAttributes(FileInfo entry) throws DoesNotExistException {
28          // check if entry must be ignored
29          String name = entry.getName();
30          if (name ==null || name.equals(".") || name.equals("..")) {
31              throw new DoesNotExistException("Ignore this entry");
32          }
33  
34          // set entry
35          m_entry = entry;
36      }
37  
38      public String getName() {
39          return m_entry.getName();
40      }
41  
42      public int getType() {
43          if (m_entry.isFile()) {
44              return FileAttributes.TYPE_FILE;
45          } else if (m_entry.isDirectory()) {
46              return FileAttributes.TYPE_DIRECTORY;
47          } else if (m_entry.isSoftLink()) {
48              return FileAttributes.TYPE_LINK;
49          } else {
50              return FileAttributes.TYPE_UNKNOWN;
51          }
52      }
53  
54      public long getSize() {
55          try {
56              return m_entry.getSize();
57          } catch(NumberFormatException e) {
58              return SIZE_UNKNOWN;
59          }
60      }
61  
62      public PermissionBytes getUserPermission() {
63          PermissionBytes perms = PermissionBytes.NONE;
64          if (m_entry.userCanRead()) {
65              perms = perms.or(PermissionBytes.READ);
66          }
67          if (m_entry.userCanWrite()) {
68              perms = perms.or(PermissionBytes.WRITE);
69          }
70          if (m_entry.userCanExecute()) {
71              perms = perms.or(PermissionBytes.EXEC);
72          }
73          return perms;
74      }
75  
76      public PermissionBytes getGroupPermission() {
77          PermissionBytes perms = PermissionBytes.NONE;
78          if (m_entry.groupCanRead()) {
79              perms = perms.or(PermissionBytes.READ);
80          }
81          if (m_entry.groupCanWrite()) {
82              perms = perms.or(PermissionBytes.WRITE);
83          }
84          if (m_entry.groupCanExecute()) {
85              perms = perms.or(PermissionBytes.EXEC);
86          }
87          return perms;
88      }
89  
90      public PermissionBytes getAnyPermission() {
91          PermissionBytes perms = PermissionBytes.NONE;
92          if (m_entry.allCanRead()) {
93              perms = perms.or(PermissionBytes.READ);
94          }
95          if (m_entry.allCanWrite()) {
96              perms = perms.or(PermissionBytes.WRITE);
97          }
98          if (m_entry.allCanExecute()) {
99              perms = perms.or(PermissionBytes.EXEC);
100         }
101         return perms;
102     }
103 
104     public String getOwner() {
105         return ID_UNKNOWN;
106     }
107 
108     public String getGroup() {
109         return ID_UNKNOWN;
110     }
111 
112     public long getLastModified() {
113         try {
114             switch (m_entry.getTime().length()) {
115                 case 4:
116                 {
117                     SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy", Locale.ENGLISH);
118                     Date date = format.parse(m_entry.getDate()+","+m_entry.getTime());
119                     return date.getTime();
120                 }
121                 case 5:
122                 {
123                     Calendar cal = Calendar.getInstance();
124                     cal.add(Calendar.MONTH, -6);
125                     String year6MonthAgo = ""+cal.get(Calendar.YEAR);
126                     SimpleDateFormat format = new SimpleDateFormat("MMM dd,hh:mm,yyyy", Locale.ENGLISH);
127                     Date date = format.parse(m_entry.getDate()+","+m_entry.getTime()+","+year6MonthAgo);
128                     return date.getTime();
129                 }
130                 default:
131                     return DATE_UNKNOWN;
132             }
133         } catch (ParseException e) {
134             return DATE_UNKNOWN;
135         }
136     }
137 }