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.common.util.log.LogManager;
00035 import org.openmobileis.synchro.client.SynchroDescriptor;
00036 import org.openmobileis.synchro.journal.*;
00037 
00047 public abstract class DefaultOpenMSPSyncListener implements OpenMSPSyncListener {
00048   protected static String ADD_ERROR_MESSAGE;
00049   protected static String UPDATE_ERROR_MESSAGE;
00050   protected static String DELETE_ERROR_MESSAGE;
00051   protected static String ITEM_ERROR_MESSAGE;
00052   protected static String SYNC_ERROR_MESSAGE;
00053   protected static String SYNC_UNAUTHORIZED_MESSAGE;
00054 
00055  // private boolean beginned;
00056 
00060   private int synchroStatus;
00061 
00062   public boolean isSyncOK() {
00063     return (synchroStatus < Status.STATUS_WRONG_FORMAT);
00064   }
00065 
00066   public DefaultOpenMSPSyncListener() {
00067   }
00068   
00069         public void initListener() throws OpenMSPException      {
00070     IntlResourceManager resourceManager = IntlResourceManager.getManager();
00071     SYNC_UNAUTHORIZED_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.SYNC_UNAUTHORIZED_MESSAGE", "Erreur acc&egrave;s non authoris&eacute;, mauvaise authentification.");
00072     SYNC_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.SYNC_ERROR_MESSAGE", "Erreur g&eacute;n&eacute;rale de la synchronisation sur le serveur distant.");
00073     ITEM_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.ITEM_ERROR_MESSAGE", "Erreur pendant la r&eacute;cup&eacute;ration des données du serveur distant.");
00074     DELETE_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.DELETE_ERROR_MESSAGE", "Erreur pendant la suppression d'une donn&eacute;e sur le serveur distant.");
00075     UPDATE_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.UPDATE_ERROR_MESSAGE", "Erreur pendant la mise à jour d'une donn&eacute;e sur le serveur distant.");
00076     ADD_ERROR_MESSAGE = resourceManager.getLocalizedProperty("OpenMSPDbSyncListener.ADD_ERROR_MESSAGE", "Erreur pendant l'ajout d'une donn&eacute;e sur le serveur distant.");
00077     String status = PersistentPropertiesManager.getManager().getProperty(this.getSyncName(), "synchroStatus");
00078     if (status == null) {
00079       synchroStatus = Status.STATUS_OK;
00080     }  else {
00081       synchroStatus = Integer.parseInt(status);
00082     }
00083     JournalLogRenderer renderer = this.getJournalLogRenderer();
00084     try {
00085         if (renderer != null)  JournalManager.getManager().registerJournalLogRenderer(renderer);
00086     } catch(Throwable ex)       {
00087         LogManager.traceError(0, "Journal Manager init error :"+ex.toString());
00088         LogManager.traceError(0, ex);
00089     }
00090   }
00091 
00095   public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00096    synchroStatus = Status.STATUS_OK;
00097    JournalManager.getManager().deleteAllJournalEntryForService(this.getSyncName());
00098   }
00099 
00100   public void endSync()  throws OpenMSPException {
00101     String status  = Integer.toString(synchroStatus);
00102     PersistentPropertiesManager.getManager().saveProperty(this.getSyncName(), "synchroStatus", status);
00103    // log journal only if a synchro has been processed
00104    if (this.isSyncOK()) {
00105        JournalManager.getManager().saveJournalEntry(this.getSyncName(), JournalEntry.OK, JournalManager.OK_MESSAGE, Status.STATUS_OK);
00106    }
00107  }
00108 
00113   public int getSynchroStatus() {
00114     return synchroStatus;
00115   }
00116 
00117   public void notifySynchroFailure() {
00118     this.setSynchroStatus(Status.STATUS_FAILED);
00119     JournalManager.getManager().saveJournalEntry(
00120 
00121       this.getSyncName()
00122 
00123       ,JournalEntry.ERROR_SYNC
00124 
00125       ,"Failure"
00126 
00127       , Status.STATUS_FAILED
00128 
00129     );
00130   }
00131   
00132   public boolean equals(Object obj) {
00133     if (
00134       (obj != null)
00135       && (obj instanceof OpenMSPSyncListener)
00136       && ((OpenMSPSyncListener)obj).getSyncName().equals(this.getSyncName())
00137     ) {
00138       return true;
00139     }
00140     return false;
00141   }
00142 
00143   public int hashCode() {
00144     return this.getSyncName().hashCode();
00145   }
00151   public void receiveSyncCommand( ContainerMessage syncContainer, long newSyncNumber)  throws OpenMSPException  {
00152     Command sync = (Command)syncContainer.getElement();
00153     Credential cred = CredentialCodec.getOpenMSPCredential(sync.getCrendential());
00154     while (syncContainer.hasMoreMessage())  {
00155       ContainerMessage commandContainer = syncContainer.nextMessage();
00156       Element command = commandContainer.getElement();
00157       if (command.getElementType() == Element.ADD) {
00158         this.receiveAddCommand( cred, commandContainer, newSyncNumber);
00159       } else if (command.getElementType() == Element.DELETE) {
00160         this.receiveDeleteCommand( cred, commandContainer, newSyncNumber);
00161       } else if (command.getElementType() == Element.REPLACE) {
00162         this.receiveReplaceCommand( cred, commandContainer, newSyncNumber);
00163       } else if (command.getElementType() == Element.ITEM) {
00164         this.receiveSyncItem( cred, (Item)command, newSyncNumber);
00165       }
00166     }
00167  }
00168 
00172   protected static void addCredentialToCommand(Credential cred, RequestCommand command) {
00173     if (cred == null) return;
00174     String currentCredential = cred.getPrincipal()+":"+cred.getPassword();
00175     try {
00176       command.setCredential("<Type>OpenMSP:auth-basic</Type><Format>b64</Format>", new String(GeneralCoder.encodeBase64(currentCredential.getBytes())));
00177     } catch (Throwable e) {
00178     }
00179 
00180   }
00181 
00185   protected void setSynchroStatus(int status)  {
00186     if (status > synchroStatus) {
00187       synchroStatus = status;
00188       if (status >= 300){  //listener in error
00189         OpenMSPSynchroManager.getManager().notifyFailure(this.getSyncName());
00190       }
00191     }
00192   }
00193   
00194   protected JournalLogRenderer getJournalLogRenderer()  {
00195     return null;
00196   }
00197 
00198 
00205   protected void receiveAddCommand( Credential cred,ContainerMessage addContainer, long newSyncNumber)  throws OpenMSPException {
00206   }
00207 
00214   protected void receiveSyncItem( Credential cred, Item item , long newSyncNumber)  throws OpenMSPException {
00215   }
00216 
00223   protected void receiveDeleteCommand( Credential cred, ContainerMessage deleteContainer, long newSyncNumber)  throws OpenMSPException {
00224   }
00225 
00232   protected void receiveReplaceCommand( Credential cred, ContainerMessage replaceContainer, long newSyncNumber)  throws OpenMSPException {
00233   }
00234 
00241   public void receiveGetCommand( Credential cred, ContainerMessage replaceContainer, long newSyncNumber)  throws OpenMSPException {
00242   }
00243   
00249   public void receiveMapCommand( ContainerMessage mapContainer)   throws OpenMSPException   {
00250   }
00251 
00258   public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand)  throws OpenMSPException  {
00259   }
00260 }

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