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.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
00049
00050
00051
00052
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
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 if (this.listener != null) this.listener.initManager();
00100
00101
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
00109
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
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
00135 OpenMSPProfileSyncListener profilListener = new OpenMSPProfileSyncListener();
00136 OpenMSPSynchroManager.getManager().addModuleListener(profilListener, null);
00137 String[] profilNameArray = new String[]{profilListener.getSyncName()};
00138
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
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
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
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 }