package demo.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; 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; import org.springframework.util.FileCopyUtils; /** * 该类的职责是在WebApp启动时自动开启HSQL服务. 依然使用Server方式,不受AppServer的影响. * * @author frank * @author calvin */ public class HsqlListener implements ServletContextListener { protected static Log logger = LogFactory.getLog(HsqlListener.class); private final static String DEFAULT_DB_PATH = "{user.home}/db"; private final static String DEFAULT_DB_SOURCE_SCRIPT = "default/default_db.script"; private final static String DEFAULT_DB_SOURCE_PROPERTIES = "default/default_db.properties"; Server server; public void contextInitialized(ServletContextEvent sce) { logger.info("HsqlListener initialize..."); String dbName = sce.getServletContext().getInitParameter("hsql.dbName"); String reset = sce.getServletContext().getInitParameter("hsql.reset"); 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 + "/"; } File scriptFile = new File(path + dbName + ".script"); File propertiesFile = new File(path + dbName + ".properties"); if (reset.equals("1") || reset.equalsIgnoreCase("true") || !scriptFile.exists()) { logger.info("Create DataFile:" + scriptFile.getPath()); try { FileCopyUtils.copy(new FileInputStream(path + DEFAULT_DB_SOURCE_SCRIPT), new FileOutputStream( scriptFile)); FileCopyUtils.copy(new FileInputStream(path + DEFAULT_DB_SOURCE_PROPERTIES), new FileOutputStream( propertiesFile)); } catch (IOException e) { logger.error("Copy Default script file error"); return; } } startServer(path, dbName, port); } private String getDbPath(ServletContextEvent sce) { String path = sce.getServletContext().getInitParameter("hsql.dbPath"); if (StringUtils.isEmpty(path)) { path = DEFAULT_DB_PATH; } if (path.startsWith("{user.home}")) { path = path.replaceFirst("\\{user.home\\}", System.getProperty( "user.home").replace('\\', '/')); } if (path.startsWith("{webapp.root}")) { path = path.replaceFirst("\\{webapp.root\\}", sce .getServletContext().getRealPath("/").replace('\\', '/')); } return path; } private void startServer(String dbPath, String dbName, int port) { try { server = new Server(); server.setDatabaseName(0, dbName); System.out.println(dbPath + dbName); server.setDatabasePath(0, dbPath + dbName); if (port != -1) { server.setPort(port); } // server.setSilent(true); server.start(); // 等待Server启动 Thread.sleep(1000); } catch (Exception e) { // do nothing } logger.info("hsqldb started..."); } public void contextDestroyed(ServletContextEvent sce) { try { server.shutdown(); server.stop(); Thread.sleep(2000); } catch (InterruptedException e) { // do nothing } logger.info("hsqldb stoped..."); } }