UpdateJarOpenMisRestarter.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2009 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  */
00025 
00026 package org.openmobileis.bundle.update.terminal;
00027 
00028 import java.io.BufferedOutputStream;
00029 import java.io.File;
00030 import java.io.FileOutputStream;
00031 import java.io.IOException;
00032 import java.io.InputStream;
00033 import java.util.Enumeration;
00034 import java.util.jar.JarEntry;
00035 import java.util.jar.JarFile;
00036 
00041 public final class UpdateJarOpenMisRestarter {
00042 
00046         public UpdateJarOpenMisRestarter() {
00047         }
00048         
00049         public void updateApplication() {
00050                 try     {
00051                         String path = System.getProperty("user.dir") + File.separator + "temp/"+ApplicationUpdateJarUtil.APPLICATION_JAR_FILENAME;
00052                         File file = new File(path);
00053                         if (file.exists()) {
00054                         
00055                 //send HTTP shutdown request.
00056         //      Send with socket no HTTP client use.
00057                 //wait end shutdown
00058 //              wait 3 second
00059                 //update jar
00060                                 this.unzipInstallJar();
00061                         }
00062                 //start openmis.
00063                 } catch (Throwable ex){
00064                         
00065                 }
00066         }
00067 
00068         private void unzipInstallJar() throws IOException {
00069                 String basepath = System.getProperty("user.dir") + File.separator;
00070                 String path = basepath + "temp/" + ApplicationUpdateJarUtil.APPLICATION_JAR_FILENAME;
00071                 File file = new File(path);
00072                 if (file.exists()) {
00073                         JarFile jarFile = new JarFile(path);
00074                         try {
00075                                 for (Enumeration e = jarFile.entries(); e.hasMoreElements();) {
00076                                         JarEntry jarEntry = (JarEntry) e.nextElement();
00077                                         String completeName = jarEntry.getName();
00078                                         // get file directory and test if exist.
00079                                         if (jarEntry.isDirectory()) {
00080                                                 File dirToCreate = new File(basepath + completeName);
00081                                                 if (!dirToCreate.exists()) {
00082                                                         dirToCreate.mkdirs();
00083                                                 }
00084                                                 continue;
00085                                         }
00086                                         int index = completeName.lastIndexOf("/");
00087                                         if (index != -1) {
00088                                                 File repToCreate = new File(basepath + completeName.substring(0, index));
00089                                                 if (!repToCreate.exists()) {
00090                                                         repToCreate.mkdirs();
00091                                                 }
00092                                         }
00093                                         FileOutputStream stream = new FileOutputStream(basepath + completeName);
00094                                         BufferedOutputStream bos = null;
00095                                         InputStream in = jarFile.getInputStream(jarEntry);
00096                                         try {
00097                                                 int read_bytes = 0;
00098                                                 byte[] buffer = new byte[4096];
00099                                                 bos = new BufferedOutputStream(stream, buffer.length);
00100                                                 // LogManager.traceDebug(0, "DEBUG TIME in ApacheHTTPCientSynchroConduit dans unZipData() av while : "+new Date(System.currentTimeMillis()));
00101                                                 while ((read_bytes = in.read(buffer, 0, buffer.length)) != -1) {
00102                                                         bos.write(buffer, 0, read_bytes);
00103                                                 }
00104                                                 // LogManager.traceDebug(0, "DEBUG TIME in ApacheHTTPCientSynchroConduit dans unZipData() ap while : "+new Date(System.currentTimeMillis()));
00105                                         } finally {
00106                                                 if (bos != null) {
00107                                                         bos.flush();
00108                                                         bos.close();
00109                                                         stream.close();
00110                                                 }
00111                                                 in.close();
00112                                         }
00113                                 }
00114                         } finally {
00115                                 if (jarFile != null) {
00116                                         try {
00117                                                 jarFile.close();
00118                                         } catch (IOException ioe) {
00119                                         }
00120                                 }
00121                         }
00122                 }
00123         }
00124 }

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