com.hihsoft.sso.systempublic.servlet.HihSoftInitDBServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.hihsoft.sso.systempublic.servlet.HihSoftInitDBServlet.java

Source

/**
 * Copyright (c) 2013-2015 www.javahih.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.hihsoft.sso.systempublic.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hihframework.osplugins.dom4j.XmlParseUtil;
import com.hihsoft.baseclass.service.InitFrameworkService;
import com.hihsoft.sso.business.service.TsysParameterService;
import com.hihsoft.sso.systempublic.constants.Constant;

/**
 * Title:??? Description:????????xml??
 * ?Spring????spring mvc 
 * Copyright:Copyright (c) 2013
 * Company:hihsoft.co.,ltd
 * 
 * @author hihsoft.co.,ltd
 * @version 1.0
 */
public class HihSoftInitDBServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    private static final long serialVersionUID = -2029458947542555939L;

    private static Logger log = Logger.getLogger(HihSoftInitDBServlet.class.getName());

    private static ApplicationContext ctx = null;
    private static ServletContext context = null;
    private InitFrameworkService initFrameworkService;
    private TsysParameterService tsysParameterService;

    public HihSoftInitDBServlet() {
        super();

    }

    /**
     * ?XML????????????
     */
    public void init() throws ServletException {
        super.init();
        // ?javahihConfigLocation
        context = getServletContext();
        if (ctx == null) {
            ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        }
        initFrameworkService = (InitFrameworkService) ctx.getBean("initFrameworkService");
        tsysParameterService = (TsysParameterService) ctx.getBean("tsysParameterService");
        // ?????
        //util.getIsFrameworkInitialized(); 2011-03-08 by
        // zhujw:?????????
        try {
            // web.xml???xml?,
            String[] folderPaths = HihSoftInitDBServlet.getParamValue(Constant.SYS_CONFIGlOCATION_XML_NAME)
                    .split(",");
            String path = null;
            // ?
            for (int j = 0; j < folderPaths.length; j++) {
                path = HihSoftInitDBServlet.getAbsolutePath(folderPaths[j]);
                File folder = new File(path);
                readFile(folder);
            }
        } catch (Exception io) {
            io.printStackTrace();
            ServletException ex = new ServletException(io.getMessage());
            ex.setStackTrace(io.getStackTrace());
            throw ex;

        }
        // log.info("" + I18n.getMessage("javahih.about"));
        initSysParam();// modify by zhujw
        // ?????InitPara????spring??
    }

    /**
     * ?javahihxml,xml,
     * 
     * @param file
     */
    private void readFile(File file) throws Exception {
        String name = file.getName();
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                readFile(files[i]);
            }
        } else if (name != null && name.indexOf("hihsoft") == 0
                && name.substring(name.lastIndexOf(".") + 1, name.length()).toUpperCase().equals("XML")) {
            loadFile(file);
        } else {
            log.error("xml?hihsoft?");
        }

    }

    /**
     * xml,"import"?xml
     * 
     * @param file
     */
    @SuppressWarnings("deprecation")
    private void loadFile(File file) throws Exception {
        XmlParseUtil xmlUtil = new XmlParseUtil();
        Document doc = xmlUtil.readXml(file);
        Element root = doc.getRootElement();
        Iterator<?> it = root.elementIterator();
        String path = file.getPath();
        String fileseparate = System.getProperty("file.separator");
        String dir = path.substring(0, path.lastIndexOf(fileseparate));
        Element el = null;
        String isLoad = null;
        String type = null;
        String resource = null;
        File file1 = null;
        Document doc1 = null;
        while (it.hasNext()) {
            el = (Element) it.next();
            isLoad = el.attributeValue("isLoad");
            type = el.attributeValue("type");
            resource = el.attributeValue("resource");
            // "import",xml?
            if (el.getName().equals("import") && isLoad != null && isLoad.equals("true")) {
                file1 = new FileSystemResource(dir + fileseparate + resource).getFile();
                if (file1.isDirectory()) {
                    readFile(file1);
                } else {
                    doc1 = xmlUtil.readXml(file1);
                    // ?
                    if (el.getName().equals("import") && isLoad != null && isLoad.equals("true") && type != null
                            && type.equals("framework")) {
                        initDB(doc1);
                        el.setAttributeValue("isLoad", "false");
                        doc1.getRootElement().element("models").setAttributeValue("isLoad", "false");
                        xmlUtil.writeXml(doc1, file1);

                    }
                }
            } else if (el.getName().equals("models")) {
                initDB(doc);
                doc.getRootElement().element("models").setAttributeValue("isLoad", "false");
                // xmlUtil.writeXml(doc1, file1);
            }
            xmlUtil.writeXml(doc, file);
        }
        initFrameworkService.saveOrUpdateInitFramework();
    }

    /**
     * ?
     * 
     * @param doc
     */
    private void initDB(Document doc) throws Exception {
        initFrameworkService.saveSysInitializeFactory(doc);
    }

    /**
     * ??
     * 
     * @throws ServletException
     */
    private void initSysParam() throws ServletException {
        try {
            tsysParameterService.saveFillParamMap(null);
        } catch (Exception e) {
            ServletException ex = new ServletException();
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
    }

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    }

    /**
     * ??????
     * 
     * @param initParamName
     * @return
     * @throws Exception
     */
    public static String getParamValue(String paramName) throws IOException {
        String ParamValue = context.getInitParameter(paramName);
        return ParamValue;
    }

    /**
     * ?
     * 
     * @param RelativePath
     * @return
     * @throws Exception
     */
    public static String getAbsolutePath(String RelativePath) throws IOException {
        //      return context.getRealPath(RelativePath);
        return ResourceUtils.getFile(RelativePath).getAbsolutePath();
    }

    public boolean accept(File dir, String name) {
        boolean a = false;
        if (name != null && name.indexOf("hihsoft") == 0
                && name.substring(name.lastIndexOf(".") + 1, name.length()).toUpperCase().equals("XML")) {
            a = true;
            // ??.xml
        }
        return a;
    }

    public static void main(String[] args) {
        String fileseparate = System.getProperty("file.separator");
    }
}