ApplicationUpdateJarUtil.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.File;
00029 import java.io.IOException;
00030 import java.util.Iterator;
00031 import java.util.jar.Attributes;
00032 import java.util.jar.JarFile;
00033 import java.util.jar.Attributes.Name;
00034 
00035 import org.openmobileis.common.util.PersistentPropertiesManager;
00036 import org.openmobileis.common.util.file.FileUtilities;
00037 
00045 public final class ApplicationUpdateJarUtil {
00046         public static String FULL_APP_UPDATE_NAME = "FWKFullAppUpdate";
00047         public static final Name VERSIONNAME = new Name("OpenMIS-AppUpdate-Version");
00048         public static final Name POSTINSTALLCLASS = new Name("OpenMIS-PostInstall-Class");
00049 
00050         public static String APPLICATION_JAR_FILENAME ="fullapplication.jar";
00051         public static String jarDirectory = System.getProperty("user.dir") + File.separator + "temp/";
00052         public static String jarPath = ApplicationUpdateJarUtil.jarDirectory + File.separator + ApplicationUpdateJarUtil.APPLICATION_JAR_FILENAME;
00053 
00054         private String version = "";
00055         private String installclass = "";
00056         
00057         public static boolean updateStarted = false;
00058 
00062         public ApplicationUpdateJarUtil() {
00063                 File dir = new File(jarDirectory);
00064                 if (!dir.exists())      {
00065                         dir.mkdirs();
00066                 }
00067         }
00068         
00069         public static String getTerminalUpdateJarPath() {
00070                 return ApplicationUpdateJarUtil.jarPath;
00071         }
00072 
00073         public void readJarMetainf() throws IOException {
00074                 String path = ApplicationUpdateJarUtil.getTerminalUpdateJarPath();
00075                 File file = new File(path);
00076                 if (file.exists()) {
00077                         JarFile jarFile = new JarFile(path);
00078                         try {
00079                                 if (jarFile.getManifest() != null) {
00080                                         Iterator iter = jarFile.getManifest().getMainAttributes().keySet().iterator();
00081                                         while (iter.hasNext()) {
00082                                                 Attributes.Name attribute = (Attributes.Name) iter.next();
00083                                                 if (attribute.equals(VERSIONNAME)) {
00084                                                         this.version = jarFile.getManifest().getMainAttributes().getValue(attribute);
00085                                                         PersistentPropertiesManager.getManager().saveProperty(ApplicationUpdateJarUtil.FULL_APP_UPDATE_NAME, "org.openmobileis.synchro.bundle.fullupdate.version", this.version);
00086                                                 } else if (attribute.equals(POSTINSTALLCLASS)) {
00087                                                         this.installclass = jarFile.getManifest().getMainAttributes().getValue(attribute);
00088                                                 }
00089                                         }
00090                                 }
00091                         } finally {
00092                                 if (jarFile != null) {
00093                                         try {
00094                                                 jarFile.close();
00095                                         } catch (IOException ioe) {
00096                                         }
00097                                 }
00098                         }
00099                 }
00100         }
00101         
00102         public String getJarVersion() throws IOException        {
00103                 if (this.version.length()==0)   {
00104                         this.readJarMetainf();
00105                 }
00106                 if (this.version.length()==0){
00107                         this.version = PersistentPropertiesManager.getManager().getProperty(ApplicationUpdateJarUtil.FULL_APP_UPDATE_NAME, "org.openmobileis.synchro.bundle.fullupdate.version");
00108                         if (this.version == null) {
00109                                 this.version = "";
00110                                 PersistentPropertiesManager.getManager().saveProperty(ApplicationUpdateJarUtil.FULL_APP_UPDATE_NAME, "org.openmobileis.synchro.bundle.fullupdate.version", this.version);
00111                         }
00112                 }
00113                 return this.version;
00114         }
00115 
00116         public void updateInstallClass() {
00117                 if (this.installclass.length() > 0) {
00118                         try {
00119                                 FullUpdatePostInstallInterface cl = (FullUpdatePostInstallInterface) Class.forName(this.installclass).newInstance();
00120                                 cl.updateInstall();
00121                         } catch (Throwable ex) {
00122                                 ex.printStackTrace();
00123                         }
00124                 }
00125         }
00126 
00127         public void deleteUpdateJar() {
00128                 String path = System.getProperty("user.dir") + File.separator + "temp/" + ApplicationUpdateJarUtil.APPLICATION_JAR_FILENAME;
00129                 File file = new File(path);
00130                 if (file.exists()) {
00131                         file.delete();
00132                         //delete meta-inf if exist.
00133                         File metadir = new File(System.getProperty("user.dir")+"/"+"META-INF");
00134                         if (metadir.exists())   {
00135                                 FileUtilities.deleteDir(metadir);
00136                         }
00137                 }
00138         }
00139 
00140         public String getVersion() {
00141                 return version;
00142         }
00143 
00144         public void setVersion(String version) {
00145                 this.version = version;
00146         }
00147 
00148         public String getInstallclass() {
00149                 return installclass;
00150         }
00151 
00152         public void setInstallclass(String installclass) {
00153                 this.installclass = installclass;
00154         }
00155 
00156 }

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