Index: topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java diff -u topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java:1.25 topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java:1.26 --- topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java:1.25 Tue Aug 22 17:27:37 2006 +++ topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java Fri Aug 25 16:58:12 2006 @@ -23,15 +23,24 @@ * * @author poussin * - * @version $Revision: 1.25 $ + * @version $Revision: 1.26 $ * - * Last update: $Date: 2006/08/22 17:27:37 $ by : $Author: bpoussin $ + * Last update: $Date: 2006/08/25 16:58:12 $ by : $Author: bpoussin $ */ package org.codelutin.topia.framework; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; import java.io.Reader; import java.io.Writer; import java.sql.Statement; @@ -46,6 +55,8 @@ import java.util.Properties; import java.util.Set; import java.util.WeakHashMap; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -78,6 +89,7 @@ import org.codelutin.topia.security.entities.hibernate.TopiaHibernateUserManager; import org.codelutin.util.ArrayUtil; import org.codelutin.util.CategorisedListenerSet; +import org.codelutin.util.GZUtil; import org.codelutin.util.ListenerSet; import org.dom4j.Document; import org.dom4j.DocumentException; @@ -91,8 +103,6 @@ import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.ReplicationMode; -import org.hibernate.SQLQuery; -import org.hibernate.ScrollableResults; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; @@ -1421,18 +1431,26 @@ } /** + * Backup database in gzip compressed file * Only work for h2 database * + * @param file file to write backup + * @param compress if true then use gzip to compress file + * * @see org.codelutin.topia.TopiaContext#backup(java.io.File) */ - public void backup(File file) throws TopiaException { + public void backup(File file, boolean compress) throws TopiaException { checkClosed("Ce contexte a ete ferme, impossible d'effectuer le backup"); try { // Statement stat = getHibernate().connection().createStatement(); // ResultSet rs = stat.executeQuery("SCRIPT TO '" + file.getAbsolutePath() + "'"); - Query query = getHibernate().createSQLQuery("SCRIPT TO '" + file.getAbsolutePath() + "'"); - query.list(); + // Bug dans h2 v0.9, on ne peut pas directement passer le fichier dans le SQL + // Il y a un ArrayOutBoundException -> org.h2.command.dml.Script.add:203 + // pour certaines lignes. C dommage, car on est obligé de rammener + // tout en texte, ce qui peut-etre gros pour la memoire :( + Query query = getHibernate().createSQLQuery("SCRIPT");// TO '" + file.getAbsolutePath() + "'"); + List lines = query.list(); // en fait on est un peu obligé d'exporter toute la base // (creation du schema compris) car sinon lors de la restauration @@ -1441,21 +1459,24 @@ // Si on ne voulait que les inserts, lors de la resauration il // faudrait desactiver les contraintes et les reactiver ensuite -// Iterator result = query.list().iterator(); + PrintStream out = new PrintStream( + new GZIPOutputStream( + new BufferedOutputStream ( + new FileOutputStream(file)))); + // Writer out = new BufferedWriter(new FileWriter(file)); -// while (result.hasNext()) { -// String insert = (String)result.next(); -// if (insert.toUpperCase().startsWith("INSERT")) { -// out.write(insert + ";\n"); -// } -// } -// out.close(); + for (String line : lines) { + out.println(line + ";"); + } + out.close(); } catch (Exception eee) { throw new TopiaException("Can't backup", eee); } } /** + * Read database from gzip compressed file + * * Only work for h2 database * * @see org.codelutin.topia.TopiaContext#restore(java.io.File) @@ -1463,10 +1484,42 @@ public void restore(File file) throws TopiaException { checkClosed("Ce contexte a ete ferme, impossible d'effectuer le restore"); try { + // decompresse file in temporary file + InputStream in = new BufferedInputStream(new FileInputStream(file)); + in.mark(2); + + // read header to see if is compressed file + int b = in.read(); + int magic = ((int)in.read() << 8) | b; + in.reset(); + + // by default we supposed that file is not compressed + File tmp = file; + if (magic == GZIPInputStream.GZIP_MAGIC) { + // in fact file is compressed, use temporaly file + tmp = File.createTempFile("tmp-topia-restore", ".sql"); + tmp.deleteOnExit(); + + // decompresse file in temporary file + InputStream gzin = new GZIPInputStream(in); + + OutputStream out = new BufferedOutputStream( + new FileOutputStream(tmp)); + + byte [] buffer = new byte[64 * 1024]; + int len = 0; + while ((len = gzin.read(buffer)) != -1) { + out.write(buffer, 0, len); + } + out.close(); + gzin.close(); + } + + // restore data Statement stat = getHibernate().connection().createStatement(); - stat.execute("RUNSCRIPT FROM '" + file.getAbsolutePath() + "'"); //:file\"); + stat.execute("RUNSCRIPT FROM '" + tmp.getAbsolutePath() + "'"); -// SQLQuery query = getHibernate().createSQLQuery("RUNSCRIPT FROM '" + file.getAbsolutePath() + "'"); //:file\"); +// SQLQuery query = getHibernate().createSQLQuery("RUNSCRIPT FROM '" + file.getAbsolutePath() + "'"); // List result = query.list(); } catch (Exception eee) { throw new TopiaException("Can't restore", eee);