TestOpenMSPSyncListener.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 package org.openmobileis.synchro.openmsp.protocol.test;
00026 
00027 import org.openmobileis.common.util.codec.GeneralCoder;
00028 import org.openmobileis.common.util.exception.SynchroException;
00029 import org.openmobileis.common.util.log.LogManager;
00030 import org.openmobileis.synchro.client.SynchroDescriptor;
00031 import org.openmobileis.synchro.journal.JournalLogRenderer;
00032 import org.openmobileis.synchro.journal.SimpleLogRenderer;
00033 import org.openmobileis.synchro.openmsp.OpenMSPException;
00034 import org.openmobileis.synchro.openmsp.client.DefaultOpenMSPSyncListener;
00035 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00036 import org.openmobileis.synchro.openmsp.protocol.Command;
00037 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00038 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00039 import org.openmobileis.synchro.openmsp.protocol.Element;
00040 import org.openmobileis.synchro.openmsp.protocol.Item;
00041 import org.openmobileis.synchro.openmsp.protocol.Message;
00042 import org.openmobileis.synchro.openmsp.protocol.RequestCommand;
00043 import org.openmobileis.synchro.openmsp.protocol.Result;
00044 import org.openmobileis.synchro.openmsp.protocol.Status;
00045 import org.openmobileis.synchro.security.auth.Credential;
00046 
00054 public final class TestOpenMSPSyncListener extends DefaultOpenMSPSyncListener {
00055   private boolean syncGoOn = false;
00056   private Status resultStatus = null;
00057 
00061   public TestOpenMSPSyncListener() {
00062     super();
00063   }
00064   
00065   public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException   {
00066     super.startSync(cred, synchrodescriptor);
00067     syncGoOn = true;
00068   }
00069 
00070   /* (non-Javadoc)
00071    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#sendData(org.openmobileis.synchro.openmsp.protocol.Message)
00072    */
00073   public void sendData(Message message) throws OpenMSPException {
00074         LogManager.traceDebug(0, "TestOpenMSPSyncListener sendData BEGIN");
00075     if (resultStatus != null) {
00076       message.add(new ContainerMessage(resultStatus));
00077       resultStatus = null;
00078     }
00079     if (!syncGoOn) { // synchronize data only for the first call.
00080         LogManager.traceDebug(0, "TestOpenMSPSyncListener sendData NO MESSAGE TO SEND");
00081       return;
00082     }
00083     try {
00084         LogManager.traceDebug(0, "TestOpenMSPSyncListener sendData CONTRUCT MESSAGE");
00085       Command syncCommand = new Command(Element.SYNC, this.getSyncName(), "testtarget");
00086       long ns = NumSyncManagerDB.getManager().getSyncNumberForService(this.getSyncName());
00087       syncCommand.setSourceSessionID(ns);
00088       String commandMetadata = "testmetedata";
00089       if (commandMetadata != null)  {
00090         syncCommand.setMetaInformation(commandMetadata);
00091       }
00092       Credential cred = new Credential("test", "test");
00093       DefaultOpenMSPSyncListener.addCredentialToCommand(cred, syncCommand);
00094       ContainerMessage syncContainer = new ContainerMessage(syncCommand);
00095 
00096       // add ADD COMMANDE
00097       RequestCommand newCommand = new RequestCommand(Element.ADD);
00098       newCommand.setMetaInformation("ADD META INFO");
00099       ContainerMessage newContainer = new ContainerMessage(newCommand);
00100       Item newItem = new DataItem(
00101                   Element.ITEM
00102                   , new String(GeneralCoder.encodeBase64("AOUID:111".getBytes()))
00103                   , new String(GeneralCoder.encodeBase64("ADD COMMANDE CONTENT".getBytes()))
00104                   , null
00105                   , null
00106               );
00107       newContainer.add(newItem);
00108       syncContainer.add(newContainer);
00109       
00110       //add REPLACE COMMANDE
00111       newCommand = new RequestCommand(Element.REPLACE);
00112       newCommand.setMetaInformation("REPLACE META INFO");
00113       newContainer = new ContainerMessage(newCommand);
00114       newItem = new DataItem(
00115                   Element.ITEM
00116                   , new String(GeneralCoder.encodeBase64(("AOUID:222").getBytes()))
00117                   , new String(GeneralCoder.encodeBase64("REPLACE COMMANDE CONTENT".getBytes()))
00118                   , null
00119                   , null
00120               );
00121       newContainer.add(newItem);
00122       syncContainer.add(newContainer);
00123 
00124       //add DELETE COMMANDE
00125       newCommand = new RequestCommand(Element.DELETE);
00126       newContainer = new ContainerMessage(newCommand);
00127       newCommand.setMetaInformation("DELETE META INFO");
00128       newItem = new DataItem(
00129                   Element.ITEM
00130                   , new String(GeneralCoder.encodeBase64(("AOUID:3333").getBytes()))
00131                   , ""
00132                   , null
00133                   , null
00134               );
00135       newContainer.add(newItem);
00136       syncContainer.add(newContainer);
00137       
00138       // add sync ITEM 
00139       newItem = new Item(
00140                 Element.ITEM
00141                 , null
00142                 , null
00143               );
00144       syncContainer.add(newItem);
00145       message.add(syncContainer);
00146       
00147       //add GET COMMANDE
00148       Command getCommand = new Command(Element.GET, this.getSyncName(), "testtarget");
00149       getCommand.setSourceSessionID(ns);
00150       DefaultOpenMSPSyncListener.addCredentialToCommand(cred, getCommand);
00151       getCommand.setMetaInformation("GET META INFO");
00152       ContainerMessage getContainer = new ContainerMessage(getCommand);
00153       newItem = new DataItem(
00154                   Element.ITEM
00155                   , new String("AOUID:GET")
00156                   , new String("GET COMMANDE CONTENT")
00157                   , null
00158                   , null
00159               );
00160       getContainer.add(newItem);
00161       message.add(getContainer);
00162       
00163     } catch (Throwable ex)  {
00164       this.setSynchroStatus(Status.STATUS_FAILED);
00165       LogManager.trace(new SynchroException("DefaultDbSyncListener "+this.getSyncName()+" Send data exception ", ex));
00166     }
00167     syncGoOn = false;
00168         LogManager.traceDebug(0, "TestOpenMSPSyncListener sendData END");
00169 
00170   }
00171 
00172   /* (non-Javadoc)
00173    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#getSyncName()
00174    */
00175   public String getSyncName() {
00176     return "testlistener";
00177   }
00178 
00179   /* (non-Javadoc)
00180    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveMapCommand(org.openmobileis.synchro.openmsp.protocol.ContainerMessage, long)
00181    */
00182   public void receiveMapCommand(ContainerMessage mapContainer) throws OpenMSPException {
00183      LogManager.traceInfo(0, "Receive Map command :");
00184   }
00185 
00186   /* (non-Javadoc)
00187    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveResultCommand(org.openmobileis.synchro.openmsp.protocol.Result, long)
00188    */
00189   public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand)  throws OpenMSPException  {
00190     Result result = (Result)resultContainer.getElement();
00191     LogManager.traceInfo(0, "TestOpenMSSyncListener receiveResultCommand META DATA :"+result.getMetaInformation());
00192     DataItem item = (DataItem) resultContainer.nextMessage().getElement();
00193     // get meta info
00194     // meta info are of type : AOUID:<PDAUID>contact
00195     try {
00196       String meta = item.getMetaInformation();
00197       String data = item.getData();
00198       LogManager.traceInfo(0, "TestOpenMSSyncListener receiveResultCommand ITEM META DATA :"+meta);
00199       LogManager.traceInfo(0, "TestOpenMSSyncListener receiveResultCommand ITEM DATA :"+data);     
00200       resultStatus =  new Status(result.getCmdId(), Status.STATUS_OK);
00201     } catch (Throwable ex)  {
00202       LogManager.traceError(0, ex);
00203       resultStatus = new Status(result.getCmdId(), Status.STATUS_FAILED);
00204     }
00205   }
00206 
00207   /* (non-Javadoc)
00208    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveStatusCommand(org.openmobileis.synchro.openmsp.protocol.Status, long)
00209    */
00210   public void receiveStatusCommand( Status statusCommande, ContainerMessage initialCommand)  throws OpenMSPException  {
00211     LogManager.traceInfo(0, "TestOpenMSSyncListener receiveStatusCommand status :"+statusCommande.getStatus()+" commandRef:"+statusCommande.getCmdRef());
00212   }
00213   
00214   protected JournalLogRenderer getJournalLogRenderer()  {
00215     return new SimpleLogRenderer("test synchro", "service de test de synchro");
00216   }
00217 
00218 }

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