ModuleManager.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  */
00025 package org.openmobileis.module.terminal;
00026 
00027 import java.io.File;
00028 import java.io.FileNotFoundException;
00029 import java.io.FileReader;
00030 import java.io.IOException;
00031 import java.io.Reader;
00032 
00033 import org.openmobileis.common.util.collection.Array;
00034 import org.openmobileis.common.util.collection.tree.DependTree;
00035 import org.openmobileis.common.util.exception.ServiceException;
00036 import org.openmobileis.common.util.log.LogManager;
00037 import org.openmobileis.module.core.DependProfileModule;
00038 import org.openmobileis.module.core.ProfileModule;
00039 import org.openmobileis.module.core.XmlModuleParser;
00040 import org.openmobileis.module.profiles.terminal.OpenMSPProfileSyncListener;
00041 import org.openmobileis.module.profiles.terminal.TerminalProfilModule;
00042 import org.openmobileis.module.profiles.terminal.TerminalProfile;
00043 import org.openmobileis.module.profiles.terminal.TerminalProfileManager;
00044 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00045 import org.xmlpull.v1.XmlPullParserException;
00046 
00047 /*
00048  * Title:        OpenMobileIS project source <BR>
00049  * Description:
00050  * @author      Philippe Delrieu
00051  * @since       JDK 1.1
00052  * @version 1.0.
00053  */
00054 
00055 public final class ModuleManager {
00056         public static final String OpenMobileISModuleName="openMobileIS";
00057 
00058   private static ModuleManager manager;
00059   private ModuleManagerListener listener;
00060   private String moduleRepositoryPath;
00061 
00065   private ModuleManager() {
00066   }
00067 
00068   public static ModuleManager getManager() {
00069     if (manager == null) {
00070       synchronized (ModuleManager.class) {
00071         if (manager == null) {
00072           manager = new ModuleManager();
00073         }
00074       }
00075     }
00076     return manager;
00077   }
00078   
00079   public String getModuleRepositoryPath()       {
00080         return this.moduleRepositoryPath;
00081   }
00082   
00083   public void registerModuleManagerListener(ModuleManagerListener listener){
00084     this.listener = listener;
00085   }
00086   
00087   public void initModule() throws ServiceException {
00088     try {
00089       //load profil management.
00090   /*    String synchronizedProfilFile = System.getProperty("user.dir")+"/WEB-INF/conf/userprofil.xml";
00091       PropertiesManager.getManager().addProperty("org.openmobileis.profil.file", synchronizedProfilFile);
00092       File file  = new File(synchronizedProfilFile);
00093       if (!file.exists()) file.createNewFile();
00094       Reader termreader = new FileReader(synchronizedProfilFile);
00095       TerminalXmlFileProfilDataFactory factory = new TerminalXmlFileProfilDataFactory(termreader);
00096       ProfileDataManager.getManager().registerProfilDataFactoryForGroup("terminal", factory);
00097       */
00098       
00099       if (this.listener != null) this.listener.initManager();
00100       
00101     //  PropertiesManager.getManager().addProperty("org.openmobileis.module.repository.xml", modulerep);
00102       if (this.listener != null ) this.moduleRepositoryPath = listener.getJarInstallPath();
00103       if (this.moduleRepositoryPath == null) this.moduleRepositoryPath = System.getProperty("user.dir")+"/WEB-INF/module";
00104 
00105       File modfile = new File(this.moduleRepositoryPath);
00106       if (!modfile.exists()) modfile.mkdirs();
00107              
00108       //load all module in rigth order.
00109       //construct profil list with depend.
00110       DependTree moduledepend = new DependTree();
00111       TerminalProfile profile = TerminalProfileManager.getManager().getProfil();
00112       for (int i=0; profile!=null && i<profile.moduleList.length; i++)  {
00113         TerminalProfilModule terminalmodule = profile.moduleList[i];
00114         //load module file
00115         ProfileModule module = null;
00116         if (moduledepend.getObjectByName(terminalmodule.moduleName) == null)    {
00117           module = this.readModuleXmlFiles(terminalmodule.moduleName);
00118           if (module != null)   {
00119             if (this.validateModuleDependListWithName(profile, module)) {
00120               Array moduleDependList = module.getDependModuleList();
00121               int depsize = moduleDependList.size();
00122               String[] moduleNameDependList = new String[depsize];
00123               for (int j = 0; j < depsize; j++) {
00124                 moduleNameDependList[j] =  ((DependProfileModule)moduleDependList.get(j)).getModuleName();
00125               }
00126               moduledepend.addObject(module.getName(), module, moduleNameDependList);
00127             } else {
00128               LogManager.traceError(0, "Can't load module :" + module.getName() + " Missing depend's module: in profil.");
00129             }
00130           }
00131         }
00132       }
00133       
00134       //register profil synchronisation for module synchronisation
00135       OpenMSPProfileSyncListener profilListener = new OpenMSPProfileSyncListener();
00136       OpenMSPSynchroManager.getManager().addModuleListener(profilListener, null);
00137       String[] profilNameArray = new String[]{profilListener.getSyncName()};
00138       //load each module in rigth order and there module synchro listener
00139       moduledepend.resetPhaseList();
00140       Array moduleList = moduledepend.getNextPhaseObjectList();
00141       while (moduleList.size() != 0) {
00142         int modsize = moduleList.size();
00143         for (int i = 0; i < modsize; i++) {
00144           ProfileModule module = (ProfileModule) moduleList.get(i);
00145           try {
00146             this.loadModule(module);
00147             ModuleJarSyncListener jarlistener = (ModuleJarSyncListener)OpenMSPSynchroManager.getManager().getModuleListenerByName(module.getName());
00148             if (jarlistener == null) {
00149               jarlistener = new ModuleJarSyncListener();
00150               jarlistener.setModule(module);
00151               OpenMSPSynchroManager.getManager().addModuleListener(jarlistener, profilNameArray);
00152             } else  {
00153               jarlistener.setModule(module);
00154             }
00155           } catch (Throwable ex) {
00156             LogManager.traceError(0, "Error during module loading :" + module.getName() + " Exception:" + ex.getClass() + " Message :" + ex.getMessage());
00157             LogManager.traceError(0, ex);
00158           }
00159         }
00160         moduleList = moduledepend.getNextPhaseObjectList();
00161       }
00162      
00163     } catch (Throwable ex)  {
00164       throw new ServiceException(ex);
00165     }
00166    
00167   }
00168   
00169   //return all loaded listener name list.
00170   public void loadModule(ProfileModule module) throws ServiceException {
00171     if (this.listener != null)  {
00172         if (module != null) LogManager.traceDebug(0, "ModuleManager loadModule :"+module.getName());
00173         else LogManager.traceDebug(0, "ModuleManager loadModule NULL");
00174       listener.notifyModuleLoading(module);
00175     }
00176   }
00177       
00178   private boolean validateModuleDependListWithName(TerminalProfile profile, ProfileModule module)   {
00179     Array moduleDependList = module.getDependModuleList();
00180     int depsize = moduleDependList.size();
00181     for (int j = 0; j < depsize; j++) {
00182       //verify dependance module version
00183       DependProfileModule dep = (DependProfileModule) moduleDependList.get(j);
00184       if (!this.hasProfilModule(profile, dep.getModuleName()))  {
00185         return false;
00186       }      
00187     }
00188     return true;
00189   }
00190   
00191   private boolean hasProfilModule(TerminalProfile profile, String moduleName)   {
00192     int size = profile.moduleList.length;
00193     for (int i=0; i<size ; i++){
00194       if (profile.moduleList[i].moduleName.equals(moduleName))  {
00195         return true;
00196       }
00197     }
00198     return false;
00199   }
00200 
00201   public ProfileModule readModuleXmlFiles(String moduleName) throws FileNotFoundException, IOException, XmlPullParserException   {
00202     //load module file 
00203     File xmlFile = new File(this.moduleRepositoryPath+"/"+moduleName+".xml");
00204     if (!xmlFile.exists())   {
00205       LogManager.traceWarning(0, "Module xml file not found for module :"+moduleName+". Create empty module.");
00206       ProfileModule profil = new ProfileModule();
00207       profil.setName(moduleName);
00208       return profil;
00209     }
00210     Reader reader = new FileReader(xmlFile);
00211     XmlModuleParser parser = new XmlModuleParser(reader);
00212     parser.parse();
00213     return parser.getModuletoload();
00214   } 
00215   
00216   public void notifyModuleUpdateAfterSynchro(ProfileModule module) throws ServiceException      {
00217     if (this.listener != null)  {
00218       listener.notifyModuleUpdate(module);
00219     }
00220   }
00221 
00222 }

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