Example: user written formatter

The following is a very simple sample of a Formatter class. This class is functional, but is intended solely to demonstrate concepts. For simplicity and clarity, much code (including appropriate boundary condition checking logic) has been ignored. This sample is not intended to be an example of good programming practice.

package com.ibm.ws.ras.test.user; 
import com.ibm.ras.*; 
import java.text.*;
import java.util.*; 

/** 
 * The <code>SimpleFormatter</code> implements
 *  the RASIFormatter interface. It is a 
 * simple implementation used for demonstration purposes only. It does not do any 
 * advanced formatting, it simply formats the message and parameters in an event. 
 * It does not include the timestamp in the formatted result, for example. 
 */ 
public class SimpleFormatter implements RASIFormatter 
{ 

  /** 
   * The name of the formatter 
   */ 
  private String ivName = ""; 

  /** 
   * A vector containing the event classes this Formatter knows how to process. 
   */ 
  private Vector ivEventClasses = new Vector(); 

  /** 
   * Create a <code>SimpleFormatter</code>. 
   */ 
  public SimpleFormatter(String name)  { 
    setName(name); 
  } 

  //////////////////////////////////////////////////////// 
  // 
  // Methods required by the RASIObject Interface 
  // 
  /////////////////////////////////////////////////////// 

  /** 
   * Return this objects configuration as a set of Properties in a Hashtable.
   * <p>
   * This formatter does not support properties-based configuration. 
   * Therefore a call to this method always returns null 
   * @return null is always returned. 
   */ 
  public Hashtable getConfig()  { 
    return null; 
  } 

  /** 
   * Set this objects configuration from the properties in the specified Hashtable. 
   * <p>
   * This formatter does not support properties-based configuration. 
   * This method is a no-operation. 
   * @param hashTable a Hashtable containing the properties. Input is ignored. 
   */ 
  public void setConfig(Hashtable ht)  { 
    return; 
  } 

  /** 
   * Return the name by which this formatter is known. 
   * <p>
   * @return a String containing the name of this object, or an 
   * empty string ("") if the name has not been set. 
   */ 
  public String getName()   { 
    return ivName; 
  } 

  /** 
   * Set the name by which this formatter is known.  If the specified
   *  name is  <code>null</code>, the current name is not changed. 
   * <p>
   * @param name The new name for this object. Null is tolerated. 
   */ 
  public void setName(String name)   { 
    if (name != null) 
      ivName = name; 
  } 

  /** 
   * Return the description field of this formatter. 
   *<p>
   * This formatter does not use a description field.
   *  An empty String is always returned. 
   *<p>
   * @return an empty String. 
   */ 
  public String getDescription()  { 
    return ""; 
  } 

  /** 
   * Set the description field for this formatter. 
   *<p>
   * This formatter does not use a description field. Input is ignored and
   *  this method does nothing. 
   *<p>
   * @param desc The description of this object. Input is ignored. 
   */ 
  public void setDescription(String desc)  { 
    return; 
  } 

  /** 
   * Return the name of the {@link com.ibm.ras.mgr.RASManager RASManager} group 
   * with which this formatter is associated.  This method is
   * only used by the  RAS Manager. 
   *<p>
   * This formatter does not support RASManager configuration. Null is always returned. 
   * @return null is always returned. 
   */ 
  public String getGroup()  { 
    return null; 
  } 

  //////////////////////////////////////////////////////// 
  // 
  // Methods required by the RASIFormatter Interface 
  // 
  /////////////////////////////////////////////////////// 

  /** 
   * Set a flag that indicates whether this formatter is the default formatter used by 
   * {@link com.ibm.ras.RASHandler} objects to format events. 
   *<p>
   * Instances of com.ibm.ras.RASHandler are not allowed to be
   * instantiated in the WebSphere Application Server environment. 
   * This formatter cannot be the default formatter for handlers of this type. 
   * This method does nothing.
   *<p>
   * @param flag input is ignored, since this formatter cannot be the default formatter. 
   */ 
  public void setDefault(boolean flag)  { 
    return; 
  } 

