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.OpenMSPSynchroManager;
00041 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00042 import org.openmobileis.synchro.openmsp.protocol.Command;
00043 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00044 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00045 import org.openmobileis.synchro.openmsp.protocol.Element;
00046 import org.openmobileis.synchro.openmsp.protocol.Item;
00047 import org.openmobileis.synchro.openmsp.protocol.Message;
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 = ModuleManager.getManager().getModuleRepositoryPath();
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 = ModuleManager.getManager().getModuleRepositoryPath();
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   public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00096     super.startSync(cred, synchrodescriptor);
00097     this.syncGoOn = true;
00098     synchroPath = synchrodescriptor.getSynchroConduit().getIntallPath();
00099  }
00100 
00101   /* (non-Javadoc)
00102    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#sendData(org.openmobileis.synchro.openmsp.protocol.Message)
00103    */
00104   public void sendData(Message message) throws OpenMSPException {
00105     if (!this.isSyncOK()) {
00106       return; // return if an error occurs during synchro.
00107     }
00108     //send jar last modification date in long.
00109     if (!syncGoOn) { // synchronize data only for the first call.
00110     return;
00111     }
00112     
00113     //get modified files
00114     long ns = NumSyncManagerDB.getManager().getSyncNumberForService(this.getSyncName());
00115 
00116     try {
00117       Command getCommand = new Command(Element.GET, this.getSyncName(), this.getSyncName());
00118       getCommand.setSourceSessionID(ns);
00119       ContainerMessage getContainer = new ContainerMessage(getCommand);
00120       Item newItem = null;
00121       newItem = new DataItem(
00122                   Element.ITEM
00123                   , "Jar Server file name"
00124                   , ""
00125                   , null
00126                   , null
00127               );
00128       getContainer.add(newItem);
00129       message.add(getContainer);
00130       syncGoOn = false;
00131    } catch (Throwable ex)  {
00132       this.setSynchroStatus(Status.STATUS_FAILED);
00133       LogManager.trace(new SynchroException("DefaultDbSyncListener "+this.getSyncName()+" Send data exception ", ex));
00134     }
00135   }
00136 
00137   /* (non-Javadoc)
00138    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#getSyncName()
00139    */
00140   public String getSyncName() {
00141     return this.moduleName;
00142   }
00143 
00144   /* (non-Javadoc)
00145    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveMapCommand(org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00146    */
00147   public void receiveMapCommand(ContainerMessage mapContainer) throws OpenMSPException {
00148 
00149   }
00150 
00151   /* (non-Javadoc)
00152    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveResultCommand(org.openmobileis.synchro.openmsp.protocol.ContainerMessage, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00153    */
00154   public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00155     Result resultCommand = (Result)resultContainer.getElement(); //result
00156     Array copiedFiles = new Array();
00157     try {
00158         boolean notifyUpdate = false;
00159       long newSessionID = Long.parseLong(resultCommand.getMetaInformation());
00160       while (resultContainer.hasMoreMessage())  {
00161         DataItem item = (DataItem) resultContainer.nextMessage().getElement();
00162         String meta = item.getMetaInformation();
00163         if (meta.equals("jarfile")) {
00164           this.updateJarFile(item.getData(), copiedFiles); 
00165           notifyUpdate = true;
00166           LogManager.traceDebug(0, "ModuleJarSyncListener receiveResultCommand updateJarFile JAR for module :"+this.getSyncName());
00167         }
00168         if (meta.equals("xmlfile")) {
00169           this.updateXmlFile(item.getData()); 
00170           copiedFiles.add(item.getData());
00171           notifyUpdate = true;
00172           LogManager.traceDebug(0, "ModuleJarSyncListener receiveResultCommand updateXmlFile XML for module :"+this.getSyncName());
00173         } 
00174         if (meta.equals("nofile")) {
00175                 notifyUpdate = false;
00176           LogManager.traceDebug(0, "ModuleJarSyncListener receiveResultCommand NOFILE for module :"+this.getSyncName());
00177         } 
00178       }
00179       
00180       if (this.isSyncOK())  {
00181         if (notifyUpdate) {
00182                 ModuleManager.getManager().notifyModuleUpdateAfterSynchro(this.module);
00183                                 OpenMSPSynchroManager.getManager().restartSynchro();
00184         }
00185         NumSyncManagerDB.getManager().saveSyncNumberForService(newSessionID, this.getSyncName());
00186         //notify module update 
00187         
00188       }
00189       
00190     } catch (Throwable ex)  {
00191         //remove all copied jar file to avoid to remove old one. 
00192         int size = copiedFiles.size();
00193         for (int i=0; i<size; i++){
00194                 String filename = (String)copiedFiles.get(i);
00195                 File file = new File(filename);
00196                 file.delete();
00197         }
00198       throw new OpenMSPException(ex);
00199     }
00200   }
00201 
00202   private void updateJarFile (String filename, Array copiedFiles)  {
00203      String inPath = this.synchroPath+File.separator+"filedir"+File.separator+filename;
00204      String outPath = this.jarPath+File.separator+filename;
00205      FileUtilities.moveFile(inPath, outPath);
00206      copiedFiles.add(outPath);
00207   }
00208 
00209   private void updateXmlFile (String filename)  {
00210      String inPath = this.synchroPath+File.separator+"filedir"+File.separator+filename;
00211      String outPath = this.getModuleXmlFileName();
00212      FileUtilities.moveFile(inPath, outPath);
00213   }
00214   
00215   private String getModuleXmlFileName()   {
00216    StringBuffer modrep = new StringBuffer(this.xmlPath);
00217    modrep.append('/');
00218    modrep.append(this.moduleName);
00219    modrep.append(".xml");
00220    return modrep.toString();
00221   }
00222 
00223   /* (non-Javadoc)
00224    * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#receiveStatusCommand(org.openmobileis.synchro.openmsp.protocol.Status, org.openmobileis.synchro.openmsp.protocol.ContainerMessage)
00225    */
00226   public void receiveStatusCommand(Status statusCommande, ContainerMessage initialCommand) throws OpenMSPException {
00227 //    int cmlRef = statusCommande.getCmdRef();
00228     int status = statusCommande.getStatus();
00229     if (status != Status.STATUS_OK)  {
00230       this.notifySynchroFailure();
00231     }   
00232   }
00233 
00234 }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4