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 |
Support files are JAR files and other files that are needed by |
1132 |
18 Jun 09 |
nicklas |
visualizer modules. Since visualizer modules are executed on the |
1132 |
18 Jun 09 |
nicklas |
client computer we need a way to transfer the support files from |
1132 |
18 Jun 09 |
nicklas |
the GenePattern server to the client computer. To accomplish that |
1132 |
18 Jun 09 |
nicklas |
we need to use the BASE as proxy. This class is used for downloading |
1132 |
18 Jun 09 |
nicklas |
support files from a GenePattern server to a semi-temporary location |
1132 |
18 Jun 09 |
nicklas |
on the BASE server. With 'semi-temporary' we mean that we keep the |
1132 |
18 Jun 09 |
nicklas |
files on the BASE server as long as there is any interest in them |
1132 |
18 Jun 09 |
nicklas |
but if long enough time has passed the files will be deleted. |
1132 |
18 Jun 09 |
nicklas |
To accomplish this we simply use the static cache which has the |
1132 |
18 Jun 09 |
nicklas |
desired functionality. See {@link Application#getStaticCache()}. |
1132 |
18 Jun 09 |
nicklas |
31 |
|
1132 |
18 Jun 09 |
nicklas |
@author nicklas |
1132 |
18 Jun 09 |
nicklas |
@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 |
Create a new support file transfer gateway for a given module. |
1132 |
18 Jun 09 |
nicklas |
Note the argument must be the LSID of the module. Using the module |
1132 |
18 Jun 09 |
nicklas |
name will not work! |
1132 |
18 Jun 09 |
nicklas |
@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 |
Download the required support files. Support files that already |
1132 |
18 Jun 09 |
nicklas |
exists in the cache are not downloaded again. |
1132 |
18 Jun 09 |
nicklas |
55 |
|
1132 |
18 Jun 09 |
nicklas |
@param tiProxy The web service proxy to use for the download |
1132 |
18 Jun 09 |
nicklas |
@throws WebServiceException |
1132 |
18 Jun 09 |
nicklas |
@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 |
} |