comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 15:9ad6ec2d09c3

Implemented restoring artifacts from database. artifacts/trunk@30 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 07 Sep 2009 10:06:23 +0000
parents 0d16d1bb2df0
children 5a6b6a3debc7
comparison
equal deleted inserted replaced
14:0d16d1bb2df0 15:9ad6ec2d09c3
7 import java.sql.Connection; 7 import java.sql.Connection;
8 import java.sql.SQLException; 8 import java.sql.SQLException;
9 import java.sql.PreparedStatement; 9 import java.sql.PreparedStatement;
10 import java.sql.Types; 10 import java.sql.Types;
11 import java.sql.ResultSet; 11 import java.sql.ResultSet;
12 import java.sql.Timestamp;
12 13
13 import javax.sql.DataSource; 14 import javax.sql.DataSource;
14 15
15 import java.io.IOException; 16 import java.io.IOException;
17 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream; 18 import java.io.ByteArrayOutputStream;
17 import java.io.ObjectOutputStream; 19 import java.io.ObjectOutputStream;
20 import java.io.ObjectInputStream;
18 21
19 import java.util.zip.GZIPOutputStream; 22 import java.util.zip.GZIPOutputStream;
23 import java.util.zip.GZIPInputStream;
20 24
21 import de.intevation.artifacts.ArtifactFactory; 25 import de.intevation.artifacts.ArtifactFactory;
22 import de.intevation.artifacts.Artifact; 26 import de.intevation.artifacts.Artifact;
23 27
24 /** 28 /**
35 public static final String SQL_UPDATE = 39 public static final String SQL_UPDATE =
36 SQL.get("artifacts.update"); 40 SQL.get("artifacts.update");
37 41
38 public static final String SQL_TOUCH = 42 public static final String SQL_TOUCH =
39 SQL.get("artifacts.touch"); 43 SQL.get("artifacts.touch");
44
45 public static final String SQL_LOAD_BY_GID =
46 SQL.get("artifacts.select.gid");
40 47
41 /** 48 /**
42 * Used to wrap the calls to invole database actions. 49 * Used to wrap the calls to invole database actions.
43 */ 50 */
44 public class ArtifactProxy 51 public class ArtifactProxy
140 147
141 public Backend() { 148 public Backend() {
142 } 149 }
143 150
144 public Artifact getArtifact(String idenitfier) { 151 public Artifact getArtifact(String idenitfier) {
145 return null; 152 UUID uuid;
153
154 try {
155 uuid = UUID.fromString(idenitfier);
156 }
157 catch (IllegalArgumentException iae) {
158 return null;
159 }
160
161 return getArtifactByUUID(uuid);
146 } 162 }
147 163
148 public Artifact createArtifactWithFactory( 164 public Artifact createArtifactWithFactory(
149 ArtifactFactory factory, Object context 165 ArtifactFactory factory, Object context
150 ) { 166 ) {
156 artifact, context); 172 artifact, context);
157 173
158 int id = insertDatabase(uuid, ttl); 174 int id = insertDatabase(uuid, ttl);
159 175
160 return new ArtifactProxy(artifact, id, true); 176 return new ArtifactProxy(artifact, id, true);
177 }
178
179 protected Artifact getArtifactByUUID(UUID uuid) {
180
181 Connection connection = null;
182 PreparedStatement stmnt_load = null;
183 ResultSet load_result = null;
184
185 DataSource dataSource = DBConnection.getDataSource();
186 try {
187 connection = dataSource.getConnection();
188 stmnt_load = connection.prepareStatement(SQL_LOAD_BY_GID);
189 stmnt_load.setString(1, uuid.toString());
190
191 load_result = stmnt_load.executeQuery();
192
193 if (!load_result.next()) {
194 return null;
195 }
196
197 int id = load_result.getInt(1);
198 long ttl = load_result.getLong(3);
199
200 if (!load_result.wasNull()) { // real time to life
201 long last_access = load_result.getTimestamp(2).getTime();
202 if (last_access + ttl > System.currentTimeMillis()) {
203 artifactOutdated(id);
204 return null;
205 }
206 }
207
208 byte [] bytes = load_result.getBytes(4);
209
210 if (bytes == null) {
211 return null;
212 }
213
214 Artifact original = restoreArtifact(bytes);
215 if (original == null) {
216 return null;
217 }
218
219 return new ArtifactProxy(original, id, false);
220 }
221 catch (SQLException sqle) {
222 sqle.printStackTrace(System.err);
223 }
224 finally {
225 if (load_result != null) {
226 try { load_result.close(); }
227 catch (SQLException sqle) {}
228 }
229 if (stmnt_load != null) {
230 try { load_result.close(); }
231 catch (SQLException sqle) {}
232 }
233 if (connection != null) {
234 try { connection.close(); }
235 catch (SQLException sqle) {}
236 }
237 }
238 return null;
239 }
240
241 public static Artifact restoreArtifact(byte [] bytes) {
242
243 ObjectInputStream ois = null;
244
245 try {
246 ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
247 GZIPInputStream gis = new GZIPInputStream(bis);
248 ois = new ObjectInputStream(gis);
249
250 return (Artifact)ois.readObject();
251 }
252 catch (IOException ioe) {
253 ioe.printStackTrace(System.err);
254 }
255 catch (ClassNotFoundException cnfe) {
256 cnfe.printStackTrace(System.err);
257 }
258 catch (ClassCastException cce) {
259 cce.printStackTrace(System.err);
260 }
261 finally {
262 if (ois != null) {
263 try { ois.close(); }
264 catch (IOException ioe) { }
265 }
266 }
267
268 return null;
269 }
270
271 protected void artifactOutdated(int id) {
272 System.err.println("artifactOutdated: id = " + id);
161 } 273 }
162 274
163 protected int insertDatabase(UUID uuid, Long ttl) { 275 protected int insertDatabase(UUID uuid, Long ttl) {
164 Connection connection = null; 276 Connection connection = null;
165 PreparedStatement stmnt_next_id = null; 277 PreparedStatement stmnt_next_id = null;

http://dive4elements.wald.intevation.org