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.module.core.ProfileModule;
00037 import org.openmobileis.module.core.ProfileModuleSynchroListener;
00038 import org.openmobileis.module.terminal.ModuleManagerListener;
00039 import org.openmobileis.modules.profiles.terminal.RubricLoader;
00040 import org.openmobileis.services.common.ServiceManager;
00041 import org.openmobileis.synchro.client.SynchroDescriptor;
00042 import org.openmobileis.synchro.openmsp.OpenMSPException;
00043 import org.openmobileis.synchro.openmsp.client.EmptyOpenMSPSyncListener;
00044 import org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener;
00045 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00046 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00047 import org.openmobileis.synchro.security.auth.Credential;
00048 
00053 public final class ColdUpdateModuleManagerListener extends EmptyOpenMSPSyncListener implements ModuleManagerListener {
00054         private static Array collectionToremoveList;
00055         private boolean restartAppli = false;
00056 
00060         public ColdUpdateModuleManagerListener() {
00061                 collectionToremoveList = new Array();
00062         }
00063 
00064   public void initManager() throws ServiceException     {
00065 
00066         //copy all synchronized jar to the lib dir.
00067         File copyDir = new File(this.getJarInstallPath());
00068         File[] jarList = copyDir.listFiles();
00069         String baseDestDir = System.getProperty("user.dir")+"/WEB-INF/lib/";
00070         for (int i=0; i<jarList.length; i++)    {
00071 //              File temp1 = jarList[i].getAbsoluteFile();
00072 //              String temp2 = baseDestDir+jarList[i].getName();
00073                 jarList[i].getAbsoluteFile().renameTo(new File(baseDestDir+jarList[i].getName()));
00074         }
00075                 OpenMSPSynchroManager.getManager().addListener(this, null);
00076   }
00077 
00082          public String getSyncName()    {
00083                  return "FWK StaticModuleManagerListener";
00084          }
00085 
00086         /* (non-Javadoc)
00087          * @see org.openmobileis.module.terminal.ModuleManagerListener#getJarInstallPath()
00088          */
00089         public String getJarInstallPath() {
00090                 return System.getProperty("user.dir")+"/WEB-INF/module/jar";
00091         }
00092 
00093         /* (non-Javadoc)
00094          * @see org.openmobileis.module.terminal.ModuleManagerListener#getXMLInstallPath()
00095          */
00096         public String getXMLInstallPath() {
00097                 return System.getProperty("user.dir")+"/WEB-INF/module/xml";
00098         }
00099 
00100         /* (non-Javadoc)
00101          * @see org.openmobileis.module.terminal.ModuleManagerListener#notifyModuleLoading(org.openmobileis.module.core.ProfileModule)
00102          */
00103         public void notifyModuleLoading(ProfileModule module) throws ServiceException {
00104                 String loaderClassName = module.getModuleLoaderClass();
00105                 if (loaderClassName != null)    {
00106                         try     {
00107                                 RubricLoader loader = (RubricLoader) Thread.currentThread().getContextClassLoader().loadClass(loaderClassName).newInstance();
00108                                 ServiceManager.getManager().loadRubricLoader(loader);
00109                         } catch (Throwable ex)  {
00110                                 LogManager.traceError(0, "StaticModuleManagerListener notifyModuleLoading : Exception during module "+module.getName()+" loading :"+ex.getMessage());
00111                                 throw new ServiceException(ex);
00112                         }
00113                 }
00114         }
00115 
00116         /* (non-Javadoc)
00117          * @see org.openmobileis.module.terminal.ModuleManagerListener#notifyModuleLoading(org.openmobileis.module.core.ProfileModule)
00118          */
00119         public void notifyModuleUpdate(ProfileModule module) throws ServiceException {
00120                 try     {
00121                         if (FastObjectDBManager.getManager().isFodbStarted())   {
00122                                 Array listenerlist = module.getSynchroListenerList();
00123                                 for (int i=0; i<listenerlist.size(); i++){
00124                                         ProfileModuleSynchroListener synclistener = (ProfileModuleSynchroListener)listenerlist.get(i);
00125                                         OpenMSPSyncListener listener = OpenMSPSynchroManager.getManager().getListenerByName(synclistener.name);
00126                                         if (listener != null){ //synchronized collection
00127                                                 if (!listener.isSyncOK())       {
00128                                                         throw new ServiceException(module.getName()+" collection "+synclistener.name+" synchro error. Module can't be updated. Remove synchro module files");
00129                                                 }
00130                                                 // add collecion to the remove list.
00131                                                 collectionToremoveList.add(synclistener.name);
00132                                                 //force complete synchro for the listener.
00133                         NumSyncManagerDB.getManager().saveSyncNumberForService(0, synclistener.name);
00134                                         }
00135                                         
00136                                 }
00137                         }
00138                 } catch (Exception ex)  {
00139                         throw new ServiceException(ex);
00140                 }
00141                 this.restartAppli = true;
00142         }
00143 
00144         /* (non-Javadoc)
00145          * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#startSync(org.openmobileis.synchro.security.auth.Credential, org.openmobileis.synchro.client.SynchroDescriptor)
00146          */
00147         public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00148                 this.restartAppli = false;
00149                 collectionToremoveList.clear();
00150         }
00151 
00152         /* (non-Javadoc)
00153          * @see org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener#endSync()
00154          */
00155         public void endSync() throws OpenMSPException {
00156                 if (this.restartAppli)  {
00157                         Thread thread = new Thread(new Runnable()       {
00158                                 public void run() {
00159                                         try     {
00160                                                 Thread.currentThread().sleep(10000); //way 10 second the synchronisation finised.
00161                                         } catch (Throwable ex)  {
00162                                                 LogManager.traceError(0, ex);
00163                                         }
00164                                         System.exit(0);
00165                                 };
00166                         });
00167                         thread.start();
00168 //                      remove fodb collection before stop.
00169 //                      restart appli. show return page and after stop
00170 //                      ajout du message l'application va etre redemarrer dans quelques instant veuillez patienter.
00171 //                      Ajouter au redemarrage l'ajout du message, pour terminer la mise a jour vous devez realiser une nouvelle synchronisation.
00172                 }
00173         }
00174 
00175 }

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