comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/cache/ThematicDataCacheCleaner.java @ 1119:7c4f81f74c47

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

http://dive4elements.wald.intevation.org