00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00080
00081
00082
00083
00084 public FullAppUpdateListener() {
00085
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
00112
00113
00114 public String getJarInstallPath() {
00115 return System.getProperty("user.dir")+"/temp/application.jar";
00116 }
00117
00118
00119
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
00130
00131
00132 public void sendData(Message message) throws OpenMSPException {
00133 if (!this.isSyncOK()) {
00134 return;
00135 }
00136
00137 if (!syncGoOn) {
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
00164
00165
00166 public void receiveMapCommand(ContainerMessage mapContainer) throws OpenMSPException {
00167
00168 }
00169
00170
00171
00172
00173 public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00174
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
00181 ApplicationUpdateJarUtil util = new ApplicationUpdateJarUtil();
00182 util.readJarMetainf();
00183 this.restartAppli = true;
00184 }
00185 }
00186
00187 } catch (Throwable ex) {
00188
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
00199
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);
00207
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
00225 }
00226 }
00227
00228
00229
00230
00231 public void receiveStatusCommand(Status statusCommande, ContainerMessage initialCommand) throws OpenMSPException {
00232
00233 int status = statusCommande.getStatus();
00234 this.setSynchroStatus(status);
00235 }
00236
00237 }