  /** 
   * Return a boolean that indicates whether or not this is the default formatter 
   * used by a {@link com.ibm.ras.RASHandler} to format the RAS events. 
   *<p>
   * com.ibm.ras.RASHandlers will never be instantiated in a 
   * WebSphere Application Server environment so this method always returns false. 
   *<p>
   * @return false is always returned. 
   */ 
  public boolean isDefault()  { 
    return false; 
  } 

  /** 
   * Add the name of a {@link com.ibm.ras.RASIEvent} class to the list 
   * of classes which this formatter can process. 
   * If the specified class name is null or it is already registered, this method does nothing. 
   *<p>
   * @param name The event class name. Null is tolerated. 
   */ 
  public void addEventClass(String name)  { 
    if ((name != null) && (! ivEventClasses.contains(name))) 
      ivEventClasses.addElement(name); 
  } 

  /** 
   * Remove the name of a {@link com.ibm.ras.RASIEvent} class
   *  from the list of classes which this formatter 
   * can process. If the specified class name is null or is not 
   * registered, this method does nothing.
   *<p>
   * @param name The event class name. 
   */ 
  public void removeEventClass(String name)  { 
    if ((name != null) && (ivEventClasses.contains(name))) 
      ivEventClasses.removeElement(name); 
  } 

  /** 
   * Return an enumeration over the set of names of 
   * {@link com.ibm.ras.RASIEvent} classes which this formatter can process. 
   *<p>
   * @return An enumeration of RAS event class names. If no event classes
   * are registered, the enumeration is empty. 
   */ 
  public Enumeration getEventClasses() { 
    return ivEventClasses.elements(); 
  } 

  /** 
   * Format the specified {link com.ibm.ras.RASIEvent} object and
   * return a String containing the formatted output. 
   *<p>
   * @param event The event to format. Null is tolerated. 
   * @return The formatted event contents. Null may be returned. 
   */ 
  public String format(RASIEvent event)  { 
    if (event == null) 
      return null; 
    if (event.isMessageEvent()) 
      return formatMessage(event); 
    else 
      return formatTrace(event); 
  } 

  /** 
   * Format a message event. 
   *<p>
   * If a message key is used and that key is not found in any
   * message file, the message text becomes an error message 
   * indicating that the key was not found. 
   *<p>
   * @param event The event to format. 
   * @return The formatted event. 
   */ 
  public String formatMessage(RASIEvent event)  { 
    String messageKey = "null"; 
    try { 
      messageKey = event.getText(); 
      String[] messageInserts = event.getParameters(); 
      if (event instanceof com.ibm.ras.RASMessageEvent) { 
        // RASMessageEvents usually contain localizable messages. 
        RASMessageEvent rme = (RASMessageEvent)event; 
        String bundleName = rme.getMessageFile(); 
        if (bundleName != null) { 
          // Not a text message, get localized message and return 
          ResourceBundle bundle = 
		     ResourceBundle.getBundle(bundleName, Locale.getDefault()); 
          String localizedKey =  bundle.getString(messageKey); 
          return MessageFormat.format(localizedKey, messageInserts); 
        } 
        else { 
          // Text message 
          for (int i=0; i<messageInserts.length; ++i){messageKey = 
		     messageKey + " " + messageInserts[i]; 
          } 
          return messageKey; 
        } 
      } 
      else { 
        // A User defined type. Append paramaters to key and return 
        for (int i=0; i<messageInserts.length; ++i){
          messageKey = messageKey + " " + messageInserts[i]; 
        } 
        return messageKey; 
      } 
    } 
    catch (Throwable t) { 
      t.printStackTrace(); 
      return "SimpleFormattter: Error while formatting message "+messageKey; 
    } 
  } 

   /** 
   * Format a trace event.
   *<p>
   * Append the parameters (in order of specification) to the text 
   * message in the trace event object.
   *<p>
   * @param event The event to format. 
   * @return The formatted event. 
   */ 
  private String formatTrace(RASIEvent event)  { 
    String text = "null"; 
    try { 
      text = event.getText(); 
      String[] parms = event.getParameters(); 
      if (parms != null) { 
        for (int i=0; i<parms.length; ++i){
         text = text + " " + parms[i]; 
        } 
      } 
      return text; 
    } 
    catch (Throwable t) { 
      t.printStackTrace(); 
      return "SimpleFormatter: Error while formatting trace "+text; 
    } 
  } 

}