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.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   private long maxFileSize = -1;
00075 
00076   public LogsSynchroListener() {
00077         String mfs = PropertiesManager.getManager().getProperty("org.openmobileis.modules.common.log.terminal.maxlogfilesize");
00078         if (mfs != null)        {
00079                 try     {
00080                 this.maxFileSize = Long.parseLong(mfs);
00081                 } catch (Throwable ex){
00082                         LogManager.traceError(0, ex);
00083                 }
00084         }
00085   }
00086   
00087         public void initListener() throws OpenMSPException      {
00088     String status = PersistentPropertiesManager.getManager().getProperty(this.getSyncName(), "synchroStatus");
00089     if (status == null) {
00090       errorStatus = Status.STATUS_OK;
00091     }  else {
00092       errorStatus = Integer.parseInt(status);
00093     }
00094  //   JournalManager.getManager().registerJournalLogRenderer(new SimpleLogRenderer(LogsSynchroListener.LOG_LISTENER_NAME, "Log"));
00095  }
00096 
00097   public void setSynchroStatus(int status) {
00098     errorStatus = status;
00099   }
00100   
00101   public int getSynchroStatus() {
00102     return this.errorStatus;
00103   }
00104 
00105   public boolean isSyncOK() {
00106     return (errorStatus == Status.STATUS_OK);
00107   }
00108 
00109   public void notifySynchroFailure() {
00110     this.setSynchroStatus(Status.STATUS_FAILED);
00111     JournalManager.getManager().saveJournalEntry(LogsSynchroListener.LOG_LISTENER_NAME, JournalEntry.OK, "Failure", Status.STATUS_FAILED);
00112   }
00113 
00114   public void receiveStatusCommand( Status statusCommande, ContainerMessage initialCommand)  throws OpenMSPException  {
00115     // manage error message first
00116     // manage mail send. delete sent mails.
00117     if (statusCommande.getStatus() != Status.STATUS_OK) {
00118       OpenMSPSynchroManager.getManager().notifyFailure(this.getSyncName());
00119     } else {
00120                         String fileName = PropertiesManager.getManager().getProperty("org.openmobileis.common.log.file");
00121                         File file = new File(fileName);
00122       file.delete();
00123                 }
00124                 this.setSynchroStatus(statusCommande.getStatus());
00125   }
00126 
00127   /*
00128   
00129   * Event sent when the sync manager sends a OpenMSP message to the server
00130   
00131   */
00132   public void sendData(Message message) throws OpenMSPException   {
00133     if (!this.isSyncOK()) {
00134       return; // return if an error occurs during synchro.
00135     }
00136 
00137     if (!syncGoOn) { // synchronize data only for the first call.
00138     return;
00139     }
00140     syncGoOn = false;
00141     try {
00142       //get all log files; 
00143    //   LogManager.traceDebug(0, "LogsSynchroListener sendDatadeb");
00144       if (LogManager.getInstance() instanceof FileOpenCloseLogManager)  { 
00145     //          LogManager.traceDebug(0, "LogsSynchroListener in FileOpenCloseLogManager");
00146         Command syncCommand = new Command(Element.SYNC, LogsSynchroListener.LOG_LISTENER_NAME, "FWKSyncLogsManager");
00147         ContainerMessage syncContainer = new ContainerMessage(syncCommand);
00148         Item data = null;
00149         // manage new mail
00150         RequestCommand addCommand = null;
00151         addCommand = new RequestCommand(Element.ADD);
00152         ContainerMessage addMailContainer = new ContainerMessage(addCommand);
00153         File logfile = ((FileOpenCloseLogManager)LogManager.getInstance()).getLogFile();
00154         if (logfile.exists())   {
00155                 //test file size 
00156                 if (this.maxFileSize >0)        {
00157                         if (logfile.length() > this.maxFileSize)        {
00158                                 logfile.delete();
00159                                 LogManager.traceWarning(0, "LogsSynchroListener LOG FILE TOO BIG DELETED.");
00160                                 return;
00161                         }
00162                 }
00163                 byte[] fileData = GeneralCoder.encodeBase64(FileUtilities.readFile(logfile.getAbsolutePath()));
00164                 data = new DataItem(Element.ITEM, logfile.getName(), new String(fileData), null, null);
00165                 addMailContainer.add(data);
00166                 syncContainer.add(addMailContainer);
00167                 message.add(syncContainer);
00168         }
00169       }
00170     } catch (Throwable ex)  {
00171       this.setSynchroStatus(Status.STATUS_FAILED);
00172       LogManager.traceError(0, ex);
00173     }
00174  }
00175 
00181   public void receiveSyncCommand( ContainerMessage syncContainer, long newSyncNumber)  throws OpenMSPException  {
00182     
00183   }
00184 
00190   public void receiveMapCommand( ContainerMessage mapContainer)   throws OpenMSPException {
00191     
00192   }
00193 
00200   public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand)  throws OpenMSPException  {
00201     
00202   }
00203   
00204   public void receiveGetCommand( Credential cred, ContainerMessage replaceContainer, long newSyncNumber)  throws OpenMSPException       {
00205         }
00206 
00207   /*
00208   
00209    * Return the name used to identiy the receiver of the syncML messages (source and target)
00210   
00211    */
00212 
00213   public String getSyncName() {
00214     return LogsSynchroListener.LOG_LISTENER_NAME;
00215   }
00216 
00217   /*
00218   
00219   * Fire this event once a synchro is starting
00220   
00221   */
00222 
00223   public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00224     this.syncGoOn = true;
00225     this.setSynchroStatus(Status.STATUS_OK);
00226     JournalManager.getManager().deleteAllJournalEntryForService(this.getSyncName());
00227   }
00228 
00229   /*
00230   
00231   * Fire this event once the current synchro is ended
00232   
00233   */
00234 
00235   public void endSync() {
00236     String status  = Integer.toString(errorStatus);
00237     PersistentPropertiesManager.getManager().saveProperty(this.getSyncName(), "synchroStatus", status);
00238    // log journal only if a synchro has been processed
00239    if (this.isSyncOK()) {
00240        JournalManager.getManager().saveJournalEntry(this.getSyncName(), JournalEntry.OK, JournalManager.OK_MESSAGE, Status.STATUS_OK);
00241    }
00242   }
00243 
00247   public int getErrorStatus() {
00248     return errorStatus;
00249   }
00250 
00251 }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4