KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > template > CachedFileTemplateSource


1 /*
2  * $Id: CachedFileTemplateSource.java,v 1.5 2005/04/12 16:08:11 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.template;
15
16 import java.io.*;
17 import java.net.URL JavaDoc;
18 import java.util.Hashtable JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 /**
24  * A <CODE>CachedFileDataSource</CODE> implements a DataSource
25  * for a file, but caches small ones.
26  *
27  * @author <A HREF="mailto:zeller@think.de">Henner Zeller</A>
28  * @version $Revision: 1.5 $
29  */

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             // we want to throw an IOException here if the file is not found
42
if (file != null) {
43                 lastModified = file.lastModified();
44                 refresh();
45             }
46         }
47
48         public CacheEntry(URL JavaDoc url) throws IOException {
49             file = null;
50             lastModified = System.currentTimeMillis();
51             InputStream in = url.openStream();
52             /*
53              * unfortnunatly, we do not know the length of
54              * the data ..
55              */

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         /**
78          * returns the time, this file has been
79          * last modified. This checks the Timestamp of
80          * the file and initiates a reload to the cache
81          * if it changed.
82          */

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                     /* ignore currently, file might have been deleted, but is
98                      * still in cache.
99                      */

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     /*
120      * we should provide a way to expunge old
121      * entries here ...
122      */

123     private static Hashtable JavaDoc cache = new Hashtable JavaDoc();
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 JavaDoc url)
140             throws IOException {
141         super(null); // we never read the file directly
142
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     /**
151      * Returns the time the content of this File
152      * was last modified.
153      * <p/>
154      * The return value is used to decide whether to reparse a
155      * Source or not. Reparsing is done if the value returned
156      * here differs from the value returned at the last processing
157      * time.
158      *
159      * @return long a modification time
160      */

161     public long lastModified() {
162         if (entry != null)
163             return entry.lastModified();
164         else
165             return super.lastModified();
166     }
167
168     /**
169      * Gets an InputStream of the File.
170      */

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