1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.server.web; |
7 | |
8 | import java.sql.Connection; |
9 | import java.sql.DriverManager; |
10 | import java.sql.Statement; |
11 | |
12 | import javax.servlet.ServletContext; |
13 | import javax.servlet.ServletContextEvent; |
14 | import javax.servlet.ServletContextListener; |
15 | |
16 | import org.h2.tools.Server; |
17 | import org.h2.util.StringUtils; |
18 | |
19 | /** |
20 | * This class can be used to start the H2 TCP server (or other H2 servers, for |
21 | * example the PG server) inside a web application container such as Tomcat or |
22 | * Jetty. It can also open a database connection. |
23 | */ |
24 | public class DbStarter implements ServletContextListener { |
25 | |
26 | private Connection conn; |
27 | private Server server; |
28 | |
29 | @Override |
30 | public void contextInitialized(ServletContextEvent servletContextEvent) { |
31 | try { |
32 | org.h2.Driver.load(); |
33 | |
34 | // This will get the setting from a context-param in web.xml if |
35 | // defined: |
36 | ServletContext servletContext = servletContextEvent.getServletContext(); |
37 | String url = getParameter(servletContext, "db.url", "jdbc:h2:~/test"); |
38 | String user = getParameter(servletContext, "db.user", "sa"); |
39 | String password = getParameter(servletContext, "db.password", "sa"); |
40 | |
41 | // Start the server if configured to do so |
42 | String serverParams = getParameter(servletContext, "db.tcpServer", null); |
43 | if (serverParams != null) { |
44 | String[] params = StringUtils.arraySplit(serverParams, ' ', true); |
45 | server = Server.createTcpServer(params); |
46 | server.start(); |
47 | } |
48 | |
49 | // To access the database in server mode, use the database URL: |
50 | // jdbc:h2:tcp://localhost/~/test |
51 | conn = DriverManager.getConnection(url, user, password); |
52 | servletContext.setAttribute("connection", conn); |
53 | } catch (Exception e) { |
54 | e.printStackTrace(); |
55 | } |
56 | } |
57 | |
58 | private static String getParameter(ServletContext servletContext, |
59 | String key, String defaultValue) { |
60 | String value = servletContext.getInitParameter(key); |
61 | return value == null ? defaultValue : value; |
62 | } |
63 | |
64 | /** |
65 | * Get the connection. |
66 | * |
67 | * @return the connection |
68 | */ |
69 | public Connection getConnection() { |
70 | return conn; |
71 | } |
72 | |
73 | @Override |
74 | public void contextDestroyed(ServletContextEvent servletContextEvent) { |
75 | try { |
76 | Statement stat = conn.createStatement(); |
77 | stat.execute("SHUTDOWN"); |
78 | stat.close(); |
79 | } catch (Exception e) { |
80 | e.printStackTrace(); |
81 | } |
82 | try { |
83 | conn.close(); |
84 | } catch (Exception e) { |
85 | e.printStackTrace(); |
86 | } |
87 | if (server != null) { |
88 | server.stop(); |
89 | server = null; |
90 | } |
91 | } |
92 | |
93 | } |