comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java @ 644:02c0cce0e469

Introduce a cache for the distance-info service flys-artifacts/trunk@2028 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 30 May 2011 11:34:06 +0000
parents 4aa078e28cfd
children bcd62609c936 3dc61e00385e
comparison
equal deleted inserted replaced
643:a9bde508824a 644:02c0cce0e469
1 package de.intevation.flys.artifacts.services; 1 package de.intevation.flys.artifacts.services;
2 2
3 import java.math.BigDecimal; 3 import java.math.BigDecimal;
4 import java.util.List; 4 import java.util.Iterator;
5 5
6 import org.apache.log4j.Logger; 6 import org.apache.log4j.Logger;
7 7
8 import org.w3c.dom.Document; 8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element; 9 import org.w3c.dom.Element;
24 24
25 import de.intevation.flys.artifacts.model.AnnotationsFactory; 25 import de.intevation.flys.artifacts.model.AnnotationsFactory;
26 26
27 import org.hibernate.Session; 27 import org.hibernate.Session;
28 28
29 import de.intevation.flys.artifacts.cache.CacheFactory;
30
31 import net.sf.ehcache.Cache;
32
29 /** 33 /**
30 * This service provides information about distances of a specified river. 34 * This service provides information about distances of a specified river.
31 * 35 *
32 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 36 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
33 */ 37 */
34 public class DistanceInfoService extends DefaultService { 38 public class DistanceInfoService extends DefaultService {
35 39
36 /** The logger used in this service.*/ 40 /** The logger used in this service.*/
37 private static Logger logger = Logger.getLogger(DistanceInfoService.class); 41 private static Logger logger = Logger.getLogger(DistanceInfoService.class);
42
43 public static final String CACHE_NAME = "service-distanceinfo";
44
45 public static final String RIVER_XPATH = "/art:river/text()";
38 46
39 47
40 /** 48 /**
41 * The default constructor. 49 * The default constructor.
42 */ 50 */
49 Object globalContext, 57 Object globalContext,
50 CallMeta callMeta) 58 CallMeta callMeta)
51 { 59 {
52 logger.debug("DistanceInfoService.process"); 60 logger.debug("DistanceInfoService.process");
53 61
54 Document result = XMLUtils.newDocument(); 62 String river = XMLUtils.xpathString(
63 data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE);
55 64
56 String river = XMLUtils.xpathString( 65 if (river == null || (river = river.trim()).length() == 0) {
57 data, "/art:river/text()", ArtifactNamespaceContext.INSTANCE);
58
59 if (river == null || river.trim().length() == 0) {
60 logger.warn("No river specified. Cannot return distance info!"); 66 logger.warn("No river specified. Cannot return distance info!");
61 return result; 67 return XMLUtils.newDocument();
62 } 68 }
63 69
64 logger.debug("Search distances for river: " + river); 70 logger.debug("Search distances for river: " + river);
71
72 Cache cache = CacheFactory.getCache(CACHE_NAME);
73
74 if (cache == null) {
75 logger.debug("no cache configured for distance info");
76 return getUncached(river);
77 }
78
79 net.sf.ehcache.Element element = cache.get(river);
80
81 if (element != null) {
82 logger.debug("distance info found in cache");
83 return (Document)element.getValue();
84 }
85
86 Document result = getUncached(river);
87
88 element = new net.sf.ehcache.Element(river, result);
89
90 logger.debug("store distance info found into cache");
91
92 cache.put(element);
93
94 return result;
95 }
96
97 protected Document getUncached(String river) {
98
99 Document result = XMLUtils.newDocument();
65 100
66 ElementCreator ec = new ElementCreator( 101 ElementCreator ec = new ElementCreator(
67 result, 102 result,
68 ArtifactNamespaceContext.NAMESPACE_URI, 103 ArtifactNamespaceContext.NAMESPACE_URI,
69 ArtifactNamespaceContext.NAMESPACE_PREFIX); 104 ArtifactNamespaceContext.NAMESPACE_PREFIX);
70 105
71 Session session = SessionHolder.acquire(); 106 Session session = SessionHolder.acquire();
107
72 try { 108 try {
73 List<Annotation> annotations = AnnotationsFactory.getAnnotations(river); 109 Iterator<Annotation> iter =
74 110 AnnotationsFactory.getAnnotationsIterator(river);
75 if (annotations == null || annotations.size() == 0) {
76 logger.warn("No information found for the specified river!");
77 return result;
78 }
79 111
80 Element all = ec.create("distances"); 112 Element all = ec.create("distances");
81 113
82 for (Annotation a: annotations) { 114 while (iter.hasNext()) {
115 Annotation a = iter.next();
83 Element distance = buildDistanceNode(ec, a); 116 Element distance = buildDistanceNode(ec, a);
84 117 all.appendChild(distance);
85 if (distance != null) {
86 all.appendChild(distance);
87 }
88 } 118 }
89 119
90 result.appendChild(all); 120 result.appendChild(all);
91 } 121 }
92 finally { 122 finally {
104 * @param ec The ElementCreator. 134 * @param ec The ElementCreator.
105 * @param anno The Annotation that provides information about the distance. 135 * @param anno The Annotation that provides information about the distance.
106 * 136 *
107 * @return an Element that contains information about a distance. 137 * @return an Element that contains information about a distance.
108 */ 138 */
109 protected Element buildDistanceNode(ElementCreator ec, Annotation anno) { 139 protected static Element buildDistanceNode(ElementCreator ec, Annotation anno) {
110 Position pos = anno.getPosition(); 140 Position pos = anno.getPosition();
111 Range range = anno.getRange(); 141 Range range = anno.getRange();
112 Attribute attr = anno.getAttribute(); 142 Attribute attr = anno.getAttribute();
113 143
114 BigDecimal a = range.getA(); 144 BigDecimal a = range.getA();

http://dive4elements.wald.intevation.org