FullAppUpdateListener.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  *  Creation P.Delrieu
00026  *
00027  */
00028 package org.openmobileis.bundle.update.terminal;
00029 
00030 import java.io.File;
00031 import java.io.IOException;
00032 
00033 import org.openmobileis.common.util.PropertiesManager;
00034 import org.openmobileis.common.util.exception.ServiceException;
00035 import org.openmobileis.common.util.exception.SynchroException;
00036 import org.openmobileis.common.util.file.FileUtilities;
00037 import org.openmobileis.common.util.log.LogManager;
00038 import org.openmobileis.embedded.util.SystemAPI;
00039 import org.openmobileis.services.common.ServiceManager;
00040 import org.openmobileis.synchro.client.SynchroDescriptor;
00041 import org.openmobileis.synchro.openmsp.OpenMSPException;
00042 import org.openmobileis.synchro.openmsp.client.DefaultOpenMSPSyncListener;
00043 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00044 import org.openmobileis.synchro.openmsp.protocol.Command;
00045 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00046 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00047 import org.openmobileis.synchro.openmsp.protocol.Element;
00048 import org.openmobileis.synchro.openmsp.protocol.Item;
00049 import org.openmobileis.synchro.openmsp.protocol.Message;
00050 import org.openmobileis.synchro.openmsp.protocol.Status;
00051 import org.openmobileis.synchro.security.auth.Credential;
00052 
00072 public final class FullAppUpdateListener extends DefaultOpenMSPSyncListener {
00073         
00074         private boolean restartAppli = false;
00075   private boolean syncGoOn = false;
00076   protected String synchroPath;
00077 
00078         /***
00079          * Module that synchronize the application jar.
00080          * The jar are copied in the user.dir/temp directory.
00081          * The application restarted and the jar unzipped in at the root path defined in the jar META_INF/rootunzippath directive
00082          * Use to update full application
00083          */
00084         public FullAppUpdateListener() {
00085                 //update jar info if exist.
00086                 ApplicationUpdateJarUtil util = new ApplicationUpdateJarUtil();
00087                 try     {
00088                         util.readJarMetainf();
00089                         util.updateInstallClass();
00090                         util.deleteUpdateJar();
00091                         ServiceManager.getManager().loadService(new ApplicationUpdateStatusService());
00092                 } catch (IOException ex)        {
00093                         LogManager.traceError(0, "FullAppUpdateListener init error during jar read:"+ex.toString());
00094                 }
00095                 
00096                 
00097                 ApplicationUpdateJarUtil.updateStarted = false;
00098 }
00099 
00100   public void initManager() throws ServiceException     {
00101   }
00102 
00107          public String getSyncName()    {
00108                  return ApplicationUpdateJarUtil.FULL_APP_UPDATE_NAME;
00109          }
00110 
00111         /* (non-Javadoc)
00112          * @see org.openmobileis.module.terminal.ModuleManagerListener#getJarInstallPath()
00113          */
00114         public String getJarInstallPath() {
00115                 return System.getProperty("user.dir")+"/temp/application.jar";
00116         }
00117 
00118         /* (non-Javadoc)
00119          * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#startSync(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.client.SynchroDescriptor)
00120          */
00121         public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00122                 super.startSync(cred, synchrodescriptor);
00123                 this.restartAppli = false;
00124                 this.syncGoOn = true;
00125     synchroPath = synchrodescriptor.getSynchroConduit().getIntallPath();
00126     synchroPath = synchroPath+File.separator+ApplicationUpdateJarUtil.APPLICATION_JAR_FILENAME;
00127         }
00128 
00129         /* (non-Javadoc)
00130          * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#sendData(org.openmobileis.synchro.openmsp.protocol.Message)
00131          */
00132         public void sendData(Message message) throws OpenMSPException {
00133     if (!this.isSyncOK()) {
00134       return; // return if an error occurs during synchro.
00135     }
00136     //send jar last modification date in long.
00137     if (!syncGoOn) { // synchronize data only for the first call.
00138     return;
00139     }
00140     
00141     try {
00142       Command getCommand = new Command(Element.GET, this.getSyncName(), this.getSyncName());
00143       getCommand.setSourceSessionID(0);
00144                 ApplicationUpdateJarUtil util = new ApplicationUpdateJarUtil();
00145       ContainerMessage getContainer = new ContainerMessage(getCommand);
00146       Item newItem = null;
00147       newItem = new DataItem(
00148                   Element.ITEM
00149                   , "Jar Server file name"
00150                   , util.getJarVersion()
00151                   , null
00152                   , null
00153               );
00154       getContainer.add(newItem);
00155       message.add(getContainer);
00156       syncGoOn = false;
00157    } catch (Throwable ex)  {
00158       this.setSynchroStatus(Status.STATUS_FAILED);
00159       LogManager.trace(new SynchroException("DefaultDbSyncListener "+this.getSyncName()+" Send data exception ", ex));
00160     }
00161         }
00162 
00163   /* (non-Javadoc)
00164    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveMapCommand(org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00165    */
00166   public void receiveMapCommand(ContainerMessage mapContainer) throws OpenMSPException {
00167 
00168   }
00169 
00170   /* (non-Javadoc)
00171    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveResultCommand(org.openmobileis.synchro.openmsp.protocol.ContainerMessage, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00172    */
00173   public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00174 //    Result resultCommand = (Result)resultContainer.getElement(); //result
00175     try {
00176       if (resultContainer.hasMoreMessage())  {
00177         DataItem item = (DataItem) resultContainer.nextMessage().getElement();
00178         if (item.getMetaInformation().equals("jarfile"))        {
00179                 FileUtilities.moveFile(this.synchroPath, ApplicationUpdateJarUtil.getTerminalUpdateJarPath());
00180                 //update jar version
00181                 ApplicationUpdateJarUtil util = new ApplicationUpdateJarUtil();
00182                 util.readJarMetainf();
00183                 this.restartAppli = true;   
00184         }
00185       }
00186       
00187     } catch (Throwable ex)  {
00188         //remove all copied jar file to avoid to remove old one. 
00189                 File file = new File(this.synchroPath);
00190                 if (file.exists()) file.delete();
00191                 file = new File(ApplicationUpdateJarUtil.getTerminalUpdateJarPath());
00192                 if (file.exists()) file.delete();
00193                 this.setSynchroStatus(Status.STATUS_FAILED);
00194       throw new OpenMSPException(ex);
00195     }
00196   }
00197 
00198         /* (non-Javadoc)
00199          * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#endSync()
00200          */
00201         public void endSync() throws OpenMSPException {
00202                 if (this.restartAppli)  {
00203                         Thread thread = new Thread(new Runnable()       {
00204                                 public void run() {
00205                                         try     {
00206                                                 Thread.currentThread().sleep(3000); //way 2 second the synchronisation finised.
00207                                                 //start update PRG
00208                                                 String prgname = PropertiesManager.getManager().getProperty("org.openmobileis.bundle.update.updateprg");
00209                                                 if (prgname != null)    {
00210                                                         String param = PropertiesManager.getManager().getProperty("org.openmobileis.bundle.update.updateprgparam");
00211                                                         if (param ==null) param = "";
00212                                                         LogManager.traceError(0, "FullAppUpdateListener endSync restart appli prgname:"+prgname+" param:"+param);
00213                                                         SystemAPI.getManager().execProgram(prgname, param);
00214                                                 }
00215                                         } catch (Throwable ex)  {
00216                                                 LogManager.traceError(0, ex);
00217                                         }
00218                                 };
00219                         });
00220                         ApplicationUpdateJarUtil.updateStarted = true;
00221                         thread.start();
00222                         this.setSynchroStatus(Status.STATUS_OK);
00223                         OpenMSPSynchroManager.getManager().setGlobalSynchroStatut(Status.STATUS_OK_RESTART_APPLICATION);
00224 //                      restart appli. show return page and after stop
00225                 }
00226         }
00227 
00228   /* (non-Javadoc)
00229    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveStatusCommand(org.openmobileis.synchro.openmsp.protocol.Status, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00230    */
00231   public void receiveStatusCommand(Status statusCommande, ContainerMessage initialCommand) throws OpenMSPException {
00232 //    int cmlRef = statusCommande.getCmdRef();
00233     int status = statusCommande.getStatus();
00234     this.setSynchroStatus(status);
00235   }
00236 
00237 }

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