extensions/net.sf.basedb.genepattern/trunk/src/net/sf/basedb/genepattern/file/SupportFileTransferGateway.java

Code
Comments
Other
Rev Date Author Line
1132 18 Jun 09 nicklas 1 package net.sf.basedb.genepattern.file;
1132 18 Jun 09 nicklas 2
1132 18 Jun 09 nicklas 3 import java.io.BufferedInputStream;
1132 18 Jun 09 nicklas 4 import java.io.File;
1132 18 Jun 09 nicklas 5 import java.io.FileInputStream;
1132 18 Jun 09 nicklas 6 import java.io.IOException;
1132 18 Jun 09 nicklas 7 import java.util.ArrayList;
1132 18 Jun 09 nicklas 8 import java.util.List;
1132 18 Jun 09 nicklas 9
1132 18 Jun 09 nicklas 10 import org.genepattern.webservice.WebServiceException;
1132 18 Jun 09 nicklas 11
1132 18 Jun 09 nicklas 12 import net.sf.basedb.core.Application;
1132 18 Jun 09 nicklas 13 import net.sf.basedb.core.ConfigurationException;
1134 22 Jun 09 nicklas 14 import net.sf.basedb.genepattern.wrapper.GPClient;
1132 18 Jun 09 nicklas 15 import net.sf.basedb.genepattern.wrapper.SupportFileInfo;
1134 22 Jun 09 nicklas 16 import net.sf.basedb.genepattern.wrapper.TaskInfo;
1132 18 Jun 09 nicklas 17 import net.sf.basedb.util.StaticCache;
1132 18 Jun 09 nicklas 18
1132 18 Jun 09 nicklas 19 /**
1132 18 Jun 09 nicklas 20   Support files are JAR files and other files that are needed by 
1132 18 Jun 09 nicklas 21   visualizer modules. Since visualizer modules are executed on the
1132 18 Jun 09 nicklas 22   client computer we need a way to transfer the support files from
1132 18 Jun 09 nicklas 23   the GenePattern server to the client computer. To accomplish that
1132 18 Jun 09 nicklas 24   we need to use the BASE as proxy. This class is used for downloading
1132 18 Jun 09 nicklas 25   support files from a GenePattern server to a semi-temporary location
1132 18 Jun 09 nicklas 26   on the BASE server. With 'semi-temporary' we mean that we keep the
1132 18 Jun 09 nicklas 27   files on the BASE server as long as there is any interest in them
1132 18 Jun 09 nicklas 28   but if long enough time has passed the files will be deleted. 
1132 18 Jun 09 nicklas 29   To accomplish this we simply use the static cache which has the
1132 18 Jun 09 nicklas 30   desired functionality. See {@link Application#getStaticCache()}.
1132 18 Jun 09 nicklas 31   
1132 18 Jun 09 nicklas 32   @author nicklas
1132 18 Jun 09 nicklas 33   @since 1.0
1132 18 Jun 09 nicklas 34 */
1132 18 Jun 09 nicklas 35 public class SupportFileTransferGateway 
1132 18 Jun 09 nicklas 36 {
1132 18 Jun 09 nicklas 37
1134 22 Jun 09 nicklas 38   private final TaskInfo ti;
1134 22 Jun 09 nicklas 39
1132 18 Jun 09 nicklas 40   
1132 18 Jun 09 nicklas 41   /**
1132 18 Jun 09 nicklas 42     Create a new support file transfer gateway for a given module.
1132 18 Jun 09 nicklas 43     Note the argument must be the LSID of the module. Using the module
1132 18 Jun 09 nicklas 44     name will not work!
1132 18 Jun 09 nicklas 45     @param lsid The LSID of a module
1132 18 Jun 09 nicklas 46   */
1134 22 Jun 09 nicklas 47   public SupportFileTransferGateway(TaskInfo ti)
1132 18 Jun 09 nicklas 48   {
1134 22 Jun 09 nicklas 49     this.ti = ti;
1132 18 Jun 09 nicklas 50   }
1132 18 Jun 09 nicklas 51   
1132 18 Jun 09 nicklas 52   /**
1132 18 Jun 09 nicklas 53     Download the required support files. Support files that already
1132 18 Jun 09 nicklas 54     exists in the cache are not downloaded again.
1132 18 Jun 09 nicklas 55     
1132 18 Jun 09 nicklas 56     @param tiProxy The web service proxy to use for the download
1132 18 Jun 09 nicklas 57     @throws WebServiceException
1132 18 Jun 09 nicklas 58     @throws IOException
1132 18 Jun 09 nicklas 59   */
1134 22 Jun 09 nicklas 60   public void downloadSupportFiles(GPClient gp)
1132 18 Jun 09 nicklas 61     throws WebServiceException, IOException
1132 18 Jun 09 nicklas 62   {
1132 18 Jun 09 nicklas 63     StaticCache cache = Application.getStaticCache();
1132 18 Jun 09 nicklas 64     if (cache.isDisabled())
1132 18 Jun 09 nicklas 65     {
1132 18 Jun 09 nicklas 66       throw new ConfigurationException("The static cache is disabled. " +
1132 18 Jun 09 nicklas 67         "Please set 'cache.static.disabled = false' in 'base.config'.");
1132 18 Jun 09 nicklas 68     }
1132 18 Jun 09 nicklas 69     
1134 22 Jun 09 nicklas 70     String moduleLsid = ti.getLsid();
1134 22 Jun 09 nicklas 71     String validKey = StaticCache.makeValidKey(moduleLsid, "-");
1134 22 Jun 09 nicklas 72     
1134 22 Jun 09 nicklas 73     List<SupportFileInfo> toDownload = new ArrayList<SupportFileInfo>();
1134 22 Jun 09 nicklas 74     String cacheRoot = "gp-integration/" + validKey + "/";
1134 22 Jun 09 nicklas 75     SupportFileInfo[] supportFiles = gp.getSupportFiles(moduleLsid);
1132 18 Jun 09 nicklas 76     for (SupportFileInfo file : supportFiles)
1132 18 Jun 09 nicklas 77     {
1132 18 Jun 09 nicklas 78       if (!cache.exists(cacheRoot + file.getFileName()))
1132 18 Jun 09 nicklas 79       {
1134 22 Jun 09 nicklas 80         toDownload.add(file);
1132 18 Jun 09 nicklas 81       }
1132 18 Jun 09 nicklas 82     }
1134 22 Jun 09 nicklas 83     File workDir = new File(System.getProperty("java.io.tmpdir"), validKey);
1134 22 Jun 09 nicklas 84     workDir.mkdirs();
1134 22 Jun 09 nicklas 85     gp.downloadSupportFiles(moduleLsid, toDownload.toArray(new SupportFileInfo[0]), workDir);
1134 22 Jun 09 nicklas 86     for (SupportFileInfo file : toDownload)
1132 18 Jun 09 nicklas 87     {
1132 18 Jun 09 nicklas 88       File sf = new File(workDir, file.getFileName());
1132 18 Jun 09 nicklas 89       if (sf.exists())
1132 18 Jun 09 nicklas 90       {
1132 18 Jun 09 nicklas 91         cache.write(cacheRoot + file.getFileName(), new BufferedInputStream(new FileInputStream(sf)), 1000);
1132 18 Jun 09 nicklas 92         sf.delete();
1132 18 Jun 09 nicklas 93       }
1132 18 Jun 09 nicklas 94     }
1132 18 Jun 09 nicklas 95     
1132 18 Jun 09 nicklas 96   }
1132 18 Jun 09 nicklas 97   
1132 18 Jun 09 nicklas 98   
1132 18 Jun 09 nicklas 99 }