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 newCommand = new RequestCommand(Element.GET);
00137 newCommand.setMetaInformation("GET META INFO");
00138 newContainer = new ContainerMessage(newCommand);
00139 newItem = new DataItem(
00140 Element.ITEM
00141 , new String("AOUID:GET")
00142 , new String("GET COMMANDE CONTENT")
00143 , null
00144 , null
00145 );
00146 newContainer.add(newItem);
00147 syncContainer.add(newContainer);
00148
00149
00150 newItem = new Item(
00151 Element.ITEM
00152 , null
00153 , null
00154 );
00155 syncContainer.add(newItem);
00156 message.add(syncContainer);
00157
00158 } catch (Throwable ex) {
00159 this.setSynchroStatus(Status.STATUS_FAILED);
00160 LogManager.trace(new SynchroException("DefaultDbSyncListener "+this.getSyncName()+" Send data exception ", ex));
00161 }
00162 syncGoOn = false;
00163
00164 }
00165
00166
00167
00168
00169 public String getSyncName() {
00170 return "testlistener";
00171 }
00172
00173
00174
00175
00176 public void receiveMapCommand(ContainerMessage mapContainer) throws OpenMSPException {
00177 LogManager.traceInfo(0, "Receive Map command :");
00178 }
00179
00180
00181
00182
00183 public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00184 Result result = (Result)resultContainer.getElement();
00185 LogManager.traceInfo(0, "TestOpenMSSyncListener receiveResultCommand META DATA :"+result.getMetaInformation());
00186 DataItem item = (DataItem) resultContainer.nextMessage().getElement();
00187
00188
00189 try {
00190 String meta = item.getMetaInformation();
00191 String data = item.getData();
00192 LogManager.traceInfo(0, "TestOpenMSSyncListener receiveResultCommand ITEM META DATA :"+meta);
00193 LogManager.traceInfo(0, "TestOpenMSSyncListener receiveResultCommand ITEM DATA :"+data);
00194 resultStatus = new Status(result.getCmdId(), Status.STATUS_OK);
00195 } catch (Throwable ex) {
00196 LogManager.traceError(0, ex);
00197 resultStatus = new Status(result.getCmdId(), Status.STATUS_FAILED);
00198 }
00199 }
00200
00201
00202
00203
00204 public void receiveStatusCommand( Status statusCommande, ContainerMessage initialCommand) throws OpenMSPException {
00205 LogManager.traceInfo(0, "TestOpenMSSyncListener receiveStatusCommand status :"+statusCommande.getStatus()+" commandRef:"+statusCommande.getCmdRef());
00206 }
00207
00208 protected JournalLogRenderer getJournalLogRenderer() {
00209 return new SimpleLogRenderer("test synchro", "service de test de synchro");
00210 }
00211
00212 }