1 24 25 package com.mckoi.database.control; 26 27 import java.net.InetAddress ; 28 import java.net.UnknownHostException ; 29 import com.mckoi.database.jdbcserver.TCPServer; 30 31 44 45 public class TCPJDBCServer { 46 47 50 private final static int DEFAULT_TCP_PORT = 9157; 51 52 55 private DBSystem system; 56 57 61 private InetAddress bind_address; 62 63 66 private int tcp_port; 67 68 71 private TCPServer server; 72 73 74 82 public TCPJDBCServer(DBSystem system, 83 InetAddress 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 99 public TCPJDBCServer(DBSystem system, int tcp_port) { 100 this(system, null, tcp_port); 101 } 102 103 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 interface_address = null; 119 120 String jdbc_port_str = config.getValue("jdbc_server_port"); 122 String 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 e) { 129 throw new RuntimeException ("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 e) { 137 throw new RuntimeException ("Unknown host: " + e.getMessage()); 138 } 139 } 140 141 this.tcp_port = jdbc_port; 143 this.bind_address = interface_address; 144 145 registerShutdownDelegate(); 146 } 147 148 152 private void registerShutdownDelegate() { 153 system.getDatabase().registerShutDownDelegate(new Runnable () { 154 public void run() { 155 if (server != null) { 156 stop(); 157 } 158 } 159 }); 160 } 161 162 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 ( 173 "'start' method called when a server was already started."); 174 } 175 } 176 177 184 public synchronized void stop() { 185 if (server != null) { 186 server.close(); 187 server = null; 188 } 189 else { 190 throw new RuntimeException ( 191 "'stop' method called when no server was started."); 192 } 193 } 194 195 196 200 public String toString() { 201 return server.toString(); 202 } 203 204 } 205 | Popular Tags |