jacquard 1.12.0 by The Web Engineering Factory and Toolworks

uk.co.weft.alert
Class JDBCWatcher

java.lang.Object
  extended byuk.co.weft.alert.Watcher
      extended byuk.co.weft.alert.JDBCWatcher
All Implemented Interfaces:
java.lang.Runnable

public class JDBCWatcher
extends Watcher

An agent which watches for changes to a file and sends alerts based on those events. This class is primarily an example of how to use Watcher, although it is of course useful in its own right.

This agent knows about the following tokens which can be uses as either command-line parameters or configuration file properties:

db_password, p
String: the password to use to connect to the database. Not optional. No default.
db_username, n
String: the username to use to connect to the database. Not optional. No default.
db_url, u
String: the URL to use to connect to the database. Not optional. No default.
driver_name
String: the fully qualified name of the JDBC driver class to use. Note that this class must be available on the classpath. Optional. Defaults to org.postgresql.Driver.
id_field, i
String: the name of an integer field returned by the watch query which when its value is substituted into the mark query will mark the record as having had notification sent. Optional. Defaults to id.
mark_query, m
String: an SQL query with exactly one question mark ('?') serving as a placeholder into which the contents of id_field will be pasted. If supplied, the mark_query will be called once for each row returned by the watch_query to allow you to mark these records as having been serviced by the agent (and thus to prevent duplicate mails being sent). Optional. No default.
watch_query, w
String: an SQL query which will return rows for which emails should be sent. Each fieldname in the query, provided it contains only alpha-numeric characters and no spaces, may be used in the email template, and the field values will be substituted when the email is sent. Not optional. No default.

These are in addition to the tokens known by Watcher, q.v.

In the alert context, this agent sets as tokens each of the fieldnames from the watch query, so that the values of these can be substituted into the template. Note that if the query returns fields with names identical to those of watcher tokens, those field values will over-ride the default values. For example, to send email to each email address returned by a query, you should ensure that the email address is returned in a field called mail_to_address.

Since:
Jacquard 1.9 This revision: $Author: simon $
Version:
$Revision: 1.7.4.1 $
Author:
Simon Brooke (simon@jasmine.org.uk)

Field Summary
static java.lang.String DBDRIVERTOKEN
          names of fields that I am interested in
static java.lang.String DBPASSTOKEN
          names of fields that I am interested in
static java.lang.String DBURLTOKEN
          names of fields that I am interested in
static java.lang.String DBUSERTOKEN
          names of fields that I am interested in
protected  java.lang.String driverName
          the name of the JDBC driver class I use
protected  java.lang.String idField
          the name of the field I use to uniquely identify a record
protected  java.lang.String mark
          the query I run to mark that I've done something
static java.lang.String MARKIDTOKEN
          names of fields that I am interested in
static java.lang.String MARKQUERYTOKEN
          names of fields that I am interested in
protected  java.lang.String pass
          the password I use
protected  java.lang.String url
          the url of the database I watch
protected  java.lang.String user
          the username I use
protected  java.lang.String watch
          the query I run to see whether I've anything to do
static java.lang.String WATCHQUERYTOKEN
          names of fields that I am interested in
 
Fields inherited from class uk.co.weft.alert.Watcher
alerter, alerts, configuration, debug, DEBUGTOKEN, ERRORTOKEN, INTERVALTOKEN, name, NAMETOKEN, recipient, resourceBundleName, sender, subject, SUBJECTTOKEN, template, TEMPLATETOKEN, thread, tooManyErrors, TOOMANYERRORS, whinges, winks
 
Constructor Summary
JDBCWatcher()
           
 
Method Summary
protected  java.util.Vector check()
          perform a check to see whether the event I am watching for has occurred.
 void init(java.util.Map config)
          initialise me with this configuration
static void main(java.lang.String[] args)
          Test harness only; you're not really expected to run watchers from the command line
protected  boolean mark(java.util.Map details)
          mark the event indicated by details as having had it's alert sent
 
Methods inherited from class uk.co.weft.alert.Watcher
coerceToUrl, getAlerts, getArgs, getName, getNextCheck, getResource, getResourceString, getState, getStringWithDefault, getWhinges, grs, grs, maybeCheck, reset, resetWhinges, run, sendAlert, start, stop, usage, whinge
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DBURLTOKEN

public static final java.lang.String DBURLTOKEN
names of fields that I am interested in

See Also:
Constant Field Values

DBDRIVERTOKEN

public static final java.lang.String DBDRIVERTOKEN
names of fields that I am interested in

See Also:
Constant Field Values

DBUSERTOKEN

public static final java.lang.String DBUSERTOKEN
names of fields that I am interested in

See Also:
Constant Field Values

DBPASSTOKEN

public static final java.lang.String DBPASSTOKEN
names of fields that I am interested in

See Also:
Constant Field Values

WATCHQUERYTOKEN

public static final java.lang.String WATCHQUERYTOKEN
names of fields that I am interested in

See Also:
Constant Field Values

MARKQUERYTOKEN

public static final java.lang.String MARKQUERYTOKEN
names of fields that I am interested in

See Also:
Constant Field Values

MARKIDTOKEN

public static final java.lang.String MARKIDTOKEN
names of fields that I am interested in

See Also:
Constant Field Values

url

protected java.lang.String url
the url of the database I watch


user

protected java.lang.String user
the username I use


pass

protected java.lang.String pass
the password I use


watch

protected java.lang.String watch
the query I run to see whether I've anything to do


mark

protected java.lang.String mark
the query I run to mark that I've done something


idField

protected java.lang.String idField
the name of the field I use to uniquely identify a record


driverName

protected java.lang.String driverName
the name of the JDBC driver class I use

Constructor Detail

JDBCWatcher

public JDBCWatcher()
Method Detail

init

public void init(java.util.Map config)
          throws AlertingException
initialise me with this configuration

Overrides:
init in class Watcher
Throws:
AlertingException

check

protected java.util.Vector check()
                          throws java.lang.Exception
perform a check to see whether the event I am watching for has occurred.

Specified by:
check in class Watcher
Returns:
a vector of maps each one of which represents an alert which should be sent. If no alerts should be sent, return an empty vector.
Throws:
java.lang.Exception

mark

protected boolean mark(java.util.Map details)
                throws java.lang.Exception
mark the event indicated by details as having had it's alert sent

Overrides:
mark in class Watcher
Parameters:
details - details of the event seen
Returns:
true if alert was despatched successfully (doesn't prove it will be received)
Throws:
java.lang.Exception

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Test harness only; you're not really expected to run watchers from the command line

Throws:
java.lang.Exception

jacquard 1.12.0 by The Web Engineering Factory and Toolworks