/*******************************************************************
 * $Header: $
 *******************************************************************
 *  Created : 01/09/2002                                           *
 *  Author  : Ben Feinstein <Ben.Feinstein@guardent.com>           *
 *                                                                 *
 *  Copyright (C) 2002 Guardent, Inc.                              *
 *  All rights reserved.                                           *
 *                                                                 *
 *******************************************************************
 * Description :
 *                                                                 *
 * Revision History:                                               *
 *                                                                 *
 *******************************************************************/

import java.sql.*;

public class Pgsql_JDBC_Listen
{
	protected static final String dbDriver = "org.postgresql.Driver";
	protected static final String dbURL = "jdbc:postgresql://localhost/snort";
	protected static final String dbUsr = "postgres";
	protected static final String dbPwd = "";

	protected static final String sql = "LISTEN event";

	protected static final int waitTime = 10000;


	public static void main(String args[])
	{
		Connection conn = null;
		Statement stmt = null;
		SQLWarning warn;
		boolean ret = false;

		try
		{
			Class.forName(dbDriver).newInstance();
			conn = DriverManager.getConnection(dbURL, dbUsr, dbPwd);

			warn = conn.getWarnings();

			while (warn != null)
			{
				System.out.println(warn);
				warn = warn.getNextWarning();
			}

			stmt = conn.createStatement();

			ret = stmt.execute(sql);
			stmt.close();

			System.out.println("Sent listen, result " + ret);

			while(true)
			{
				System.out.println("Checking for warnings...");
				
				warn = conn.getWarnings();

				while (warn != null)
				{
					System.out.println(warn);
					warn = warn.getNextWarning();
				}

				Thread.sleep(waitTime);
			}
		}
		catch(Exception e)
		{
			System.out.println(e);
			System.exit(1);
		}
		finally
		{
			try
			{
				if (stmt != null)
					stmt.close();
	
				if (conn != null)
					conn.close();
			}
			catch (Exception e) {}
		}
	}
}