DefaultOpenMSPSyncListener.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  */
00025 
00026 package org.openmobileis.synchro.openmsp.client;
00027 
00028 import org.openmobileis.synchro.openmsp.OpenMSPException;
00029 import org.openmobileis.synchro.openmsp.protocol.*;
00030 import org.openmobileis.synchro.security.auth.*;
00031 import org.openmobileis.common.intl.IntlResourceManager;
00032 import org.openmobileis.common.util.PersistentPropertiesManager;
00033 import org.openmobileis.common.util.codec.GeneralCoder;
00034 import org.openmobileis.synchro.client.SynchroDescriptor;
00035 import org.openmobileis.synchro.journal.*;
00036 
00046 public abstract class DefaultOpenMSPSyncListener implements OpenMSPSyncListener {
00047   protected static String ADD_ERROR_MESSAGE;
00048   protected static String UPDATE_ERROR_MESSAGE;
00049   protected static String DELETE_ERROR_MESSAGE;
00050   protected static String ITEM_ERROR_MESSAGE;
00051   protected static String SYNC_ERROR_MESSAGE;
00052   protected static String SYNC_UNAUTHORIZED_MESSAGE;
00053 
00054  // private boolean beginned;
00055 
00059   private int synchroStatus;
00060 
00061   public boolean isSyncOK() {
00062     return (synchroStatus == Status.STATUS_OK);
00063   }
00064 
00065   public DefaultOpenMSPSyncListener() {
00066   }
00067   
00068         public void initListener() throws OpenMSPException      {
00069     IntlResourceManager resourceManager = IntlResourceManager.getManager();
00070     SYNC_UNAUTHORIZED_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.SYNC_UNAUTHORIZED_MESSAGE", "Erreur accès non authorisé, mauvaise authentification.");
00071     SYNC_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.SYNC_ERROR_MESSAGE", "Erreur générale de la synchronisation sur le serveur distant.");
00072     ITEM_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.ITEM_ERROR_MESSAGE", "Erreur pendant la récupération des données du serveur distant.");
00073     DELETE_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.DELETE_ERROR_MESSAGE", "Erreur pendant la suppression d'une donnée sur le serveur distant.");
00074     UPDATE_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.UPDATE_ERROR_MESSAGE", "Erreur pendant la mise à jour d'une donnée sur le serveur distant.");
00075     ADD_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.ADD_ERROR_MESSAGE", "Erreur pendant l'ajout d'une donnée sur le serveur distant.");
00076     String status = PersistentPropertiesManager.getManager().getProperty(this.getSyncName(), "synchroStatus");
00077     if (status == null) {
00078       synchroStatus = Status.STATUS_OK;
00079     }  else {
00080       synchroStatus = Integer.parseInt(status);
00081     }
00082     JournalLogRenderer renderer = this.getJournalLogRenderer();
00083     if (renderer != null)  JournalManager.getManager().registerJournalLogRenderer(renderer);
00084   }
00085 
00089   public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00090    synchroStatus = Status.STATUS_OK;
00091    JournalManager.getManager().deleteAllJournalEntryForService(this.getSyncName());
00092   }
00093 
00094   public void endSync()  throws OpenMSPException {
00095     String status  = Integer.toString(synchroStatus);
00096     PersistentPropertiesManager.getManager().saveProperty(this.getSyncName(), "synchroStatus", status);
00097    // log journal only if a synchro has been processed
00098    if (this.isSyncOK()) {
00099        JournalManager.getManager().saveJournalEntry(this.getSyncName(), JournalEntry.ERROR_SYNC, JournalManager.OK_MESSAGE, Status.STATUS_OK);
00100    }
00101  }
00102 
00107   public int getSynchroStatus() {
00108     return synchroStatus;
00109   }
00110 
00111   public void notifySynchroFailure() {
00112     this.setSynchroStatus(Status.STATUS_FAILED);
00113     JournalManager.getManager().saveJournalEntry(
00114 
00115       this.getSyncName()
00116 
00117       ,JournalEntry.ERROR_SYNC
00118 
00119       ,"Failure"
00120 
00121       , Status.STATUS_FAILED
00122 
00123     );
00124   }
00125   
00126   public boolean equals(Object obj) {
00127     if (
00128       (obj != null)
00129       && (obj instanceof OpenMSPSyncListener)
00130       && ((OpenMSPSyncListener)obj).getSyncName().equals(this.getSyncName())
00131     ) {
00132       return true;
00133     }
00134     return false;
00135   }
00136 
00137   public int hashCode() {
00138     return this.getSyncName().hashCode();
00139   }
00145   public void receiveSyncCommand( ContainerMessage syncContainer, long newSyncNumber)  throws OpenMSPException  {
00146     Command sync = (Command)syncContainer.getElement();
00147     Credential cred = CredentialCodec.getOpenMSPCredential(sync.getCrendential());
00148     while (syncContainer.hasMoreMessage())  {
00149       ContainerMessage commandContainer = syncContainer.nextMessage();
00150       Element command = commandContainer.getElement();
00151       if (command.getElementType() == Element.ADD) {
00152         this.receiveAddCommand( cred, commandContainer, newSyncNumber);
00153       } else if (command.getElementType() == Element.DELETE) {
00154         this.receiveDeleteCommand( cred, commandContainer, newSyncNumber);
00155       } else if (command.getElementType() == Element.REPLACE) {
00156         this.receiveReplaceCommand( cred, commandContainer, newSyncNumber);
00157       } else if (command.getElementType() == Element.ITEM) {
00158         this.receiveSyncItem( cred, (Item)command, newSyncNumber);
00159       }
00160     }
00161  }
00162 
00166   protected static void addCredentialToCommand(Credential cred, RequestCommand command) {
00167     if (cred == null) return;
00168     String currentCredential = cred.getPrincipal()+":"+cred.getPassword();
00169     try {
00170       command.setCredential("<Type>OpenMSP:auth-basic</Type><Format>b64</Format>", new String(GeneralCoder.encodeBase64(currentCredential.getBytes())));
00171     } catch (Throwable e) {
00172     }
00173 
00174   }
00175 
00179   protected void setSynchroStatus(int status)  {
00180     if (status > synchroStatus) {
00181       synchroStatus = status;
00182       if (status >= 300){  //listener in error
00183         OpenMSPSynchroManager.getManager().notifyFailure(this.getSyncName());
00184       }
00185     }
00186   }
00187   
00188   protected JournalLogRenderer getJournalLogRenderer()  {
00189     return null;
00190   }
00191 
00192 
00199   protected void receiveAddCommand( Credential cred,ContainerMessage addContainer, long newSyncNumber)  throws OpenMSPException {
00200   }
00201 
00208   protected void receiveSyncItem( Credential cred, Item item , long newSyncNumber)  throws OpenMSPException {
00209   }
00210 
00217   protected void receiveDeleteCommand( Credential cred, ContainerMessage deleteContainer, long newSyncNumber)  throws OpenMSPException {
00218   }
00219 
00226   protected void receiveReplaceCommand( Credential cred, ContainerMessage replaceContainer, long newSyncNumber)  throws OpenMSPException {
00227   }
00228 
00235   public void receiveGetCommand( Credential cred, ContainerMessage replaceContainer, long newSyncNumber)  throws OpenMSPException {
00236   }
00237   
00243   public void receiveMapCommand( ContainerMessage mapContainer)   throws OpenMSPException   {
00244   }
00245 
00252   public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand)  throws OpenMSPException  {
00253   }
00254 }

Generated on Mon Dec 4 11:03:25 2006 for OpenMobileIS by  doxygen 1.5.1-p1