ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/jrasuwf.htm

306 lines
9.8 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Example: user written formatter</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="jrasuwf"></a>Example: user written formatter</h5>
<p>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.</p>
<pre>package com.ibm.ws.ras.test.user;
import com.ibm.ras.*;
import java.text.*;
import java.util.*;
/**
* The &lt;code&gt;SimpleFormatter&lt;/code&gt; 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 = &quot;&quot;;
/**
* A vector containing the event classes this Formatter knows how to process.
*/
private Vector ivEventClasses = new Vector();
/**
* Create a &lt;code&gt;SimpleFormatter&lt;/code&gt;.
*/
public SimpleFormatter(String name) {
setName(name);
}
////////////////////////////////////////////////////////
//
// Methods required by the RASIObject Interface
//
///////////////////////////////////////////////////////
/**
* Return this objects configuration as a set of Properties in a Hashtable.
* &lt;p&gt;
* 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.
* &lt;p&gt;
* 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.
* &lt;p&gt;
* @return a String containing the name of this object, or an
* empty string (&quot;&quot;) 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 &lt;code&gt;null&lt;/code&gt;, the current name is not changed.
* &lt;p&gt;
* @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.
*&lt;p&gt;
* This formatter does not use a description field.
* An empty String is always returned.
*&lt;p&gt;
* @return an empty String.
*/
public String getDescription() {
return &quot;&quot;;
}
/**
* Set the description field for this formatter.
*&lt;p&gt;
* This formatter does not use a description field. Input is ignored and
* this method does nothing.
*&lt;p&gt;
* @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.
*&lt;p&gt;
* 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.
*&lt;p&gt;
* 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.
*&lt;p&gt;
* @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.
*&lt;p&gt;
* com.ibm.ras.RASHandlers will never be instantiated in a
* WebSphere Application Server environment so this method always returns false.
*&lt;p&gt;
* @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.
*&lt;p&gt;
* @param name The event class name. Null is tolerated.
*/
public void addEventClass(String name) {
if ((name != null) &amp;&amp; (! 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.
*&lt;p&gt;
* @param name The event class name.
*/
public void removeEventClass(String name) {
if ((name != null) &amp;&amp; (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.
*&lt;p&gt;
* @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.
*&lt;p&gt;
* @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.
*&lt;p&gt;
* 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.
*&lt;p&gt;
* @param event The event to format.
* @return The formatted event.
*/
public String formatMessage(RASIEvent event) {
String messageKey = &quot;null&quot;;
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&lt;messageInserts.length; ++i){messageKey =
messageKey + &quot; &quot; + messageInserts[i];
}
return messageKey;
}
}
else {
// A User defined type. Append paramaters to key and return
for (int i=0; i&lt;messageInserts.length; ++i){
messageKey = messageKey + &quot; &quot; + messageInserts[i];
}
return messageKey;
}
}
catch (Throwable t) {
t.printStackTrace();
return &quot;SimpleFormattter: Error while formatting message &quot;+messageKey;
}
}
/**
* Format a trace event.
*&lt;p&gt;
* Append the parameters (in order of specification) to the text
* message in the trace event object.
*&lt;p&gt;
* @param event The event to format.
* @return The formatted event.
*/
private String formatTrace(RASIEvent event) {
String text = &quot;null&quot;;
try {
text = event.getText();
String[] parms = event.getParameters();
if (parms != null) {
for (int i=0; i&lt;parms.length; ++i){
text = text + &quot; &quot; + parms[i];
}
}
return text;
}
catch (Throwable t) {
t.printStackTrace();
return &quot;SimpleFormatter: Error while formatting trace &quot;+text;
}
}
}
</pre>
</body>
</html>