00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00071
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) {
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
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
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
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
00139 newItem = new Item(
00140 Element.ITEM
00141 , null
00142 , null
00143 );
00144 syncContainer.add(newItem);
00145 message.add(syncContainer);
00146
00147
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
00173
00174
00175 public String getSyncName() {
00176 return "testlistener";
00177 }
00178
00179
00180
00181
00182 public void receiveMapCommand(ContainerMessage mapContainer) throws OpenMSPException {
00183 LogManager.traceInfo(0, "Receive Map command :");
00184 }
00185
00186
00187
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
00194
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
00208
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 }