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

DefaultOpenMSPSyncListener.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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.embedded.util.ServicePropertiesManager;
00032 import org.openmobileis.common.util.codec.GeneralCoder;
00033 import org.openmobileis.synchro.client.SynchroDescriptor;
00034 import org.openmobileis.synchro.journal.*;
00035 
00045 public abstract class DefaultOpenMSPSyncListener implements OpenMSPSyncListener {
00046 
00047  // private boolean beginned;
00048 
00052   private int synchroStatus;
00053 
00054   public boolean isSyncOK() {
00055     return (synchroStatus == Status.STATUS_OK);
00056   }
00057 
00058   public DefaultOpenMSPSyncListener() {
00059     String status = ServicePropertiesManager.getManager().getProperty(this.getSyncName(), "synchroStatus");
00060     if (status == null) {
00061       synchroStatus = Status.STATUS_OK;
00062     }  else {
00063       synchroStatus = Integer.parseInt(status);
00064     }
00065     JournalLogRenderer renderer = this.getJournalLogRenderer();
00066     if (renderer != null)  JournalManager.getManager().registerJournalLogRenderer(renderer);
00067   }
00068 
00072   public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00073    synchroStatus = Status.STATUS_OK;
00074    JournalManager.getManager().deleteAllJournalEntryForService(this.getSyncName());
00075   }
00076 
00077   public void endSync()  throws OpenMSPException {
00078     String status  = Integer.toString(synchroStatus);
00079     ServicePropertiesManager.getManager().saveProperty(this.getSyncName(), "synchroStatus", status);
00080    // log journal only if a synchro has been processed
00081    if (this.isSyncOK()) {
00082        JournalManager.getManager().saveJournalEntry(this.getSyncName(), JournalEntry.ERROR_SYNC, JournalManager.OK_MESSAGE, Status.STATUS_OK);
00083    }
00084  }
00085 
00090   public int getSynchroStatus() {
00091     return synchroStatus;
00092   }
00093 
00094   public void notifySynchroFailure() {
00095     this.setSynchroStatus(Status.STATUS_FAILED);
00096     JournalManager.getManager().saveJournalEntry(
00097 
00098       this.getSyncName()
00099 
00100       ,JournalEntry.ERROR_SYNC
00101 
00102       ,"Failure"
00103 
00104       , Status.STATUS_FAILED
00105 
00106     );
00107   }
00108   
00109   public boolean equals(Object obj) {
00110     if (
00111       (obj != null)
00112       && (obj instanceof OpenMSPSyncListener)
00113       && ((OpenMSPSyncListener)obj).getSyncName().equals(this.getSyncName())
00114     ) {
00115       return true;
00116     }
00117     return false;
00118   }
00119 
00120   public int hashCode() {
00121     return this.getSyncName().hashCode();
00122   }
00128   public void receiveSyncCommand( ContainerMessage syncContainer, long newSyncNumber)  throws OpenMSPException  {
00129     Command sync = (Command)syncContainer.getElement();
00130     Credential cred = CredentialCodec.getOpenMSPCredential(sync.getCrendential());
00131     while (syncContainer.hasMoreMessage())  {
00132       ContainerMessage commandContainer = syncContainer.nextMessage();
00133       Element command = commandContainer.getElement();
00134       if (command.getElementType() == Element.ADD) {
00135         this.receiveAddCommand( cred, commandContainer, newSyncNumber);
00136       } else if (command.getElementType() == Element.DELETE) {
00137         this.receiveDeleteCommand( cred, commandContainer, newSyncNumber);
00138       } else if (command.getElementType() == Element.REPLACE) {
00139         this.receiveReplaceCommand( cred, commandContainer, newSyncNumber);
00140       } else if (command.getElementType() == Element.GET) {
00141         this.receiveGetCommand( cred, commandContainer, newSyncNumber);
00142       } else if (command.getElementType() == Element.ITEM) {
00143         this.receiveSyncItem( cred, (Item)command, newSyncNumber);
00144       }
00145     }
00146  }
00147 
00151   protected static void addCredentialToCommand(Credential cred, RequestCommand command) {
00152     if (cred == null) return;
00153     String currentCredential = cred.getPrincipal()+":"+cred.getPassword();
00154     try {
00155       command.setCredential("<Type>OpenMSP:auth-basic</Type><Format>b64</Format>", new String(GeneralCoder.encodeBase64(currentCredential.getBytes())));
00156     } catch (Throwable e) {
00157     }
00158 
00159   }
00160 
00164   protected void setSynchroStatus(int status)  {
00165     if (status > synchroStatus) {
00166       synchroStatus = status;
00167       if (status >= 300){  //listener in error
00168         OpenMSPSynchroManager.getManager().notifyFailure(this.getSyncName());
00169       }
00170     }
00171   }
00172   
00173   protected JournalLogRenderer getJournalLogRenderer()  {
00174     return null;
00175   }
00176 
00177 
00184   protected void receiveAddCommand( Credential cred,ContainerMessage addContainer, long newSyncNumber)  throws OpenMSPException {
00185   }
00186 
00193   protected void receiveSyncItem( Credential cred, Item item , long newSyncNumber)  throws OpenMSPException {
00194   }
00195 
00202   protected void receiveDeleteCommand( Credential cred, ContainerMessage deleteContainer, long newSyncNumber)  throws OpenMSPException {
00203   }
00204 
00211   protected void receiveReplaceCommand( Credential cred, ContainerMessage replaceContainer, long newSyncNumber)  throws OpenMSPException {
00212   }
00213 
00220   protected void receiveGetCommand( Credential cred, ContainerMessage replaceContainer, long newSyncNumber)  throws OpenMSPException {
00221   }
00222 }

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