You are on page 1of 6

Table

of Contents
1. Introduction
2. About this book
3. How to start
4. WebServer
H2 Database Source book
This is a book about h2 database source code.
How to start

When reading code, how can we find where to start? How can we find the class with the main method in lots of java codes?

Actually, it's very easy if you know how to start the program you write. For h2 database, we can easily find how to start it on
its website. We can just run 'java -jar h2-*.jar' to start. But how dose the jar command know which class contains the main
method. It's in the jar file's menifest file. You can unzip the jar file, and you can also find a line with 'Mainclass', which
indicate the entry of you program.

And now, we know where to start. Let's begin with the main class h2.org.tools.Console .

The main method

Main method is where our program starts. So we can just follow it and find out how h2 database was implemented.

The main method is pretty easy:

public static void main(String... args) throws SQLException {


new Console().runTool(args);
}

In the main method we initialized an instance of Console, and then run it's runtTool method with args.

The runTool method

As the comment tells us, this tool starts the H2 Console (web-) server, as well as the TCP and PG server.

First part of the code parses parameters from command line. This parameters contains:

isWindows : wether current is windows or not


tcpStart : -tcp This option starts the tcp server
pgStart : -pg
webStart : -web
toolStart : -tool
browserStart : -browser
startDefaultServers :
printStatus :
driver
url
user
password : -password
tcpShutdown
tcpShutdownForce : -tcpShutdownForce
tcpPassword : -tcpPassword
tcpShutdownServer

webStart

This option starts a standalone HTTP server that implements the H2 Console application.

if (webStart) {
try {
web = Server.createWebServer(args);
web.setShutdownHandler(this);
web.start();
if (printStatus) {
out.println(web.getStatus());
}
webRunning = true;
} catch (SQLException e) {
printProblem(e, web);
startException = e;
}
}

tcpStart

if (tcpStart) {
try {
tcp = Server.createTcpServer(args);
tcp.start();
if (printStatus) {
out.println(tcp.getStatus());
}
tcp.setShutdownHandler(this);
} catch (SQLException e) {
printProblem(e, tcp);
if (startException == null) {
startException = e;
}
}
}

pgStart

if (pgStart) {
try {
pg = Server.createPgServer(args);
pg.start();
if (printStatus) {
out.println(pg.getStatus());
}
} catch (SQLException e) {
printProblem(e, pg);
if (startException == null) {
startException = e;
}
}
}

Exception

If there is any exception in starting process, the server shuts down.

if (startException != null) {
shutdown();
throw startException;
}
WebServer

The next part we are going to talk about is the webserver.

First let's start with where we started about webserver in last chapter. Let's see following codes:

1 if (webStart) {
2 try {
3 web = Server.createWebServer(args);
4 web.setShutdownHandler(this);
5 web.start();
6 if (printStatus) {
7 out.println(web.getStatus());
8 }
9 webRunning = true;
10 } catch (SQLException e) {
11 printProblem(e, web);
12 startException = e;
13 }
14}

This is how we started a webserver.

line 3 : we create a webserver


line 4 : we set a shutdown handler for our server
line 5 : we started our server
line 6 to line 8 : if the variable printStatus is set to true, the status of webserver is printed
line 9 : set webRunning to true
line 10 to 13 : deal with Excepiton

createWebServer

Now, let's see how dose the method createWebServer create the webserver for us.

1 public static Server createWebServer(String... args) throws SQLException {


2 WebServer service = new WebServer();
3 Server server = new Server(service, args);
4 service.setShutdownHandler(server);
5 return server;
6 }

From above code, we can see that :

First, we create a web server instance.


Second, we use our newly created service and args to create a Server.
Third, set the shutdown hanlder for our server.

Now, you may konw how this works.We use service to provide service for our server, and the server deal with our clients.

You might also like