ModuleJarSyncListener.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.module.terminal;
00026 
00027 import java.io.File;
00028 
00029 import org.openmobileis.common.util.PropertiesManager;
00030 import org.openmobileis.common.util.collection.Array;
00031 import org.openmobileis.common.util.exception.SynchroException;
00032 import org.openmobileis.common.util.file.FileUtilities;
00033 import org.openmobileis.common.util.log.LogManager;
00034 import org.openmobileis.module.core.ProfileModule;
00035 import org.openmobileis.synchro.client.SynchroDescriptor;
00036 import org.openmobileis.synchro.journal.JournalManager;
00037 import org.openmobileis.synchro.journal.SimpleLogRenderer;
00038 import org.openmobileis.synchro.openmsp.OpenMSPException;
00039 import org.openmobileis.synchro.openmsp.client.DefaultOpenMSPSyncListener;
00040 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
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.Item;
00046 import org.openmobileis.synchro.openmsp.protocol.Message;
00047 import org.openmobileis.synchro.openmsp.protocol.RequestCommand;
00048 import org.openmobileis.synchro.openmsp.protocol.Result;
00049 import org.openmobileis.synchro.openmsp.protocol.Status;
00050 import org.openmobileis.synchro.security.auth.Credential;
00051 
00059 public final class ModuleJarSyncListener extends DefaultOpenMSPSyncListener {
00060   private boolean syncGoOn = false;
00061   protected String jarPath;
00062   protected String synchroPath;
00063   protected String moduleName;
00064   protected ProfileModule module;
00065   protected String xmlPath;
00066 
00070   public ModuleJarSyncListener() {
00071     super();
00072     //register the log renderer.
00073     JournalManager.getManager().registerJournalLogRenderer(new SimpleLogRenderer(this.getSyncName(), this.getSyncName()+" application"));
00074     jarPath = PropertiesManager.getManager().getProperty("org.openmobileis.module.repository.jar");
00075     if (jarPath == null)    {
00076       LogManager.traceWarning(0, "Property org.openmobileis.module.repository.jar not set. Module synchro can't be done.");
00077     } else  {
00078       File file = new File(jarPath);
00079       if (!file.exists()) file.mkdirs();
00080     }
00081     
00082     xmlPath = PropertiesManager.getManager().getProperty("org.openmobileis.module.repository.xml");
00083     if (xmlPath == null)    {
00084       LogManager.traceWarning(0, "Property org.openmobileis.module.repository.xml not set. Module synchro can't be done.");
00085     } else  {
00086       File file = new File(xmlPath);
00087       if (!file.exists()) file.mkdirs();
00088     }
00089  }
00090   public void setModule(ProfileModule module)   {
00091     this.moduleName = module.getName();
00092     this.module = module;
00093  }
00094   
00095   private String getModuleXmlFileName()   {
00096    StringBuffer modrep = new StringBuffer(this.xmlPath);
00097    modrep.append('/');
00098    modrep.append(this.moduleName);
00099    modrep.append(".xml");
00100    return modrep.toString();
00101   }
00102 
00103   public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00104     super.startSync(cred, synchrodescriptor);
00105     this.syncGoOn = true;
00106     synchroPath = synchrodescriptor.getSynchroConduit().getIntallPath();
00107  }
00108 
00109   /* (non-Javadoc)
00110    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#sendData(org.openmobileis.synchro.openmsp.protocol.Message)
00111    */
00112   public void sendData(Message message) throws OpenMSPException {
00113     if (!this.isSyncOK()) {
00114       return; // return if an error occurs during synchro.
00115     }
00116     //send jar last modification date in long.
00117     if (!syncGoOn) { // synchronize data only for the first call.
00118     return;
00119     }
00120     
00121     //get modified files
00122     long ns = NumSyncManagerDB.getManager().getSyncNumberForService(this.getSyncName());
00123 
00124     try {
00125       Command getCommand = new Command(Element.GET, this.getSyncName(), this.getSyncName());
00126       getCommand.setSourceSessionID(ns);
00127       ContainerMessage getContainer = new ContainerMessage(getCommand);
00128       Item newItem = null;
00129       newItem = new DataItem(
00130                   Element.ITEM
00131                   , "Jar Server file name"
00132                   , ""
00133                   , null
00134                   , null
00135               );
00136       getContainer.add(newItem);
00137       message.add(getContainer);
00138       syncGoOn = false;
00139    } catch (Throwable ex)  {
00140       this.setSynchroStatus(Status.STATUS_FAILED);
00141       LogManager.trace(new SynchroException("DefaultDbSyncListener "+this.getSyncName()+" Send data exception ", ex));
00142     }
00143   }
00144 
00145   /* (non-Javadoc)
00146    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#getSyncName()
00147    */
00148   public String getSyncName() {
00149     return this.moduleName;
00150   }
00151 
00152   /* (non-Javadoc)
00153    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveMapCommand(org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00154    */
00155   public void receiveMapCommand(ContainerMessage mapContainer) throws OpenMSPException {
00156 
00157   }
00158 
00159   /* (non-Javadoc)
00160    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveResultCommand(org.openmobileis.synchro.openmsp.protocol.ContainerMessage, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00161    */
00162   public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00163     Result resultCommand = (Result)resultContainer.getElement(); //result
00164     Array copiedFiles = new Array();
00165     try {
00166       long newSessionID = Long.parseLong(resultCommand.getMetaInformation());
00167       while (resultContainer.hasMoreMessage())  {
00168         DataItem item = (DataItem) resultContainer.nextMessage().getElement();
00169         String meta = item.getMetaInformation();
00170         if (meta.equals("jarfile")) {
00171           this.updateJarFile(item.getData(), copiedFiles); 
00172         }
00173         if (meta.equals("xmlfile")) {
00174           this.updateXmlFile(item.getData()); 
00175           copiedFiles.add(item.getData());
00176         }
00177       }
00178       
00179       if (this.isSyncOK())  {
00180         ModuleManager.getManager().notifyModuleUpdateAfterSynchro(this.module);
00181         NumSyncManagerDB.getManager().saveSyncNumberForService(newSessionID, this.getSyncName());
00182         //notify module update 
00183         
00184       }
00185       
00186     } catch (Throwable ex)  {
00187         //remove all copied jar file to avoid to remove old one. 
00188         int size = copiedFiles.size();
00189         for (int i=0; i<size; i++){
00190                 String filename = (String)copiedFiles.get(i);
00191                 File file = new File(filename);
00192                 file.delete();
00193         }
00194       throw new OpenMSPException(ex);
00195     }
00196   }
00197 
00198   private void updateJarFile (String filename, Array copiedFiles)  {
00199      String inPath = this.synchroPath+File.separator+"filedir"+File.separator+filename;
00200      String outPath = this.jarPath+File.separator+filename;
00201      FileUtilities.moveFile(inPath, outPath);
00202      copiedFiles.add(outPath);
00203   }
00204 
00205   private void updateXmlFile (String filename)  {
00206      String inPath = this.synchroPath+File.separator+"filedir"+File.separator+filename;
00207      String outPath = this.getModuleXmlFileName();
00208      FileUtilities.moveFile(inPath, outPath);
00209   }
00210 
00211   /* (non-Javadoc)
00212    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveStatusCommand(org.openmobileis.synchro.openmsp.protocol.Status, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00213    */
00214   public void receiveStatusCommand(Status statusCommande, ContainerMessage initialCommand) throws OpenMSPException {
00215 //    int cmlRef = statusCommande.getCmdRef();
00216     int status = statusCommande.getStatus();
00217     if (status != Status.STATUS_OK)  {
00218       this.notifySynchroFailure();
00219     }   
00220   }
00221 
00222 }

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