📄 hsqllistener.java
字号:
package cn.haose.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hsqldb.Server;
/**
* 该类的职责是在WebApp启动时自动开启HSQL服务, 依然使用Server方式,不受AppServer的影响.
* 采用springside的HsqlListener类
*/
public class HsqlListener implements ServletContextListener {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(HsqlListener.class);
private final static String DEFAULT_DB_PATH = "{user.home}/haose/db";
private final static String DEFAULT_DB_SOURCE_SCRIPT = "/default-db.script";
private final static String DEFAULT_DB_SOURCE_PROPERTIES = "/default-db.properties";
public static boolean copy(InputStream in1, OutputStream out1) {
try {
byte[] bytes = new byte[65536];
int c;
while ((c = in1.read(bytes)) != -1) {
out1.write(bytes, 0, c);
}
in1.close();
out1.close();
return true; // if success then return true
}
catch (Exception e) {
return false;
} finally {
try {
in1.close();
out1.close();
} catch (Throwable t) {
// do nothing
}
}
}
private Server server = null;
private Connection cn = null; // for test the server.
public void contextInitialized(ServletContextEvent sce) {
logger.info("HsqlListener initialize...");
String path = getDbPath(sce, "hsql.dbPath");
String dbName = sce.getServletContext().getInitParameter("hsql.dbName");
int port = -1;
try {
port = Integer.parseInt(sce.getServletContext().getInitParameter(
"hsql.port"));
} catch (Exception e) {
// do nothing
}
if (StringUtils.isEmpty(dbName)) {
logger.error("Cant' get hsqldb.dbName from web.xml Context Param");
return;
}
File dbPath = new File(path);
if (!dbPath.exists()) {
logger.info("Create Path:" + path);
if (!dbPath.mkdirs()) {
logger.error("Can not create dbPath4Hsql:" + dbPath);
return;
}
}
if (!path.endsWith("/")) {
path = path + "/";
}
File dbFileScript = new File(path + dbName + ".script");
File dbFileProperties = new File(path + dbName + ".properties");
if (!dbFileScript.exists()) {
logger.info("Create DataFile:" + dbFileScript.getPath());
try {
copy(this.getClass().getResourceAsStream(
DEFAULT_DB_SOURCE_SCRIPT), new FileOutputStream(
dbFileScript));
copy(this.getClass().getResourceAsStream(
DEFAULT_DB_SOURCE_PROPERTIES), new FileOutputStream(
dbFileProperties));
} catch (FileNotFoundException e) {
logger.error("Can not create dbFile4Hsql:" + dbPath);
try {
dbFileScript.delete();
} catch (Exception ex) {
// do nothing: only for try again.
}
return;
}
}
startServer(path, dbName, port);
}
private String getDbPath(ServletContextEvent sce, String param) {
String path = sce.getServletContext().getInitParameter(param);
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("{webExtDemo.root}")) {
path = path.replaceFirst("\\{webExtDemo.root\\}", sce
.getServletContext().getRealPath("/").replace('\\', '/'));
}
return path;
}
private void startServer(String dbPath, String dbName, int port) {
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(1000);
} catch (InterruptedException e) {
// do nothing
}
}
public void contextDestroyed(ServletContextEvent arg0) {
try {
if (server != null) {
server.shutdown();
}
} catch (Exception e) {
;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -