Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

OpenMSPSyncMessageFactory.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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.client.core;
00026 
00027 import java.util.Hashtable;
00028 
00029 import org.openmobileis.common.util.codec.GeneralCoder;
00030 import org.openmobileis.common.util.collection.Array;
00031 import org.openmobileis.common.util.exception.SynchroException;
00032 import org.openmobileis.common.util.log.LogManager;
00033 import org.openmobileis.common.util.log.LogServices;
00034 import org.openmobileis.synchro.client.SynchroDescriptor;
00035 import org.openmobileis.synchro.openmsp.OpenMSPException;
00036 import org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener;
00037 import org.openmobileis.synchro.openmsp.protocol.AbstractCommand;
00038 import org.openmobileis.synchro.openmsp.protocol.Command;
00039 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00040 import org.openmobileis.synchro.openmsp.protocol.Element;
00041 import org.openmobileis.synchro.openmsp.protocol.Header;
00042 import org.openmobileis.synchro.openmsp.protocol.Message;
00043 import org.openmobileis.synchro.openmsp.protocol.MessageFactory;
00044 import org.openmobileis.synchro.openmsp.protocol.RequestCommand;
00045 import org.openmobileis.synchro.openmsp.protocol.Result;
00046 import org.openmobileis.synchro.openmsp.protocol.Status;
00047 import org.openmobileis.synchro.security.auth.Credential;
00048 
00056 public final class OpenMSPSyncMessageFactory {
00057   public static final String OpenMSPTARGET = "OpenMSPService";
00058   public static final String OpenMSPSOURCE = "OpenMSPSource";
00059   
00060   private static final String sessionIDServiceName = "OpenMSPSynchroManager";
00061 
00062   private Credential credential;
00063   private long sessionID = 0;
00064 
00068   private Hashtable synchroCmdIDServiceMapTable;
00069 
00073   private Hashtable synchroCmdIDCommandMapTable;
00074 
00078   public OpenMSPSyncMessageFactory() {
00079     super();
00080     sessionID = NumSyncManagerDB.getManager().getSyncNumberForService(sessionIDServiceName);
00081     synchroCmdIDServiceMapTable = new Hashtable();
00082     synchroCmdIDCommandMapTable = new Hashtable();
00083   }
00084   
00085   public void beginSynchro(Credential cred, SynchroDescriptor descriptor) throws SynchroException {
00086     this.credential = cred;
00087   }
00088   
00089   public void endSynchro() throws SynchroException {
00090     this.credential = null;
00091     synchroCmdIDCommandMapTable.clear();
00092     synchroCmdIDServiceMapTable.clear();
00093   }
00094   
00095   public Message getOpenMSPMessage(Array listenerList)  throws OpenMSPException {
00096    Header header = new Header(this.getSessionID(), OpenMSPSyncMessageFactory.OpenMSPSOURCE, OpenMSPSyncMessageFactory.OpenMSPTARGET);
00097    // add credential
00098    try {
00099      String currentCredential = this.credential.getPrincipal()+":"+this.credential.getPassword();
00100      header.setCredential("<Type>OpenMSP:auth-basic</Type><Format>b64</Format>", new String(GeneralCoder.encodeBase64(currentCredential.getBytes())));
00101    } catch (Throwable ex)  {
00102      LogManager.traceError(LogServices.WEBSERVICE, "Error during B64 encoding of synchro credential. Synchro abort.");
00103      throw new OpenMSPException(ex);
00104    }
00105    Message message = MessageFactory.getFactory().createMessage(header);
00106     int lsize = listenerList.size();
00107    for (int i=0; i<lsize; i++) {
00108      try {
00109        ((OpenMSPSyncListener)listenerList.get(i)).sendData(message);
00110      } catch (Throwable ex)  {
00111        LogManager.traceError(LogServices.WEBSERVICE, ex);
00112      }
00113    }
00114    if (!message.isEmpty()) {
00115      this.saveCommandMessageIDs(message);
00116    }
00117    return message;
00118    
00119   }
00120 
00121   /*
00122    * Dispath the server answer OpenMSP message to the appropriate syncListener.
00123    * To manager status properly, we create a hashtable containing for each cmdId of the sent message
00124    * and the associated syncName.
00125    */
00126   public void receiveOpenMSPMessage(Array synclistenerList, Message message)  throws OpenMSPException {
00127     long newSessionID =message.getHeader().getSessionID();
00128     this.setSessionID(newSessionID);
00129     message.resetCursor();
00130     while (message.hasMoreMessage()) {
00131       ContainerMessage container = message.nextMessage();
00132       Element element = container.getElement();
00133       try {
00134          if ( (element.getElementType() == Element.SYNC) || (element.getElementType() == Element.MAP) ) {
00135           String target = ((Command)element).getTarget();
00136           OpenMSPSyncListener listener = (OpenMSPSyncListener)this.getListenerFromListWithName(synclistenerList, target);              
00137           if (listener != null) {
00138             listener.receiveSyncCommand(container, newSessionID);
00139           }
00140         } else  if (element.getElementType() == Element.MAP) {
00141             String target = ((Command)element).getTarget();
00142             OpenMSPSyncListener listener = (OpenMSPSyncListener)this.getListenerFromListWithName(synclistenerList, target);              
00143             if (listener != null) {
00144               listener.receiveMapCommand(container);
00145             }
00146         } else if (element.getElementType() == Element.RESULT) {
00147             Result result = (Result)container.getElement();
00148             String syncName = (String)synchroCmdIDServiceMapTable.get(new Integer(result.getCmdRef()));
00149             if (syncName != null) {
00150               OpenMSPSyncListener listener = (OpenMSPSyncListener)this.getListenerFromListWithName(synclistenerList,syncName);              
00151               if (listener != null) {
00152                 ContainerMessage initialContainer = (ContainerMessage)synchroCmdIDCommandMapTable.get(new Integer(result.getCmdRef()));
00153                 listener.receiveResultCommand(container, initialContainer);
00154               }
00155             }
00156         } else if (element.getElementType() == Element.STATUS) {
00157             Status status = (Status)container.getElement();
00158             String syncName = (String)synchroCmdIDServiceMapTable.get(new Integer(status.getCmdRef()));
00159             if (syncName != null) {
00160               OpenMSPSyncListener listener = (OpenMSPSyncListener)this.getListenerFromListWithName(synclistenerList,syncName);              
00161               if (listener != null) {
00162                 ContainerMessage initialContainer = (ContainerMessage)synchroCmdIDCommandMapTable.get(new Integer(status.getCmdRef()));
00163                 listener.receiveStatusCommand(status, initialContainer);
00164               }
00165             }
00166         } else  // unknown command
00167             LogManager.traceError(LogServices.WEBSERVICE, "Unknown command : " + element.getElementType());
00168       } catch (Throwable ex)  {
00169         String target = ((Command)element).getTarget();
00170         OpenMSPSyncListener listener = (OpenMSPSyncListener)this.getListenerFromListWithName(synclistenerList, target);              
00171         if (listener != null) {
00172           listener.notifySynchroFailure();
00173         }
00174         LogManager.traceError(LogServices.WEBSERVICE, ex);
00175       }
00176     } //end while nextMessage()
00177   }
00178   
00179   private OpenMSPSyncListener getListenerFromListWithName(Array list, String name)  {
00180     for (int i=0; i<list.size(); i++) {
00181       OpenMSPSyncListener listener = (OpenMSPSyncListener) list.get(i);
00182       if (listener.getSyncName().equals(name))  {
00183         return listener;
00184       }
00185     }
00186     return null;
00187   }
00188   
00189 
00190   private long getSessionID() {
00191     return sessionID;
00192   }
00193 
00194   private void setSessionID (long sessionid) {
00195     NumSyncManagerDB.getManager().saveSyncNumberForService(sessionid, sessionIDServiceName);
00196     sessionID = sessionid;
00197   }
00198 
00199   private void saveCommandMessageIDs(Message message)  {
00200     synchroCmdIDServiceMapTable.clear();
00201     synchroCmdIDCommandMapTable.clear();
00202     message.resetCursor();
00203     while (message.hasMoreMessage())  {
00204       ContainerMessage container = message.nextMessage();
00205       saveOpenMSPSourcesAndIDs(container, null);
00206       saveOpenMSPCommandAndIds(container);
00207     }
00208   }
00209 
00210   private void saveOpenMSPSourcesAndIDs(ContainerMessage container, String currentSourceName) {
00211     container.resetCursor();
00212     Element element = container.getElement();
00213     if (element instanceof Command) {
00214       Command command = (Command)element;
00215       if (command.getSource() != null)  {
00216         currentSourceName = command.getSource();
00217       }
00218       synchroCmdIDServiceMapTable.put(new Integer(command.getCmdId()), currentSourceName);
00219     } else if (element instanceof RequestCommand) {
00220       RequestCommand command = (RequestCommand)element;
00221       if (currentSourceName != null)  {
00222         synchroCmdIDServiceMapTable.put(new Integer(command.getCmdId()), currentSourceName);
00223       } else  {
00224         LogManager.traceError(LogServices.SYNCHROSERVICE, "CmlID with no source in message. Can't make reference in return message :"+command.getCmdId());
00225       }
00226     }
00227 
00228     while (container.hasMoreMessage())  {
00229       saveOpenMSPSourcesAndIDs(container.nextMessage(), currentSourceName);
00230     }
00231   }
00232 
00233   private void saveOpenMSPCommandAndIds(ContainerMessage container) {
00234     Element element = container.getElement();
00235     if (element instanceof AbstractCommand) {
00236       synchroCmdIDCommandMapTable.put(new Integer(((AbstractCommand)element).getCmdId()), container);
00237       if (element.getElementType() == Element.SYNC)  {
00238         container.resetCursor();
00239         while (container.hasMoreMessage())  {
00240           saveOpenMSPCommandAndIds(container.nextMessage());
00241         }
00242       }
00243     }
00244   }
00245 
00246 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4