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

Generated on Mon Dec 4 11:03:28 2006 for OpenMobileIS by  doxygen 1.5.1-p1