[Cherbot-commit] SF.net SVN: cherbot: [116] trunk/src/net/sf/cherbot
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-07-07 12:07:48
|
Revision: 116
http://svn.sourceforge.net/cherbot/?rev=116&view=rev
Author: christianhujer
Date: 2007-07-07 05:07:45 -0700 (Sat, 07 Jul 2007)
Log Message:
-----------
Implemented rudimentary configurator. Servers can now be added or listed.
Modified Paths:
--------------
trunk/src/net/sf/cherbot/Configurator.java
trunk/src/net/sf/cherbot/Server.java
trunk/src/net/sf/cherbot/ServerModule.java
Modified: trunk/src/net/sf/cherbot/Configurator.java
===================================================================
--- trunk/src/net/sf/cherbot/Configurator.java 2007-07-07 12:07:08 UTC (rev 115)
+++ trunk/src/net/sf/cherbot/Configurator.java 2007-07-07 12:07:45 UTC (rev 116)
@@ -6,18 +6,105 @@
package net.sf.cherbot;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.List;
+import net.sf.japi.io.args.ArgParser;
import net.sf.japi.io.args.BasicCommand;
+import net.sf.japi.io.args.Option;
import org.jetbrains.annotations.NotNull;
-import java.util.List;
-/** The Configurator creates or modifies the initial server database for CherBot.
+/** The Configurator creates or modifies the server database for CherBot.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
*/
public class Configurator extends BasicCommand {
+ /** The default database URL. */
+ @NotNull private static final String DEFAULT_DATABASE_URL = "jdbc:derby:cherbotdb;create=true";
+
+ /** The database URL. */
+ @NotNull private String databaseURL = DEFAULT_DATABASE_URL;
+
+ /** The JDBC connection for data persistence. */
+ @NotNull private Connection connection;
+
+ /** The server module. */
+ @NotNull private ServerModule serverModule = new ServerModule();
+
+ /** The command mode. */
+ @NotNull private CommandMode commandMode = CommandMode.LIST;
+
+ /** Main program.
+ * @param args Command line arguments (try --help)
+ */
+ public static void main(@NotNull final String... args) {
+ ArgParser.simpleParseAndRun(new Configurator(), args);
+ }
+
/** {@inheritDoc} */
public int run(@NotNull final List<String> args) throws Exception {
+ initConnection();
+ serverModule.load(connection);
+ switch (commandMode) {
+ case LIST:
+ for (final Server server : serverModule.getServers()) {
+ System.out.println(server);
+ }
+ break;
+ case ADD:
+ final Server server = new Server(0, args.get(0), Integer.parseInt(args.get(1)), args.get(2));
+ serverModule.add(server);
+ serverModule.save(connection);
+ System.out.println("Successfully added " + server);
+ break;
+ }
return 0;
}
+ /** Initializes the connection.
+ * @throws SQLException in case of SQL problems.
+ */
+ private void initConnection() throws SQLException {
+ synchronized (this) {
+ if (connection == null) {
+ connection = DriverManager.getConnection(databaseURL);
+ }
+ }
+ }
+
+ /** Lists the current server database. */
+ @Option({"l", "list"})
+ public void list() {
+ commandMode = CommandMode.LIST;
+ }
+
+ /** Adds the specified server spec. */
+ @Option({"a", "add"})
+ public void add() {
+ commandMode = CommandMode.ADD;
+ }
+
+ /** Returns the database URL.
+ * @return The database URL.
+ */
+ @NotNull public String getDatabaseURL() {
+ return databaseURL;
+ }
+
+ /** Sets the database URL.
+ * @param databaseURL The database URL.
+ */
+ @Option({"databaseURL"})
+ public void setDatabaseURL(@NotNull final String databaseURL) {
+ this.databaseURL = databaseURL;
+ }
+
+ /** Enumeration of supported command modes.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+ private enum CommandMode {
+ ADD, LIST
+ }
+
} // class Configurator
Modified: trunk/src/net/sf/cherbot/Server.java
===================================================================
--- trunk/src/net/sf/cherbot/Server.java 2007-07-07 12:07:08 UTC (rev 115)
+++ trunk/src/net/sf/cherbot/Server.java 2007-07-07 12:07:45 UTC (rev 116)
@@ -25,15 +25,20 @@
/** The port of this server. */
private int port;
+ /** The protocol of this server. */
+ @NotNull private String protocol;
+
/** Create a Server.
* @param id Id of this server.
* @param hostname Hostname of this server.
* @param port Port of this server.
+ * @param protocol Protocol of this server.
*/
- public Server(final int id, @NotNull final String hostname, final int port) {
+ public Server(final int id, @NotNull final String hostname, final int port, @NotNull final String protocol) {
this.id = id;
this.hostname = hostname;
this.port = port;
+ this.protocol = protocol;
}
/** Returns the id of this server.
@@ -71,4 +76,23 @@
this.port = port;
}
+ /** Returns the protocol of this server.
+ * @return The protocol of this server.
+ */
+ @NotNull public String getProtocol() {
+ return protocol;
+ }
+
+ /** Sets the protocol of this server.
+ * @param protocol The protocol of this server.
+ */
+ public void setProtocol(@NotNull final String protocol) {
+ this.protocol = protocol;
+ }
+
+ /** {@inheritDoc} */
+ @NotNull @Override public String toString() {
+ return protocol + "://" + hostname + ":" + port + "/";
+ }
+
} // class Server
Modified: trunk/src/net/sf/cherbot/ServerModule.java
===================================================================
--- trunk/src/net/sf/cherbot/ServerModule.java 2007-07-07 12:07:08 UTC (rev 115)
+++ trunk/src/net/sf/cherbot/ServerModule.java 2007-07-07 12:07:45 UTC (rev 116)
@@ -13,6 +13,7 @@
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
+import java.util.Collections;
import net.sf.cherbot.connection.Channel;
/** Module for managing servers.
@@ -25,13 +26,15 @@
/** {@inheritDoc} */
public void load(@NotNull final Connection con) throws SQLException {
- final PreparedStatement stmt = con.prepareStatement("SELECT id, serverId, name FROM Player");
+ createTables(con);
+ final PreparedStatement stmt = con.prepareStatement("SELECT id, host, port, protocol FROM Server");
final ResultSet result = stmt.executeQuery();
while (result.next()) {
final int id = result.getInt(1);
final String host = result.getString(2);
final int port = result.getInt(3);
- servers.add(new Server(id, host, port));
+ final String protocol = result.getString(4);
+ servers.add(new Server(id, host, port, protocol));
}
stmt.close();
}
@@ -40,21 +43,21 @@
public void save(@NotNull final Connection con) throws SQLException {
createTables(con);
final PreparedStatement lookup = con.prepareStatement("SELECT id FROM Server WHERE id = ?");
- final PreparedStatement update = con.prepareStatement("UPDATE Server SET host = ?, port = ? WHERE id = ?");
- final PreparedStatement insert = con.prepareStatement("INSERT INTO Server (id, host, port) VALUES (?, ?, ?)");
+ final PreparedStatement update = con.prepareStatement("UPDATE Server SET host = ?, port = ?, protocol = ? WHERE id = ?");
+ final PreparedStatement insert = con.prepareStatement("INSERT INTO Server (host, port, protocol) VALUES (?, ?, ?)");
for (final Server server : servers) {
lookup.setInt(1, server.getId());
+ final PreparedStatement stmt;
if (lookup.executeQuery().next()) {
- update.setString(1, server.getHostname());
- update.setInt(2, server.getPort());
- update.setInt(3, server.getId());
- update.executeUpdate();
+ stmt = update;
+ update.setInt(4, server.getId());
} else {
- insert.setInt(1, server.getId());
- insert.setString(2, server.getHostname());
- insert.setInt(3, server.getPort());
- insert.executeUpdate();
+ stmt = insert;
}
+ stmt.setString(1, server.getHostname());
+ stmt.setInt(2, server.getPort());
+ stmt.setString(3, server.getProtocol());
+ stmt.executeUpdate();
}
lookup.close();
update.close();
@@ -67,11 +70,11 @@
*/
private void createTables(@NotNull final Connection con) throws SQLException {
try {
- final PreparedStatement stmt = con.prepareStatement("CREATE TABLE Server (id INT, host VARCHAR(64), port INT");
+ final PreparedStatement stmt = con.prepareStatement("CREATE TABLE Server (id INT NOT NULL GENERATED ALWAYS AS IDENTITY, host VARCHAR(64), port INT, protocol VARCHAR(64))");
stmt.execute();
stmt.close();
} catch (final SQLException ignore) {
- System.err.println(ignore);
+ // System.err.println(ignore);
}
}
@@ -79,4 +82,18 @@
public void parseMessage(@NotNull final Channel channel, @NotNull final String msg) {
}
+ /** Returns a list of servers.
+ * @return a list of servers
+ */
+ public List<Server> getServers() {
+ return Collections.unmodifiableList(servers);
+ }
+
+ /** Adds a server.
+ * @param server Server to add.
+ */
+ public void add(@NotNull final Server server) {
+ servers.add(server);
+ }
+
} // class ServerModule
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|