diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 32:c2d53bd30ab8

Re-factored artifact API for better integration of background processing. artifacts/trunk@78 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 13 Sep 2009 14:50:53 +0000
parents c4d85a8532d1
children 9935e1c928de
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Sat Sep 12 10:45:28 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Sun Sep 13 14:50:53 2009 +0000
@@ -1,7 +1,5 @@
 package de.intevation.artifactdatabase;
 
-import org.w3c.dom.Document;
-
 import java.util.UUID;
 
 import java.sql.Connection;
@@ -17,12 +15,10 @@
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectOutputStream;
 import java.io.ObjectInputStream;
-import java.io.OutputStream;
 
 import java.util.zip.GZIPOutputStream;
 import java.util.zip.GZIPInputStream;
 
-import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.Artifact;
 
 import org.apache.log4j.Logger;
@@ -51,113 +47,28 @@
 
     protected DatabaseCleaner cleaner;
 
-    /**
-     * Used to wrap the calls to invole database actions.
-     */
-    public class ArtifactProxy
-    implements   Artifact
+    public final class PersistentArtifact
+    extends            Id
     {
-        protected Artifact original;
-        protected int      id;
-        protected boolean  unwritten;
-
-        public ArtifactProxy() {
-        }
-
-        public ArtifactProxy(Artifact original, int id, boolean unwritten) {
-            this.original  = original;
-            this.id        = id;
-            this.unwritten = unwritten;
-        }
-
-        public Artifact getOriginal() {
-            return original;
-        }
+        private Artifact artifact;
 
-        public int getId() {
-            return id;
-        }
-
-        public boolean isUnwritten() {
-            return unwritten;
-        }
-
-        public String identifier() {
-            return original.identifier();
-        }
-
-        public String hash() {
-            return original.hash();
-        }
-
-        public Document describe(Object context) {
-            try {
-                return original.describe(context);
-            }
-            finally {
-                touch(this);
-            }
+        public PersistentArtifact(Artifact artifact, int id) {
+            super(id);
+            this.artifact = artifact;
         }
 
-        public Document advance(Document target, Object context) {
-            try {
-                return original.advance(target, context);
-            }
-            finally {
-                store(this);
-            }
-        }
-
-        public Document feed(Document data, Object context) {
-            try {
-                return original.feed(data, context);
-            }
-            finally {
-                store(this);
-            }
+        public Artifact getArtifact() {
+            return artifact;
         }
 
-        public void out(
-            Document     format, 
-            OutputStream output,
-            Object       context
-        ) 
-        throws IOException
-        {
-            try {
-                original.out(format, output, context);
-            }
-            finally {
-                touch(this);
-            }
-        }
-
-        public void setup(String identifier, ArtifactFactory factory, Object context) {
-            original.setup(identifier, factory, context);
+        public void store() {
+            Backend.this.store(this);
         }
 
-        public void endOfLife(Object context) {
-            original.endOfLife(context);
+        public void touch() {
+            Backend.this.touch(this);
         }
-
-        public byte [] toBytes() {
-            try {
-                ByteArrayOutputStream bos = new ByteArrayOutputStream();
-                GZIPOutputStream      gos = new GZIPOutputStream(bos);
-                ObjectOutputStream    oos = new ObjectOutputStream(gos);
-
-                oos.writeObject(original);
-                oos.flush();
-                oos.close();
-
-                return bos.toByteArray();
-            }
-            catch (IOException ioe) {
-                logger.error(ioe.getLocalizedMessage(), ioe);
-                throw new RuntimeException(ioe);
-            }
-        }
-    } // class ArtifactProxy
+    } // class ArtifactWithId
 
     public Backend() {
     }
@@ -166,42 +77,37 @@
         this.cleaner = cleaner;
     }
 
-    public Artifact getArtifact(String idenitfier) {
-        UUID uuid;
+    public void setCleaner(DatabaseCleaner cleaner) {
+        this.cleaner = cleaner;
+    }
+
+    public String newIdentifier() {
+        UUID uuid = UUID.randomUUID();
+        // TODO: check database for collisions.
+        return uuid.toString();
+    }
+
+    public PersistentArtifact storeInitially(
+        Artifact artifact,
+        Long     ttl
+    ) 
+    throws Exception
+    {
+        return new PersistentArtifact(
+            artifact,
+            insertDatabase(artifact, ttl));
+    }
+    
+    public PersistentArtifact getArtifact(String identifer) {
 
         try {
-            uuid = UUID.fromString(idenitfier);
+            UUID.fromString(identifer);
         }
         catch (IllegalArgumentException iae) {
             logger.warn(iae.getLocalizedMessage());
             return null;
         }
 
-        return getArtifactByUUID(uuid);
-    }
-
-    public Artifact createArtifactWithFactory(
-        ArtifactFactory factory, Object context
-    ) {
-        UUID uuid = UUID.randomUUID();
-        Artifact artifact = factory.createArtifact(
-            uuid.toString(), context);
-
-        Long ttl = factory.timeToLiveUntouched(
-            artifact, context);
-
-        try {
-            int id = insertDatabase(uuid, ttl);
-            return new ArtifactProxy(artifact, id, true);
-        }
-        catch (Exception e) {
-            logger.error(e.getLocalizedMessage(), e);
-        }
-        return null;
-    }
-
-    protected Artifact getArtifactByUUID(UUID uuid) {
-
         Connection        connection  = null;
         PreparedStatement stmnt_load  = null;
         ResultSet         load_result = null;
@@ -210,7 +116,7 @@
         try {
             connection = dataSource.getConnection();
             stmnt_load = connection.prepareStatement(SQL_LOAD_BY_GID);
-            stmnt_load.setString(1, uuid.toString());
+            stmnt_load.setString(1, identifer);
 
             load_result = stmnt_load.executeQuery();
 
@@ -231,16 +137,11 @@
 
             byte [] bytes = load_result.getBytes(4);
 
-            if (bytes == null) {
-                return null;
-            }
+            Artifact artifact = restoreArtifact(bytes);
 
-            Artifact original = restoreArtifact(bytes);
-            if (original == null) {
-                return null;
-            }
-
-            return new ArtifactProxy(original, id, false);
+            return artifact == null
+                ? null
+                : new PersistentArtifact(artifact, id);
         }
         catch (SQLException sqle) {
             logger.error(sqle.getLocalizedMessage(), sqle);
@@ -262,6 +163,24 @@
         return null;
     }
 
+    public static byte [] toBytes(Artifact artifact) {
+        try {
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            GZIPOutputStream      gos = new GZIPOutputStream(bos);
+            ObjectOutputStream    oos = new ObjectOutputStream(gos);
+
+            oos.writeObject(artifact);
+            oos.flush();
+            oos.close();
+
+            return bos.toByteArray();
+        }
+        catch (IOException ioe) {
+            logger.error(ioe.getLocalizedMessage(), ioe);
+            throw new RuntimeException(ioe);
+        }
+    }
+
     public static Artifact restoreArtifact(byte [] bytes) {
 
         if (bytes == null) {
@@ -303,7 +222,10 @@
         }
     }
 
-    protected int insertDatabase(UUID uuid, Long ttl) {
+    protected int insertDatabase(Artifact artifact, Long ttl) {
+
+        String uuid = artifact.identifier();
+
         Connection        connection    = null;
         PreparedStatement stmnt_next_id = null;
         PreparedStatement stmnt_insert  = null;
@@ -327,7 +249,7 @@
                 int id = res_id.getInt(1);
 
                 stmnt_insert.setInt(1, id);
-                stmnt_insert.setString(2, uuid.toString());
+                stmnt_insert.setString(2, uuid);
                 if (ttl == null) {
                     stmnt_insert.setNull(3, Types.BIGINT);
                 }
@@ -335,6 +257,8 @@
                     stmnt_insert.setLong(3, ttl.longValue());
                 }
 
+                stmnt_insert.setBytes(4, toBytes(artifact));
+
                 stmnt_insert.execute();
 
                 connection.commit();
@@ -370,12 +294,7 @@
         throw new RuntimeException("failed insert artifact into database");
     }
 
-    public void touch(ArtifactProxy proxy) {
-
-        if (proxy.isUnwritten()) {
-            store(proxy);
-            return;
-        }
+    public void touch(PersistentArtifact artifact) {
 
         try {
             Connection        connection  = null;
@@ -386,7 +305,7 @@
                 try {
                     connection.setAutoCommit(false);
                     stmnt_touch = connection.prepareStatement(SQL_UPDATE);
-                    stmnt_touch.setInt(1, proxy.getId());
+                    stmnt_touch.setInt(1, artifact.getId());
                     stmnt_touch.execute();
                     connection.commit();
                 }
@@ -413,7 +332,7 @@
         }
     }
 
-    public void store(ArtifactProxy proxy) {
+    public void store(PersistentArtifact artifact) {
 
         try {
             Connection        connection   = null;
@@ -424,9 +343,9 @@
                 try {
                     connection.setAutoCommit(false);
                     stmnt_update = connection.prepareStatement(SQL_UPDATE);
-                    stmnt_update.setInt(2, proxy.getId());
+                    stmnt_update.setInt(2, artifact.getId());
 
-                    byte [] bytes = proxy.toBytes();
+                    byte [] bytes = toBytes(artifact.getArtifact());
 
                     stmnt_update.setBytes(1, bytes);
                     stmnt_update.execute();
@@ -454,6 +373,5 @@
             logger.error(e.getLocalizedMessage(), e);
         }
     }
-
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org