DefaultOpenMSPSynchroTargetListener.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
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  // private Array addFileRepository;
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       Status status = new Status(command.getCmdId(), Status.STATUS_FAILED);
00110       ContainerMessage statusContainer = new ContainerMessage(status);
00111       returnList.add(statusContainer);
00112       LogManager.traceError(0, ex);
00113     }
00114     SyncTargetAnswer answer  = new SyncTargetAnswer();
00115     Array addFileRepository = this.getAddFileRepository(false);
00116     if (addFileRepository != null)  {
00117       OpenMISFile[] addCyberFiles = new OpenMISFile[addFileRepository.size()];
00118       addFileRepository.toArray(addCyberFiles);
00119       answer.answerfiles = addCyberFiles;
00120     }
00121     if (returnList.size() != 0) {
00122       ContainerMessage[] returnContainer = new ContainerMessage[returnList.size()];
00123       returnList.toArray(returnContainer);
00124       answer.containerMessage = returnContainer;
00125      }
00126     return answer;
00127   }
00128 
00129   protected ContainerMessage[] processSyncCommand(long sessionId, ContainerMessage syncCommande) throws OpenMSPException {
00130     // get data
00131     Command sync = (Command)syncCommande.getElement();
00132     Credential cred = CredentialCodec.getOpenMSPCredential(sync.getCrendential());
00133     Array returnList = new Array(10);
00134     this.notifySyncAction(sessionId, cred, sync);
00135     while (syncCommande.hasMoreMessage())  {
00136       ContainerMessage commandContainer = syncCommande.nextMessage();
00137       Element command = commandContainer.getElement();
00138       if (command.getElementType() == Element.ADD) {
00139         Status status = this.processAddCommand(cred, commandContainer);
00140         if (status != null) {
00141           returnList.add(new ContainerMessage(status));
00142         }
00143       } else if (command.getElementType() == Element.DELETE) {
00144         Status status = this.processDeleteCommand(cred, commandContainer);
00145         if (status != null) {
00146           returnList.add(new ContainerMessage(status));
00147         }
00148       } else if (command.getElementType() == Element.REPLACE) {
00149         Status status = this.processReplaceCommand(cred, commandContainer);
00150         if (status != null) {
00151           returnList.add(new ContainerMessage(status));
00152         }
00153       }
00154     }
00155     ContainerMessage[] ret = null;
00156     if (returnList.size() != 0) {
00157        ret = new ContainerMessage[returnList.size()];
00158       returnList.toArray(ret);
00159     }
00160     return ret;
00161   }
00162 
00168    protected void addFileToSynchro(OpenMISFile file) throws ServiceException {
00169 
00170      this.getAddFileRepository(true).add(file);
00171    }
00172    
00173    private Array getAddFileRepository(boolean create)   {
00174            SessionContext context = SessionContextManager.getManager().getSessionContext();
00175            Array addFileRepository = (Array) context.getAttribute("FWKopemis_addFileRepository_attr");
00176            if (addFileRepository == null && create)     {
00177                    addFileRepository = new Array(15);
00178                    context.setAttribute("FWKopemis_addFileRepository_attr", addFileRepository);
00179            }
00180            return addFileRepository;
00181    }
00182    
00183    private void clearaddFileRepository()        {
00184            SessionContext context = SessionContextManager.getManager().getSessionContext();
00185            context.removeAttribute("FWKopemis_addFileRepository_attr");
00186    }
00187   
00188   public abstract String getTargetName();
00189 
00190   protected abstract ContainerMessage processMapCommand(Credential cred,ContainerMessage mapContainer) throws OpenMSPException;
00191   
00192   protected abstract void notifySyncAction(long sessionId,Credential cred, Command syncCommand)  throws OpenMSPException;
00193 
00194   protected abstract  void processResultCommand(Credential cred,Result resultCommande)  throws OpenMSPException;
00195 
00196   protected abstract void processStatusCommand(Credential cred,Status statusCommande)  throws OpenMSPException;
00197 
00198   protected abstract Status processAddCommand(Credential cred, ContainerMessage addContainer) throws OpenMSPException;
00199 
00200   protected abstract Status processDeleteCommand(Credential cred, ContainerMessage deleteContainer) throws OpenMSPException;
00201  
00202   protected abstract Status processReplaceCommand(Credential cred, ContainerMessage replaceContainer) throws OpenMSPException;
00203 
00204   protected abstract void beginProcessOpenMSpCommand(Credential cred)  throws OpenMSPException;
00205 
00206   protected abstract ContainerMessage[] endProcessOpenMSpCommand()  throws OpenMSPException;
00207 
00208   protected abstract ContainerMessage[] processGetCommand(Credential cred, ContainerMessage getCommande)  throws OpenMSPException;
00209 }

Generated on Tue May 22 23:01:08 2007 for OpenMobileIS by  doxygen 1.5.1-p1