00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00051
00052
00053
00054
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
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
00095
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
00108
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
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
00134 OpenMSPProfileSyncListener profilListener = new OpenMSPProfileSyncListener();
00135 OpenMSPSynchroManager.getManager().addModuleListener(profilListener, null);
00136 String[] profilNameArray = new String[]{profilListener.getSyncName()};
00137
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
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
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
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 }