1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.BufferedWriter; |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileInputStream; |
---|
7 | import java.io.FileNotFoundException; |
---|
8 | import java.io.FileReader; |
---|
9 | import java.io.FileWriter; |
---|
10 | import java.io.IOException; |
---|
11 | import java.io.InputStream; |
---|
12 | import java.io.Writer; |
---|
13 | |
---|
14 | /** |
---|
15 | * This is a rudimentary caching mechanism. |
---|
16 | * serializes the inputstream to a file (identifier => filename) |
---|
17 | * and returns the inpustream based on the identifier |
---|
18 | * @author master |
---|
19 | * |
---|
20 | */ |
---|
21 | /* TODO: necessary to maintain own index |
---|
22 | * for resolution of IDs to filenames |
---|
23 | */ |
---|
24 | |
---|
25 | |
---|
26 | public class Cache { |
---|
27 | public static String PREFIX = "_xc"; |
---|
28 | |
---|
29 | private static Cache singleton; |
---|
30 | private String cachepath; |
---|
31 | private Integer cachecounter; |
---|
32 | private static final Integer start_cache = 1; |
---|
33 | |
---|
34 | |
---|
35 | public Cache () { |
---|
36 | cachepath = Admin.getConfig().getProperty("cache.path"); |
---|
37 | cachecounter = initCachecounter(); |
---|
38 | } |
---|
39 | |
---|
40 | public static Cache getCache() { |
---|
41 | if (singleton == null) { |
---|
42 | singleton = new Cache(); |
---|
43 | } |
---|
44 | return singleton; |
---|
45 | } |
---|
46 | |
---|
47 | protected void finalize() throws Throwable { |
---|
48 | try { |
---|
49 | updateCachecounter(); |
---|
50 | } catch(Exception e) { |
---|
51 | } |
---|
52 | finally { |
---|
53 | super.finalize(); |
---|
54 | //more code can be written here as per need of application |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * here the external key for the data gets cache-internal counter-id assigned |
---|
60 | * which is also returned back |
---|
61 | * can be used (as shorthand instead of the (long) external key) from outside to retrieve the data. |
---|
62 | * This is primarily meant to be able to reuse the recordset-data, when reading one record. |
---|
63 | * @param id |
---|
64 | * @param instream |
---|
65 | * @return |
---|
66 | */ |
---|
67 | public String putInCache(String id, InputStream instream) { |
---|
68 | |
---|
69 | Integer c = getCounter(); |
---|
70 | String xc = PREFIX + c.toString(); |
---|
71 | //Admin.writeToFile(formPath(id + xc),instream); |
---|
72 | Admin.writeToFile(formPath(id),instream); |
---|
73 | return xc; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | public InputStream getFromCache(String id) { |
---|
78 | File f = new File (formPath(id)); |
---|
79 | |
---|
80 | if (f.exists()) { // read from file to InputStream; |
---|
81 | InputStream instream; |
---|
82 | try { |
---|
83 | instream = new FileInputStream(f); |
---|
84 | return instream; |
---|
85 | } catch (FileNotFoundException e) { |
---|
86 | // TODO Auto-generated catch block |
---|
87 | Admin.notifyUser("Cache: file not found" + f.getPath()); |
---|
88 | e.printStackTrace(); |
---|
89 | return null; |
---|
90 | } |
---|
91 | |
---|
92 | } else { //not cached, my dear |
---|
93 | return null; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | //TODO: sanitize-key |
---|
98 | public String formPath (String id) { |
---|
99 | String path = cachepath + id + ".xml"; |
---|
100 | return path; |
---|
101 | } |
---|
102 | |
---|
103 | public Integer getCounter () { |
---|
104 | cachecounter = cachecounter +1 ; |
---|
105 | return cachecounter; |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * read counter-integer from file in cache |
---|
110 | * or start that file |
---|
111 | * @return |
---|
112 | */ |
---|
113 | public Integer initCachecounter() { |
---|
114 | Integer counter = start_cache; |
---|
115 | boolean init=false; |
---|
116 | File f = new File (Admin.getConfig().getProperty("cache.path") + "counter.txt"); |
---|
117 | FileInputStream fis ; |
---|
118 | if (!f.exists()) { |
---|
119 | try { |
---|
120 | f.createNewFile(); |
---|
121 | } catch (IOException e2) { |
---|
122 | Admin.notifyUser("ERROR creating cache counter"); |
---|
123 | e2.printStackTrace(); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | try { |
---|
128 | //use buffering, reading one line at a time |
---|
129 | //FileReader always assumes default encoding is OK! |
---|
130 | BufferedReader input = new BufferedReader(new FileReader(f)); |
---|
131 | try { |
---|
132 | String line = null; //not declared within while loop |
---|
133 | /* |
---|
134 | * readLine is a bit quirky : |
---|
135 | * it returns the content of a line MINUS the newline. |
---|
136 | * it returns null only for the END of the stream. |
---|
137 | * it returns an empty String if two newlines appear in a row. |
---|
138 | */ |
---|
139 | try { |
---|
140 | if (( line = input.readLine()) != null){ |
---|
141 | try { |
---|
142 | counter = new Integer(line); |
---|
143 | } catch (NumberFormatException e) { |
---|
144 | // if not a number, write a number |
---|
145 | init=true; |
---|
146 | } |
---|
147 | } else { |
---|
148 | init=true; |
---|
149 | } |
---|
150 | } catch (IOException e) { |
---|
151 | init=true; |
---|
152 | } |
---|
153 | |
---|
154 | if (init) { |
---|
155 | writeCachecounter( start_cache); |
---|
156 | } |
---|
157 | |
---|
158 | } finally { |
---|
159 | input.close(); |
---|
160 | } |
---|
161 | } catch (IOException ex){ |
---|
162 | ex.printStackTrace(); |
---|
163 | } |
---|
164 | return counter; |
---|
165 | } |
---|
166 | |
---|
167 | public void updateCachecounter () { |
---|
168 | writeCachecounter( cachecounter); |
---|
169 | } |
---|
170 | public void writeCachecounter (Integer i) { |
---|
171 | File f = new File (Admin.getConfig().getProperty("cache.path") + "counter.txt"); |
---|
172 | |
---|
173 | try { |
---|
174 | Writer output = new BufferedWriter(new FileWriter(f)); |
---|
175 | //FileWriter always assumes default encoding is OK! |
---|
176 | try { |
---|
177 | output.write( i); |
---|
178 | } finally { |
---|
179 | output.close(); |
---|
180 | } |
---|
181 | } catch (IOException e) { |
---|
182 | // TODO Auto-generated catch block |
---|
183 | e.printStackTrace(); |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|