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