1 22 23 package de.laures.cewolf.storage; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.io.Serializable ; 32 import java.util.ArrayList ; 33 import java.util.List ; 34 35 import javax.servlet.ServletContext ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.jsp.PageContext ; 38 39 import de.laures.cewolf.CewolfException; 40 import de.laures.cewolf.ChartImage; 41 import de.laures.cewolf.Configuration; 42 import de.laures.cewolf.Storage; 43 import de.laures.cewolf.taglib.util.KeyGenerator; 44 45 63 public class FileStorage implements Storage { 64 65 String basePath = null; 66 List stored = new ArrayList (); 67 private boolean deleteOnExit = false; 68 69 72 public String storeChartImage(ChartImage cid, PageContext pageContext) { 73 if(contains(cid, pageContext)){ 74 return getKey(cid); 75 } 76 String id = getKey(cid); 77 ObjectOutputStream oos = null; 78 try { 79 String fileName = getFileName(id); 80 pageContext.getServletContext().log("Storing image to file " + fileName); 81 File f = new File (fileName); 82 if (deleteOnExit) { 83 f.deleteOnExit(); 84 } 85 oos = new ObjectOutputStream (new FileOutputStream (f)); 86 oos.writeObject(new SerializableChartImage(cid)); 87 oos.close(); 88 } catch(IOException ioex){ 89 ioex.printStackTrace(); 90 } catch(CewolfException cwex){ 91 cwex.printStackTrace(); 92 } finally { 93 if(oos != null){ 94 try { 95 oos.close(); 96 } catch(IOException ioex){ 97 ioex.printStackTrace(); 98 } 99 } 100 } 101 return id; 102 } 103 104 107 public ChartImage getChartImage(String id, HttpServletRequest request) { 108 ChartImage res = null; 109 ObjectInputStream ois = null; 110 try { 111 ois = new ObjectInputStream (new FileInputStream (getFileName(id))); 112 res = (ChartImage)ois.readObject(); 113 ois.close(); 114 } catch(Exception ex){ 115 ex.printStackTrace(); 116 } finally { 117 if(ois != null){ 118 try { 119 ois.close(); 120 } catch(IOException ioex){ 121 ioex.printStackTrace(); 122 } 123 } 124 } 125 return res; 126 } 127 128 131 public boolean contains(ChartImage chartImage, PageContext pageContext) { 132 return new File (getFileName(chartImage)).exists(); 133 } 134 135 138 public String getKey(ChartImage chartImage) { 139 return String.valueOf(KeyGenerator.generateKey((Serializable )chartImage)); 140 } 141 142 145 public void init(ServletContext servletContext) throws CewolfException { 146 basePath = servletContext.getRealPath("/"); 147 Configuration config = Configuration.getInstance(servletContext); 148 deleteOnExit = "true".equalsIgnoreCase("" + config.getParameters().get("FileStorage.deleteOnExit")); 149 servletContext.log("FileStorage initialized, deleteOnExit=" + deleteOnExit); 150 } 151 152 private String getFileName(ChartImage chartImage){ 153 return getFileName(getKey(chartImage)); 154 } 155 156 private String getFileName(String id){ 157 return basePath + "_chart" + id; 158 } 159 160 163 public String removeChartImage(String imgKey, HttpServletRequest pageContext) throws CewolfException { 164 File file = new File (getFileName(imgKey)); 165 if (file.exists()) 166 { 167 if (!file.delete()) 168 { 169 throw new CewolfException("Could not delete file " + file.getAbsolutePath()); 170 } 171 } 172 return imgKey; 173 } 174 175 } 176 | Popular Tags |