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 package org.openmobileis.module.terminal;
00026
00027 import java.io.File;
00028
00029 import org.openmobileis.common.util.PropertiesManager;
00030 import org.openmobileis.common.util.collection.Array;
00031 import org.openmobileis.common.util.exception.SynchroException;
00032 import org.openmobileis.common.util.file.FileUtilities;
00033 import org.openmobileis.common.util.log.LogManager;
00034 import org.openmobileis.module.core.ProfileModule;
00035 import org.openmobileis.synchro.client.SynchroDescriptor;
00036 import org.openmobileis.synchro.journal.JournalManager;
00037 import org.openmobileis.synchro.journal.SimpleLogRenderer;
00038 import org.openmobileis.synchro.openmsp.OpenMSPException;
00039 import org.openmobileis.synchro.openmsp.client.DefaultOpenMSPSyncListener;
00040 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00041 import org.openmobileis.synchro.openmsp.protocol.Command;
00042 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00043 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00044 import org.openmobileis.synchro.openmsp.protocol.Element;
00045 import org.openmobileis.synchro.openmsp.protocol.Item;
00046 import org.openmobileis.synchro.openmsp.protocol.Message;
00047 import org.openmobileis.synchro.openmsp.protocol.RequestCommand;
00048 import org.openmobileis.synchro.openmsp.protocol.Result;
00049 import org.openmobileis.synchro.openmsp.protocol.Status;
00050 import org.openmobileis.synchro.security.auth.Credential;
00051
00059 public final class ModuleJarSyncListener extends DefaultOpenMSPSyncListener {
00060 private boolean syncGoOn = false;
00061 protected String jarPath;
00062 protected String synchroPath;
00063 protected String moduleName;
00064 protected ProfileModule module;
00065 protected String xmlPath;
00066
00070 public ModuleJarSyncListener() {
00071 super();
00072
00073 JournalManager.getManager().registerJournalLogRenderer(new SimpleLogRenderer(this.getSyncName(), this.getSyncName()+" application"));
00074 jarPath = PropertiesManager.getManager().getProperty("org.openmobileis.module.repository.jar");
00075 if (jarPath == null) {
00076 LogManager.traceWarning(0, "Property org.openmobileis.module.repository.jar not set. Module synchro can't be done.");
00077 } else {
00078 File file = new File(jarPath);
00079 if (!file.exists()) file.mkdirs();
00080 }
00081
00082 xmlPath = PropertiesManager.getManager().getProperty("org.openmobileis.module.repository.xml");
00083 if (xmlPath == null) {
00084 LogManager.traceWarning(0, "Property org.openmobileis.module.repository.xml not set. Module synchro can't be done.");
00085 } else {
00086 File file = new File(xmlPath);
00087 if (!file.exists()) file.mkdirs();
00088 }
00089 }
00090 public void setModule(ProfileModule module) {
00091 this.moduleName = module.getName();
00092 this.module = module;
00093 }
00094
00095 private String getModuleXmlFileName() {
00096 StringBuffer modrep = new StringBuffer(this.xmlPath);
00097 modrep.append('/');
00098 modrep.append(this.moduleName);
00099 modrep.append(".xml");
00100 return modrep.toString();
00101 }
00102
00103 public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00104 super.startSync(cred, synchrodescriptor);
00105 this.syncGoOn = true;
00106 synchroPath = synchrodescriptor.getSynchroConduit().getIntallPath();
00107 }
00108
00109
00110
00111
00112 public void sendData(Message message) throws OpenMSPException {
00113 if (!this.isSyncOK()) {
00114 return;
00115 }
00116
00117 if (!syncGoOn) {
00118 return;
00119 }
00120
00121
00122 long ns = NumSyncManagerDB.getManager().getSyncNumberForService(this.getSyncName());
00123
00124 try {
00125 Command getCommand = new Command(Element.GET, this.getSyncName(), this.getSyncName());
00126 getCommand.setSourceSessionID(ns);
00127 ContainerMessage getContainer = new ContainerMessage(getCommand);
00128 Item newItem = null;
00129 newItem = new DataItem(
00130 Element.ITEM
00131 , "Jar Server file name"
00132 , ""
00133 , null
00134 , null
00135 );
00136 getContainer.add(newItem);
00137 message.add(getContainer);
00138 syncGoOn = false;
00139 } catch (Throwable ex) {
00140 this.setSynchroStatus(Status.STATUS_FAILED);
00141 LogManager.trace(new SynchroException("DefaultDbSyncListener "+this.getSyncName()+" Send data exception ", ex));
00142 }
00143 }
00144
00145
00146
00147
00148 public String getSyncName() {
00149 return this.moduleName;
00150 }
00151
00152
00153
00154
00155 public void receiveMapCommand(ContainerMessage mapContainer) throws OpenMSPException {
00156
00157 }
00158
00159
00160
00161
00162 public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00163 Result resultCommand = (Result)resultContainer.getElement();
00164 Array copiedFiles = new Array();
00165 try {
00166 long newSessionID = Long.parseLong(resultCommand.getMetaInformation());
00167 while (resultContainer.hasMoreMessage()) {
00168 DataItem item = (DataItem) resultContainer.nextMessage().getElement();
00169 String meta = item.getMetaInformation();
00170 if (meta.equals("jarfile")) {
00171 this.updateJarFile(item.getData(), copiedFiles);
00172 }
00173 if (meta.equals("xmlfile")) {
00174 this.updateXmlFile(item.getData());
00175 copiedFiles.add(item.getData());
00176 }
00177 }
00178
00179 if (this.isSyncOK()) {
00180 ModuleManager.getManager().notifyModuleUpdateAfterSynchro(this.module);
00181 NumSyncManagerDB.getManager().saveSyncNumberForService(newSessionID, this.getSyncName());
00182
00183
00184 }
00185
00186 } catch (Throwable ex) {
00187
00188 int size = copiedFiles.size();
00189 for (int i=0; i<size; i++){
00190 String filename = (String)copiedFiles.get(i);
00191 File file = new File(filename);
00192 file.delete();
00193 }
00194 throw new OpenMSPException(ex);
00195 }
00196 }
00197
00198 private void updateJarFile (String filename, Array copiedFiles) {
00199 String inPath = this.synchroPath+File.separator+"filedir"+File.separator+filename;
00200 String outPath = this.jarPath+File.separator+filename;
00201 FileUtilities.moveFile(inPath, outPath);
00202 copiedFiles.add(outPath);
00203 }
00204
00205 private void updateXmlFile (String filename) {
00206 String inPath = this.synchroPath+File.separator+"filedir"+File.separator+filename;
00207 String outPath = this.getModuleXmlFileName();
00208 FileUtilities.moveFile(inPath, outPath);
00209 }
00210
00211
00212
00213
00214 public void receiveStatusCommand(Status statusCommande, ContainerMessage initialCommand) throws OpenMSPException {
00215
00216 int status = statusCommande.getStatus();
00217 if (status != Status.STATUS_OK) {
00218 this.notifySynchroFailure();
00219 }
00220 }
00221
00222 }