com.scase.core.listener.InitListener.java Source code

Java tutorial

Introduction

Here is the source code for com.scase.core.listener.InitListener.java

Source

/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package com.scase.core.listener;

import java.io.File;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.scase.core.service.CacheService;
import com.scase.core.service.SearchService;
import com.scase.core.service.StaticService;

/**
 * Listener - ?
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Component("initListener")
public class InitListener implements ServletContextAware, ApplicationListener<ContextRefreshedEvent> {

    /** ?? */
    private static final String INSTALL_INIT_CONFIG_FILE_PATH = "/install_init.conf";

    /** logger */
    private static final Logger logger = Logger.getLogger(InitListener.class.getName());

    /** servletContext */
    private ServletContext servletContext;

    @Value("${system.version}")
    private String systemVersion;
    @Resource(name = "staticServiceImpl")
    private StaticService staticService;
    @Resource(name = "cacheServiceImpl")
    private CacheService cacheService;
    @Resource(name = "searchServiceImpl")
    private SearchService searchService;

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (servletContext != null && contextRefreshedEvent.getApplicationContext().getParent() == null) {
            String info = "I|n|i|t|i|a|l|i|z|i|n|g| |S|H|O|P|+|+| |" + systemVersion;
            logger.info(info.replace("|", ""));
            File installInitConfigFile = new File(servletContext.getRealPath(INSTALL_INIT_CONFIG_FILE_PATH));
            if (installInitConfigFile.exists()) {
                cacheService.clear();
                staticService.buildAll();
                //   searchService.purge();
                //   searchService.index();
                installInitConfigFile.delete();
            } else {
                staticService.buildIndex();
                staticService.buildOther();
            }
        }
    }

}