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
00029 package org.openmobileis.modules.common.log.terminal;
00030
00031 import java.io.File;
00032
00033 import org.openmobileis.common.util.PropertiesManager;
00034 import org.openmobileis.common.util.PersistentPropertiesManager;
00035 import org.openmobileis.common.util.codec.GeneralCoder;
00036 import org.openmobileis.common.util.file.FileUtilities;
00037 import org.openmobileis.common.util.log.FileOpenCloseLogManager;
00038 import org.openmobileis.common.util.log.LogManager;
00039 import org.openmobileis.synchro.client.SynchroDescriptor;
00040 import org.openmobileis.synchro.journal.JournalEntry;
00041 import org.openmobileis.synchro.journal.JournalManager;
00042 import org.openmobileis.synchro.openmsp.OpenMSPException;
00043 import org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener;
00044 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00045 import org.openmobileis.synchro.openmsp.protocol.Command;
00046 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00047 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00048 import org.openmobileis.synchro.openmsp.protocol.Element;
00049 import org.openmobileis.synchro.openmsp.protocol.Item;
00050 import org.openmobileis.synchro.openmsp.protocol.Message;
00051 import org.openmobileis.synchro.openmsp.protocol.RequestCommand;
00052 import org.openmobileis.synchro.openmsp.protocol.Status;
00053 import org.openmobileis.synchro.security.auth.Credential;
00054
00063 public class LogsSynchroListener implements OpenMSPSyncListener {
00064
00065 public static final String LOG_LISTENER_NAME = "FWKLogSynchroListener";
00066
00067 private boolean syncGoOn = false;
00068
00073 private int errorStatus;
00074
00075 public LogsSynchroListener() {
00076 }
00077
00078 public void initListener() throws OpenMSPException {
00079 String status = PersistentPropertiesManager.getManager().getProperty(this.getSyncName(), "synchroStatus");
00080 if (status == null) {
00081 errorStatus = Status.STATUS_OK;
00082 } else {
00083 errorStatus = Integer.parseInt(status);
00084 }
00085
00086 }
00087
00088 public void setSynchroStatus(int status) {
00089 errorStatus = status;
00090 }
00091
00092 public int getSynchroStatus() {
00093 return this.errorStatus;
00094 }
00095
00096 public boolean isSyncOK() {
00097 return (errorStatus == Status.STATUS_OK);
00098 }
00099
00100 public void notifySynchroFailure() {
00101 this.setSynchroStatus(Status.STATUS_FAILED);
00102 JournalManager.getManager().saveJournalEntry(LogsSynchroListener.LOG_LISTENER_NAME, JournalEntry.ERROR_SYNC, "Failure", Status.STATUS_FAILED);
00103 }
00104
00105 public void receiveStatusCommand( Status statusCommande, ContainerMessage initialCommand) throws OpenMSPException {
00106
00107
00108 if (statusCommande.getStatus() != Status.STATUS_OK) {
00109 OpenMSPSynchroManager.getManager().notifyFailure(this.getSyncName());
00110 } else {
00111 String fileName = PropertiesManager.getManager().getProperty("org.openmobileis.common.log.file");
00112 File file = new File(fileName);
00113 file.delete();
00114 }
00115 this.setSynchroStatus(statusCommande.getStatus());
00116 }
00117
00118
00119
00120
00121
00122
00123 public void sendData(Message message) throws OpenMSPException {
00124 if (!this.isSyncOK()) {
00125 return;
00126 }
00127
00128 if (!syncGoOn) {
00129 return;
00130 }
00131 syncGoOn = false;
00132 try {
00133
00134 if (LogManager.getInstance() instanceof FileOpenCloseLogManager) {
00135 Command syncCommand = new Command(Element.SYNC, LogsSynchroListener.LOG_LISTENER_NAME, "FWKSyncLogsManager");
00136 ContainerMessage syncContainer = new ContainerMessage(syncCommand);
00137 Item data = null;
00138
00139 RequestCommand addCommand = null;
00140 addCommand = new RequestCommand(Element.ADD);
00141 ContainerMessage addMailContainer = new ContainerMessage(addCommand);
00142 File logfile = ((FileOpenCloseLogManager)LogManager.getInstance()).getLogFile();
00143 if (logfile.exists()) {
00144 byte[] fileData = GeneralCoder.encodeBase64(FileUtilities.readFile(logfile.getAbsolutePath()));
00145 data = new DataItem(Element.ITEM, logfile.getName(), new String(fileData), null, null);
00146 addMailContainer.add(data);
00147 syncContainer.add(addMailContainer);
00148 message.add(syncContainer);
00149 }
00150 }
00151 } catch (Throwable ex) {
00152 this.setSynchroStatus(Status.STATUS_FAILED);
00153 LogManager.traceError(0, ex);
00154 }
00155 }
00156
00162 public void receiveSyncCommand( ContainerMessage syncContainer, long newSyncNumber) throws OpenMSPException {
00163
00164 }
00165
00171 public void receiveMapCommand( ContainerMessage mapContainer) throws OpenMSPException {
00172
00173 }
00174
00181 public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00182
00183 }
00184
00185 public void receiveGetCommand( Credential cred, ContainerMessage replaceContainer, long newSyncNumber) throws OpenMSPException {
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 public String getSyncName() {
00195 return LogsSynchroListener.LOG_LISTENER_NAME;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00205 this.syncGoOn = true;
00206 this.setSynchroStatus(Status.STATUS_OK);
00207 JournalManager.getManager().deleteAllJournalEntryForService(this.getSyncName());
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 public void endSync() {
00217 String status = Integer.toString(errorStatus);
00218 PersistentPropertiesManager.getManager().saveProperty(this.getSyncName(), "synchroStatus", status);
00219
00220 if (this.isSyncOK()) {
00221 JournalManager.getManager().saveJournalEntry(this.getSyncName(), JournalEntry.ERROR_SYNC, JournalManager.OK_MESSAGE, Status.STATUS_OK);
00222 }
00223 }
00224
00228 public int getErrorStatus() {
00229 return errorStatus;
00230 }
00231
00232 }