KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > hsqldb > HSQLEngineServlet


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * 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 JavaDoc;
19
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.http.HttpServlet JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import dlog4j.util.StringUtils;
26
27 /**
28  * 用于启动HSQLEngine的小服务程序,由于数据库引擎必须在应用启动前初始化
29  * 因此不能使用Struts的插件来启动HSQLDB
30  * @author Winter Lau
31  */

32 public class HSQLEngineServlet extends HttpServlet JavaDoc {
33
34     String JavaDoc path = "/WEB-INF/db";
35     String JavaDoc database = "mydlog";
36     int port = 9001;
37     
38     /**
39      * 执行HSQLDB数据库库引擎初始化过程
40      */

41     public void init() throws ServletException JavaDoc {
42         //获取参数
43
String JavaDoc tPath = getInitParameter("path");
44         if(StringUtils.isNotEmpty(tPath))
45             path = tPath;
46         String JavaDoc tDatabase = getInitParameter("database");
47         if(StringUtils.isNotEmpty(tDatabase))
48             database = tDatabase;
49         String JavaDoc 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 JavaDoc dataPath;
56         if(path.startsWith("/"))
57             dataPath = getServletContext().getRealPath(path);
58         else
59             dataPath = path;
60         //启动引擎
61
HSQLEngine engine = HSQLEngine.getEngine(dataPath,port,database);
62         engine.start();
63         while(!engine.isRunning()){
64             try{
65                 Thread.sleep(200);
66             }catch(Exception JavaDoc e){}
67         }
68         log("HSQLEngine of DLOG4J started.");
69     }
70     
71     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
72             throws ServletException JavaDoc, IOException JavaDoc {
73         //TODO:输出数据库当前状态(只有管理员才有权限查看)
74
}
75     
76     /**
77      * 停止数据库引擎
78      */

79     public void destroy() {
80         try{
81             HSQLEngine.getEngine(null,-1,null).stop();
82         }catch(Exception JavaDoc e){}
83     }
84 }
85
Popular Tags