/* Look for the run method of CommentsConnection class. */ import java.sql.*; import java.util.Properties; import java.io.*; import java.net.*; public class CommentsServer extends Thread { public static final int DEFAULT_PORT = 7777; protected int port; protected ServerSocket server; private static final String driverPrefixURL = "jdbc:odbc:"; private static String username = null; private static String password = null; private static String dataSource = null; public static String getUsername() { return username; } public static String getPassword() { return password; } public static String getDriverURL() { return driverPrefixURL+dataSource; } public static void main (String args[]) { int port=0; if (args.length == 1) { try { port = Integer.parseInt (args[0]); } catch (NumberFormatException e) { } } try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { System.err.println("Failed to load JDBC/ODBC driver."); System.exit (1); } try { // Look for resource file 'odbc.datasource' InputStream is = ClassLoader.getSystemResourceAsStream ("odbc.datasource"); Properties p = new Properties(); p.load (is); dataSource = p.getProperty("datasource.name"); if (dataSource == null) throw new Exception (); username = p.getProperty("datasource.username", ""); password = p.getProperty("datasource.password", ""); } catch (Exception e) { System.err.println("Unable to read resource to get data source"); System.exit (1); } new CommentsServer (port); } public CommentsServer (int port) { super ("Comments Server"); if (port == 0) port = DEFAULT_PORT; this.port = port; try { server = new ServerSocket (port); } catch (IOException e) { System.err.println ("Error creating server"); System.exit (1); } start(); } public void run() { System.out.println ("Server Running"); ThreadGroup connections = new ThreadGroup ("Comment Connections"); connections.setMaxPriority(this.getPriority()-1); try { while (true) { Socket client = server.accept(); System.out.println ("Connection from: " + client.getInetAddress().getHostName()); CommentsConnection c = new CommentsConnection (connections, client); } } catch (IOException e) { System.err.println ("Exception listening"); System.exit (1); } System.exit (0); } } class CommentsConnection extends Thread { static int counter = 0; protected ObjectInputStream in; protected PrintWriter out; public CommentsConnection (ThreadGroup group, Socket client) { super (group, "Connection " + counter++); try { in = new ObjectInputStream (client.getInputStream ()); out = new PrintWriter (client.getOutputStream(), true); } catch (IOException e) { try { client.close(); } catch (IOException e2) { } System.err.println ("Unable to connect."); return; } start(); } /* * Commands are sent to thread across the Socket connection * using an ObjectInputStream (Serialization). * Results are sent back across the Socket connection * using a PrinterWriter, which is functionally equivalent to * a PrintStream but works with 16-bit characters. * If command is 'insert', three Strings follow representing * the name, username, and comments. * Send back any error messages in PrintWriter. * If command is 'query', just select all from comments table. */ public void run () { try { String mode = (String)in.readObject(); if (/* Insert Mode */) { // Read in three String objects from 'in' ObjectInputStream. // Follow example of 'mode' above. try { // Create Connection // Create Prepared Statement / Statement and fill it up. // Execute it and check if it inserted one row } catch (Exception e) { out.println ("Error updating: " + e); return; } } else if (/* Query Mode */) { try { // Create Connection // Create Statement // Execute Statement and get Results // Loop through records } catch (Exception e) { out.println ("Error querying: " + e); return; } } else { out.println ("Invalid Command: " + mode); } } catch (Exception e) { out.println ("Error reading Stream: " + e); } out.close(); } }