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