diff 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
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Sep 23 08:27:35 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Sep 23 16:55:12 2009 +0000
@@ -20,6 +20,8 @@
 import java.util.zip.GZIPInputStream;
 
 import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactFactory;
+import de.intevation.artifacts.ArtifactSerializer;
 
 import org.apache.log4j.Logger;
 
@@ -27,6 +29,7 @@
  *  @author Sascha L. Teichmann
  */
 public class Backend
+implements   DatabaseCleaner.ArtifactReviver
 {
     private static Logger logger = Logger.getLogger(Backend.class);
 
@@ -47,20 +50,38 @@
 
     protected DatabaseCleaner cleaner;
 
+    protected FactoryLookup   factoryLookup;
+
+    public interface FactoryLookup {
+
+        ArtifactFactory getArtifactFactory(String factoryName);
+
+    } // interface FactoryLookup
+
     public final class PersistentArtifact
     extends            Id
     {
-        private Artifact artifact;
+        private Artifact           artifact;
+        private ArtifactSerializer serializer;
 
-        public PersistentArtifact(Artifact artifact, int id) {
+        public PersistentArtifact(
+            Artifact           artifact, 
+            ArtifactSerializer serializer,
+            int                id
+        ) {
             super(id);
-            this.artifact = artifact;
+            this.artifact   = artifact;
+            this.serializer = serializer;
         }
 
         public Artifact getArtifact() {
             return artifact;
         }
 
+        public ArtifactSerializer getSerializer() {
+            return serializer;
+        }
+
         public void store() {
             if (logger.isDebugEnabled()) {
                 logger.debug("storing artifact id = " + getId());
@@ -83,6 +104,10 @@
         this.cleaner = cleaner;
     }
 
+    public void setFactoryLookup(FactoryLookup factoryLookup) {
+        this.factoryLookup = factoryLookup;
+    }
+
     public void setCleaner(DatabaseCleaner cleaner) {
         this.cleaner = cleaner;
     }
@@ -94,14 +119,16 @@
     }
 
     public PersistentArtifact storeInitially(
-        Artifact artifact,
-        Long     ttl
+        Artifact        artifact,
+        ArtifactFactory factory,
+        Long            ttl
     ) 
     throws Exception
     {
         return new PersistentArtifact(
             artifact,
-            insertDatabase(artifact, ttl));
+            factory.getSerializer(),
+            insertDatabase(artifact, factory, ttl));
     }
     
     public PersistentArtifact getArtifact(String identifer) {
@@ -141,13 +168,31 @@
                 }
             }
 
-            byte [] bytes = load_result.getBytes(4);
+            String factoryName = load_result.getString(4);
 
-            Artifact artifact = restoreArtifact(bytes);
+            if (factoryLookup == null) {
+                logger.error("factory lookup == null");
+                return null;
+            }
+
+            ArtifactFactory factory = factoryLookup
+                .getArtifactFactory(factoryName);
+
+            if (factory == null) {
+                logger.error("factory '" + factoryName + "' not found");
+                return null;
+            }
+
+            ArtifactSerializer serializer =
+                factory.getSerializer();
+
+            byte [] bytes = load_result.getBytes(5);
+
+            Artifact artifact = serializer.fromBytes(bytes);
 
             return artifact == null
                 ? null
-                : new PersistentArtifact(artifact, id);
+                : new PersistentArtifact(artifact, serializer, id);
         }
         catch (SQLException sqle) {
             logger.error(sqle.getLocalizedMessage(), sqle);
@@ -169,58 +214,6 @@
         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) {
-            return null;
-        }
-
-        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) {
-            logger.error(ioe.getLocalizedMessage(), ioe);
-        }
-        catch (ClassNotFoundException cnfe) {
-            logger.error(cnfe.getLocalizedMessage(), cnfe);
-        }
-        catch (ClassCastException cce) {
-            logger.error(cce.getLocalizedMessage(), cce);
-        }
-        finally {
-            if (ois != null) {
-                try { ois.close(); }
-                catch (IOException ioe) { }
-            }
-        }
-
-        return null;
-    }
-
     protected void artifactOutdated(int id) {
         logger.info("artifactOutdated: id = " + id);
         if (cleaner != null) {
@@ -228,8 +221,29 @@
         }
     }
 
-    protected int insertDatabase(Artifact artifact, Long ttl) {
+    public Artifact reviveArtifact(String factoryName, byte [] bytes) {
+        if (factoryLookup == null) {
+            logger.error("reviveArtifact: factory lookup == null");
+            return null;
+        }
+        ArtifactFactory factory = factoryLookup
+            .getArtifactFactory(factoryName);
 
+        if (factory == null) {
+            logger.error("reviveArtifact: no factory '" + factoryName + "' found");
+            return null;
+        }
+
+        ArtifactSerializer serializer = factory.getSerializer();
+
+        return serializer.fromBytes(bytes);
+    }
+
+    protected int insertDatabase(
+        Artifact        artifact, 
+        ArtifactFactory factory,
+        Long            ttl
+    ) {
         String uuid = artifact.identifier();
 
         Connection        connection    = null;
@@ -263,7 +277,11 @@
                     stmnt_insert.setLong(3, ttl.longValue());
                 }
 
-                stmnt_insert.setBytes(4, toBytes(artifact));
+                stmnt_insert.setString(4, factory.getName());
+
+                stmnt_insert.setBytes(
+                    5,
+                    factory.getSerializer().toBytes(artifact));
 
                 stmnt_insert.execute();
 
@@ -310,7 +328,7 @@
                 connection = dataSource.getConnection();
                 try {
                     connection.setAutoCommit(false);
-                    stmnt_touch = connection.prepareStatement(SQL_UPDATE);
+                    stmnt_touch = connection.prepareStatement(SQL_TOUCH);
                     stmnt_touch.setInt(1, artifact.getId());
                     stmnt_touch.execute();
                     connection.commit();
@@ -351,7 +369,9 @@
                     stmnt_update = connection.prepareStatement(SQL_UPDATE);
                     stmnt_update.setInt(2, artifact.getId());
 
-                    byte [] bytes = toBytes(artifact.getArtifact());
+                    byte [] bytes = artifact
+                        .getSerializer()
+                        .toBytes(artifact.getArtifact());
 
                     stmnt_update.setBytes(1, bytes);
                     stmnt_update.execute();

http://dive4elements.wald.intevation.org