01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: * GNU Library General Public License for more details.
11: *
12: * You should have received a copy of the GNU General Public License
13: * along with this program; if not, write to the Free Software
14: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15: */
16: package dlog4j.hsqldb;
17:
18: import java.io.IOException;
19:
20: import javax.servlet.ServletException;
21: import javax.servlet.http.HttpServlet;
22: import javax.servlet.http.HttpServletRequest;
23: import javax.servlet.http.HttpServletResponse;
24:
25: import dlog4j.util.StringUtils;
26:
27: /**
28: * 用于启动HSQLEngine的小服务程序,由于数据库引擎必须在应用启动前初始化
29: * 因此不能使用Struts的插件来启动HSQLDB
30: * @author Winter Lau
31: */
32: public class HSQLEngineServlet extends HttpServlet {
33:
34: String path = "/WEB-INF/db";
35: String database = "mydlog";
36: int port = 9001;
37:
38: /**
39: * 执行HSQLDB数据库库引擎初始化过程
40: */
41: public void init() throws ServletException {
42: //获取参数
43: String tPath = getInitParameter("path");
44: if (StringUtils.isNotEmpty(tPath))
45: path = tPath;
46: String tDatabase = getInitParameter("database");
47: if (StringUtils.isNotEmpty(tDatabase))
48: database = tDatabase;
49: String tPort = getInitParameter("port");
50: if (StringUtils.isNumeric(tPort)) {
51: int nPort = Integer.parseInt(tPort);
52: if (nPort > 1024 && nPort < 65536)
53: port = nPort;
54: }
55: String dataPath;
56: if (path.startsWith("/"))
57: dataPath = getServletContext().getRealPath(path);
58: else
59: dataPath = path;
60: //启动引擎
61: HSQLEngine engine = HSQLEngine.getEngine(dataPath, port,
62: database);
63: engine.start();
64: while (!engine.isRunning()) {
65: try {
66: Thread.sleep(200);
67: } catch (Exception e) {
68: }
69: }
70: log("HSQLEngine of DLOG4J started.");
71: }
72:
73: protected void doGet(HttpServletRequest req, HttpServletResponse res)
74: throws ServletException, IOException {
75: //TODO:输出数据库当前状态(只有管理员才有权限查看)
76: }
77:
78: /**
79: * 停止数据库引擎
80: */
81: public void destroy() {
82: try {
83: HSQLEngine.getEngine(null, -1, null).stop();
84: } catch (Exception e) {
85: }
86: }
87: }
|