Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

WinCESynchroFileGenerator.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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.profil.Profil;
00033 import org.openmobileis.common.util.file.FileUtilities;
00034 import org.openmobileis.common.util.collection.Array;
00035 import org.openmobileis.common.util.exception.SynchroException;
00036 
00037 import java.io.IOException;
00038 import java.io.ByteArrayOutputStream;
00039 import java.util.HashMap;
00040 import java.util.Iterator;
00041 import java.util.zip.*;
00042 
00043 import javax.servlet.http.HttpServletRequest;
00044 import javax.servlet.http.HttpServletResponse;
00045 
00053 public class WinCESynchroFileGenerator implements SynchroFileGenerator {
00054   class ZipArrayEntryFile {
00055     ByteArrayOutputStream array;
00056 
00057     ZipOutputStream zip;
00058 
00059     String fileName;
00060 
00061     public int hashCode() {
00062       return fileName.hashCode();
00063     }
00064 
00065     public boolean equals(Object obj) {
00066       if (obj instanceof ZipArrayEntryFile) {
00067         return ((ZipArrayEntryFile) obj).fileName.equals(this.fileName);
00068       }
00069       return false;
00070     }
00071 
00072   }
00073 
00074   public WinCESynchroFileGenerator() {
00075   }
00076 
00077   public void generateSynchroFile(UserTerminal terminal, Profil profil, HttpServletRequest request, HttpServletResponse res, FileSystem fileSystem,
00078       String newSessionId) throws SynchroException {
00079     ByteArrayOutputStream array = new ByteArrayOutputStream(100000);
00080     try {
00081       ZipOutputStream zip = new ZipOutputStream(array);
00082       try {
00083         //manage added zipEntryMemoryFiles
00084         HashMap zipFileMap = new HashMap(10);
00085         OpenMISFile[] syncfileList = fileSystem.getFileList();
00086         for (int i = 0; i < syncfileList.length; i++) {
00087           if (syncfileList[i] instanceof ZipEntryMemoryFile) {
00088             String zipFileName = ((ZipEntryMemoryFile) syncfileList[i]).getZipFileName();
00089             ZipArrayEntryFile zipEntry = (ZipArrayEntryFile) zipFileMap.get(zipFileName);
00090             if (zipEntry == null) {
00091               zipEntry = new ZipArrayEntryFile();
00092               zipEntry.array = new ByteArrayOutputStream(100000);
00093               zipEntry.zip = new ZipOutputStream(zipEntry.array);
00094               zipEntry.fileName = zipFileName;
00095               zipFileMap.put(zipFileName, zipEntry);
00096             }
00097             ZipEntry entry = new ZipEntry(FileUtilities.concertToUnixFomat(syncfileList[i].getFileName()));
00098             entry.setTime(syncfileList[i].getFileDate());
00099             entry.setMethod(ZipEntry.DEFLATED);
00100             zipEntry.zip.putNextEntry(entry);
00101             zipEntry.zip.write(syncfileList[i].getFileData());
00102             zipEntry.zip.closeEntry();
00103           } else {
00104             ZipEntry entry = new ZipEntry(FileUtilities.concertToUnixFomat(syncfileList[i].getFileCompleteName()));
00105             entry.setTime(syncfileList[i].getFileDate());
00106             entry.setMethod(ZipEntry.DEFLATED);
00107             zip.putNextEntry(entry);
00108             zip.write(syncfileList[i].getFileData());
00109             zip.closeEntry();
00110           }
00111         }
00112         
00113         //add zip entry files to synchro file.
00114         Iterator iter = zipFileMap.keySet().iterator();
00115         while (iter.hasNext())  {
00116           String filename = (String) iter.next();
00117           ZipArrayEntryFile zipEntry = (ZipArrayEntryFile) zipFileMap.get(filename);
00118           zipEntry.zip.flush();
00119           zipEntry.zip.close();
00120           ZipEntry entry = new ZipEntry(FileUtilities.concertToUnixFomat(filename));
00121          // entry.setTime(syncfileList[i].getFileDate());
00122           entry.setMethod(ZipEntry.DEFLATED);
00123           zip.putNextEntry(entry);
00124           zip.write(zipEntry.array.toByteArray());
00125           zip.closeEntry();          
00126         }
00127       } finally {
00128         zip.flush();
00129         zip.close();
00130       }
00131       res.addHeader("SyncNum", newSessionId);
00132       res.setContentType("application/zip");
00133       res.getOutputStream().write(array.toByteArray());
00134     } catch (IOException ex) {
00135       throw new SynchroException("Error during synchro file genration ", ex);
00136     }
00137   }
00138 
00139 }

Generated on Wed Dec 14 21:05:36 2005 for OpenMobileIS by  doxygen 1.4.4