ColdUpdateModuleManagerListener.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  *  Modifications :
00025  *  Creation P.Delrieu
00026  *
00027  */
00028 package org.openmobileis.bundle.coldupdate.terminal;
00029 
00030 import java.io.File;
00031 
00032 import org.openmobileis.common.util.collection.Array;
00033 import org.openmobileis.common.util.exception.ServiceException;
00034 import org.openmobileis.common.util.log.LogManager;
00035 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00036 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00037 import org.openmobileis.module.core.ProfileModule;
00038 import org.openmobileis.module.core.ProfileModuleRubric;
00039 import org.openmobileis.module.terminal.ModuleManagerListener;
00040 import org.openmobileis.modules.profiles.terminal.RubricLoader;
00041 import org.openmobileis.services.common.ServiceManager;
00042 import org.openmobileis.synchro.client.SynchroDescriptor;
00043 import org.openmobileis.synchro.openmsp.OpenMSPException;
00044 import org.openmobileis.synchro.openmsp.client.EmptyOpenMSPSyncListener;
00045 import org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener;
00046 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00047 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00048 import org.openmobileis.synchro.security.auth.Credential;
00049 
00054 public final class ColdUpdateModuleManagerListener extends EmptyOpenMSPSyncListener implements ModuleManagerListener {
00055         private static Array collectionToremoveList;
00056         private boolean restartAppli = false;
00057 
00061         public ColdUpdateModuleManagerListener() {
00062                 collectionToremoveList = new Array();
00063         }
00064 
00065   public void initManager() throws ServiceException     {
00066 
00067         //copy all synchronized jar to the lib dir.
00068         File copyDir = new File(this.getJarInstallPath());
00069         File[] jarList = copyDir.listFiles();
00070         String baseDestDir = System.getProperty("user.dir")+"/WEB-INF/lib/";
00071         for (int i=0; i<jarList.length; i++)    {
00072 //              File temp1 = jarList[i].getAbsoluteFile();
00073 //              String temp2 = baseDestDir+jarList[i].getName();
00074                 jarList[i].getAbsoluteFile().renameTo(new File(baseDestDir+jarList[i].getName()));
00075         }
00076                 OpenMSPSynchroManager.getManager().addListener(this, null);
00077   }
00078 
00083          public String getSyncName()    {
00084                  return "FWK StaticModuleManagerListener";
00085          }
00086 
00087         /* (non-Javadoc)
00088          * @see org.openmobileis.module.terminal.ModuleManagerListener#getJarInstallPath()
00089          */
00090         public String getJarInstallPath() {
00091                 return System.getProperty("user.dir")+"/WEB-INF/module/jar";
00092         }
00093 
00094         /* (non-Javadoc)
00095          * @see org.openmobileis.module.terminal.ModuleManagerListener#getXMLInstallPath()
00096          */
00097         public String getXMLInstallPath() {
00098                 return System.getProperty("user.dir")+"/WEB-INF/module/xml";
00099         }
00100 
00101         /* (non-Javadoc)
00102          * @see org.openmobileis.module.terminal.ModuleManagerListener#notifyModuleLoading(org.openmobileis.module.core.ProfileModule)
00103          */
00104         public void notifyModuleLoading(ProfileModule module) throws ServiceException {
00105                 Array rubList = module.getModuleRubrics();
00106                 for (int i=0; i<rubList.size(); i++)    {
00107                         ProfileModuleRubric rubric = (ProfileModuleRubric) rubList.get(i);
00108                         String loaderClassName = rubric.getLoaderClass();
00109                         if (loaderClassName != null)    {
00110                                 try     {
00111                                         RubricLoader loader = (RubricLoader) Class.forName(loaderClassName).newInstance();
00112                                         ServiceManager.getManager().loadRubricLoader(loader);
00113                                 } catch (Throwable ex)  {
00114                                         LogManager.traceError(0, "StaticModuleManagerListener notifyModuleLoading : Exception during module "+module.getName()+" loading :"+ex.getMessage());
00115                                         throw new ServiceException(ex);
00116                                 }
00117                         }
00118                 }
00119 
00120 
00121         }
00122 
00123         /* (non-Javadoc)
00124          * @see org.openmobileis.module.terminal.ModuleManagerListener#notifyModuleLoading(org.openmobileis.module.core.ProfileModule)
00125          */
00126         public void notifyModuleUpdate(ProfileModule module) throws ServiceException {
00127                 try     {
00128                         if (FastObjectDBManager.getManager().isFodbStarted())   {
00129                                 Array rubrics = module.getModuleRubrics(); //all database collection is a rubric in the profil.
00130                                 FODBCollection[] collist = FastObjectDBManager.getManager().getCurrentFODB().getDatabaseCollectionArray();
00131                                 int rubsize = rubrics.size();
00132                                 for (int i=0; i<rubsize; i++)   {
00133                                         ProfileModuleRubric rubric = (ProfileModuleRubric)rubrics.get(i);
00134                                         for (int j=0; j<collist.length; j++)    {
00135                                                 if (rubric.getName().equals(collist[j].getName()))      {
00136                                                         //validate collection synchro OK.
00137                                                         OpenMSPSyncListener listener = OpenMSPSynchroManager.getManager().getListenerByName(collist[j].getName());
00138                                                         if (listener != null){ //synchronized collection
00139                                                                 if (!listener.isSyncOK())       {
00140                                                                         throw new ServiceException(module.getName()+" collection "+collist[j].getName()+" synchro error. Module can't be updated. Remove synchro module files");
00141                                                                 }
00142                                                                 // add collecion to the remove list.
00143                                                                 collectionToremoveList.add(collist[j].getName());
00144                                                                 //force complete synchro for the listener.
00145                                         NumSyncManagerDB.getManager().saveSyncNumberForService(0, collist[j].getName());
00146                                                         }
00147                                                 }
00148                                         }
00149                                 }
00150                         }
00151                 } catch (Exception ex)  {
00152                         throw new ServiceException(ex);
00153                 }
00154                 this.restartAppli = true;
00155         }
00156 
00157         /* (non-Javadoc)
00158          * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#startSync(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.client.SynchroDescriptor)
00159          */
00160         public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00161                 this.restartAppli = false;
00162                 collectionToremoveList.clear();
00163         }
00164 
00165         /* (non-Javadoc)
00166          * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#endSync()
00167          */
00168         public void endSync() throws OpenMSPException {
00169                 if (this.restartAppli)  {
00170                         Thread thread = new Thread(new Runnable()       {
00171                                 public void run() {
00172                                         try     {
00173                                                 Thread.currentThread().sleep(10000); //way 10 second the synchronisation finised.
00174                                         } catch (Throwable ex)  {
00175                                                 LogManager.traceError(0, ex);
00176                                         }
00177                                         System.exit(0);
00178                                 };
00179                         });
00180                         thread.start();
00181 //                      remove fodb collection before stop.
00182 //                      restart appli. show return page and after stop
00183 //                      ajout du message l'application va être redémarrer dans quelques instant veuillez patienter.
00184 //                      Ajouter au redémarrage l'ajout du message, pour terminer la mise à jour vous devez réaliser une nouvelle synchronisation.
00185                 }
00186         }
00187 
00188 }

Generated on Mon Jan 14 17:29:45 2008 for OpenMobileIS by  doxygen 1.5.4