Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java @ 481:20dde2b6f1b5
Added end of life support for artifact states. Implemented ZIP download
for "Horizontalschnitte". Laid some tracks for WMS (un-)publishing.
gnv-artifacts/trunk@554 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 17 Jan 2010 16:34:11 +0000 |
parents | 211cad2fb5ba |
children | 64e65daa65e9 |
comparison
equal
deleted
inserted
replaced
480:211cad2fb5ba | 481:20dde2b6f1b5 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.state.profile.horizontalcrosssection; | 1 package de.intevation.gnv.state.profile.horizontalcrosssection; |
5 | 2 |
6 import com.vividsolutions.jts.geom.Coordinate; | 3 import com.vividsolutions.jts.geom.Coordinate; |
7 import com.vividsolutions.jts.geom.Envelope; | 4 import com.vividsolutions.jts.geom.Envelope; |
8 import com.vividsolutions.jts.geom.Polygon; | 5 import com.vividsolutions.jts.geom.Polygon; |
34 | 31 |
35 import de.intevation.gnv.state.exception.StateException; | 32 import de.intevation.gnv.state.exception.StateException; |
36 | 33 |
37 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState; | 34 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState; |
38 | 35 |
36 import de.intevation.gnv.utils.FileUtils; | |
39 import de.intevation.gnv.utils.StringUtils; | 37 import de.intevation.gnv.utils.StringUtils; |
40 import de.intevation.gnv.utils.WKTUtils; | 38 import de.intevation.gnv.utils.WKTUtils; |
41 | 39 |
42 import java.io.File; | 40 import java.io.File; |
43 import java.io.IOException; | 41 import java.io.IOException; |
46 import java.util.Collection; | 44 import java.util.Collection; |
47 | 45 |
48 import org.apache.log4j.Logger; | 46 import org.apache.log4j.Logger; |
49 | 47 |
50 import org.w3c.dom.Document; | 48 import org.w3c.dom.Document; |
49 import org.w3c.dom.Element; | |
51 import org.w3c.dom.Node; | 50 import org.w3c.dom.Node; |
52 | 51 |
53 /** | 52 /** |
54 * @author Tim Englich (tim.englich@intevation.de) | 53 * @author Tim Englich (tim.englich@intevation.de) |
55 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) | 54 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) |
63 /** | 62 /** |
64 * The UID of this Class | 63 * The UID of this Class |
65 */ | 64 */ |
66 private static final long serialVersionUID = 3233620652465061860L; | 65 private static final long serialVersionUID = 3233620652465061860L; |
67 | 66 |
68 private String ijkQueryID = null; | 67 private String ijkQueryID; |
68 | |
69 private Boolean shapeFileLock = new Boolean(true); | |
70 | |
71 private String shapeFilePath; | |
69 | 72 |
70 /** | 73 /** |
71 * Constructor | 74 * Constructor |
72 */ | 75 */ |
73 public HorizontalCrossSectionMeshOutputState() { | 76 public HorizontalCrossSectionMeshOutputState() { |
77 } | |
78 | |
79 public String getShapeFilePath() { | |
80 synchronized (shapeFileLock) { | |
81 return shapeFilePath; | |
82 } | |
83 } | |
84 | |
85 public void setShapeFilePath(String shapeFilePath) { | |
86 synchronized (shapeFileLock) { | |
87 this.shapeFilePath = shapeFilePath; | |
88 } | |
89 } | |
90 | |
91 public String resetShapeFilePath() { | |
92 synchronized (shapeFileLock) { | |
93 String path = shapeFilePath; | |
94 shapeFilePath = null; | |
95 return path; | |
96 } | |
97 } | |
98 | |
99 public void endOfLife(Object globalContext) { | |
100 super.endOfLife(globalContext); | |
101 | |
102 // do it in background | |
103 new Thread() { | |
104 public void run() { | |
105 // TODO: Do the un-publishing WMS stuff. | |
106 String path = resetShapeFilePath(); | |
107 | |
108 if (path == null) { | |
109 return; | |
110 } | |
111 | |
112 File dir = new File(path); | |
113 | |
114 for (int i = 0; i < 10; ++i) { | |
115 if (!dir.exists() || FileUtils.deleteRecursive(dir)) { | |
116 return; | |
117 } | |
118 try { | |
119 Thread.sleep(10000L); | |
120 } | |
121 catch (InterruptedException ie) { | |
122 } | |
123 } | |
124 | |
125 log.error("failed to remove directory '" + path + "'"); | |
126 } // run | |
127 }.start(); | |
74 } | 128 } |
75 | 129 |
76 public void out( | 130 public void out( |
77 Document format, | 131 Document format, |
78 Collection<InputData> inputData, | 132 Collection<InputData> inputData, |
95 outputMode = outputMode.toLowerCase(); | 149 outputMode = outputMode.toLowerCase(); |
96 | 150 |
97 log.debug("---- asking for: " + outputMode); | 151 log.debug("---- asking for: " + outputMode); |
98 | 152 |
99 if ("zip".equals(outputMode)) { | 153 if ("zip".equals(outputMode)) { |
154 writeZip(uuid, callContext, outputStream); | |
100 } | 155 } |
101 else if ("wms".equals(outputMode)) { | 156 else if ("wms".equals(outputMode)) { |
157 XMLUtils.toStream( | |
158 getWMS(uuid, callContext), | |
159 outputStream); | |
102 } | 160 } |
103 else if ("statistics".equals(outputMode)) { | 161 else if ("statistics".equals(outputMode)) { |
104 // TODO: REMOVE THIS! | 162 // TODO: REMOVE THIS! |
105 try { | 163 try { outputStream.write("<fake/>\n".getBytes()); } |
106 outputStream.write("<fake/>\n".getBytes()); | 164 catch (IOException ioe) {} |
107 } | |
108 catch (IOException ioe) { | |
109 } | |
110 } | 165 } |
111 else { | 166 else { |
112 throw new StateException("unsupported output mode"); | 167 throw new StateException("unsupported output mode"); |
113 } | 168 } |
114 } | 169 } |
115 | 170 |
116 protected Object getResult(String uuid, CallContext callContext) | 171 protected void writeZip( |
172 String uuid, | |
173 CallContext callContext, | |
174 OutputStream output | |
175 ) | |
176 throws StateException | |
177 { | |
178 try { | |
179 String p = getShapeFilePath(); | |
180 if (p != null) { | |
181 File dir = new File(p); | |
182 if (dir.isDirectory()) { | |
183 FileUtils.createZipArchive(dir, output); | |
184 } | |
185 } | |
186 AttributedPoint2ds result = getResult(uuid, callContext); | |
187 if (result != null | |
188 && (p = writeToShapeFile(uuid, result, callContext)) != null) { | |
189 FileUtils.createZipArchive(new File(p), output); | |
190 } | |
191 } | |
192 catch (IOException ioe) { | |
193 log.error(ioe.getLocalizedMessage(), ioe); | |
194 } | |
195 } | |
196 | |
197 protected Document getWMS(String uuid, CallContext callContext) | |
198 throws StateException | |
199 { | |
200 // TODO: Do the real WMS publishing here! | |
201 Document document = XMLUtils.newDocument(); | |
202 | |
203 Element pathElement = document.createElement("path"); | |
204 document.appendChild(pathElement); | |
205 | |
206 String path = getShapeFilePath(); | |
207 | |
208 if (path != null && new File(path).isDirectory()) { | |
209 pathElement.setTextContent(path); | |
210 } | |
211 else { | |
212 AttributedPoint2ds result = getResult(uuid, callContext); | |
213 if (result != null | |
214 && (path = writeToShapeFile(uuid, result, callContext)) != null) { | |
215 pathElement.setTextContent(path); | |
216 } | |
217 } | |
218 | |
219 return document; | |
220 } | |
221 | |
222 protected String writeToShapeFile( | |
223 String uuid, | |
224 AttributedPoint2ds result, | |
225 CallContext callContext | |
226 ) { | |
227 File baseDir = shapefileDirectory(callContext); | |
228 | |
229 File shapeDir = new File(baseDir, uuid); | |
230 | |
231 int count = 0; | |
232 | |
233 synchronized (shapeFileLock) { | |
234 while (shapeDir.exists()) { | |
235 shapeDir = new File(baseDir, uuid + "-" + count); | |
236 ++count; | |
237 } | |
238 | |
239 if (!shapeDir.mkdirs()) { | |
240 log.error("cannot create directory '" | |
241 + shapeDir.getAbsolutePath() + "'"); | |
242 return null; | |
243 } | |
244 shapeFilePath = shapeDir.getAbsolutePath(); | |
245 } | |
246 | |
247 // TODO: Do the writing | |
248 | |
249 return shapeFilePath; | |
250 } | |
251 | |
252 protected AttributedPoint2ds getResult(String uuid, CallContext callContext) | |
117 throws StateException | 253 throws StateException |
118 { | 254 { |
119 CacheFactory cf = CacheFactory.getInstance(); | 255 CacheFactory cf = CacheFactory.getInstance(); |
120 String key = uuid + super.getID(); | 256 String key = uuid + super.getID(); |
121 | 257 |
122 if (cf.isInitialized()) { | 258 if (cf.isInitialized()) { |
123 net.sf.ehcache.Element value = cf.getCache().get(key); | 259 net.sf.ehcache.Element value = cf.getCache().get(key); |
124 if (value != null) { | 260 if (value != null) { |
125 return value.getObjectValue(); | 261 return (AttributedPoint2ds)value.getObjectValue(); |
126 } | 262 } |
127 } | 263 } |
128 | 264 |
129 Object result = produceResult(callContext); | 265 AttributedPoint2ds result = produceResult(callContext); |
130 | 266 |
131 if (result != null && cf.isInitialized()) { | 267 if (result != null && cf.isInitialized()) { |
132 cf.getCache().put(new net.sf.ehcache.Element(key, result)); | 268 cf.getCache().put(new net.sf.ehcache.Element(key, result)); |
133 } | 269 } |
134 | 270 |
135 return result; | 271 return result; |
136 } | 272 } |
137 | 273 |
138 protected Object produceResult(CallContext callContext) | 274 protected AttributedPoint2ds produceResult(CallContext callContext) |
139 throws StateException | 275 throws StateException |
140 { | 276 { |
141 InputData meshPolygon = inputData.get("mesh_polygon"); | 277 InputData meshPolygon = inputData.get("mesh_polygon"); |
142 InputData meshId = inputData.get("meshid"); | 278 InputData meshId = inputData.get("meshid"); |
143 | 279 |
216 ResultDescriptor rd = result.getResultDescriptor(); | 352 ResultDescriptor rd = result.getResultDescriptor(); |
217 log.debug(rd); | 353 log.debug(rd); |
218 } | 354 } |
219 } | 355 } |
220 | 356 |
357 // TODO: do the interpolation | |
358 | |
221 return ap2ds; | 359 return ap2ds; |
222 } | 360 } |
223 | 361 |
224 public Object process( | 362 public AttributedPoint2ds process( |
225 Envelope env, | 363 Envelope env, |
226 Polygon polygon, | 364 Polygon polygon, |
227 int numSamples, | 365 int numSamples, |
228 AttributedPoint2ds input | 366 AttributedPoint2ds input |
229 ) { | 367 ) { |