changeset 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 635310c6a20e
files Changelog TODO artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java
diffstat 3 files changed, 124 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Changelog	Mon Sep 07 08:41:05 2009 +0000
+++ b/Changelog	Mon Sep 07 10:06:23 2009 +0000
@@ -1,3 +1,12 @@
+2009-09-07	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	Implemented loading of artifacts from database. If the last access of an 
+	artifact is too long ago for its time to life, null is returned.
+
+	* TODO: Added remark to implement a "killer" thread which periodically cleans the
+	database from outdated artifacts and calls the endOfLife() method on those.
+
 2009-09-07	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/SQL.java:
--- a/TODO	Mon Sep 07 08:41:05 2009 +0000
+++ b/TODO	Mon Sep 07 10:06:23 2009 +0000
@@ -1,3 +1,5 @@
 TODO:
     * integrate logging into artifact database.
 	* Document the XML of the configuration file.
+	* Implement a "killer" thread in artifact database which
+	  periodically kills all outdated artifacts.
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Mon Sep 07 08:41:05 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Mon Sep 07 10:06:23 2009 +0000
@@ -9,14 +9,18 @@
 import java.sql.PreparedStatement;
 import java.sql.Types;
 import java.sql.ResultSet;
+import java.sql.Timestamp;
 
 import javax.sql.DataSource;
 
 import java.io.IOException;
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectOutputStream;
+import java.io.ObjectInputStream;
 
 import java.util.zip.GZIPOutputStream;
+import java.util.zip.GZIPInputStream;
 
 import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.Artifact;
@@ -38,6 +42,9 @@
     public static final String SQL_TOUCH =
         SQL.get("artifacts.touch");
 
+    public static final String SQL_LOAD_BY_GID =
+        SQL.get("artifacts.select.gid");
+
     /**
      * Used to wrap the calls to invole database actions.
      */
@@ -142,7 +149,16 @@
     }
 
     public Artifact getArtifact(String idenitfier) {
-        return null;
+        UUID uuid;
+
+        try {
+            uuid = UUID.fromString(idenitfier);
+        }
+        catch (IllegalArgumentException iae) {
+            return null;
+        }
+
+        return getArtifactByUUID(uuid);
     }
 
     public Artifact createArtifactWithFactory(
@@ -160,6 +176,102 @@
         return new ArtifactProxy(artifact, id, true);
     }
 
+    protected Artifact getArtifactByUUID(UUID uuid) {
+
+        Connection        connection  = null;
+        PreparedStatement stmnt_load  = null;
+        ResultSet         load_result = null;
+
+        DataSource dataSource = DBConnection.getDataSource();
+        try {
+            connection = dataSource.getConnection();
+            stmnt_load = connection.prepareStatement(SQL_LOAD_BY_GID);
+            stmnt_load.setString(1, uuid.toString());
+
+            load_result = stmnt_load.executeQuery();
+
+            if (!load_result.next()) {
+                return null;
+            }
+
+            int  id  = load_result.getInt(1);
+            long ttl = load_result.getLong(3);
+
+            if (!load_result.wasNull()) { // real time to life
+                long last_access = load_result.getTimestamp(2).getTime();
+                if (last_access + ttl > System.currentTimeMillis()) {
+                    artifactOutdated(id);
+                    return null;
+                }
+            }
+
+            byte [] bytes = load_result.getBytes(4);
+
+            if (bytes == null) {
+                return null;
+            }
+
+            Artifact original = restoreArtifact(bytes);
+            if (original == null) {
+                return null;
+            }
+
+            return new ArtifactProxy(original, id, false);
+        }
+        catch (SQLException sqle) {
+            sqle.printStackTrace(System.err);
+        }
+        finally {
+            if (load_result != null) {
+                try { load_result.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (stmnt_load != null) {
+                try { load_result.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (connection != null) {
+                try { connection.close(); } 
+                catch (SQLException sqle) {}
+            }
+        }
+        return null;
+    }
+
+    public static Artifact restoreArtifact(byte [] bytes) {
+
+        ObjectInputStream ois = null;
+
+        try {
+            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
+            GZIPInputStream      gis = new GZIPInputStream(bis);
+                                 ois = new ObjectInputStream(gis);
+
+            return (Artifact)ois.readObject();
+        }
+        catch (IOException ioe) {
+            ioe.printStackTrace(System.err);
+        }
+        catch (ClassNotFoundException cnfe) {
+            cnfe.printStackTrace(System.err);
+        }
+        catch (ClassCastException cce) {
+            cce.printStackTrace(System.err);
+        }
+        finally {
+            if (ois != null) {
+                try { ois.close(); }
+                catch (IOException ioe) { }
+            }
+        }
+
+        return null;
+    }
+
+    protected void artifactOutdated(int id) {
+        System.err.println("artifactOutdated: id = " + id);
+    }
+
     protected int insertDatabase(UUID uuid, Long ttl) {
         Connection        connection    = null;
         PreparedStatement stmnt_next_id = null;

http://dive4elements.wald.intevation.org