com.nec.harvest.servlet.listener.WebApplicationContextLoaderListener.java Source code

Java tutorial

Introduction

Here is the source code for com.nec.harvest.servlet.listener.WebApplicationContextLoaderListener.java

Source

/**
 * Copyright(C) 2014
 * NEC Corporation All rights reserved.
 * 
 * No permission to use, copy, modify and distribute this software
 * and its documentation for any purpose is granted.
 * This software is provided under applicable license agreement only.
 */
package com.nec.harvest.servlet.listener;

import java.io.File;
import java.io.IOException;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nec.harvest.jasperreport.resolver.JasperReportResolver;
import com.nec.harvest.util.FileUtil;

/**
 * Bootstrap listener to start up and shut down Spring's root
 * WebApplicationContext.
 * 
 * @author sondn
 * 
 */
public class WebApplicationContextLoaderListener extends ContextLoaderListener {

    private static final Logger logger = LoggerFactory.getLogger(WebApplicationContextLoaderListener.class);

    /**
     * Initialize the root web application context
     */
    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);

        // Starting Harvest environment initialization...
        if (logger.isDebugEnabled()) {
            logger.debug("Starting Harvest environment initialization...");
        }

        /// You can get Servlet Context
        ServletContext servletContext = event.getServletContext();
        WebApplicationContext webApplicationContext = WebApplicationContextUtils
                .getRequiredWebApplicationContext(servletContext);
        String jasperReportPath = getReportPath(webApplicationContext);

        // Create new a folder to store all of reports
        logger.info("Trying to create a new local storage {} folder for all reports", jasperReportPath);

        File folder = new File(jasperReportPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        // ????
        logger.info("Successfully created a report storage folder to store all of temporary reports");

        // 
        logger.info("Context instance: {}", webApplicationContext);
        logger.info("Application name: {} && path: {}", webApplicationContext.getApplicationName(),
                servletContext.getContextPath());
    }

    /**
     * Close the root web application context
     */
    @Override
    public void contextDestroyed(ServletContextEvent event) {
        super.contextDestroyed(event);

        // Destroying Harvest application.......
        if (logger.isDebugEnabled()) {
            logger.debug("Destroying Harvest application.......");
        }

        /// You can get Servlet Context
        ServletContext servletContext = event.getServletContext();
        WebApplicationContext webApplicationContext = WebApplicationContextUtils
                .getRequiredWebApplicationContext(servletContext);
        String jasperReportPath = getReportPath(webApplicationContext);

        // Delete jasper report folder
        logger.info("Trying to remove storaged folder for all reports", jasperReportPath);

        File folder = new File(jasperReportPath);
        if (folder.exists()) {
            try {
                FileUtil.delete(folder);
            } catch (IOException ex) {
                logger.warn(ex.getMessage());
            }
        }
    }

    @Override
    protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext context,
            ServletContext event) {
        super.configureAndRefreshWebApplicationContext(context, event);
    }

    /**
     * Get report path
     * 
     * @param webApplicationContext
     * @return
     */
    private String getReportPath(WebApplicationContext webApplicationContext) {
        JasperReportResolver jasperReportResolver = webApplicationContext.getBean(JasperReportResolver.class);

        // No JasperReportResolver specified of Bean
        Assert.notNull(jasperReportResolver, "No JasperReportResolver Bean Specified");
        return jasperReportResolver.getReportPath();
    }

}