« - »

Notification Service: MessageCourier

11 November 2008

Now that we have temporarily put to bed the whole notice template creation and maintenance side of things, we can turn our attention to the actual Notification Service itself. There are a number of potential places from which to start here, but I think that working from the end-result backwards might be the best of all possible approaches. Ultimately, what we want our little service to do here is to deliver the notice. For that little specific piece of the process, I have devised the concept of the MessageCourier. The purpose of the MessageCourier is simply to deliver the message using whatever means is appropriate for the particular delivery mechanism that is being supported by the implementation. It’s a simple interface, with a single, simple method:

package org.restafarian.notify.service;

import org.restafarian.notify.beans.MessageAddress;

/**
 * <p>This is the MessageCourier interface.</p>
 */
public interface MessageCourier {

  /**
   * <p>Delivers the message to specified recipient(s).</p>
   *
   * @param addresses an array of "address" objects containing the
   * implementation-specific addresses required for successful delivery of
   * the message
   * @param subjectTitle an optional subject and/or title of the message to
   * be delivered
   * @param body the text of the message to be delivered
   */
  public void deliverMessage(MessageAddress[] addresses, String subjectTitle, String body);
}

There is one parameter there that has yet to be introduced, and that is the MessageAddress. The idea behind the MessageAddress is simply an attempt to simplify the MessageCourier interface and make it applicable to as many possible delivery methods without having all kinds of variations of from, to, cc, bcc, and god-knows-what kinds of other addresses mucking up the interface. By simply specifying an array of MessageAddress objects, you can concieve of new types of destination addresses for new types of delivery mechanisms without having to constantly redefine the MessageCourier interface. Here is the MessageAddress bean in its entirety:

package org.restafarian.notify.beans;

/**
 * <p>This transient entity bean defines an address list of a specific type
 * for a message to be delivered.</p>
 */
public class MessageAddress {
  private static final long serialVersionUID = 1;
  private String addressType = null;
  private String[] addressValues = new String[0];

  /**
   * <p>Constructs a new MessageAddress.</p>
   */
  public MessageAddress() {
    super();
  }

  /**
   * <p>Constructs a new MessageAddress using the parameters provided.</p>
   *
   * @param addressType the addressType of this MessageAddress
   */
  public MessageAddress(String addressType) {
    super();
    this.addressType = addressType;
  }

  /**
   * @param addressValues the addressValues to set
   */
  public void addAddressValue(String addressValue) {
    if (addressValues == null) {
      addressValues = new String[0];
    }
    String[] newValues = new String[addressValues.length + 1];
    for (int i=0; i<addressValues.length; i++) {
      newValues[i] = addressValues[i];
    }
    newValues[addressValues.length] = addressValue;
    addressValues = newValues;
  }

  /**
   * @return the addressType
   */
  public String getAddressType() {
    return addressType;
  }
  /**
   * @param addressType the addressType to set
   */
  public void setAddressType(String addressType) {
    this.addressType = addressType;
  }
  /**
   * @return the addressValues
   */
  public String[] getAddressValues() {
    return addressValues;
  }
  /**
   * @param addressValues the addressValues to set
   */
  public void setAddressValues(String[] addressValues) {
    this.addressValues = addressValues;
  }
}

Since our initial focus is on the “e-mail” delivery mechanism, next time we’ll create an e-mail message courier that implements the MessageCourier interface. That should be sufficient for the first iteration of the complete service, and then we can come back and implement additional couriers as needed at a later time.


http://blog.restafarian.org/2008/11/notification-service-messagecourier/

Leave a reply

You must be logged in to post a comment.