Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java @ 1675:19d0eb41e923
AnnotationArtifact: Bugfixes and enabled cache support.
flys-artifacts/trunk@2892 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 05 Oct 2011 10:26:54 +0000 |
parents | 26e19cdaed5e |
children | 5d8b3880a553 |
comparison
equal
deleted
inserted
replaced
1674:b5209452f6bb | 1675:19d0eb41e923 |
---|---|
6 import org.w3c.dom.Document; | 6 import org.w3c.dom.Document; |
7 import org.w3c.dom.Element; | 7 import org.w3c.dom.Element; |
8 | 8 |
9 import org.apache.log4j.Logger; | 9 import org.apache.log4j.Logger; |
10 | 10 |
11 //import net.sf.ehcache.Cache; | 11 import net.sf.ehcache.Cache; |
12 | 12 |
13 import de.intevation.artifacts.Artifact; | 13 import de.intevation.artifacts.Artifact; |
14 import de.intevation.artifacts.ArtifactNamespaceContext; | 14 import de.intevation.artifacts.ArtifactNamespaceContext; |
15 import de.intevation.artifacts.CallContext; | 15 import de.intevation.artifacts.CallContext; |
16 import de.intevation.artifacts.CallMeta; | 16 import de.intevation.artifacts.CallMeta; |
36 * Artifact to access names of Points Of Interest along a segment of a river. | 36 * Artifact to access names of Points Of Interest along a segment of a river. |
37 */ | 37 */ |
38 public class AnnotationArtifact extends StaticFLYSArtifact { | 38 public class AnnotationArtifact extends StaticFLYSArtifact { |
39 | 39 |
40 /** The logger for this class. */ | 40 /** The logger for this class. */ |
41 private static Logger logger = Logger.getLogger(WINFOArtifact.class); | 41 private static Logger logger = Logger.getLogger(AnnotationArtifact.class); |
42 | 42 |
43 /** The name of the artifact. */ | 43 /** The name of the artifact. */ |
44 public static final String ARTIFACT_NAME = "annotation"; | 44 public static final String ARTIFACT_NAME = "annotation"; |
45 | 45 |
46 /* Name of cache. */ | 46 /* Name of cache. */ |
47 //public static final String CACHE_NAME = "annotations"; | 47 public static final String CACHE_NAME = "annotations"; |
48 | 48 |
49 @Override | 49 @Override |
50 protected void initialize(Artifact artifact, Object context, | 50 protected void initialize(Artifact artifact, Object context, |
51 CallMeta meta) { | 51 CallMeta meta) { |
52 logger.debug("AnnotationArtifact.initialize, id: " | 52 logger.debug("AnnotationArtifact.initialize, id: " |
224 * | 224 * |
225 * @return list of Annotations. | 225 * @return list of Annotations. |
226 */ | 226 */ |
227 public List<Annotation> getAnnotations() { | 227 public List<Annotation> getAnnotations() { |
228 String river = FLYSUtils.getRiver(this).getName(); | 228 String river = FLYSUtils.getRiver(this).getName(); |
229 logger.debug("Search annotations for river: " /*+ river*/); | 229 logger.debug("Search annotations for river: " + river); |
230 //Cache cache = CacheFactory.getCache(CACHE_NAME); | 230 |
231 | 231 Cache cache = CacheFactory.getCache(CACHE_NAME); |
232 List<Annotation> annotations = new ArrayList<Annotation>(); | 232 String key = river; |
233 | 233 Object old = null; |
234 return getAnnotationsUncached(river); | 234 |
235 if (cache != null) { | |
236 logger.debug("We are using a cache for annotations."); | |
237 | |
238 net.sf.ehcache.Element element = cache.get(key); | |
239 if (element != null) { | |
240 logger.info("Fetched annotations from cache."); | |
241 old = element.getValue(); | |
242 } | |
243 } | |
244 | |
245 if (old == null) { | |
246 old = getAnnotationsUncached(river); | |
247 } | |
248 | |
249 if (cache != null && old != null) { | |
250 cache.put(new net.sf.ehcache.Element(key, old)); | |
251 } | |
252 | |
253 return old != null | |
254 ? (List<Annotation>) old | |
255 : new ArrayList<Annotation>(); | |
235 } | 256 } |
236 | 257 |
237 /** | 258 /** |
238 * Gets Annotations from Session/Database. | 259 * Gets Annotations from Session/Database. |
239 * | 260 * |
240 * @return List of Annotations fetched fresh from session/database. | 261 * @return List of Annotations fetched fresh from session/database. |
241 * @see DistanceInfoService to access cached documents. | 262 * @see DistanceInfoService to access cached documents. |
242 */ | 263 */ |
243 protected List<Annotation> getAnnotationsUncached(String river) { | 264 protected List<Annotation> getAnnotationsUncached(String river) { |
265 logger.info("Fetch annotations from database."); | |
266 | |
244 List<Annotation> annotations = new ArrayList<Annotation>(); | 267 List<Annotation> annotations = new ArrayList<Annotation>(); |
245 annotations = AnnotationsFactory.getPointAnnotations(river); | 268 annotations = AnnotationsFactory.getPointAnnotations(river); |
246 | 269 |
247 return annotations; | 270 return annotations; |
248 } | 271 } |