1 14 package org.wings.template; 15 16 import java.io.*; 17 import java.net.URL ; 18 import java.util.Hashtable ; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 30 public class CachedFileTemplateSource 31 extends FileTemplateSource { 32 private final transient static Log log = LogFactory.getLog(CachedFileTemplateSource.class); 33 private final static class CacheEntry { 34 private byte[] filebuffer = null; 35 private long lastModified; 36 private File file; 37 38 public CacheEntry(File f) throws IOException { 39 this.file = f; 40 lastModified = -1; 41 if (file != null) { 43 lastModified = file.lastModified(); 44 refresh(); 45 } 46 } 47 48 public CacheEntry(URL url) throws IOException { 49 file = null; 50 lastModified = System.currentTimeMillis(); 51 InputStream in = url.openStream(); 52 56 int totLen = 0; 57 int copyLen = 0; 58 byte[] tempBuffer = new byte[1024]; 59 do { 60 copyLen = in.read(tempBuffer); 61 if (copyLen > 0) { 62 byte[] newFileBuf = new byte[totLen + copyLen]; 63 if (filebuffer != null) 64 System.arraycopy(filebuffer, 0, newFileBuf, 0, totLen); 65 System.arraycopy(tempBuffer, 0, newFileBuf, totLen, 66 copyLen); 67 totLen += copyLen; 68 filebuffer = newFileBuf; 69 } 70 } while (copyLen >= 0); 71 } 72 73 public byte[] getBuffer() { 74 return filebuffer; 75 } 76 77 83 public long lastModified() { 84 checkModified(); 85 return lastModified; 86 } 87 88 private void checkModified() { 89 if (file == null) 90 return; 91 long timestamp = file.lastModified(); 92 if (lastModified != timestamp) { 93 lastModified = timestamp; 94 try { 95 refresh(); 96 } catch (IOException e) { 97 100 if (log.isErrorEnabled()) { 101 log.error(file.getAbsolutePath() + " not found. Maybe it has been deleted from the filesystem."); 102 } 103 } 104 } 105 } 106 107 private void refresh() throws IOException { 108 int len = (int) file.length(); 109 filebuffer = new byte[len]; 110 FileInputStream in = new FileInputStream(file); 111 int pos = 0; 112 while (pos < len) { 113 pos += in.read(filebuffer, pos, len - pos); 114 } 115 in.close(); 116 } 117 } 118 119 123 private static Hashtable cache = new Hashtable (); 124 private static final int CACHED_LIMIT = 1024; 125 126 private CacheEntry entry; 127 128 public CachedFileTemplateSource(File f) 129 throws IOException { 130 super(f); 131 132 entry = (CacheEntry) cache.get(f); 133 if (entry == null && f.length() <= CACHED_LIMIT) { 134 entry = new CacheEntry(f); 135 cache.put(f, entry); 136 } 137 } 138 139 public CachedFileTemplateSource(URL url) 140 throws IOException { 141 super(null); entry = (CacheEntry) cache.get(url); 143 if (entry == null) { 144 entry = new CacheEntry(url); 145 cache.put(url, entry); 146 } 147 canonicalName = url.toString(); 148 } 149 150 161 public long lastModified() { 162 if (entry != null) 163 return entry.lastModified(); 164 else 165 return super.lastModified(); 166 } 167 168 171 public InputStream getInputStream() 172 throws IOException { 173 if (entry != null) 174 return new ByteArrayInputStream(entry.getBuffer()); 175 else 176 return super.getInputStream(); 177 } 178 } 179 180 181 | Popular Tags |