EMMA Coverage Report (generated Sun Mar 01 22:06:14 CET 2015)
[all classes][org.h2.server.web]

COVERAGE SUMMARY FOR SOURCE FILE [DbStarter.java]

nameclass, %method, %block, %line, %
DbStarter.java100% (1/1)100% (5/5)89%  (92/103)82%  (27.8/34)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DbStarter100% (1/1)100% (5/5)89%  (92/103)82%  (27.8/34)
getParameter (ServletContext, String, String): String 100% (1/1)80%  (8/10)90%  (1.8/2)
contextDestroyed (ServletContextEvent): void 100% (1/1)81%  (25/31)71%  (10/14)
contextInitialized (ServletContextEvent): void 100% (1/1)95%  (53/56)88%  (14/16)
DbStarter (): void 100% (1/1)100% (3/3)100% (1/1)
getConnection (): Connection 100% (1/1)100% (3/3)100% (1/1)

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 */
6package org.h2.server.web;
7 
8import java.sql.Connection;
9import java.sql.DriverManager;
10import java.sql.Statement;
11 
12import javax.servlet.ServletContext;
13import javax.servlet.ServletContextEvent;
14import javax.servlet.ServletContextListener;
15 
16import org.h2.tools.Server;
17import 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 */
24public 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}

[all classes][org.h2.server.web]
EMMA 2.0.5312 (C) Vladimir Roubtsov