View Javadoc

1   /*
2    * $Id: LocalFile.java,v 1.3 2004/09/06 11:27:16 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.store.delegate.local;
8   import org.astrogrid.store.delegate.*;
9   
10  import java.io.File;
11  import java.net.MalformedURLException;
12  import org.astrogrid.store.Agsl;
13  import java.util.Date;
14  
15  /***
16   * Represents a local file (ie a File class) as a StoreFile
17   * <p>
18   * Note that because this uses the File class to navigate around,
19   * it is not necessarily true that child[0].getParent() == child[1].getParent()
20   *
21   * @author M Hill
22   */
23  
24  
25  public class LocalFile implements StoreFile {
26     
27     
28     File file = null;
29     LocalFileStore store = null;
30     
31     public LocalFile(LocalFileStore theStore, File aFile) {
32        assert aFile != null : "Must set file";
33        assert theStore != null : "Must set store";
34        
35        this.file = aFile;
36        this.store = theStore;
37     }
38     
39     /*** Lists children files if this is a container - returns null otherwise */
40     public StoreFile[] listFiles() {
41        if (!isFolder()) {
42           return null;
43        }
44  
45        File[] localFiles = file.listFiles();
46        StoreFile[] storeFiles = new StoreFile[localFiles.length];
47        for (int i=0;i<localFiles.length;i++) {
48           storeFiles[i] = new LocalFile(store, localFiles[i]);
49        }
50        return storeFiles;
51     }
52     
53     /*** Returns parent folder of this file/folder */
54     public StoreFile getParent() {
55        //check to see if we're at the root
56        String serverPath = getPath();
57        if (serverPath == null) return null;
58  
59        return new LocalFile(store, file.getParentFile());//but specify full File
60     }
61     
62     /*** Returns true if this is a self-contained file.  For example, a database
63      * table might be represented as a StoreFile but it is not a file */
64     public boolean isFile() {
65        return file.isFile();
66     }
67     
68     /*** Returns the filename/foldername/tablename/etc */
69     public String getName() {
70        return file.getName();
71     }
72     
73     /*** Returns the path to this file on the server */
74     public String getPath() {
75        try {
76           return store.getServerPath(file);
77        }
78        catch (StoreException se) {
79           //should never happen
80           throw new RuntimeException("Program Error: ", se);
81        }
82     }
83  
84     /*** Returns the owner of the file */
85     public String getOwner() {
86        return null;
87     }
88     
89     /*** Returns the mime type (null if unknown) */
90     public String getMimeType() {
91        if (file.getName().endsWith(".txt"))   return "text/plain";
92        if (file.getName().endsWith(".xml"))   return "text/xml";
93        if (file.getName().endsWith(".vot"))   return "text/xml+votable";
94        if (file.getName().endsWith(".adql"))  return "text/xml+adql";
95        if (file.getName().endsWith(".job"))   return "text/xml+job";
96        return null;
97     }
98     
99     /*** Returns the date the file was last modified (null if unknown) */
100    public Date getModified() {
101       return new Date(file.lastModified());
102    }
103    
104    /*** Returns the creation date  (null if unknown) */
105    public Date getCreated() {
106       return null;
107    }
108    
109    /*** Returns the size of the file in bytes (-1 if unknown) */
110    public long getSize() {
111       return file.length();
112    }
113    
114    
115    /*** Returns true if this is a container that can hold other files/folders */
116    public boolean isFolder() {
117       return file.isDirectory();
118    }
119    
120    /*** Returns true if this represents the same file as the given one */
121    public boolean equals(StoreFile anotherFile) {
122       if (anotherFile instanceof LocalFile) {
123          return file.equals( ((LocalFile) anotherFile).file);
124       }
125       return false;
126    }
127    
128    /*** Returns user representation - server path */
129    public String toString() {
130       return getPath();
131    }
132    
133 }
134 
135 /*
136 $Log: LocalFile.java,v $
137 Revision 1.3  2004/09/06 11:27:16  mch
138 Added softwired mime types
139 
140 Revision 1.2  2004/06/14 23:08:53  jdt
141 Merge from branches
142 
143 ClientServerSplit_JDT
144 
145 and
146 
147 MySpaceClientServerSplit_JDT
148 
149 
150 
151 MySpace now split into a client/delegate jar
152 
153 astrogrid-myspace-<version>.jar
154 
155 and a server/manager war
156 
157 astrogrid-myspace-server-<version>.war
158 
159 Revision 1.1.2.1  2004/06/14 22:33:21  jdt
160 Split into delegate jar and server war.
161 Delegate: astrogrid-myspace-SNAPSHOT.jar
162 Server/Manager: astrogrid-myspace-server-SNAPSHOT.war
163 
164 Package names unchanged.
165 If you regenerate the axis java/wsdd/wsdl files etc you'll need
166 to move some files around to ensure they end up in the client
167 or the server as appropriate.
168 As of this check-in the tests/errors/failures is 162/1/22 which
169 matches that before the split.
170 
171 Revision 1.5  2004/05/03 08:55:53  mch
172 Fixes to getFiles(), introduced getSize(), getOwner() etc to StoreFile
173 
174 Revision 1.4  2004/04/06 13:56:00  mch
175 Fix to getParent null
176 
177 Revision 1.3  2004/04/06 11:01:33  mch
178 Fixed null pointer when LocalFile is root
179 
180 Revision 1.2  2004/04/06 10:16:23  mch
181 Fixed isFolder() bug
182 
183 Revision 1.1  2004/03/04 12:51:31  mch
184 Moved delegate implementations into subpackages
185 
186 Revision 1.1  2004/03/01 22:38:46  mch
187 Part II of copy from It4.1 datacenter + updates from myspace meetings + test fixes
188 
189  */
190