Changeset 1129


Ignore:
Timestamp:
Jun 16, 2009, 1:33:33 PM (12 years ago)
Author:
Nicklas Nordborg
Message:

References #222: Utility for transfering file between BASE and a GenePattern? server

Separated adding and copying of files to two separate steps. Added a convenience method
for downloading a file and reading it into a string.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.genepattern/trunk/src/net/sf/basedb/genepattern/file/FileTransferGateway.java

    r1127 r1129  
    22
    33import java.io.BufferedInputStream;
     4import java.io.ByteArrayOutputStream;
    45import java.io.File;
    56import java.io.FileFilter;
     
    89import java.io.InputStream;
    910import java.io.OutputStream;
     11import java.util.ArrayList;
    1012import java.util.Date;
    1113import java.util.HashSet;
     14import java.util.List;
    1215import java.util.Set;
    1316
     
    1518import net.sf.basedb.core.ConfigurationException;
    1619import net.sf.basedb.core.ItemAlreadyExistsException;
     20import net.sf.basedb.core.ProgressReporter;
     21import net.sf.basedb.core.signal.ThreadSignalHandler;
    1722import net.sf.basedb.genepattern.wrapper.JobResult;
    1823import net.sf.basedb.util.FileUtil;
     
    2732  job should be added to the gateway with {@link #addFile(String, FileProxy)}
    2833  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).
    3038  <p>
    3139  Once the job has finished the result files can be downloaded with
     
    5260  private final Set<String> uploadFiles;
    5361  private final Set<File> downloadFiles;
     62  private final List<FileProxy> proxies;
    5463  private final File workDir;
    5564  private final Set<String> directories;
     
    6978    this.directories = new HashSet<String>();
    7079    this.downloadFiles = new HashSet<File>();
     80    this.proxies = new ArrayList<FileProxy>();
    7181    this.workDir = new File(new File(System.getProperty("java.io.tmpdir")), getId());
    7282    directories.add(cacheRoot);
     
    113123      throw new ItemAlreadyExistsException("File already exists: " + filename);
    114124    }
     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  {
    115140    StaticCache cache = Application.getStaticCache();
    116141    if (cache.isDisabled())
     
    120145    }
    121146   
    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;
    150186  }
    151187 
     
    193229  {
    194230    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);
    195247  }
    196248 
Note: See TracChangeset for help on using the changeset viewer.