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.server.synctarget;
00026
00027 import org.openmobileis.common.util.collection.Array;
00028 import org.openmobileis.common.util.exception.ServiceException;
00029 import org.openmobileis.synchro.openmsp.OpenMSPException;
00030 import org.openmobileis.synchro.openmsp.protocol.AbstractCommand;
00031 import org.openmobileis.synchro.openmsp.protocol.Command;
00032 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00033 import org.openmobileis.synchro.openmsp.protocol.Element;
00034 import org.openmobileis.synchro.openmsp.protocol.Result;
00035 import org.openmobileis.synchro.openmsp.protocol.Status;
00036 import org.openmobileis.synchro.openmsp.server.util.OpenMISFile;
00037 import org.openmobileis.synchro.security.auth.Credential;
00038 import org.openmobileis.synchro.security.auth.CredentialCodec;
00039
00047 public abstract class DefaultOpenMSPSynchroTargetListener implements OpenMSPSynchroTargetListener {
00048 private Array addFileRepository;
00052 public DefaultOpenMSPSynchroTargetListener() {
00053 super();
00054 }
00055
00056 public SyncTargetAnswer processCommand(Credential cred,ContainerMessage containerMessage) throws OpenMSPException {
00057 AbstractCommand command = (AbstractCommand) containerMessage.getElement();
00058 Array returnList = new Array(5);
00059 try {
00060 beginProcessOpenMSpCommand(cred);
00061 try {
00062 Object returnMessage = null;
00063 switch (command.getElementType()) {
00064 case Element.MAP :
00065 containerMessage.resetCursor();
00066 returnMessage = this.processMapCommand(cred, containerMessage);
00067 break;
00068 case Element.RESULT :
00069 this.processResultCommand(cred, (Result)command);
00070 break;
00071 case Element.STATUS :
00072 this.processStatusCommand(cred, (Status)command);
00073 break;
00074 case Element.SYNC :
00075 long sessionID = ((Command) command).getSourceSessionID();
00076 containerMessage.resetCursor();
00077 returnMessage = this.processSyncCommand(sessionID, containerMessage);
00078 break;
00079 case Element.GET :
00080 containerMessage.resetCursor();
00081 returnMessage = this.processGetCommand(cred, containerMessage);
00082 break;
00083 default:
00084 break;
00085 }
00086 if (returnMessage != null) {
00087 if (returnMessage instanceof ContainerMessage[]) {
00088 ContainerMessage[] tempCont = (ContainerMessage[])returnMessage;
00089 for (int i=0; i<tempCont.length ;i++) {
00090 returnList.add(tempCont[i]);
00091 }
00092 } else {
00093 returnList.add(returnMessage);
00094 }
00095 }
00096 } finally {
00097 ContainerMessage[] endMess = endProcessOpenMSpCommand();
00098 if (endMess != null) {
00099 for (int i=0; i<endMess.length ;i++) {
00100 returnList.add(endMess[i]);
00101 }
00102 }
00103 }
00104 } catch (Throwable ex) {
00105 Status status = new Status(command.getCmdId(), Status.STATUS_FAILED);
00106 ContainerMessage statusContainer = new ContainerMessage(status);
00107 returnList.add(statusContainer);
00108 }
00109 SyncTargetAnswer answer = new SyncTargetAnswer();
00110 if (addFileRepository != null) {
00111 OpenMISFile[] addCyberFiles = new OpenMISFile[addFileRepository.size()];
00112 addFileRepository.toArray(addCyberFiles);
00113 answer.answerfiles = addCyberFiles;
00114 }
00115 if (returnList.size() != 0) {
00116 ContainerMessage[] returnContainer = new ContainerMessage[returnList.size()];
00117 returnList.toArray(returnContainer);
00118 answer.containerMessage = returnContainer;
00119 }
00120 return answer;
00121 }
00122
00123 protected ContainerMessage[] processSyncCommand(long sessionId, ContainerMessage syncCommande) throws OpenMSPException {
00124
00125 Command sync = (Command)syncCommande.getElement();
00126 Credential cred = CredentialCodec.getOpenMSPCredential(sync.getCrendential());
00127 Array returnList = new Array(10);
00128 this.notifySyncAction(sessionId, cred, sync);
00129 while (syncCommande.hasMoreMessage()) {
00130 ContainerMessage commandContainer = syncCommande.nextMessage();
00131 Element command = commandContainer.getElement();
00132 if (command.getElementType() == Element.ADD) {
00133 Status status = this.processAddCommand(cred, commandContainer);
00134 if (status != null) {
00135 returnList.add(new ContainerMessage(status));
00136 }
00137 } else if (command.getElementType() == Element.DELETE) {
00138 Status status = this.processDeleteCommand(cred, commandContainer);
00139 if (status != null) {
00140 returnList.add(new ContainerMessage(status));
00141 }
00142 } else if (command.getElementType() == Element.REPLACE) {
00143 Status status = this.processReplaceCommand(cred, commandContainer);
00144 if (status != null) {
00145 returnList.add(new ContainerMessage(status));
00146 }
00147 }
00148 }
00149 ContainerMessage[] ret = null;
00150 if (returnList.size() != 0) {
00151 ret = new ContainerMessage[returnList.size()];
00152 returnList.toArray(ret);
00153 }
00154 return ret;
00155 }
00156
00162 protected void addFileToSynchro(OpenMISFile file) throws ServiceException {
00163 if (addFileRepository == null) {
00164 addFileRepository = new Array(15);
00165 }
00166 addFileRepository.add(file);
00167 }
00168
00169 public abstract String getTargetName();
00170
00171 protected abstract ContainerMessage processMapCommand(Credential cred,ContainerMessage mapContainer) throws OpenMSPException;
00172
00173 protected abstract void notifySyncAction(long sessionId,Credential cred, Command syncCommand) throws OpenMSPException;
00174
00175 protected abstract void processResultCommand(Credential cred,Result resultCommande) throws OpenMSPException;
00176
00177 protected abstract void processStatusCommand(Credential cred,Status statusCommande) throws OpenMSPException;
00178
00179 protected abstract Status processAddCommand(Credential cred, ContainerMessage addContainer) throws OpenMSPException;
00180
00181 protected abstract Status processDeleteCommand(Credential cred, ContainerMessage deleteContainer) throws OpenMSPException;
00182
00183 protected abstract Status processReplaceCommand(Credential cred, ContainerMessage replaceContainer) throws OpenMSPException;
00184
00185 protected abstract void beginProcessOpenMSpCommand(Credential cred) throws OpenMSPException;
00186
00187 protected abstract ContainerMessage[] endProcessOpenMSpCommand() throws OpenMSPException;
00188
00189 protected abstract ContainerMessage[] processGetCommand(Credential cred, ContainerMessage getCommande) throws OpenMSPException;
00190 }