| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | package org.pathvisio.wikipathways.server; |
|---|
| 18 | |
|---|
| 19 | import java.io.File; |
|---|
| 20 | import java.io.FileInputStream; |
|---|
| 21 | import java.io.FileNotFoundException; |
|---|
| 22 | import java.io.IOException; |
|---|
| 23 | import java.io.InputStream; |
|---|
| 24 | import java.util.List; |
|---|
| 25 | |
|---|
| 26 | import javax.xml.rpc.ServiceException; |
|---|
| 27 | |
|---|
| 28 | import org.bridgedb.IDMapperException; |
|---|
| 29 | import org.bridgedb.bio.Organism; |
|---|
| 30 | import org.bridgedb.rdb.GdbProvider; |
|---|
| 31 | import org.pathvisio.model.BatikImageExporter; |
|---|
| 32 | import org.pathvisio.model.ConverterException; |
|---|
| 33 | import org.pathvisio.model.ImageExporter; |
|---|
| 34 | |
|---|
| 35 | import atlas.model.Factor; |
|---|
| 36 | import atlas.model.GeneSet; |
|---|
| 37 | |
|---|
| 38 | public class ImageCache { |
|---|
| 39 | static final String CACHE_PATH = "cache_images/"; |
|---|
| 40 | static final String SEP_REV = "@"; |
|---|
| 41 | public static final String GET_ID = "id"; |
|---|
| 42 | |
|---|
| 43 | private PathwayCache pathwayCache; |
|---|
| 44 | private AtlasCache atlasCache; |
|---|
| 45 | private String basePath; |
|---|
| 46 | private GdbProvider gdbs; |
|---|
| 47 | |
|---|
| 48 | private long retention_time = -1; |
|---|
| 49 | |
|---|
| 50 | public ImageCache(String basePath, PathwayCache pathwayCache, AtlasCache atlasCache, GdbProvider gdbs) { |
|---|
| 51 | this.pathwayCache = pathwayCache; |
|---|
| 52 | this.atlasCache = atlasCache; |
|---|
| 53 | this.basePath = basePath; |
|---|
| 54 | this.gdbs = gdbs; |
|---|
| 55 | |
|---|
| 56 | new File(basePath + "/" + CACHE_PATH).mkdirs(); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public void setRetentionTime(long retention_time) { |
|---|
| 60 | this.retention_time = retention_time; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | public String getImageUrl(String pathwayId, List<Factor> factors) throws ConverterException, FileNotFoundException, ServiceException, IOException, ClassNotFoundException, IDMapperException { |
|---|
| 64 | WPPathway pathway = pathwayCache.getPathway(pathwayId); |
|---|
| 65 | GeneSet atlasGenes = atlasCache.getGeneSet(pathwayId); |
|---|
| 66 | |
|---|
| 67 | File cache = getCacheFile(pathway.getId(), pathway.getRevision(), factors); |
|---|
| 68 | if(!cache.exists() || !CacheManager.checkCacheAge(cache, retention_time)) { |
|---|
| 69 | Organism org = Organism.fromLatinName(pathway.getPathway().getMappInfo().getOrganism()); |
|---|
| 70 | AtlasVisualizer visualizer = new AtlasVisualizer( |
|---|
| 71 | pathway.getPathway(), |
|---|
| 72 | atlasGenes, |
|---|
| 73 | factors, |
|---|
| 74 | gdbs.getGdbs(org) |
|---|
| 75 | ); |
|---|
| 76 | visualizer.export(new BatikImageExporter(ImageExporter.TYPE_PNG), cache); |
|---|
| 77 | } |
|---|
| 78 | return "getImage?" + GET_ID + "=" + cache.getName(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | public byte[] getImageData(String id) throws IOException { |
|---|
| 82 | return getBytesFromFile(getCacheFile(id)); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | public static byte[] getBytesFromFile(File file) throws IOException { |
|---|
| 89 | InputStream is = new FileInputStream(file); |
|---|
| 90 | |
|---|
| 91 | long length = file.length(); |
|---|
| 92 | byte[] bytes = new byte[(int)length]; |
|---|
| 93 | |
|---|
| 94 | int offset = 0; |
|---|
| 95 | int numRead = 0; |
|---|
| 96 | while (offset < bytes.length |
|---|
| 97 | && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { |
|---|
| 98 | offset += numRead; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if (offset < bytes.length) { |
|---|
| 102 | throw new IOException("Could not completely read file "+file.getName()); |
|---|
| 103 | } |
|---|
| 104 | is.close(); |
|---|
| 105 | return bytes; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | private File getCacheFile(String imageId) { |
|---|
| 109 | return new File(basePath + "/" + CACHE_PATH, imageId); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | private File getCacheFile(String id, String revision, List<Factor> factors) { |
|---|
| 113 | String factorId = ""; |
|---|
| 114 | for(Factor f : factors) factorId += f.hashCode() + SEP_REV; |
|---|
| 115 | return new File(basePath + "/" + CACHE_PATH, id + SEP_REV + revision + SEP_REV + factorId.hashCode()); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|