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 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
00071
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) {
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
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
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
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
00136 newItem = new Item(
00137 Element.ITEM
00138 , null
00139 , null
00140 );
00141 syncContainer.add(newItem);
00142 message.add(syncContainer);
00143
00144
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
00169
00170
00171 public String getSyncName() {
00172 return "testlistener";
00173 }
00174
00175
00176
00177
00178 public void receiveMapCommand(ContainerMessage mapContainer) throws OpenMSPException {
00179 LogManager.traceInfo(0, "Receive Map command :");
00180 }
00181
00182
00183
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
00190
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
00204
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 }