使用定制公共基本事件工厂 Home 来控制配置及唯一事件工厂的实现。
开始之前
事件工厂 Home 创建并为事件工厂实例提供 Home。每个事件工厂 Home 都有内容处理程序。此内容处理程序将指定给事件工厂 Home 创建的每个事件工厂。反过来,创建公共基本事件时,会对其指定事件工厂中的内容处理程序。事件工厂实例由关联事件工厂 Home 根据其唯一名称进行维护。例如,当应用程序代码请求指定的事件工厂时,会返回刚创建的事件工厂实例,并且为此指定的事件工厂的将来请求保留该事件工厂实例。
添加了以下类以便于使用事件工厂 Home 记录公共基本事件:
类名 |
描述 |
WsEventFactoryHomeImpl |
此类扩展了 org.eclipse.hyades.logging.events.cbe.impl.AbstractEventFactoryHome
类。此事件工厂 home 返回与 WsContentHandlerImpl 内容处理程序关联的事件工厂实例。WsContentHandlerImpl
是在没有使用事件工厂模板时缺省由 WebSphere® Application Server 使用的内容处理程序。 |
WsTemplateEventFactory
HomeImpl
|
此类扩展了 org.eclipse.hyades.logging.events.cbe.impl.EventXMLFileEventFactoryHomeImpl
类。此事件工厂 home 返回与 WsTemplateContentHandlerImpl 内容处理程序相关联的事件工厂实例。WsTemplateContentHandlerImpl 是在需要事件工厂模板时由 WebSphere Application Server 使用的内容处理程序。 |
关于此任务
定制事件工厂 Home 支持使用公共基本事件在 WebSphere Application Server 中进行日志记录,并使日志记录过程容易且在 WebSphere Application Server 运行时和此 API 的开发者之间一致。
CustomEventFactoryHome 和
CustomTemplateEventFactoryHome
类将用来获取事件工厂。这些类用来确保正确的内容处理程序与特定的事件工厂在一起使用。
CustomEventFactoryHelper
类是一个基础结构提供程序如何对基础结构用户隐藏工厂选择详细信息的示例,可通过使用它们自己的参数集来决定适当的事件工厂。
过程
- 下面的代码样本提供了如何实现和使用 CustomEventFactoryHome 类的示例。
- CustomEventFactoryHome 类的实现如下:
public class CustomEventFactoryHome extends AbstractEventFactoryHome {
public CustomEventFactoryHome() {
super();
// TODO Custom intialization code goes here
}
public ContentHandler createContentHandler(String arg0) {
// Always use custom content handler
return resolveContentHandler();
}
public ContentHandler resolveContentHandler() {
// Always use custom content handler
return new CustomContentHandler();
}
}
- 以下是如何使用 CustomEventFactoryHome 类的示例:
// get the event factory
EventFactory eventFactory=(new CustomEventFactoryHome()).getEventFactory("XYZ");
// create an event - call appropriate method
eventFactory.createCommonBaseEvent();
// log event ...
- 对于 CustomTemplateEventFactoryHome 类,您可以使用下列代码用于实现和使用:
- 通过使用此代码实现 CustomTemplateEventFactoryHome 类:
public class CustomTemplateEventFactoryHome extends
EventXMLFileEventFactoryHomeImpl {
public CustomTemplateEventFactoryHome() {
super();
// TODO Custom intialization code goes here
}
public ContentHandler createContentHandler(String arg0) {
// Always use custom content handler
return resolveContentHandler();
}
public ContentHandler resolveContentHandler() {
// Always use custom content handler
return new CustomTemplateContentHandler();
}
}
- 通过遵循以下样本代码使用 CustomTemplateEventFactoryHome 类:
// get the event factory
EventFactory eventFactory=(new
CustomTemplateEventFactoryHome()).getEventFactory("XYZ");
// create an event - call appropriate method
eventFactory.createCommonBaseEvent();
// log event ...
- 通过遵循以下代码,可以实现和使用 CustomEventFactoryHelper 类:
- 使用此代码实现 custom CustomEventFactoryHelper 类:
public class CustomTemplateEventFactoryHome extends
EventXMLFileEventFactoryHomeImpl {
public CustomTemplateEventFactoryHome() {
super();
// TODO Custom intialization code goes here
}
public ContentHandler createContentHandler(String arg0) {
// Always use custom content handler
return resolveContentHandler();
}
public ContentHandler resolveContentHandler() {
// Always use custom content handler
return new CustomTemplateContentHandler();
}
}
// Figure 4 CustomTemplateEventFactoryHome class
public class CustomEventFactoryHelper {
// name of the event factory to use
public static final String FACTORY_NAME="XYZ";
public static EventFactory getEventFactory(String param1, String param2) {
EventFactory factory=null;
switch (resolveFactory(param1,param2)) {
case 1:
factory=(new CustomEventFactoryHome()).getEventFactory(FACTORY_NAME);
break;
case 2:
factory=(new
CustomTemplateEventFactoryHome()).getEventFactory(FACTORY_NAME);
break;
default:
// Add default for event factory
break;
}
return factory;
}
private static int resolveFactory(String param1, String param2) {
int factory=0;
// Add code here to resolve which factory to use
return factory;
}
}
- 要使用 CustomEventFactoryHelper 类,使用下列代码:
// get the event factory
EventFactory eventFactory=
CustomEventFactoryHelper.getEventFactory("param1","param2","param3");
// create an event - call appropriate method
eventFactory.createCommonBaseEvent();
// log event ...
结果
使用在此提供的信息,根据指定的设置来实现定制的内容工厂 Home 和关联的类。