BackupInitServlet.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2006-2008 Ubikis
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  Creation P.Delrieu
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                 // TODO Auto-generated constructor stub
00066         }
00067   public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, java.io.IOException {
00068     //copy files and backup.
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          //add log file.
00090          if (LogManager.getInstance() instanceof FileOpenCloseLogManager)       { 
00091                  File logfile = ((FileOpenCloseLogManager)LogManager.getInstance()).getLogFile();
00092                  if (logfile.exists() && logfile.length() < 500000)     { // don't backup log if too big. Synchro will delete. Avoid fill of the PDA memory.
00093                    FileInputStream in = new FileInputStream(logfile);
00094                    addFileToZip(in, logfile.getName(), out);                                             
00095                  }
00096          }
00097         
00098          // Complete the ZIP file
00099          out.close();
00100          
00101          //delete dabatase files.
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         // write answer
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            // wait to end request
00126   //        LogManager.traceDebug(0, "before shut wait:");
00127            Thread.currentThread().sleep(2000);
00128            // stop
00129 //                                      LogManager.traceDebug(0, "after shut wait :");
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 //              LogManager.traceDebug(0, "return shutdown servlet");
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    // Add ZIP entry to output stream.
00147     out.putNextEntry(new ZipEntry(filename));  
00148     // Transfer bytes from the file to the ZIP file
00149     int len;
00150     while ((len = in.read(buf)) > 0) {
00151         out.write(buf, 0, len);
00152     }   
00153     // Complete the entry
00154     out.closeEntry();
00155     in.close();
00156   }
00157 
00158 }

Generated on Mon Jan 11 21:19:13 2010 for OpenMobileIS by  doxygen 1.5.4