1 24 25 package com.mckoi.runtime; 26 27 import com.mckoi.database.DatabaseException; 28 import com.mckoi.database.DatabaseSystem; 29 import com.mckoi.database.Database; 30 import com.mckoi.database.control.DefaultDBConfig; 31 import com.mckoi.database.control.TCPJDBCServer; 32 import com.mckoi.database.control.DBController; 33 import com.mckoi.database.control.DBConfig; 34 import com.mckoi.database.control.DBSystem; 35 import com.mckoi.util.CommandLine; 36 import com.mckoi.database.global.StandardMessages; 37 import com.mckoi.database.jdbc.*; 38 39 import java.io.FileInputStream ; 40 import java.io.IOException ; 41 import java.io.File ; 42 import java.sql.*; 43 44 50 51 public class McKoiDBMain { 52 53 56 private static void printSyntax() { 57 System.out.println( 58 "Command Line Arguments:\n" + 59 "\n" + 60 "-- Configuration --\n" + 61 "\n" + 62 " -conf [database_config_file]\n" + 63 " The database configuration file to use. If not specified then it searches\n" + 64 " for 'db.conf' in the current directory.\n" + 65 " -dbpath [database_data_path]\n" + 66 " Specifies where the database data files are located.\n" + 67 " -logpath [log_path]\n" + 68 " Specifies where the logs are to be kept.\n" + 69 " -jdbcaddress [address]\n" + 70 " For multi-homed machines, allows for the database to bind to a particular\n" + 71 " host address.\n" + 72 " -jdbcport [port_number]\n" + 73 " Sets the TCP port where the JDBC clients must connect to.\n" + 74 " -C[key]=[value]\n" + 75 " Where [key] is a configuration property and [value] is a value to set the\n" + 76 " property to. This can be used to override any property in the config.\n" + 77 " file. Example: -Cmaximum_worker_threads=2\n" + 78 "\n" + 79 "-- Functions --\n" + 80 "\n" + 81 " -create [admin_username] [admin_password]\n" + 82 " Creates an empty database and adds a user with the given username and\n" + 83 " password with complete privs. This will not start the database server.\n" + 84 " -shutdown [host] [port] [admin_username] [admin_password]\n" + 85 " Shuts down the database server running on the host/port. [host] and\n" + 86 " [port] are optional, they default to 'localhost' and port 9157.\n" + 87 " -boot\n" + 88 " Boots the database server from the information given in the configuration\n" + 89 " file. This switch is implied if no other function switch is provided.\n" + 90 "\n" + 91 "Examples:\n" + 92 "\n" + 93 " McKoiDBMain -create admuser ad944\n" + 94 " == Creates a new database with admin username 'admuser' and admin password\n" + 95 " 'ad944'.\n" + 96 " McKoiDBMain -conf /home/mckoi/db.conf\n" + 97 " == Boots a database from the configuration file found at\n" + 98 " /home/mckoi/db.conf\n" + 99 " McKoiDBMain -conf db.conf -dbpath /home/myapp/data\n" + 100 " == Boots a database and specifies that the database data path is found at\n" + 101 " /home/myapp/data (overriding the path set in the configuration file).\n" 102 ); 103 } 104 105 108 private static void doCreate(String database_name, 109 String username, String password, 110 DBConfig config) { 111 112 DBController controller = DBController.getDefault(); 113 DBSystem database = controller.createDatabase(config, username, password); 115 database.close(); 116 117 } 118 119 122 private static void doShutDown(String host, String port, 123 String username, String password) { 124 125 Connection connection; 127 try { 128 Class.forName("com.mckoi.JDBCDriver").newInstance(); 129 130 String url = ":jdbc:mckoi://" + host + ":" + port + "/"; 131 connection = DriverManager.getConnection(url, username, password); 132 } 133 catch (Exception e) { 134 e.printStackTrace(System.err); 135 return; 136 } 137 138 try { 139 Statement statement = connection.createStatement(); 140 ResultSet result_set = statement.executeQuery("SHUTDOWN"); 141 statement.close(); 142 } 143 catch (SQLException e) { 144 System.out.println("Unable to shutdown database: " + e.getMessage()); 145 } 146 147 try { 148 connection.close(); 149 } 150 catch (SQLException e) { 151 System.out.println("Unable to close connection: " + e.getMessage()); 152 } 153 154 } 155 156 159 private static void doBoot(DBConfig conf) { 160 161 DBController controller = DBController.getDefault(); 162 DBSystem database = controller.startDatabase(conf); 164 TCPJDBCServer server = new TCPJDBCServer(database); 166 server.start(); 168 169 System.out.print(server.toString()); 171 System.out.println("."); 172 173 } 174 175 178 public static void main(String [] args) { 179 180 CommandLine command_line = new CommandLine(args); 181 182 System.out.println(); 184 System.out.println(StandardMessages.NAME); 185 System.out.println(StandardMessages.COPYRIGHT); 186 System.out.println("Use: -h for help."); 187 188 System.out.println("\n" + 189 " Mckoi SQL Database comes with ABSOLUTELY NO WARRANTY.\n" + 190 " This is free software, and you are welcome to redistribute it\n" + 191 " under certain conditions. See LICENSE.txt for details of the\n" + 192 " GPL License.\n"); 193 194 if (command_line.containsSwitchFrom("-h,--help,/?")) { 196 printSyntax(); 197 return; 198 } 199 200 String database_name = "DefaultDatabase"; 202 203 if (command_line.containsSwitch("-shutdown")) { 204 String [] sd_parm = command_line.switchArguments("-shutdown", 4); 206 if (sd_parm == null) { 207 sd_parm = command_line.switchArguments("-shutdown", 3); 208 if (sd_parm == null) { 209 sd_parm = command_line.switchArguments("-shutdown", 2); 210 if (sd_parm != null) { 211 doShutDown("localhost", "9157", sd_parm[0], sd_parm[1]); 212 return; 213 } 214 else { 215 System.out.println("Incorrect '-shutdown' format."); 216 return; 217 } 218 } 219 else { 220 doShutDown(sd_parm[0], "9157", sd_parm[1], sd_parm[2]); 221 return; 222 } 223 } 224 else { 225 doShutDown(sd_parm[0], sd_parm[1], sd_parm[2], sd_parm[3]); 226 return; 227 } 228 } 229 230 String conf_file = command_line.switchArgument("-conf", "./db.conf"); 232 233 File absolute_config_path = new File ( 236 new File (conf_file).getAbsolutePath()); 237 File root_path = new File (absolute_config_path.getParent()); 238 DefaultDBConfig config = new DefaultDBConfig(root_path); 240 try { 241 config.loadFromFile(new File (conf_file)); 242 } 243 catch (IOException e) { 244 System.out.println( 245 "Error: configuration file '" + conf_file + "' was not found."); 246 System.out.println(); 247 System.exit(1); 248 } 249 250 String [] cparam = command_line.switchArguments("-dbpath", 1); 252 if (cparam != null) { 253 config.setValue("database_path", cparam[0]); 254 } 255 cparam = command_line.switchArguments("-logpath", 1); 256 if (cparam != null) { 257 config.setValue("log_path", cparam[0]); 258 } 259 cparam = command_line.switchArguments("-jdbcaddress", 1); 260 if (cparam != null) { 261 config.setValue("jdbc_server_address", cparam[0]); 262 } 263 cparam = command_line.switchArguments("-jdbcport", 1); 264 if (cparam != null) { 265 config.setValue("jdbc_server_port", cparam[0]); 266 } 267 String [] c_args = command_line.allSwitchesStartingWith("-C"); 269 for (int i = 0; i < c_args.length; ++i) { 270 if (c_args[i].length() > 2) { 271 String c_arg = c_args[i].substring(2); 272 int split_point = c_arg.indexOf("="); 273 if (split_point > 0) { 274 String key = c_arg.substring(0, split_point); 275 String value = c_arg.substring(split_point + 1); 276 config.setValue(key, value); 277 } 278 else { 279 System.out.println("Ignoring -C switch: '" + c_arg + "'"); 280 } 281 } 282 } 283 284 String [] create_parm = command_line.switchArguments("-create", 2); 286 if (create_parm != null) { 287 doCreate(database_name, create_parm[0], create_parm[1], 288 config); 289 return; 290 } 291 292 long start_time = System.currentTimeMillis(); 294 295 doBoot(config); 297 298 long count_time = System.currentTimeMillis() - start_time; 299 System.out.println("Boot time: " + count_time + "ms."); 300 301 return; 302 } 303 304 } 305 | Popular Tags |