KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > control > TCPJDBCServer


1 /**
2  * com.mckoi.database.control.TCPJDBCServer 27 Mar 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.control;
26
27 import java.net.InetAddress JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import com.mckoi.database.jdbcserver.TCPServer;
30
31 /**
32  * Attaches to a DBSystem, and binds a TCP port and serves queries for JDBC
33  * connections. This object is used to programmatically create a TCP JDBC
34  * server on the local machine.
35  * <p>
36  * Note that multiple servers can be constructed to serve the same DBSystem.
37  * You can not use this object to connect a single TCP server to multiple
38  * DBSystem objects.
39  * <p>
40  * If the underlying database is shut down then this server is also shut down.
41  *
42  * @author Tobias Downer
43  */

44
45 public class TCPJDBCServer {
46
47   /**
48    * The default TCP port for Mckoi SQL Database.
49    */

50   private final static int DEFAULT_TCP_PORT = 9157;
51
52   /**
53    * The DBSystem object that we are serving.
54    */

55   private DBSystem system;
56
57   /**
58    * An InetAddress representing the interface that server is bound to - useful
59    * for multi-homed machines. null means we bind to all interfaces.
60    */

61   private InetAddress JavaDoc bind_address;
62
63   /**
64    * The TCP port that this server is bound to.
65    */

66   private int tcp_port;
67
68   /**
69    * The TCPServer object that is managing the connections to this database.
70    */

71   private TCPServer server;
72
73
74   /**
75    * Constructs the TCP JDBC with the given DBSystem object, and sets the
76    * inet address and TCP port that we serve the database from.
77    * <p>
78    * Constructing this server does not open the port to receive connections
79    * from outside. To start the JDBC server you need to call the 'start'
80    * method.
81    */

82   public TCPJDBCServer(DBSystem system,
83                        InetAddress JavaDoc bind_address, int tcp_port) {
84     this.system = system;
85     this.bind_address = bind_address;
86     this.tcp_port = tcp_port;
87     registerShutdownDelegate();
88   }
89
90   /**
91    * Constructs the TCP JDBC with the given DBSystem object, and sets the
92    * TCP port that we serve the database from. This binds the server to all
93    * interfaces on the local machine.
94    * <p>
95    * Constructing this server does not open the port to receive connections
96    * from outside. To start the JDBC server you need to call the 'start'
97    * method.
98    */

99   public TCPJDBCServer(DBSystem system, int tcp_port) {
100     this(system, null, tcp_port);
101   }
102
103   /**
104    * Constructs the TCP JDBC with the given DBSystem object, and sets the
105    * TCP port and address (for multi-homed computers) to the setting of the
106    * configuration in 'system'.
107    * <p>
108    * Constructing this server does not open the port to receive connections
109    * from outside. To start the JDBC server you need to call the 'start'
110    * method.
111    */

112   public TCPJDBCServer(DBSystem system) {
113     this.system = system;
114
115     DBConfig config = system.getConfig();
116
117     int jdbc_port = DEFAULT_TCP_PORT;
118     InetAddress JavaDoc interface_address = null;
119
120     // Read the JDBC config properties.
121
String JavaDoc jdbc_port_str = config.getValue("jdbc_server_port");
122     String JavaDoc interface_addr_str = config.getValue("jdbc_server_address");
123
124     if (jdbc_port_str != null) {
125       try {
126         jdbc_port = Integer.parseInt(jdbc_port_str);
127       }
128       catch (Exception JavaDoc e) {
129         throw new RuntimeException JavaDoc("Unable to parse 'jdbc_server_port'");
130       }
131     }
132     if (interface_addr_str != null) {
133       try {
134         interface_address = InetAddress.getByName(interface_addr_str);
135       }
136       catch (UnknownHostException JavaDoc e) {
137         throw new RuntimeException JavaDoc("Unknown host: " + e.getMessage());
138       }
139     }
140
141     // Set up this port and bind address
142
this.tcp_port = jdbc_port;
143     this.bind_address = interface_address;
144
145     registerShutdownDelegate();
146   }
147
148   /**
149    * Registers the delegate that closes this server when the database
150    * shuts down.
151    */

152   private void registerShutdownDelegate() {
153     system.getDatabase().registerShutDownDelegate(new Runnable JavaDoc() {
154       public void run() {
155         if (server != null) {
156           stop();
157         }
158       }
159     });
160   }
161
162   /**
163    * Starts the server and binds it to the given port. This method will start
164    * a new thread that listens for incoming connections.
165    */

166   public synchronized void start() {
167     if (server == null) {
168       server = new TCPServer(system.getDatabase());
169       server.start(bind_address, tcp_port, "multi_threaded");
170     }
171     else {
172       throw new RuntimeException JavaDoc(
173                 "'start' method called when a server was already started.");
174     }
175   }
176
177   /**
178    * Stops the server running on the given port. This method will stop any
179    * threads that are listening for incoming connections.
180    * <p>
181    * Note that this does NOT close the underlying DBSystem object. The
182    * DBSystem object must be closed separately.
183    */

184   public synchronized void stop() {
185     if (server != null) {
186       server.close();
187       server = null;
188     }
189     else {
190       throw new RuntimeException JavaDoc(
191                         "'stop' method called when no server was started.");
192     }
193   }
194
195
196   /**
197    * Returns a string that contains some information about the server that
198    * is running.
199    */

200   public String JavaDoc toString() {
201     return server.toString();
202   }
203
204 }
205
Popular Tags