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
00026 package org.openmobileis.module.profiles.terminal;
00027
00028 import org.openmobileis.common.user.profile.Profile;
00029 import org.openmobileis.common.util.codec.GeneralCoder;
00030 import org.openmobileis.common.util.log.*;
00031 import org.openmobileis.module.terminal.ModuleManager;
00032 import org.openmobileis.synchro.client.SynchroDescriptor;
00033 import org.openmobileis.synchro.journal.JournalEntry;
00034 import org.openmobileis.synchro.journal.JournalManager;
00035 import org.openmobileis.synchro.openmsp.OpenMSPException;
00036 import org.openmobileis.synchro.openmsp.client.DefaultOpenMSPSyncListener;
00037 import org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener;
00038 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00039 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00040 import org.openmobileis.synchro.openmsp.protocol.AbstractCommand;
00041 import org.openmobileis.synchro.openmsp.protocol.Command;
00042 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00043 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00044 import org.openmobileis.synchro.openmsp.protocol.Element;
00045 import org.openmobileis.synchro.openmsp.protocol.Message;
00046 import org.openmobileis.synchro.openmsp.protocol.Result;
00047 import org.openmobileis.synchro.openmsp.protocol.Status;
00048 import org.openmobileis.synchro.security.auth.Credential;
00049
00061 public class OpenMSPProfileSyncListener extends DefaultOpenMSPSyncListener {
00062
00063 public static final String profilSynchroSourceName = "ProfilSyncListener";
00064
00065 public static final String profilSynchroTargetName = "ProfilSynchroTarget";
00066
00067 private boolean firstSync = false;
00068
00069
00070
00071 public OpenMSPProfileSyncListener() {
00072 }
00073
00074 public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00075 Result result = (Result) resultContainer.getElement();
00076 if (result.getTargetRef().equals(profilSynchroSourceName)) {
00077
00078 try {
00079 String encodeddata = ((DataItem) (resultContainer.nextMessage().getElement())).getData();
00080 String data = new String(GeneralCoder.decodeBase64(encodeddata.getBytes()));
00081 TerminalProfile oldproffile = TerminalProfileManager.getManager().getProfil();
00082 TerminalProfileManager.getManager().saveProfilFile(data);
00083
00084
00085 TerminalProfile newproffile = TerminalProfileManager.getManager().getProfil();
00086 if (!newproffile.equals(oldproffile)) {
00087 LogManager.traceDebug(0, "OpenMSPProfileSyncListener change profile, INIT MODULE !!!");
00088
00089 ModuleManager.getManager().initModule();
00090 OpenMSPSynchroManager.getManager().restartSynchro();
00091 }
00092
00093 } catch (Exception e) {
00094 LogManager.traceError(LogServices.WEBSERVICE, "Wrong result format for profile, impossible to read profile data");
00095 LogManager.traceError(LogServices.WEBSERVICE, e);
00096 }
00097 }
00098 }
00099
00100
00101
00102
00103 public void receiveStatusCommand(Status statusCommande, ContainerMessage initialContenaire) throws OpenMSPException {
00104 try {
00105
00106 int status = statusCommande.getStatus();
00107 if (initialContenaire != null) {
00108 AbstractCommand command = (AbstractCommand) initialContenaire.getElement();
00109 this.manageReturnStatusLog(status, command.getElementType(), initialContenaire);
00110 if (status != Status.STATUS_OK) {
00111
00112 this.notifySynchroFailure();
00113 }
00114 }
00115 } catch (Exception ex) {
00116 this.notifySynchroFailure();
00117 LogManager.traceError(0, ex);
00118 }
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 public void sendData(Message message) {
00130 if (firstSync) {
00131 try {
00132 Command getCommand = new Command(Element.GET, this.getSyncName(), profilSynchroTargetName);
00133 long ns = NumSyncManagerDB.getManager().getSyncNumberForService(this.getSyncName());
00134 getCommand.setSourceSessionID(ns);
00135 ContainerMessage getContainer = new ContainerMessage(getCommand);
00136
00137
00138 DataItem newItem = new DataItem(Element.ITEM, "", "", null, null);
00139
00140 getContainer.add(newItem);
00141 message.add(getContainer);
00142 } catch (OpenMSPException e) {
00143 LogManager.traceError(LogServices.WEBSERVICE, "Error during building syncML message for Sync Profil service : " + e.getMessage());
00144 }
00145 firstSync = false;
00146 }
00147 }
00148
00149
00150
00151
00152 public String getSyncName() {
00153 return profilSynchroSourceName;
00154 }
00155
00156 public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00157 super.startSync(cred, synchrodescriptor);
00158 firstSync = true;
00159 }
00160
00165 protected void manageReturnStatusLog(int statusCode, int cmlType, ContainerMessage container) {
00166 this.setSynchroStatus(statusCode);
00167 if (statusCode != Status.STATUS_OK) {
00168 String message = null;
00169 short journalEntryType = JournalEntry.ERROR_SYNC_MOBILE;
00170 if (statusCode == Status.STATUS_UNAUTHORIZED) {
00171 message = OpenMSPDBSyncListener.SYNC_UNAUTHORIZED_MESSAGE;
00172 } else {
00173 message = OpenMSPDBSyncListener.SYNC_ERROR_MESSAGE;
00174 }
00175 if (message != null) {
00176 JournalManager.getManager().saveJournalEntry(
00177
00178 this.getSyncName()
00179
00180 , journalEntryType
00181
00182 , message
00183
00184 , statusCode
00185
00186 );
00187 }
00188 }
00189
00190 }
00191
00192 }