Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 41:5e4bc24ea438
Made serilization more flexible. DB update required!!!
Fixed problem with touching artifacts in database.
artifacts/trunk@119 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 23 Sep 2009 16:55:12 +0000 |
parents | 93edc04f3a10 |
children | f2648672c9c4 |
comparison
equal
deleted
inserted
replaced
40:af22d4de275c | 41:5e4bc24ea438 |
---|---|
18 | 18 |
19 import java.util.zip.GZIPOutputStream; | 19 import java.util.zip.GZIPOutputStream; |
20 import java.util.zip.GZIPInputStream; | 20 import java.util.zip.GZIPInputStream; |
21 | 21 |
22 import de.intevation.artifacts.Artifact; | 22 import de.intevation.artifacts.Artifact; |
23 import de.intevation.artifacts.ArtifactFactory; | |
24 import de.intevation.artifacts.ArtifactSerializer; | |
23 | 25 |
24 import org.apache.log4j.Logger; | 26 import org.apache.log4j.Logger; |
25 | 27 |
26 /** | 28 /** |
27 * @author Sascha L. Teichmann | 29 * @author Sascha L. Teichmann |
28 */ | 30 */ |
29 public class Backend | 31 public class Backend |
32 implements DatabaseCleaner.ArtifactReviver | |
30 { | 33 { |
31 private static Logger logger = Logger.getLogger(Backend.class); | 34 private static Logger logger = Logger.getLogger(Backend.class); |
32 | 35 |
33 public static final String SQL_NEXT_ID = | 36 public static final String SQL_NEXT_ID = |
34 SQL.get("artifacts.id.nextval"); | 37 SQL.get("artifacts.id.nextval"); |
44 | 47 |
45 public static final String SQL_LOAD_BY_GID = | 48 public static final String SQL_LOAD_BY_GID = |
46 SQL.get("artifacts.select.gid"); | 49 SQL.get("artifacts.select.gid"); |
47 | 50 |
48 protected DatabaseCleaner cleaner; | 51 protected DatabaseCleaner cleaner; |
52 | |
53 protected FactoryLookup factoryLookup; | |
54 | |
55 public interface FactoryLookup { | |
56 | |
57 ArtifactFactory getArtifactFactory(String factoryName); | |
58 | |
59 } // interface FactoryLookup | |
49 | 60 |
50 public final class PersistentArtifact | 61 public final class PersistentArtifact |
51 extends Id | 62 extends Id |
52 { | 63 { |
53 private Artifact artifact; | 64 private Artifact artifact; |
54 | 65 private ArtifactSerializer serializer; |
55 public PersistentArtifact(Artifact artifact, int id) { | 66 |
67 public PersistentArtifact( | |
68 Artifact artifact, | |
69 ArtifactSerializer serializer, | |
70 int id | |
71 ) { | |
56 super(id); | 72 super(id); |
57 this.artifact = artifact; | 73 this.artifact = artifact; |
74 this.serializer = serializer; | |
58 } | 75 } |
59 | 76 |
60 public Artifact getArtifact() { | 77 public Artifact getArtifact() { |
61 return artifact; | 78 return artifact; |
79 } | |
80 | |
81 public ArtifactSerializer getSerializer() { | |
82 return serializer; | |
62 } | 83 } |
63 | 84 |
64 public void store() { | 85 public void store() { |
65 if (logger.isDebugEnabled()) { | 86 if (logger.isDebugEnabled()) { |
66 logger.debug("storing artifact id = " + getId()); | 87 logger.debug("storing artifact id = " + getId()); |
79 public Backend() { | 100 public Backend() { |
80 } | 101 } |
81 | 102 |
82 public Backend(DatabaseCleaner cleaner) { | 103 public Backend(DatabaseCleaner cleaner) { |
83 this.cleaner = cleaner; | 104 this.cleaner = cleaner; |
105 } | |
106 | |
107 public void setFactoryLookup(FactoryLookup factoryLookup) { | |
108 this.factoryLookup = factoryLookup; | |
84 } | 109 } |
85 | 110 |
86 public void setCleaner(DatabaseCleaner cleaner) { | 111 public void setCleaner(DatabaseCleaner cleaner) { |
87 this.cleaner = cleaner; | 112 this.cleaner = cleaner; |
88 } | 113 } |
92 // TODO: check database for collisions. | 117 // TODO: check database for collisions. |
93 return uuid.toString(); | 118 return uuid.toString(); |
94 } | 119 } |
95 | 120 |
96 public PersistentArtifact storeInitially( | 121 public PersistentArtifact storeInitially( |
97 Artifact artifact, | 122 Artifact artifact, |
98 Long ttl | 123 ArtifactFactory factory, |
124 Long ttl | |
99 ) | 125 ) |
100 throws Exception | 126 throws Exception |
101 { | 127 { |
102 return new PersistentArtifact( | 128 return new PersistentArtifact( |
103 artifact, | 129 artifact, |
104 insertDatabase(artifact, ttl)); | 130 factory.getSerializer(), |
131 insertDatabase(artifact, factory, ttl)); | |
105 } | 132 } |
106 | 133 |
107 public PersistentArtifact getArtifact(String identifer) { | 134 public PersistentArtifact getArtifact(String identifer) { |
108 | 135 |
109 try { | 136 try { |
139 artifactOutdated(id); | 166 artifactOutdated(id); |
140 return null; | 167 return null; |
141 } | 168 } |
142 } | 169 } |
143 | 170 |
144 byte [] bytes = load_result.getBytes(4); | 171 String factoryName = load_result.getString(4); |
145 | 172 |
146 Artifact artifact = restoreArtifact(bytes); | 173 if (factoryLookup == null) { |
174 logger.error("factory lookup == null"); | |
175 return null; | |
176 } | |
177 | |
178 ArtifactFactory factory = factoryLookup | |
179 .getArtifactFactory(factoryName); | |
180 | |
181 if (factory == null) { | |
182 logger.error("factory '" + factoryName + "' not found"); | |
183 return null; | |
184 } | |
185 | |
186 ArtifactSerializer serializer = | |
187 factory.getSerializer(); | |
188 | |
189 byte [] bytes = load_result.getBytes(5); | |
190 | |
191 Artifact artifact = serializer.fromBytes(bytes); | |
147 | 192 |
148 return artifact == null | 193 return artifact == null |
149 ? null | 194 ? null |
150 : new PersistentArtifact(artifact, id); | 195 : new PersistentArtifact(artifact, serializer, id); |
151 } | 196 } |
152 catch (SQLException sqle) { | 197 catch (SQLException sqle) { |
153 logger.error(sqle.getLocalizedMessage(), sqle); | 198 logger.error(sqle.getLocalizedMessage(), sqle); |
154 } | 199 } |
155 finally { | 200 finally { |
164 if (connection != null) { | 209 if (connection != null) { |
165 try { connection.close(); } | 210 try { connection.close(); } |
166 catch (SQLException sqle) {} | 211 catch (SQLException sqle) {} |
167 } | 212 } |
168 } | 213 } |
169 return null; | |
170 } | |
171 | |
172 public static byte [] toBytes(Artifact artifact) { | |
173 try { | |
174 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
175 GZIPOutputStream gos = new GZIPOutputStream(bos); | |
176 ObjectOutputStream oos = new ObjectOutputStream(gos); | |
177 | |
178 oos.writeObject(artifact); | |
179 oos.flush(); | |
180 oos.close(); | |
181 | |
182 return bos.toByteArray(); | |
183 } | |
184 catch (IOException ioe) { | |
185 logger.error(ioe.getLocalizedMessage(), ioe); | |
186 throw new RuntimeException(ioe); | |
187 } | |
188 } | |
189 | |
190 public static Artifact restoreArtifact(byte [] bytes) { | |
191 | |
192 if (bytes == null) { | |
193 return null; | |
194 } | |
195 | |
196 ObjectInputStream ois = null; | |
197 | |
198 try { | |
199 ByteArrayInputStream bis = new ByteArrayInputStream(bytes); | |
200 GZIPInputStream gis = new GZIPInputStream(bis); | |
201 ois = new ObjectInputStream(gis); | |
202 | |
203 return (Artifact)ois.readObject(); | |
204 } | |
205 catch (IOException ioe) { | |
206 logger.error(ioe.getLocalizedMessage(), ioe); | |
207 } | |
208 catch (ClassNotFoundException cnfe) { | |
209 logger.error(cnfe.getLocalizedMessage(), cnfe); | |
210 } | |
211 catch (ClassCastException cce) { | |
212 logger.error(cce.getLocalizedMessage(), cce); | |
213 } | |
214 finally { | |
215 if (ois != null) { | |
216 try { ois.close(); } | |
217 catch (IOException ioe) { } | |
218 } | |
219 } | |
220 | |
221 return null; | 214 return null; |
222 } | 215 } |
223 | 216 |
224 protected void artifactOutdated(int id) { | 217 protected void artifactOutdated(int id) { |
225 logger.info("artifactOutdated: id = " + id); | 218 logger.info("artifactOutdated: id = " + id); |
226 if (cleaner != null) { | 219 if (cleaner != null) { |
227 cleaner.wakeup(); | 220 cleaner.wakeup(); |
228 } | 221 } |
229 } | 222 } |
230 | 223 |
231 protected int insertDatabase(Artifact artifact, Long ttl) { | 224 public Artifact reviveArtifact(String factoryName, byte [] bytes) { |
232 | 225 if (factoryLookup == null) { |
226 logger.error("reviveArtifact: factory lookup == null"); | |
227 return null; | |
228 } | |
229 ArtifactFactory factory = factoryLookup | |
230 .getArtifactFactory(factoryName); | |
231 | |
232 if (factory == null) { | |
233 logger.error("reviveArtifact: no factory '" + factoryName + "' found"); | |
234 return null; | |
235 } | |
236 | |
237 ArtifactSerializer serializer = factory.getSerializer(); | |
238 | |
239 return serializer.fromBytes(bytes); | |
240 } | |
241 | |
242 protected int insertDatabase( | |
243 Artifact artifact, | |
244 ArtifactFactory factory, | |
245 Long ttl | |
246 ) { | |
233 String uuid = artifact.identifier(); | 247 String uuid = artifact.identifier(); |
234 | 248 |
235 Connection connection = null; | 249 Connection connection = null; |
236 PreparedStatement stmnt_next_id = null; | 250 PreparedStatement stmnt_next_id = null; |
237 PreparedStatement stmnt_insert = null; | 251 PreparedStatement stmnt_insert = null; |
261 } | 275 } |
262 else { | 276 else { |
263 stmnt_insert.setLong(3, ttl.longValue()); | 277 stmnt_insert.setLong(3, ttl.longValue()); |
264 } | 278 } |
265 | 279 |
266 stmnt_insert.setBytes(4, toBytes(artifact)); | 280 stmnt_insert.setString(4, factory.getName()); |
281 | |
282 stmnt_insert.setBytes( | |
283 5, | |
284 factory.getSerializer().toBytes(artifact)); | |
267 | 285 |
268 stmnt_insert.execute(); | 286 stmnt_insert.execute(); |
269 | 287 |
270 connection.commit(); | 288 connection.commit(); |
271 | 289 |
308 DataSource dataSource = DBConnection.getDataSource(); | 326 DataSource dataSource = DBConnection.getDataSource(); |
309 try { | 327 try { |
310 connection = dataSource.getConnection(); | 328 connection = dataSource.getConnection(); |
311 try { | 329 try { |
312 connection.setAutoCommit(false); | 330 connection.setAutoCommit(false); |
313 stmnt_touch = connection.prepareStatement(SQL_UPDATE); | 331 stmnt_touch = connection.prepareStatement(SQL_TOUCH); |
314 stmnt_touch.setInt(1, artifact.getId()); | 332 stmnt_touch.setInt(1, artifact.getId()); |
315 stmnt_touch.execute(); | 333 stmnt_touch.execute(); |
316 connection.commit(); | 334 connection.commit(); |
317 } | 335 } |
318 catch (SQLException sqle) { | 336 catch (SQLException sqle) { |
349 try { | 367 try { |
350 connection.setAutoCommit(false); | 368 connection.setAutoCommit(false); |
351 stmnt_update = connection.prepareStatement(SQL_UPDATE); | 369 stmnt_update = connection.prepareStatement(SQL_UPDATE); |
352 stmnt_update.setInt(2, artifact.getId()); | 370 stmnt_update.setInt(2, artifact.getId()); |
353 | 371 |
354 byte [] bytes = toBytes(artifact.getArtifact()); | 372 byte [] bytes = artifact |
373 .getSerializer() | |
374 .toBytes(artifact.getArtifact()); | |
355 | 375 |
356 stmnt_update.setBytes(1, bytes); | 376 stmnt_update.setBytes(1, bytes); |
357 stmnt_update.execute(); | 377 stmnt_update.execute(); |
358 connection.commit(); | 378 connection.commit(); |
359 } | 379 } |