Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

LogsSynchroListener.java

00001 /*
00002  *        OpenMobileIS - a free Java Framework for mobile applications
00003  *
00004  *   Copyright (C) 2002  Philippe Delrieu.
00005  *
00006  *   This program is free software; you can redistribute it and/or
00007  *   modify it under the terms of the GNU General Public
00008  *   License as published by the Free Software Foundation; either
00009  *   version 2 of the License, or (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *   General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this library; if not, write to the Free
00018  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  * 
00020  * Philippe Delrieu kept the rigth to distribute all code Copyrighted by philippe Delrieu
00021  *  under other licence term even commercial one.
00022  *  
00023  *  Modifications :
00024  *  2002 Creation P.Delrieu
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  //   JournalManager.getManager().registerJournalLogRenderer(new SimpleLogRenderer(LogsSynchroListener.LOG_LISTENER_NAME, "Log"));
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     // manage error message first
00103     // manage mail send. delete sent mails.
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   * Event sent when the sync manager sends a OpenMSP message to the server
00117   
00118   */
00119   public void sendData(Message message) throws OpenMSPException   {
00120     if (!this.isSyncOK()) {
00121       return; // return if an error occurs during synchro.
00122     }
00123 
00124     if (!syncGoOn) { // synchronize data only for the first call.
00125     return;
00126     }
00127     syncGoOn = false;
00128     try {
00129       //get all log files; 
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         // manage new mail
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    * Return the name used to identiy the receiver of the syncML messages (source and target)
00182   
00183    */
00184 
00185   public String getSyncName() {
00186     return LogsSynchroListener.LOG_LISTENER_NAME;
00187   }
00188 
00189   /*
00190   
00191   * Fire this event once a synchro is starting
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   * Fire this event once the current synchro is ended
00204   
00205   */
00206 
00207   public void endSync() {
00208     String status  = Integer.toString(errorStatus);
00209     ServicePropertiesManager.getManager().saveProperty(this.getSyncName(), "synchroStatus", status);
00210    // log journal only if a synchro has been processed
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 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4