1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/* ------------------------------------ JavaOnTracks Thibaut Colar tcolar-jot AT colar DOT net Artistic Licence 2.0 http://www.javaontracks.net ------------------------------------ */ package net.jot.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.Hashtable; import net.jot.logger.JOTLogger; import net.jot.web.view.JOTViewParser; /** * Allow the caching in memory of the text files. * Rather than having to read the template from file continously, we will cache them in memory.. * If the file is updated (changed timestamp), then reload it * @author thibautc * */ public class JOTTextFileCache { public static Hashtable files = new Hashtable(); /** * Retrieve a template (as a string String) from the cache. * Loads it first if not in cache yet * @param templatePath * @return * @throws Exception if reading the template file fails. */ public static String getFileText(String filePath) throws Exception { if (needCaching(filePath)) { // we need to load it first synchronized (JOTTextFileCache.class) { loadTextFile(filePath); } } return ((JOTTextFileCacheItem) files.get(filePath)).getTemplate(); } public static boolean needCaching(String filePath) { JOTTextFileCacheItem item = (JOTTextFileCacheItem) files.get(filePath); File f = new File(filePath); return item==null || item.getTimestamp() != f.lastModified(); } /** * Load a user template from file * @param templatePath * @throws Exception */ private static void loadTextFile(String filePath) throws Exception { BufferedReader reader = null; String fileString = ""; long timestamp = 0; try { JOTLogger.log(JOTLogger.CAT_FLOW, JOTLogger.DEBUG_LEVEL, JOTViewParser.class, "Caching text file: " + filePath); File f = new File(filePath); timestamp = f.lastModified(); reader = new BufferedReader(new FileReader(filePath)); String s = null; while ((s = reader.readLine()) != null) { fileString += s + "\n"; } reader.close(); } catch (Exception e) { if (reader != null) { reader.close(); } throw (e); } fileString = JOTViewParser.doRemoveTags(fileString); JOTTextFileCacheItem item = new JOTTextFileCacheItem(timestamp, fileString); files.put(filePath, item); } static class JOTTextFileCacheItem { private long timestamp; private String template; public JOTTextFileCacheItem(long timestamp, String template) { this.timestamp = timestamp; this.template = template; } public String getTemplate() { return template; } public long getTimestamp() { return timestamp; } } }