Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/cache/ThematicDataCacheCleaner.java @ 845:797a6264b89b
Integrated the CacheCleaner for the ThematicDataCache
gnv-artifacts/trunk@961 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Tue, 20 Apr 2010 18:38:22 +0000 |
parents | |
children | dfd02f8d3602 |
comparison
equal
deleted
inserted
replaced
844:74608c12b4fe | 845:797a6264b89b |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.state.cache; | |
5 | |
6 import java.util.ArrayList; | |
7 import java.util.Collection; | |
8 import java.util.Iterator; | |
9 import java.util.List; | |
10 | |
11 import net.sf.ehcache.Cache; | |
12 | |
13 import org.apache.log4j.Logger; | |
14 import org.w3c.dom.Document; | |
15 import org.w3c.dom.Element; | |
16 import org.w3c.dom.NodeList; | |
17 | |
18 import de.intevation.artifactdatabase.Config; | |
19 import de.intevation.gnv.artifacts.cache.CacheFactory; | |
20 import de.intevation.gnv.geobackend.base.query.cache.CacheCleaner; | |
21 import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory; | |
22 import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException; | |
23 import de.intevation.gnv.geobackend.base.query.exception.QueryException; | |
24 import de.intevation.gnv.state.StateBase; | |
25 import de.intevation.gnv.utils.ArtifactXMLUtilities; | |
26 | |
27 | |
28 /** | |
29 * Extended Class of the CacheCleaner. | |
30 * This Cleaner has the job to cleanup the ThematicData-Cache if it | |
31 * is necessary. | |
32 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a> | |
33 * | |
34 */ | |
35 public class ThematicDataCacheCleaner extends CacheCleaner { | |
36 | |
37 /** | |
38 * the logger, used to log exceptions and additonaly information | |
39 */ | |
40 private static Logger log = Logger.getLogger(ThematicDataCacheCleaner.class); | |
41 | |
42 private final static String XPATH_ARTIFACTS = | |
43 "/artifact-database/artifacts/artifact"; | |
44 private final static String XPATH_STATES = "states/state"; | |
45 private final static String XPATH_STATEID = "id"; | |
46 private final static String XPATH_QUERYID = "queryID"; | |
47 | |
48 /** | |
49 * The Queries that should be Cleaned with its links to the | |
50 * StateIds. | |
51 */ | |
52 private Collection<QueryObject> queryObjects = null; | |
53 | |
54 /** | |
55 * Constructor | |
56 */ | |
57 public ThematicDataCacheCleaner() { | |
58 this.setUp(); | |
59 } | |
60 | |
61 /** | |
62 * Constructor | |
63 * @param arg0 | |
64 */ | |
65 public ThematicDataCacheCleaner(Runnable arg0) { | |
66 this.setUp(); | |
67 } | |
68 | |
69 /** | |
70 * Constructor | |
71 * @param arg0 | |
72 */ | |
73 public ThematicDataCacheCleaner(String arg0) { | |
74 this.setUp(); | |
75 } | |
76 | |
77 /** | |
78 * Constructor | |
79 * @param arg0 | |
80 * @param arg1 | |
81 */ | |
82 public ThematicDataCacheCleaner(ThreadGroup arg0, Runnable arg1) { | |
83 this.setUp(); | |
84 } | |
85 | |
86 /** | |
87 * Constructor | |
88 * @param arg0 | |
89 * @param arg1 | |
90 */ | |
91 public ThematicDataCacheCleaner(ThreadGroup arg0, String arg1) { | |
92 this.setUp(); | |
93 } | |
94 | |
95 /** | |
96 * Constructor | |
97 * @param arg0 | |
98 * @param arg1 | |
99 */ | |
100 public ThematicDataCacheCleaner(Runnable arg0, String arg1) { | |
101 this.setUp(); | |
102 } | |
103 | |
104 /** | |
105 * Constructor | |
106 * @param arg0 | |
107 * @param arg1 | |
108 * @param arg2 | |
109 */ | |
110 public ThematicDataCacheCleaner(ThreadGroup arg0, Runnable arg1, String arg2) { | |
111 this.setUp(); | |
112 } | |
113 | |
114 /** | |
115 * Constructor | |
116 * @param arg0 | |
117 * @param arg1 | |
118 * @param arg2 | |
119 * @param arg3 | |
120 */ | |
121 public ThematicDataCacheCleaner(ThreadGroup arg0, Runnable arg1, | |
122 String arg2, long arg3) { | |
123 this.setUp(); | |
124 } | |
125 | |
126 /** | |
127 * Initializes the QueryObjects. | |
128 * The Queryobjects will be read from the Configuration. | |
129 * Only Queries which are defined in <code>queryID</code>-Elements | |
130 * are used. | |
131 * The other Queries are currently not put into the Cache. | |
132 */ | |
133 @Override | |
134 protected void setUp(){ | |
135 super.setUp(); | |
136 this.queryObjects = new ArrayList<QueryObject>(); | |
137 Document configuration = Config.getConfig(); | |
138 NodeList artifactList = Config.getNodeSetXPath(configuration, | |
139 XPATH_ARTIFACTS); | |
140 log.debug("ThematicDataCacheCleaner.setUp()"); | |
141 if (artifactList != null && artifactList.getLength() > 0){ | |
142 for (int i = 0; i < artifactList.getLength(); i++){ | |
143 Element currentArtifactNode = (Element)artifactList.item(i); | |
144 | |
145 String link = currentArtifactNode.getAttribute("xlink:href"); | |
146 if (link != null && link.length() > 0){ | |
147 String absolutFileName = Config.replaceConfigDir(link); | |
148 currentArtifactNode = (Element)new ArtifactXMLUtilities() | |
149 .readConfiguration(absolutFileName); | |
150 } | |
151 NodeList stateList = Config.getNodeSetXPath(currentArtifactNode, | |
152 XPATH_STATES); | |
153 if (stateList != null && stateList.getLength() > 0){ | |
154 for (int j = 0; j < stateList.getLength() ; j++){ | |
155 Element currentStateNode = (Element)stateList.item(j); | |
156 String stateId = currentStateNode | |
157 .getAttribute(XPATH_STATEID); | |
158 String queryID = Config.getStringXPath(currentStateNode, | |
159 XPATH_QUERYID); | |
160 try { | |
161 if (queryID != null){ | |
162 String query = QueryContainerFactory | |
163 .getInstance() | |
164 .getQueryContainer() | |
165 .getQuery(queryID); | |
166 QueryObject qo = new QueryObject(stateId, query); | |
167 queryObjects.add(qo); | |
168 } | |
169 } catch (QueryContainerException e) { | |
170 log.error(e,e); | |
171 } | |
172 } | |
173 } | |
174 } | |
175 } | |
176 } | |
177 | |
178 @Override | |
179 protected void cleanup() { | |
180 log.debug("ThematicDataCacheCleaner.cleanup"); | |
181 try { | |
182 if (queryObjects != null && queryObjects.size() > 0){ | |
183 String[] tableNames = this.getUpdatedTableNames(); | |
184 if (tableNames != null && tableNames.length > 0){ | |
185 Iterator<QueryObject> it = queryObjects.iterator(); | |
186 while (it.hasNext()){ | |
187 QueryObject qo = it.next(); | |
188 for (int i = 0; i < tableNames.length; i++){ | |
189 if (qo.queryContainsTableName(tableNames[i])){ | |
190 String stateId = qo.getStateId(); | |
191 this.cleanUpCache(stateId); | |
192 break; | |
193 } | |
194 } | |
195 } | |
196 }else{ | |
197 log.debug("No Tables found to cleanup."); | |
198 } | |
199 }else{ | |
200 log.warn("No Queries to clean"); | |
201 } | |
202 } catch (QueryException e) { | |
203 log.error(e,e); | |
204 } | |
205 } | |
206 | |
207 /** | |
208 * Removes the Entries which Keys matches to the <code>stateId</code> | |
209 * from the Cache. | |
210 * @param stateId The Id of the State which Entries has to be removed. | |
211 */ | |
212 private void cleanUpCache(String stateId){ | |
213 log.debug("ThematicDataCacheCleaner.cleanUpCache "+stateId); | |
214 CacheFactory factory = CacheFactory.getInstance(); | |
215 Cache cache = factory.getCache(); | |
216 List<String> keys = cache.getKeys(); | |
217 String keySample = StateBase.HASH_ID_SEPARATOR + | |
218 stateId + | |
219 StateBase.HASH_ID_SEPARATOR; | |
220 if (keys != null && keys.size() > 0){ | |
221 Iterator<String> it = keys.iterator(); | |
222 while (it.hasNext()){ | |
223 String key = it.next(); | |
224 if (key != null && key.contains(keySample)){ | |
225 boolean removed = cache.remove(key); | |
226 if (!removed){ | |
227 log.warn("Object with Key " + | |
228 key + "could not be removed from Cache"); | |
229 }else{ | |
230 log.debug("Object with Key " + | |
231 key + "has been removed from Cache"); | |
232 } | |
233 } | |
234 } | |
235 } | |
236 } | |
237 } |