Changeset 1109 for extensions/net.sf.basedb.genepattern/trunk/src
- Timestamp:
- Jun 4, 2009, 11:35:59 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/net.sf.basedb.genepattern/trunk/src/net/sf/basedb/genepattern/file/FileTransferGateway.java
r1107 r1109 1 1 package net.sf.basedb.genepattern.file; 2 2 3 import java.io.BufferedInputStream; 3 4 import java.io.File; 4 5 import java.io.FileFilter; 6 import java.io.FileInputStream; 5 7 import java.io.IOException; 6 8 import java.io.InputStream; … … 17 19 import net.sf.basedb.util.formatter.DateFormatter; 18 20 21 import org.genepattern.webservice.JobResult; 19 22 import org.genepattern.webservice.Parameter; 20 23 21 24 /** 22 Handles file transfers to a GenePattern server. One getaway instance can25 Handles file transfers to and from a GenePattern server. One getaway instance can 23 26 handle file for one GenePattern job. The files that are needed for the 24 27 job should be added to the gateway with {@link #addFile(String, FileProxy)} 25 28 which returns a {@link Parameter} object that can be used when invoking 26 29 GenePattern. 30 <p> 31 Once the job has finished the result files can be downloaded with 32 {@link #downloadResultFile(JobResult, String, OutputStream)} or 33 {@link #downloadResultFile(JobResult, String)}. 34 <p> 35 After the gateway has been used it is recommended to call {@link #cleanUp()} 36 to make sure that any temporary files are deleted. 27 37 28 38 @author nicklas … … 40 50 private final String id; 41 51 private final String baseUrl; 42 private final Set<String> files; 52 private final Set<String> uploadFiles; 53 private final Set<File> downloadFiles; 54 private final File workDir; 43 55 private final Set<String> directories; 44 56 private final String subDir; … … 56 68 this.id = "gp-" + System.identityHashCode(this); 57 69 this.subDir = new DateFormatter("yyyy-MM-dd").format(new Date()); 58 this. files = new HashSet<String>();70 this.uploadFiles = new HashSet<String>(); 59 71 this.directories = new HashSet<String>(); 72 this.downloadFiles = new HashSet<File>(); 73 this.workDir = new File(new File(System.getProperty("java.io.tmpdir")), getId()); 60 74 directories.add(cacheRoot); 61 75 directories.add(subDir); … … 97 111 { 98 112 String filename = file.getFileName(); 99 if ( files.contains(filename))113 if (uploadFiles.contains(filename)) 100 114 { 101 115 throw new ItemAlreadyExistsException("File already exists: " + filename); … … 134 148 FileUtil.close(fromProxy); 135 149 } 136 files.add(filename);150 uploadFiles.add(filename); 137 151 return new Parameter(gpParameterName, baseUrl + "/Download/" + downloadPath + ".servlet"); 138 152 } 139 153 140 154 /** 141 Clean up and remove all files that has been added to this gateway. 155 Download a GenePattern result file and make it available as a InputStream. 156 Downloaded file are cached and subseqent calls to this method requesting 157 the same filename will use the cached file. 158 159 @param result The job result information 160 @param filename A filename that is part of the job result 161 @return An input stream that reads from the downloaded file or null if the 162 file doesn't exists as part of the job result 163 @throws IOException 164 */ 165 public InputStream downloadResultFile(JobResult result, String filename) 166 throws IOException 167 { 168 File workDir = getWorkingDirectory(); 169 File tmpFile = new File(workDir, filename); 170 if (!tmpFile.exists()) 171 { 172 tmpFile = result.downloadFile(filename, workDir.getAbsolutePath(), true); 173 if (tmpFile != null) downloadFiles.add(tmpFile); 174 } 175 InputStream in = null; 176 if (tmpFile != null) 177 { 178 in = new BufferedInputStream(new FileInputStream(tmpFile)); 179 } 180 return in; 181 } 182 183 /** 184 Download a GenePattern result file and copy it to the given ouput stream. 185 Downloaded file are cached and subseqent calls to this method requesting 186 the same filename will use the cached file. 187 188 @param result The job result information 189 @param filename A filename that is part of the job result 190 @param to The output stream to write to 191 @throws IOException 192 */ 193 public long downloadResultFile(JobResult result, String filename, OutputStream to) 194 throws IOException 195 { 196 return FileUtil.copy(downloadResultFile(result, filename), to); 197 } 198 199 private File getWorkingDirectory() 200 { 201 workDir.mkdirs(); 202 return workDir; 203 } 204 205 /** 206 Clean up and remove all files that has been added or downloaded 207 to this gateway. 142 208 */ 143 209 public void cleanUp() 144 210 { 145 if ( files.size() > 0)211 if (uploadFiles.size() > 0) 146 212 { 147 213 StaticCache cache = Application.getStaticCache(); … … 153 219 { 154 220 String name = file.getName(); 155 boolean accept = file.isDirectory() ? directories.contains(name) :156 id.equals(file.getParentFile().getName()) && files.contains(name);221 boolean accept = file.isDirectory() ? directories.contains(name) : 222 id.equals(file.getParentFile().getName()) && uploadFiles.contains(name); 157 223 return accept; 158 224 } 159 225 } 160 226 ); 161 files.clear(); 227 uploadFiles.clear(); 228 } 229 if (downloadFiles.size() > 0) 230 { 231 for (File f : downloadFiles) 232 { 233 f.delete(); 234 } 235 workDir.delete(); 236 downloadFiles.clear(); 162 237 } 163 238 }
Note: See TracChangeset
for help on using the changeset viewer.