OpenMSPProfileSyncListener.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 
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.core.NumSyncManagerDB;
00039 import org.openmobileis.synchro.openmsp.protocol.AbstractCommand;
00040 import org.openmobileis.synchro.openmsp.protocol.Command;
00041 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00042 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00043 import org.openmobileis.synchro.openmsp.protocol.Element;
00044 import org.openmobileis.synchro.openmsp.protocol.Message;
00045 import org.openmobileis.synchro.openmsp.protocol.RequestCommand;
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   //  private int syncCMDID = 0;
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       // set new profil
00078       try {
00079         String encodeddata = ((DataItem) (resultContainer.nextMessage().getElement())).getData();
00080         String data = new String(GeneralCoder.decodeBase64(encodeddata.getBytes()));
00081         Profile oldproffile = TerminalProfileManager.getManager().getProfil();
00082         TerminalProfileManager.getManager().saveProfilFile(data);
00083         
00084         //If the profil has changed application must be reloaded and resynchronized if needed.
00085         Profile newproffile = TerminalProfileManager.getManager().getProfil();
00086         if (!newproffile.equals(oldproffile))   {
00087           //reinit ModuleManager with new profil and new profil modules.
00088           ModuleManager.getManager().initModule();
00089         }
00090         
00091       } catch (Exception e) {
00092         LogManager.traceError(LogServices.WEBSERVICE, "Wrong result format for profile, impossible to read profile data");
00093         LogManager.traceError(LogServices.WEBSERVICE, e);
00094       }
00095     }
00096   }
00097 
00098   /* (non-Javadoc)
00099    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveStatusCommand(org.openmobileis.synchro.openmsp.protocol.Status, long)
00100    */
00101   public void receiveStatusCommand(Status statusCommande, ContainerMessage initialContenaire) throws OpenMSPException {
00102     try {
00103       //      int cmlRef = statusCommande.getCmdRef();
00104       int status = statusCommande.getStatus();
00105       if (initialContenaire != null) {
00106         AbstractCommand command = (AbstractCommand) initialContenaire.getElement();
00107         this.manageReturnStatusLog(status, command.getElementType(), initialContenaire);
00108         if (status != Status.STATUS_OK) {
00109           //set sync in error
00110           this.notifySynchroFailure();
00111         }
00112       }
00113     } catch (Exception ex) {
00114       this.notifySynchroFailure();
00115       LogManager.traceError(0, ex);
00116     }
00117   }
00118 
00119   /*
00120    * Event sent when the sync manager sends a openML message to the server
00121    * Build a sequence with two or three message :
00122    * Sync message if the user has updated his profil
00123    * Get message to get the updated profile from the server
00124    * Sync message to get synchronized channels (could be all channels in the profile or specific channels)
00125    * we don't use the context since we don't manage status
00126    */
00127   public void sendData(Message message) {
00128     if (firstSync) {
00129       try {
00130         Command getCommand = new Command(Element.GET, this.getSyncName(), profilSynchroTargetName);
00131         long ns = NumSyncManagerDB.getManager().getSyncNumberForService(this.getSyncName());
00132         getCommand.setSourceSessionID(ns);
00133         ContainerMessage getContainer = new ContainerMessage(getCommand);
00134 
00135         // add GET command to make synchro of profil
00136          DataItem newItem = new DataItem(Element.ITEM, "", "", null, null);
00137 
00138         getContainer.add(newItem);
00139         message.add(getContainer);
00140       } catch (OpenMSPException e) {
00141         LogManager.traceError(LogServices.WEBSERVICE, "Error during building syncML message for Sync Profil service : " + e.getMessage());
00142       }
00143       firstSync = false;
00144     }
00145   }
00146 
00147   /*
00148    * Return the name used to identiy the receiver of the syncML messages (source and target)
00149    */
00150   public String getSyncName() {
00151     return profilSynchroSourceName;
00152   }
00153 
00154   public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00155     super.startSync(cred, synchrodescriptor);
00156     firstSync = true;
00157   }
00158 
00163   protected void manageReturnStatusLog(int statusCode, int cmlType, ContainerMessage container) {
00164     this.setSynchroStatus(statusCode);
00165     if (statusCode != Status.STATUS_OK) {
00166       String message = null;
00167       short journalEntryType = JournalEntry.ERROR_SYNC_MOBILE;
00168       if (statusCode == Status.STATUS_UNAUTHORIZED) {
00169         message = OpenMSPDBSyncListener.SYNC_UNAUTHORIZED_MESSAGE;
00170       } else {
00171         message = OpenMSPDBSyncListener.SYNC_ERROR_MESSAGE;
00172       }
00173       if (message != null) {
00174         JournalManager.getManager().saveJournalEntry(
00175 
00176         this.getSyncName()
00177 
00178         , journalEntryType
00179 
00180         , message
00181 
00182         , statusCode
00183 
00184         );
00185       }
00186     }
00187 
00188   }
00189 
00190 }

Generated on Mon Dec 4 11:03:28 2006 for OpenMobileIS by  doxygen 1.5.1-p1