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

Java tutorial

Introduction

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

Source

/**
 * Copyright(c) MQM Science & Technology Ltd. 
 */
package com.qm.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.qm.frame.infrastructure.util.ContextUtil;
import com.qm.frame.infrastructure.util.ValidateLicense;
import com.qm.frame.util.license.LicenseColl;

/** 
 * @Description: ?Spring DIContextLoaderListener
 * @author meihu2007@sina.com
 * @date 20151229 ?9:00:03  
 */
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);

        // 
        setContext(WebApplicationContextUtils.getRequiredWebApplicationContext(context.getServletContext()));

        // 
        ContextUtil.setApplicationContext(ContextInit.context);

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

        LicenseColl licenseList = null;
        try {
            licenseList = (LicenseColl) ContextInit.getContext().getBean("qm_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);
    }

}