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

LogsSynchroListener.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  Creation P.Delrieu
00026  * 
00027  */
00028 
00029 package org.openmobileis.modules.common.log.client;
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     String status = PersistentPropertiesManager.getManager().getProperty(this.getSyncName(), "synchroStatus");
00077     if (status == null) {
00078       errorStatus = Status.STATUS_OK;
00079     }  else {
00080       errorStatus = Integer.parseInt(status);
00081     }
00082  //   JournalManager.getManager().registerJournalLogRenderer(new SimpleLogRenderer(LogsSynchroListener.LOG_LISTENER_NAME, "Log"));
00083   }
00084 
00085   public void setSynchroStatus(int status) {
00086     errorStatus = status;
00087   }
00088   
00089   public int getSynchroStatus() {
00090     return this.errorStatus;
00091   }
00092 
00093   public boolean isSyncOK() {
00094     return (errorStatus == Status.STATUS_OK);
00095   }
00096 
00097   public void notifySynchroFailure() {
00098     this.setSynchroStatus(Status.STATUS_FAILED);
00099     JournalManager.getManager().saveJournalEntry(LogsSynchroListener.LOG_LISTENER_NAME, JournalEntry.ERROR_SYNC, "echec", Status.STATUS_FAILED);
00100   }
00101 
00102   public void receiveStatusCommand( Status statusCommande, ContainerMessage initialCommand)  throws OpenMSPException  {
00103     // manage error message first
00104     // manage mail send. delete sent mails.
00105     if (statusCommande.getStatus() != Status.STATUS_OK) {
00106       OpenMSPSynchroManager.getManager().notifyFailure(this.getSyncName());
00107     } else {
00108                         String fileName = PropertiesManager.getManager().getProperty("org.openmobileis.common.log.archivefile");
00109                         File file = new File(fileName);
00110       file.delete();
00111                 }
00112                 this.setSynchroStatus(statusCommande.getStatus());
00113   }
00114 
00115   /*
00116   
00117   * Event sent when the sync manager sends a OpenMSP message to the server
00118   
00119   */
00120   public void sendData(Message message) throws OpenMSPException   {
00121     if (!this.isSyncOK()) {
00122       return; // return if an error occurs during synchro.
00123     }
00124 
00125     if (!syncGoOn) { // synchronize data only for the first call.
00126     return;
00127     }
00128     syncGoOn = false;
00129     try {
00130       //get all log files; 
00131       if (LogManager.getInstance() instanceof FileOpenCloseLogManager)  { 
00132         Command syncCommand = new Command(Element.SYNC, LogsSynchroListener.LOG_LISTENER_NAME, "FWKSyncLogsManager");
00133         ContainerMessage syncContainer = new ContainerMessage(syncCommand);
00134         Item data = null;
00135         // manage new mail
00136         RequestCommand addCommand = null;
00137         addCommand = new RequestCommand(Element.ADD);
00138         ContainerMessage addMailContainer = new ContainerMessage(addCommand);
00139         File logfile = ((FileOpenCloseLogManager)LogManager.getInstance()).getLogFile();
00140         byte[] fileData = GeneralCoder.encodeBase64(FileUtilities.readFile(logfile.getAbsolutePath()));
00141         data = new DataItem(Element.ITEM, logfile.getName(), new String(fileData), null, null);
00142         addMailContainer.add(data);
00143         syncContainer.add(addMailContainer);
00144         message.add(syncContainer);
00145       }
00146     } catch (Throwable ex)  {
00147       this.setSynchroStatus(Status.STATUS_FAILED);
00148       LogManager.traceError(0, ex);
00149     }
00150  }
00151 
00157   public void receiveSyncCommand( ContainerMessage syncContainer, long newSyncNumber)  throws OpenMSPException  {
00158     
00159   }
00160 
00166   public void receiveMapCommand( ContainerMessage mapContainer)   throws OpenMSPException {
00167     
00168   }
00169 
00176   public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand)  throws OpenMSPException  {
00177     
00178   }
00179 
00180   /*
00181   
00182    * Return the name used to identiy the receiver of the syncML messages (source and target)
00183   
00184    */
00185 
00186   public String getSyncName() {
00187     return LogsSynchroListener.LOG_LISTENER_NAME;
00188   }
00189 
00190   /*
00191   
00192   * Fire this event once a synchro is starting
00193   
00194   */
00195 
00196   public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00197     this.syncGoOn = true;
00198     this.setSynchroStatus(Status.STATUS_OK);
00199     JournalManager.getManager().deleteAllJournalEntryForService(this.getSyncName());
00200   }
00201 
00202   /*
00203   
00204   * Fire this event once the current synchro is ended
00205   
00206   */
00207 
00208   public void endSync() {
00209     String status  = Integer.toString(errorStatus);
00210     PersistentPropertiesManager.getManager().saveProperty(this.getSyncName(), "synchroStatus", status);
00211    // log journal only if a synchro has been processed
00212    if (this.isSyncOK()) {
00213        JournalManager.getManager().saveJournalEntry(this.getSyncName(), JournalEntry.ERROR_SYNC, JournalManager.OK_MESSAGE, Status.STATUS_OK);
00214    }
00215   }
00216 
00220   public int getErrorStatus() {
00221     return errorStatus;
00222   }
00223 
00224 }

Generated on Mon Jul 10 10:29:31 2006 for OpenMobileIS by  doxygen 1.4.4