WinCESynchroFileGenerator.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
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  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 package org.openmobileis.synchro.openmsp.server.util;
00030 
00031 import org.openmobileis.common.context.UserTerminal;
00032 import org.openmobileis.common.user.profile.Profile;
00033 import org.openmobileis.common.util.file.FileUtilities;
00034 import org.openmobileis.common.util.exception.SynchroException;
00035 
00036 import java.io.File;
00037 import java.io.IOException;
00038 import java.io.ByteArrayOutputStream;
00039 import java.io.InputStream;
00040 import java.util.HashMap;
00041 import java.util.Iterator;
00042 import java.util.zip.*;
00043 
00044 import javax.servlet.http.HttpServletRequest;
00045 import javax.servlet.http.HttpServletResponse;
00046 
00054 public class WinCESynchroFileGenerator implements SynchroFileGenerator {
00055   class ZipArrayEntryFile {
00056     ByteArrayOutputStream array;
00057 
00058     ZipOutputStream zip;
00059 
00060     String fileName;
00061 
00062     public int hashCode() {
00063       return fileName.hashCode();
00064     }
00065 
00066     public boolean equals(Object obj) {
00067       if (obj instanceof ZipArrayEntryFile) {
00068         return ((ZipArrayEntryFile) obj).fileName.equals(this.fileName);
00069       }
00070       return false;
00071     }
00072 
00073   }
00074 
00075   public WinCESynchroFileGenerator() {
00076         File rep = new File(System.getProperty("user.dir")+File.separator+FileStoredOpenMISFile.tempDir);
00077         if (!rep.exists())      {
00078                 rep.mkdirs();
00079         }
00080         File[] list = rep.listFiles();
00081         for (int i=0; i<list.length; i++)       {
00082                 list[i].delete();
00083         }
00084   }
00085 
00086   public void generateSynchroFile(UserTerminal terminal, Profile profil, HttpServletRequest request, HttpServletResponse res, FileSystem fileSystem,
00087       String newSessionId) throws SynchroException {
00088     ByteArrayOutputStream array = new ByteArrayOutputStream(100000);
00089     try {
00090       ZipOutputStream zip = new ZipOutputStream(array);
00091       try {
00092         //manage added zipEntryMemoryFiles
00093         HashMap zipFileMap = new HashMap(10);
00094         OpenMISFile[] syncfileList = fileSystem.getFileList();
00095         for (int i = 0; i < syncfileList.length; i++) {
00096           if (syncfileList[i] instanceof ZipEntryFile) {
00097                 try     {
00098                     String zipFileName = ((ZipEntryFile) syncfileList[i]).getZipFileName();
00099                     ZipArrayEntryFile zipEntry = (ZipArrayEntryFile) zipFileMap.get(zipFileName);
00100                     if (zipEntry == null) {
00101                       zipEntry = new ZipArrayEntryFile();
00102                       zipEntry.array = new ByteArrayOutputStream(10000);
00103                       zipEntry.zip = new ZipOutputStream(zipEntry.array);
00104                       zipEntry.fileName = zipFileName;
00105                       zipFileMap.put(zipFileName, zipEntry);
00106                     }
00107                     ZipEntry entry = new ZipEntry(FileUtilities.convertToUnixFomat(syncfileList[i].getFileName()));
00108                     entry.setTime(syncfileList[i].getFileDate());
00109                     entry.setMethod(ZipEntry.DEFLATED);
00110                     zipEntry.zip.putNextEntry(entry);
00111            //         zipEntry.zip.write(syncfileList[i].getFileData());
00112                            //-------------------------- Modified Manuel Gomez 2007 ---------------------------------
00113                            int n=0;
00114                            byte inbuf[] = new byte[100000];
00115                            InputStream in = syncfileList[i].getFileDataStream();        // usele getFileStream to allow file reading optimisation
00116                            try  {
00117                                    while ((n = in.read(inbuf)) != -1)
00118                                          zipEntry.zip.write(inbuf, 0, n);
00119                            } finally    {
00120                                    in.close();
00121                            }
00122                            //------------------------------------------------------------------
00123                      zipEntry.zip.closeEntry();
00124                 } finally       {
00125                         if (syncfileList[i] instanceof FileStoreZipEntryFile)
00126                                 ((FileStoreZipEntryFile)syncfileList[i]).removeTempFile();
00127                 }
00128           } else {
00129             ZipEntry entry = new ZipEntry(FileUtilities.convertToUnixFomat(syncfileList[i].getFileCompleteName()));
00130             entry.setTime(syncfileList[i].getFileDate());
00131             entry.setMethod(ZipEntry.DEFLATED);
00132             zip.putNextEntry(entry);
00133                    //-------------------------- Modified Manuel Gomez 2007 ---------------------------------
00134                    // zip.write(syncfileList[i].getFileData());
00135                    int n=0;
00136                    byte inbuf[] = new byte[100000];
00137                    InputStream in = syncfileList[i].getFileDataStream();        // usele getFileStream to allow file reading optimisation
00138                    try  {
00139                            while ((n = in.read(inbuf)) != -1)
00140                                zip.write(inbuf, 0, n);
00141                    } finally    {
00142                            in.close();
00143                    }
00144                    //------------------------------------------------------------------
00145              zip.closeEntry();
00146           }
00147         }
00148         
00149         //add zip entry files to synchro file.
00150         Iterator iter = zipFileMap.keySet().iterator();
00151         while (iter.hasNext())  {
00152           String filename = (String) iter.next();
00153           ZipArrayEntryFile zipEntry = (ZipArrayEntryFile) zipFileMap.get(filename);
00154           zipEntry.zip.flush();
00155           zipEntry.zip.close();
00156           ZipEntry entry = new ZipEntry(FileUtilities.convertToUnixFomat(filename));
00157          // entry.setTime(syncfileList[i].getFileDate());
00158           entry.setMethod(ZipEntry.DEFLATED);
00159           zip.putNextEntry(entry);
00160           zip.write(zipEntry.array.toByteArray());
00161           zip.closeEntry();          
00162         }
00163       } finally {
00164         zip.flush();
00165         zip.close();
00166       }
00167       res.addHeader("SyncNum", newSessionId);
00168       res.setContentType("application/zip");
00169       byte[] retArray = array.toByteArray();
00170       res.setContentLength(retArray.length);
00171       res.getOutputStream().write(retArray);
00172     } catch (IOException ex) {
00173       throw new SynchroException("Error during synchro file genration ", ex);
00174     }
00175   }
00176 
00177 }

Generated on Mon Jan 14 17:29:50 2008 for OpenMobileIS by  doxygen 1.5.4