jacquard 1.10.0 by The Web Engineering Factory and Toolworks

Package uk.co.weft.alert

Alert is a Java language package for sending automated emails constructed by substituting values for tokens in templates.

See:
          Description

Class Summary
Alert An agent which can send a message to a user based on a template.
FileWatcher An agent which watches for changes to a file and sends alerts based on those events.
JDBCWatcher An agent which watches for changes to a file and sends alerts based on those events.
MessageBuffer A StringBuffer which knows about substitutions and text wrapping...
SMSAddress An SMS address.
Watcher An agent which watches for events and sends alerts based on those events.
 

Exception Summary
AlertingException An agent which can send a message to a user based on a tmeplate.
 

Package uk.co.weft.alert Description

Alert is a Java language package for sending automated emails constructed by substituting values for tokens in templates. In the future it is intended that it should be able to send SMS messages and other sorts of alerts as well.

Dependencies | Using | Template Syntax | License

Dependencies

Alert, although designed as a part of Jacquard, is not dependent only on dbutils from the jacquard toolkit. It is however dependent on the following third party packages. All can be downloaded and used for free but not all are open source:

Package Description Supplier License Version
Jakarta Regexp A regular expression parser/matcher. Apache Foundation Apache license 1.2
Java Activation Framework Infrastructure layer required by Java Mail Sun Microsystems Proprietary license 1.0
Java Mail Java API for sending electronic mail Sun Microsystems Proprietary license 1.0

You will have to download all these packages and include their jar files in your classpath before you can use Alert.

Patterns for Using Alert

Using Alert from the command line

Alert can be called stand-alone from the command line. In this configuration it is best to set up the following variables in the alert.properties file:

mail_from_address
String: the from address of the emails to be sent. Not optional. There is a default but you'll piss me off if you use it.
mail_host
String: the fully qualified domain name of the SMTP host through which to send mail. Optional. Defaults to localhost.
mail_subject
String: the subject of the email to be sent. Oprional. Defaults to Alert!
mail_template_url
String: the URL or file path to a suitable template file. Not optional. No sensible default.

When invoked from the command line as an application, Alert knows about the following command line switches:

-a address
Adds the email address address to the list of recipients. This switch may be repeated arbitrarily many times to send similar mail to many recipients.
-s key value
Substitute value for all instances of the token key in the template.

Any argument which is not protected by a -a or -s switch will be treated as the filename or URL of a template file. If a template file is specified on the command line that will over-ride that specified in the configuration. If more than one template file is specified on the command line they will be concatenated in the order specified.

Using Alert from within a Jacquard Servlet

Jacquard is a Java-language toolkit which supports the writing of servlets and web applications. Alert, while it consciously has no direct dependencies on Jacquard and thus can be used independently of it, is designed to support the Jacquard toolkit.

In a Jaquard servlet one would typically use Alert something like this:

public class AlertExample extends Form
{
    Alert alerter = null;

    /** Initialise me: initialise my alerter as well. */
    public void init( Context config)
        throws InitialisationException
    {
        alerter = new Alert( config);
                                     // initialise the alerter with the config

        /* other stuff, widgets etc... */

        super.init( config);
    }

    /* other methods... */

    /** Send email based on the default template and with the default subject
     *  (as defined in the initial configuration) to all the users who match
     *  the current context
     *
     *  @param context the context for this service
     *  @exception DataStoreException if we can't access the database
     *  @exception AlertingException if we fail to send the message
     */
    public void sendAlerts( Context context)
        throws AlertingException, DataStoreException
    {
        Contexts users = 
            TableDescriptor.getDescriptor( "USERS", "UserId", 
                                           context). match( context);
                                     // fetch applicable rows from users table
        Vector addresses = new Vector();
                                     // create a vector to store addresses in

        Enumeration e = users.elements();

        while ( e.hasMoreElements())
        {                            // iterate over the users
            Context user = ( Context)e.nextElement();
            addresses.add( user.getValueAsString( "email"));
                                     // add the email address of each user 
                                     // to the addresses
        }

        alerter.sendAlert( addresses, context);
                                     // send email to all the users    
    }
}

Obviously it is possible to use Alert from within other Java programs. The pattern above will give some general clues as to how but it is designed to be flexible.

Syntax of Message Templates

Within a template, a token will be recognised as substitutable if it begins with <%, comprises only alphanumeric characters, and ends with %>. For each such token in the tempate, the context will be searched using that token as key (with the <% and %> removed), and the value found will be substituted for the substitutable token in the template. If no value is found, no substitution will be made.

Thus if the template is

I write applications in <%language%> to run on <%operatingsystem%>

and the context contains the mappings

then the substituted message will be

I write applications in java to run on any platform

Copyright and License

The following is the whole of your license and warranty to use this package:

[Note: this is the 'BSD License' as endorsed by OpenSource.org.]

Copyright (c) 2003 Simon Brooke. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SIMON BROOKE OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



Simon Brooke
Last modified: Tue Aug 12 19:10:20 BST 2003


jacquard 1.10.0 by The Web Engineering Factory and Toolworks