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