⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hsqllistener.java

📁 wmoa办公自动化系统 小型的JAVA项目 功能包括审批流 消息发布等
💻 JAVA
字号:
package com.dudu.moa.commons;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

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

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hsqldb.Server;

/**
 * Created on 2006-10-27
 * <p>Title: HSQLDB数据库web容器监听器</p>
 * <p>Description: 该类的职责是在WebApp启动时自动开启HSQL服务.
 * 依然使用Server方式,不受AppServer的影响.</p>
 * <p>Copyright: Copyright GSWW (c) 2006</p>
 * <p>Company: wanwei.com</p>
 * @author <a href="mailto:denglq@wanwei.com.cn">邓利强</a>
 * @version 1.0
 */
public class HsqlListener implements ServletContextListener {
    protected static Log logger = LogFactory.getLog(HsqlListener.class);
    
	private static final String WEB_APP_ROOT_KEY_PARAM = "webAppRootKey";

	/** Default web app root key: "webapp.root" */
	private static final String DEFAULT_WEB_APP_ROOT_KEY = "webapp.root";

    public void contextInitialized(ServletContextEvent sce) {
        logger.info("HsqlListener initialize...");

        String dbName = sce.getServletContext().getInitParameter("hsql.dbName");
        dbName = "moa";
        int port = -1;
        try {
            port = Integer.parseInt(sce.getServletContext().getInitParameter("hsql.port"));
        }
        catch (Exception e) {
        }

        if (StringUtils.isEmpty(dbName)) {
            logger.error("Cant' get hsqldb.dbName from web.xml Context Param");
            return;
        }

        String path = getDbPath(sce);

        File dbDir = new File(path);
        if (!dbDir.exists()) {
            logger.info("Create Path:" + path);
            if (!dbDir.mkdirs()) {
                logger.error("Can not create DB Dir for Hsql:" + dbDir);
                return;
            }
        }

        if (!path.endsWith("/"))
            path = path + "/";

        startServer(path, dbName, port);
    }

    private String getDbPath(ServletContextEvent sce) {
        String path = "";
        path = sce.getServletContext().getInitParameter("hsql.dbPath");
        
        String webAppRoot = sce.getServletContext().getInitParameter("webAppRootKey");
        if (StringUtils.isEmpty(webAppRoot)) {
        	throw new IllegalStateException("WARNING: Web app root system property not set: webAppRootKey = XXX");
        }
        
        if (StringUtils.isEmpty(path)) {
            path = "/";
        }
        if (path.startsWith("{user.home}")) {
            path = path.replaceFirst("\\{user.home\\}", System.getProperty(
                    "user.home").replace('\\', '/'));
        }
        if (path.startsWith("{" + webAppRoot +"}")) {
            path = path.replaceFirst("\\{" + webAppRoot +"\\}", sce
                    .getServletContext().getRealPath("/").replace('\\', '/'));
        }
        return path;
    }
    
    private void startServer(String dbPath, String dbName, int port) {
        Server server = new Server();
        server.setDatabaseName(0, dbName);
        server.setDatabasePath(0, dbPath + dbName);
        if (port != -1)
            server.setPort(port);

        server.setSilent(true);
        server.start();
        logger.info("hsqldb started...");
        // 等待Server启动
        try {
            Thread.sleep(1500);
        }
        catch (InterruptedException e) {
            // do nothing
        }
        logger.info("hsqldb success start");
    }

    public void contextDestroyed(ServletContextEvent arg0) {

        Connection conn = null;
        try {
            Class.forName("org.hsqldb.jdbcDriver");
            conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:9002/moa",
                    "SA", "");
            Statement stmt = conn.createStatement();
            stmt.executeUpdate("SHUTDOWN;");
        } catch (Exception e) {
            //do nothing
        }

    }
    
    public static String getWebAppRoot(ServletContext servletContext) throws IllegalStateException { 
        String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); 
        String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); 
        String oldValue = System.getProperty(key); 
        if (oldValue != null) { 
            throw new IllegalStateException("WARNING: Web app root system property already set: " + key + " = " + 
            			oldValue + " - Choose unique webAppRootKey values in your web.xml files!"); 
        } 
        String root = servletContext.getRealPath("/"); 
        if (root == null) { 
            throw new IllegalStateException("Cannot set web app root system property when WAR file is not expanded"); 
        } 
        servletContext.log("Set web app root system property: " + key + " = " + root);
        return root;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -