Java tutorial
/** * PureInfo Quake * @(#)SRMConfigHelper.java 1.0 Oct 5, 2005 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.config; import org.apache.log4j.Logger; import org.dom4j.Element; import com.pureinfo.force.exception.PureRuntimeException; import com.pureinfo.force.io.ClassResourceUtil; import com.pureinfo.force.xml.XMLUtil; import com.pureinfo.srm.SRMConstants; import com.pureinfo.srm.SRMExceptionTypes; import com.pureinfo.srm.config.register.SNDecoder; /** * <P> * Created on Oct 5, 2005 11:29:05 AM <BR> * Last modified on Oct 5, 2005 * </P> * SRMConfigHelper: SRM system config. * * @author Why * @version 1.0, Oct 5, 2005 * @since Quake 1.0 */ public class SRMConfigHelper { // logger private final static Logger logger = Logger.getLogger(SRMConfigHelper.class.getName()); /** xml binder for configuration */ public final static String XML_BINDER = "SRM.cfg.xml"; // data private String m_sVersion; private String m_sEdition; private boolean m_bUniversityEdition; private UniversityConfig m_universityConfig; private EduOfficeConfig m_eduOfficeConfig; private ModulesConfig m_modulesConfig; private boolean m_bLoaded = false; private boolean m_bRegistered = false; private long m_lRegisterExpiredTime = 0; // singleton private static SRMConfigHelper s_instance = new SRMConfigHelper(); /** * constructor: forbid to call */ private SRMConfigHelper() { super(); } /** * Returns the instance of this helper. * * @return the instance of this helper. */ public static SRMConfigHelper getInstance() { if (!s_instance.m_bLoaded) { s_instance.insureLoaded(); } return s_instance; } /** * Sets the verion of current application. * * @param _sVersion * the version of current application. */ private void setVersion(String _sVersion) { m_sVersion = _sVersion; } /** * Returns the version of current application. * * @return the version of current application. */ public static String getVersion() { return getInstance().m_sVersion; } /** * Sets the edition. * * @param _sEdition * the application editon */ private void setEdition(String _sEdition) { m_sEdition = _sEdition; m_bUniversityEdition = SRMConstants.EDITION_UNIVERSITY.equalsIgnoreCase(m_sEdition); } /** * Returns the edition of current application. * * @return the edition of current application. */ public static String getEdition() { return getInstance().m_sEdition; } /** * Returns <code>true</code> if current application is university edition. * * @return <code>true</code> if current application is university edition; * <code>false</code> otherwise. */ public static boolean isUniversityEdition() { return getInstance().m_bUniversityEdition; } /** * Returns <code>true</code> if current application is education-office * edition. * * @return <code>true</code> if current application is education-office * edition; <code>false</code> otherwise. */ public static boolean isEduOfficeEdition() { return !getInstance().m_bUniversityEdition; } /** * Returns the university configuration. * * @return the university configuration. */ public static UniversityConfig getUniversityConfig() { return getInstance().m_universityConfig; } /** * Returns the education office configuration. * * @return the education office configuration. */ public static EduOfficeConfig getEduOfficeConfig() { return getInstance().m_eduOfficeConfig; } /** * Returns the registered. * * @return the registered. */ public static boolean isRegistered() { // notice: not to use getInstance() return s_instance.m_bRegistered && s_instance.m_lRegisterExpiredTime >= System.currentTimeMillis(); } /** * return <code>true</code> if module is enabled in config file. * * @param _sModuleName * module name * @return <code>true</code> if the specified module is enabled. */ public static boolean isModuleEnabled(String _sModuleName) { return getInstance().m_modulesConfig.isModuleEnabled(_sModuleName); } // ========================================================================= // inside logic /** * Insures this helper has loaded the configuration. */ private void insureLoaded() { if (m_bLoaded) return; synchronized (this) { if (m_bLoaded) return; this.init(); m_bLoaded = true; } } /** * Initiates this helper. */ private void init() { logger.info("SRM system configuration: to load ..."); try { String sResource = ClassResourceUtil.mapFullPath(XML_BINDER, true); Element xml = XMLUtil.fileToElement(sResource); this.setVersion(xml.elementTextTrim("version")); this.setEdition(xml.elementTextTrim("edition")); if (m_bUniversityEdition) { // to load university configuration m_universityConfig = new UniversityConfig(); m_universityConfig.fromXML(xml.element("university")); // to validate serial no String[] args; try { args = SNDecoder.decode(m_universityConfig.getSerialNo(), 5); } catch (Exception ex) { throw new Exception("www.pureinfo.com.cn"); } if (!m_universityConfig.getCode().equals(args[0]) || !m_universityConfig.getName().equals(args[1])) { throw new Exception("www.pureinfo.com.cn"); } m_lRegisterExpiredTime = Long.parseLong(args[4]); if (System.currentTimeMillis() >= m_lRegisterExpiredTime) { throw new Exception("www.pureinfo.com.cn"); } } else { // to load edu-office configuration m_eduOfficeConfig = new EduOfficeConfig(); m_eduOfficeConfig.fromXML(xml.element("edu-office")); // to validate serial no String[] args; try { args = SNDecoder.decode(m_eduOfficeConfig.getSerialNo(), 5); } catch (Exception ex) { throw new Exception("www.pureinfo.com.cn"); } if (!m_eduOfficeConfig.getCode().equals(args[0]) || !m_eduOfficeConfig.getName().equals(args[1])) { throw new Exception("www.pureinfo.com.cn"); } m_lRegisterExpiredTime = Long.parseLong(args[4]); if (System.currentTimeMillis() >= m_lRegisterExpiredTime) { throw new Exception("www.pureinfo.com.cn"); } } // endif // to load the modules config m_modulesConfig = new ModulesConfig(m_universityConfig.getCode()); m_modulesConfig.fromXML(xml.element("modules")); // OK! m_bRegistered = true; logger.info("SRM system configuration: loaded and registered"); } catch (Exception ex) { throw new PureRuntimeException(SRMExceptionTypes.CONFIG_LOAD, XML_BINDER, ex); } } }