KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > runtime > McKoiDBMain


1 /**
2  * com.mckoi.runtime.McKoiDBMain 11 Aug 2000
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.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 JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.File JavaDoc;
42 import java.sql.*;
43
44 /**
45  * The start point of the Mckoi SQL database server. This application is
46  * used as a command-line tool for creating and booting a database.
47  *
48  * @author Tobias Downer
49  */

50
51 public class McKoiDBMain {
52
53   /**
54    * Print the syntax.
55    */

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   /**
106    * Performs the create command.
107    */

108   private static void doCreate(String JavaDoc database_name,
109                                String JavaDoc username, String JavaDoc password,
110                                DBConfig config) {
111
112     DBController controller = DBController.getDefault();
113     // Create the database with the given configuration then close it
114
DBSystem database = controller.createDatabase(config, username, password);
115     database.close();
116
117   }
118
119   /**
120    * Performs the shutdown command.
121    */

122   private static void doShutDown(String JavaDoc host, String JavaDoc port,
123                                  String JavaDoc username, String JavaDoc password) {
124
125     // Actually - config bundle useless for this....
126
Connection connection;
127     try {
128       Class.forName("com.mckoi.JDBCDriver").newInstance();
129
130       String JavaDoc url = ":jdbc:mckoi://" + host + ":" + port + "/";
131       connection = DriverManager.getConnection(url, username, password);
132     }
133     catch (Exception JavaDoc 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   /**
157    * Performs the boot command.
158    */

159   private static void doBoot(DBConfig conf) {
160
161     DBController controller = DBController.getDefault();
162     // Start the database in the local JVM
163
DBSystem database = controller.startDatabase(conf);
164     // Connect a TCPJDBCServer to it.
165
TCPJDBCServer server = new TCPJDBCServer(database);
166     // And start the server
167
server.start();
168
169     // Output a message telling us about the server
170
System.out.print(server.toString());
171     System.out.println(".");
172
173   }
174
175   /**
176    * The McKoi Database application starting point.
177    */

178   public static void main(String JavaDoc[] args) {
179
180     CommandLine command_line = new CommandLine(args);
181
182     // Print the startup message,
183
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     // Print help?
195
if (command_line.containsSwitchFrom("-h,--help,/?")) {
196       printSyntax();
197       return;
198     }
199
200     // The name of the database
201
String JavaDoc database_name = "DefaultDatabase";
202
203     if (command_line.containsSwitch("-shutdown")) {
204       // Try to match the shutdown switch.
205
String JavaDoc[] 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     // Get the conf file if applicable.
231
String JavaDoc conf_file = command_line.switchArgument("-conf", "./db.conf");
232
233     // Extract the root part of the configuration path. This will be the root
234
// directory.
235
File JavaDoc absolute_config_path = new File JavaDoc(
236                                      new File JavaDoc(conf_file).getAbsolutePath());
237     File JavaDoc root_path = new File JavaDoc(absolute_config_path.getParent());
238     // Create a default DBConfig object
239
DefaultDBConfig config = new DefaultDBConfig(root_path);
240     try {
241       config.loadFromFile(new File JavaDoc(conf_file));
242     }
243     catch (IOException JavaDoc 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     // Any configuration overwritten switches?
251
String JavaDoc[] 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     // Find all '-C*' style switches,
268
String JavaDoc[] 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 JavaDoc c_arg = c_args[i].substring(2);
272         int split_point = c_arg.indexOf("=");
273         if (split_point > 0) {
274           String JavaDoc key = c_arg.substring(0, split_point);
275           String JavaDoc 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     // Try to match create switch.
285
String JavaDoc[] 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     // Log the start time.
293
long start_time = System.currentTimeMillis();
294
295     // Nothing matches, so we must be wanting to boot a new server
296
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