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

Generated on Tue May 22 23:01:10 2007 for OpenMobileIS by  doxygen 1.5.1-p1