Changeset 1129 for extensions/net.sf.basedb.genepattern
- Timestamp:
- Jun 16, 2009, 1:33:33 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/net.sf.basedb.genepattern/trunk/src/net/sf/basedb/genepattern/file/FileTransferGateway.java
r1127 r1129 2 2 3 3 import java.io.BufferedInputStream; 4 import java.io.ByteArrayOutputStream; 4 5 import java.io.File; 5 6 import java.io.FileFilter; … … 8 9 import java.io.InputStream; 9 10 import java.io.OutputStream; 11 import java.util.ArrayList; 10 12 import java.util.Date; 11 13 import java.util.HashSet; 14 import java.util.List; 12 15 import java.util.Set; 13 16 … … 15 18 import net.sf.basedb.core.ConfigurationException; 16 19 import net.sf.basedb.core.ItemAlreadyExistsException; 20 import net.sf.basedb.core.ProgressReporter; 21 import net.sf.basedb.core.signal.ThreadSignalHandler; 17 22 import net.sf.basedb.genepattern.wrapper.JobResult; 18 23 import net.sf.basedb.util.FileUtil; … … 27 32 job should be added to the gateway with {@link #addFile(String, FileProxy)} 28 33 which returns a {@link Parameter} object that can be used when invoking 29 GenePattern. 34 GenePattern. After all files has been added to the gateway, {@link #prepareForUpload()} 35 must be called. This method makes a copy of all added files to a temporary 36 location that is accessible by the GenePattern server via the BASE web server 37 (even if the file transfer is initialized from a job agent). 30 38 <p> 31 39 Once the job has finished the result files can be downloaded with … … 52 60 private final Set<String> uploadFiles; 53 61 private final Set<File> downloadFiles; 62 private final List<FileProxy> proxies; 54 63 private final File workDir; 55 64 private final Set<String> directories; … … 69 78 this.directories = new HashSet<String>(); 70 79 this.downloadFiles = new HashSet<File>(); 80 this.proxies = new ArrayList<FileProxy>(); 71 81 this.workDir = new File(new File(System.getProperty("java.io.tmpdir")), getId()); 72 82 directories.add(cacheRoot); … … 113 123 throw new ItemAlreadyExistsException("File already exists: " + filename); 114 124 } 125 uploadFiles.add(filename); 126 proxies.add(file); 127 String downloadPath = subDir + "/" + id + "/" + filename; 128 return new Parameter(gpParameterName, downloadServletUrl + "/" + downloadPath); 129 } 130 131 /** 132 Prepare all added files for upload. The preparation includes 133 copying the files to a temporary location so this method may 134 take some time if the files are large. 135 @param progress An optional progress reporter 136 @return The number of files copied 137 */ 138 public int prepareUpload(ProgressReporter progress) 139 { 115 140 StaticCache cache = Application.getStaticCache(); 116 141 if (cache.isDisabled()) … … 120 145 } 121 146 122 String downloadPath = subDir + "/" + id + "/" + filename; 123 String cacheKey = cacheRoot + "/" + downloadPath; 124 OutputStream toCache = null; 125 InputStream fromProxy = null; 126 try 127 { 128 fromProxy = file.getInputStream(); 129 if (fromProxy != null) 130 { 131 cache.write(cacheKey, fromProxy, 1000); 132 } 133 else 134 { 135 toCache = cache.write(cacheKey, 1000); 136 file.writeTo(toCache); 137 } 138 } 139 catch (IOException ex) 140 { 141 throw new RuntimeException(ex); 142 } 143 finally 144 { 145 FileUtil.close(toCache); 146 FileUtil.close(fromProxy); 147 } 148 uploadFiles.add(filename); 149 return new Parameter(gpParameterName, downloadServletUrl + "/" + downloadPath); 147 int numFiles = proxies.size(); 148 int currentFile = 0; 149 for (FileProxy file : proxies) 150 { 151 ThreadSignalHandler.checkInterrupted(); 152 String filename = file.getFileName(); 153 String downloadPath = subDir + "/" + id + "/" + filename; 154 String cacheKey = cacheRoot + "/" + downloadPath; 155 OutputStream toCache = null; 156 InputStream fromProxy = null; 157 if (progress != null) 158 { 159 progress.display(100 * currentFile / numFiles, "Copying '" + filename + "'"); 160 } 161 try 162 { 163 fromProxy = file.getInputStream(); 164 if (fromProxy != null) 165 { 166 cache.write(cacheKey, fromProxy, 1000); 167 } 168 else 169 { 170 toCache = cache.write(cacheKey, 1000); 171 file.writeTo(toCache); 172 } 173 } 174 catch (IOException ex) 175 { 176 throw new RuntimeException(ex); 177 } 178 finally 179 { 180 FileUtil.close(toCache); 181 FileUtil.close(fromProxy); 182 } 183 } 184 proxies.clear(); 185 return numFiles; 150 186 } 151 187 … … 193 229 { 194 230 return FileUtil.copy(downloadResultFile(result, filename), to); 231 } 232 233 /** 234 Download a GenePattern result file and read it in as a string. 235 @param result The job result information 236 @param filename The name of a (text) file that is part of the job result 237 @param charset The character set used in the file 238 @return The file contents as a string 239 @throws IOException 240 */ 241 public String downloadAsString(JobResult result, String filename, String charset) 242 throws IOException 243 { 244 ByteArrayOutputStream to = new ByteArrayOutputStream(); 245 downloadResultFile(result, filename, to); 246 return to.toString(charset); 195 247 } 196 248
Note: See TracChangeset
for help on using the changeset viewer.