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.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
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
00123
00124
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
00167 LogManager.traceError(LogServices.WEBSERVICE, "Unknown command : " + element.getElementType());
00168 } catch (Throwable ex) {
00169 LogManager.traceError(LogServices.WEBSERVICE, ex);
00170 try {
00171 OpenMSPSyncListener listener = null;
00172 if (element instanceof Command) {
00173 String target = ((Command)element).getTarget();
00174 listener = (OpenMSPSyncListener)this.getListenerFromListWithName(synclistenerList, target);
00175 } else if (element instanceof Result) {
00176 Result result = (Result)container.getElement();
00177 String syncName = (String)synchroCmdIDServiceMapTable.get(new Integer(result.getCmdRef()));
00178 if (syncName != null) {
00179 listener = (OpenMSPSyncListener)this.getListenerFromListWithName(synclistenerList,syncName);
00180 }
00181 } else if (element.getElementType() == Element.STATUS) {
00182 Status status = (Status)container.getElement();
00183 String syncName = (String)synchroCmdIDServiceMapTable.get(new Integer(status.getCmdRef()));
00184 if (syncName != null) {
00185 listener = (OpenMSPSyncListener)this.getListenerFromListWithName(synclistenerList,syncName);
00186 }
00187 }
00188 if (listener != null) {
00189 listener.notifySynchroFailure();
00190 }
00191 } catch (Throwable ex2) {
00192 LogManager.traceError(LogServices.WEBSERVICE, "Error during Listener synchro error management. Can't notify to listener synchro error.");
00193 LogManager.traceError(LogServices.WEBSERVICE, ex2);
00194 }
00195 }
00196 }
00197 }
00198
00199 private OpenMSPSyncListener getListenerFromListWithName(Array list, String name) {
00200 for (int i=0; i<list.size(); i++) {
00201 OpenMSPSyncListener listener = (OpenMSPSyncListener) list.get(i);
00202 if (listener.getSyncName().equals(name)) {
00203 return listener;
00204 }
00205 }
00206 return null;
00207 }
00208
00209
00210 private long getSessionID() {
00211 return sessionID;
00212 }
00213
00214 private void setSessionID (long sessionid) {
00215 NumSyncManagerDB.getManager().saveSyncNumberForService(sessionid, sessionIDServiceName);
00216 sessionID = sessionid;
00217 }
00218
00219 private void saveCommandMessageIDs(Message message) {
00220 synchroCmdIDServiceMapTable.clear();
00221 synchroCmdIDCommandMapTable.clear();
00222 message.resetCursor();
00223 while (message.hasMoreMessage()) {
00224 ContainerMessage container = message.nextMessage();
00225 saveOpenMSPSourcesAndIDs(container, null);
00226 saveOpenMSPCommandAndIds(container);
00227 }
00228 }
00229
00230 private void saveOpenMSPSourcesAndIDs(ContainerMessage container, String currentSourceName) {
00231 container.resetCursor();
00232 Element element = container.getElement();
00233 if (element instanceof Command) {
00234 Command command = (Command)element;
00235 if (command.getSource() != null) {
00236 currentSourceName = command.getSource();
00237 }
00238 synchroCmdIDServiceMapTable.put(new Integer(command.getCmdId()), currentSourceName);
00239 } else if (element instanceof RequestCommand) {
00240 RequestCommand command = (RequestCommand)element;
00241 if (currentSourceName != null) {
00242 synchroCmdIDServiceMapTable.put(new Integer(command.getCmdId()), currentSourceName);
00243 } else {
00244 LogManager.traceError(LogServices.SYNCHROSERVICE, "CmlID with no source in message. Can't make reference in return message :"+command.getCmdId());
00245 }
00246 }
00247
00248 while (container.hasMoreMessage()) {
00249 saveOpenMSPSourcesAndIDs(container.nextMessage(), currentSourceName);
00250 }
00251 }
00252
00253 private void saveOpenMSPCommandAndIds(ContainerMessage container) {
00254 Element element = container.getElement();
00255 if (element instanceof AbstractCommand) {
00256 synchroCmdIDCommandMapTable.put(new Integer(((AbstractCommand)element).getCmdId()), container);
00257 if (element.getElementType() == Element.SYNC) {
00258 container.resetCursor();
00259 while (container.hasMoreMessage()) {
00260 saveOpenMSPCommandAndIds(container.nextMessage());
00261 }
00262 }
00263 }
00264 }
00265
00266 }