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 default:
00080 break;
00081 }
00082 if (returnMessage != null) {
00083 if (returnMessage instanceof ContainerMessage[]) {
00084 ContainerMessage[] tempCont = (ContainerMessage[])returnMessage;
00085 for (int i=0; i<tempCont.length ;i++) {
00086 returnList.add(tempCont[i]);
00087 }
00088 } else {
00089 returnList.add(returnMessage);
00090 }
00091 }
00092 } finally {
00093 ContainerMessage[] endMess = endProcessOpenMSpCommand();
00094 if (endMess != null) {
00095 for (int i=0; i<endMess.length ;i++) {
00096 returnList.add(endMess[i]);
00097 }
00098 }
00099 }
00100 } catch (Throwable ex) {
00101 Status status = new Status(command.getCmdId(), Status.STATUS_FAILED);
00102 ContainerMessage statusContainer = new ContainerMessage(status);
00103 returnList.add(statusContainer);
00104 }
00105 SyncTargetAnswer answer = new SyncTargetAnswer();
00106 if (addFileRepository != null) {
00107 OpenMISFile[] addCyberFiles = new OpenMISFile[addFileRepository.size()];
00108 addFileRepository.toArray(addCyberFiles);
00109 answer.answerfiles = addCyberFiles;
00110 }
00111 if (returnList.size() != 0) {
00112 ContainerMessage[] returnContainer = new ContainerMessage[returnList.size()];
00113 returnList.toArray(returnContainer);
00114 answer.containerMessage = returnContainer;
00115 }
00116 return answer;
00117 }
00118
00119 protected ContainerMessage[] processSyncCommand(long sessionId, ContainerMessage syncCommande) throws OpenMSPException {
00120
00121 Command sync = (Command)syncCommande.getElement();
00122 Credential cred = CredentialCodec.getOpenMSPCredential(sync.getCrendential());
00123 Array returnList = new Array(10);
00124 this.notifySyncAction(sessionId, cred, sync);
00125 while (syncCommande.hasMoreMessage()) {
00126 ContainerMessage commandContainer = syncCommande.nextMessage();
00127 Element command = commandContainer.getElement();
00128 if (command.getElementType() == Element.ADD) {
00129 Status status = this.processAddCommand(cred, commandContainer);
00130 if (status != null) {
00131 returnList.add(new ContainerMessage(status));
00132 }
00133 } else if (command.getElementType() == Element.DELETE) {
00134 Status status = this.processDeleteCommand(cred, commandContainer);
00135 if (status != null) {
00136 returnList.add(new ContainerMessage(status));
00137 }
00138 } else if (command.getElementType() == Element.REPLACE) {
00139 Status status = this.processReplaceCommand(cred, commandContainer);
00140 if (status != null) {
00141 returnList.add(new ContainerMessage(status));
00142 }
00143 } else if (command.getElementType() == Element.GET) {
00144 ContainerMessage[] container = this.processGetCommand(cred, commandContainer);
00145 if (container != null) {
00146 for (int i=0; i<container.length; i++) {
00147 returnList.add(container[i]);
00148 }
00149 }
00150 }
00151 }
00152 ContainerMessage[] ret = null;
00153 if (returnList.size() != 0) {
00154 ret = new ContainerMessage[returnList.size()];
00155 returnList.toArray(ret);
00156 }
00157 return ret;
00158 }
00159
00165 protected void addFileToSynchro(OpenMISFile file) throws ServiceException {
00166 if (addFileRepository == null) {
00167 addFileRepository = new Array(15);
00168 }
00169 addFileRepository.add(file);
00170 }
00171
00172 public abstract String getTargetName();
00173
00174 protected abstract ContainerMessage processMapCommand(Credential cred,ContainerMessage mapContainer);
00175
00176 protected abstract void notifySyncAction(long sessionId,Credential cred, Command syncCommand) throws OpenMSPException;
00177
00178 protected abstract void processResultCommand(Credential cred,Result resultCommande) throws OpenMSPException;
00179
00180 protected abstract void processStatusCommand(Credential cred,Status statusCommande) throws OpenMSPException;
00181
00182 protected abstract Status processAddCommand(Credential cred, ContainerMessage addContainer) throws OpenMSPException;
00183
00184 protected abstract Status processDeleteCommand(Credential cred, ContainerMessage deleteContainer) throws OpenMSPException;
00185
00186 protected abstract Status processReplaceCommand(Credential cred, ContainerMessage replaceContainer) throws OpenMSPException;
00187
00188 protected abstract void beginProcessOpenMSpCommand(Credential cred) throws OpenMSPException;
00189
00190 protected abstract ContainerMessage[] endProcessOpenMSpCommand() throws OpenMSPException;
00191
00192 protected abstract ContainerMessage[] processGetCommand(Credential cred, ContainerMessage getCommande) throws OpenMSPException;
00193 }