com.mqm.frame.infrastructure.startup.ContextInit.java Source code

Java tutorial

Introduction

Here is the source code for com.mqm.frame.infrastructure.startup.ContextInit.java

Source

/**
 * Copyright(c) Foresee Science & Technology Ltd. 
 */
package com.mqm.frame.infrastructure.startup;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.mqm.frame.infrastructure.util.ContextUtil;
import com.mqm.frame.infrastructure.util.ValidateLicense;
import com.mqm.frame.util.license.LicenseColl;

/**
 * 
 * <pre>
 * ?Spring DIContextLoaderListener
 * </pre>
 * 
 * @author luoshifei luoshifei@foresee.cn
 * @version 1.00.00
 * 
 *          <pre>
 * 
 *    ?:       :     :
 * </pre>
 */
public class ContextInit extends ContextLoaderListener {

    private static final Log log = LogFactory.getLog(ContextInit.class);

    private static ApplicationContext context;

    private static ServletContext servletContext;

    /**
     * ServletContext
     * 
     * @return ServletContext
     */
    public static ServletContext getServletContext() {
        return servletContext;
    }

    /**
     * Spring DI
     * 
     * @return ApplicationContext
     */
    public static ApplicationContext getContext() {
        return context;
    }

    /**
     * Spring DI
     * 
     * @param ctx ApplicationContext
     */
    public static void setContext(ApplicationContext ctx) {
        context = ctx;
    }

    /**
     * ?DI
     * 
     * @param context ServletContextEvent
     */
    public void contextInitialized(ServletContextEvent context) {

        final ServletContext servletContext = context.getServletContext();

        ContextInit.servletContext = servletContext;

        super.contextInitialized(context);

        // FBRP
        setContext(WebApplicationContextUtils.getRequiredWebApplicationContext(context.getServletContext()));
        ContextUtil.setApplicationContext(ContextInit.context);

        // ?FBRP?
        ContextUtil.setApplicationPath(context.getServletContext().getRealPath("/"));

        /*log.info("?Portlet...");
        log.debug("DriverConfiguration");
        DriverConfiguration driverConfiguration = (DriverConfiguration) ContextInit.context
        .getBean("fbrp_admin_driverConfiguration");
        log.debug("DriverConfiguration");
        servletContext.setAttribute(AttributeKeys.DRIVER_CONFIG, driverConfiguration);
            
        log.debug("?AdminConfiguration");
        AdminConfiguration adminConfiguration = (AdminConfiguration) ContextInit.context
        .getBean("fbrp_admin_adminConfiguration");
            
        if (adminConfiguration != null) {
           log.debug("?AdminConfiguration");
           servletContext.setAttribute(AttributeKeys.DRIVER_ADMIN_CONFIG, adminConfiguration);
        }
            
        log.debug("Portlet");
        PortletContainer container = (PortletContainer) ContextInit.context
        .getBean("fbrp_admin_portletContainer");
            
        log.debug("Portlet");
        servletContext.setAttribute(AttributeKeys.PORTLET_CONTAINER, container);
        log.info("Portlet????");
        */

        //TODO luxiaocheng  ?? 
        /*try {
           // 
           Scheduler scheduler = (Scheduler) ContextInit.getContext().getBean(
           "fbrp_scheduler");
           if (scheduler != null) {
        scheduler.start();
           }
        } catch (Exception e) {
           log.warn("??", e);
        }*/

        LicenseColl licenseList = null;
        try {
            licenseList = (LicenseColl) ContextInit.getContext().getBean("fbrp_licenseColl");
        } catch (Exception exe) {
            log.warn("License?");
        }
        if (licenseList != null) {
            boolean licenseIsValid = ValidateLicense.validateLicense(licenseList, ContextUtil.getApplicationPath());
            ContextUtil.put("licenseIsValid", licenseIsValid, ContextUtil.SCOPE_APPLICATION);
        } else {
            ContextUtil.put("licenseIsValid", true, ContextUtil.SCOPE_APPLICATION);
        }
    }

    /**
     * ?DI
     * 
     * @param event ServletContextEvent
     */
    public void contextDestroyed(ServletContextEvent event) {
        ServletContext servletContext = event.getServletContext();
        log.info("?Portlet");

        log.info("??Portlet");
        super.contextDestroyed(event);
    }

}