00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package org.openmobileis.services.servlet;
00029
00030 import java.io.File;
00031 import java.io.FileInputStream;
00032 import java.io.FileOutputStream;
00033 import java.io.IOException;
00034 import java.io.InputStream;
00035 import java.text.SimpleDateFormat;
00036 import java.util.Date;
00037 import java.util.zip.ZipEntry;
00038 import java.util.zip.ZipOutputStream;
00039
00040 import javax.servlet.ServletException;
00041 import javax.servlet.http.HttpServlet;
00042 import javax.servlet.http.HttpServletRequest;
00043 import javax.servlet.http.HttpServletResponse;
00044
00045 import org.openmobileis.common.util.log.FileOpenCloseLogManager;
00046 import org.openmobileis.common.util.log.LogManager;
00047 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00048 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00049
00058 public final class BackupInitServlet extends HttpServlet {
00059 static final long serialVersionUID = 5521257935120563452L;
00060
00064 public BackupInitServlet() {
00065
00066 }
00067 public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, java.io.IOException {
00068
00069 try {
00070 FastObjectDB db = FastObjectDBManager.getManager().getCurrentFODB();
00071 String dbPath = "" + db.getRootDir() + "/" + db.getName()+ "/";
00072 File filed = new File(dbPath);
00073 if (filed.isDirectory()) {
00074 FastObjectDBManager.getManager().CloseCurrentFODB();
00075 try {
00076 String outFilename = System.getProperty("user.dir")+"/backup/";
00077 File fileb = new File(outFilename);
00078 if (!fileb.exists())fileb.mkdirs();
00079 Date date = new Date();
00080 SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy_hhmmss");
00081 String dd = format.format(date);
00082 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename+dd+".zip"));
00083 String[] filenames = filed.list();
00084 for (int i=0; i<filenames.length; i++) {
00085 FileInputStream in = new FileInputStream(dbPath+filenames[i]);
00086 addFileToZip(in, filenames[i], out);
00087 }
00088
00089
00090 if (LogManager.getInstance() instanceof FileOpenCloseLogManager) {
00091 File logfile = ((FileOpenCloseLogManager)LogManager.getInstance()).getLogFile();
00092 if (logfile.exists() && logfile.length() < 500000) {
00093 FileInputStream in = new FileInputStream(logfile);
00094 addFileToZip(in, logfile.getName(), out);
00095 }
00096 }
00097
00098
00099 out.close();
00100
00101
00102 for (int i=0; i<filenames.length; i++) {
00103 File delfile = new File(dbPath+filenames[i]);
00104 delfile.delete();
00105 }
00106 } finally {
00107 db.getTransactionManager().commit();
00108 }
00109
00110 }
00111
00112
00113 } catch (Throwable ex) {
00114 LogManager.traceError(0, ex);
00115 }
00116
00117
00118
00119 res.setContentType( "text/html" );
00120 res.getOutputStream().write("<HTML><BODY>Vous pouvez fermer la fenetre et redemarrer</BODY></HTML>".getBytes());
00121 this.finalizeBeforeShutdown();
00122 Runnable toShut = new Runnable(){
00123 public void run() {
00124 try {
00125
00126
00127 Thread.currentThread().sleep(2000);
00128
00129
00130 System.exit(1);
00131 } catch (Exception ex) {
00132 LogManager.traceError(0, ex);
00133 }
00134 }
00135 };
00136
00137 Thread th = new Thread(toShut);
00138 th.start();
00139
00140 }
00141
00142 protected void finalizeBeforeShutdown() {}
00143
00144 private void addFileToZip(InputStream in, String filename, ZipOutputStream out) throws IOException {
00145 byte[] buf = new byte[1024];
00146
00147 out.putNextEntry(new ZipEntry(filename));
00148
00149 int len;
00150 while ((len = in.read(buf)) > 0) {
00151 out.write(buf, 0, len);
00152 }
00153
00154 out.closeEntry();
00155 in.close();
00156 }
00157
00158 }