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

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