Package org.apache.velocity.example
Class EventExample
java.lang.Object
org.slf4j.helpers.MarkerIgnoringBase
org.apache.velocity.example.EventExample
- All Implemented Interfaces:
Serializable
,EventHandler
,InvalidReferenceEventHandler
,MethodExceptionEventHandler
,ReferenceInsertionEventHandler
,org.slf4j.Logger
public class EventExample
extends org.slf4j.helpers.MarkerIgnoringBase
implements ReferenceInsertionEventHandler, MethodExceptionEventHandler, InvalidReferenceEventHandler
This class is a simple demonstration of how the event handling
features of the Velocity Servlet Engine are used. It uses a
custom logger as well to check the log message stream
when testing the InvalidReferenceEventHandler.
- Version:
- $Id$
- Author:
- Geir Magnusson Jr.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
static final int
static final int
static final int
static final int
protected String
Fields inherited from interface org.slf4j.Logger
ROOT_LOGGER_NAME
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
void
void
void
void
void
void
void
void
void
void
formatAndLog
(int level, String format, Object... arguments) This prints the level and formatted message to System.out.getName()
Required init methods for Logger interfacevoid
void
void
void
void
Called when object is null or there is no getter for the given property.Called when object is null or the given method does not exist.boolean
invalidSetMethod
(Context context, String leftreference, String rightreference, Info info) Event handler for when the right hand side of a #set() directive is null, which results in a log message.boolean
boolean
boolean
boolean
isLevelEnabled
(int level) This always returns true because logging levels can't be disabled in this impl.boolean
boolean
void
This just prints the message and level to System.out.void
This prints the level, message, and the Throwable's message to System.out.static void
Called when a method throws an exception.protected Object
referenceInsert
(Context context, String reference, Object value) Event handler for when a reference is inserted into the output stream.void
silly method to throw an exception to demonstrate the method invocation exception event handlingvoid
void
void
void
void
void
void
void
void
void
Methods inherited from class org.slf4j.helpers.MarkerIgnoringBase
debug, debug, debug, debug, debug, error, error, error, error, error, info, info, info, info, info, isDebugEnabled, isErrorEnabled, isInfoEnabled, isTraceEnabled, isWarnEnabled, toString, trace, trace, trace, trace, trace, warn, warn, warn, warn, warn
-
Field Details
-
LOG_LEVEL_TRACE
public static final int LOG_LEVEL_TRACE- See Also:
-
LOG_LEVEL_DEBUG
public static final int LOG_LEVEL_DEBUG- See Also:
-
LOG_LEVEL_INFO
public static final int LOG_LEVEL_INFO- See Also:
-
LOG_LEVEL_WARN
public static final int LOG_LEVEL_WARN- See Also:
-
LOG_LEVEL_ERROR
public static final int LOG_LEVEL_ERROR- See Also:
-
name
-
-
Constructor Details
-
EventExample
public EventExample()
-
-
Method Details
-
main
-
throwException
silly method to throw an exception to demonstrate the method invocation exception event handling- Throws:
Exception
-
referenceInsert
Event handler for when a reference is inserted into the output stream.- Specified by:
referenceInsert
in interfaceReferenceInsertionEventHandler
- Parameters:
context
- current contextreference
- Reference from template about to be inserted.value
- Value about to be inserted (after itstoString()
method is called).- Returns:
- Object on which
toString()
should be called for output.
-
invalidGetMethod
public Object invalidGetMethod(Context context, String reference, Object object, String property, Info info) Description copied from interface:InvalidReferenceEventHandler
Called when object is null or there is no getter for the given property. Also called for invalid references without properties. invalidGetMethod() will be called in sequence for each link in the chain until the first non-null value is returned.- Specified by:
invalidGetMethod
in interfaceInvalidReferenceEventHandler
- Parameters:
context
- the context when the reference was found invalidreference
- string with complete invalid referenceobject
- the object referred to, or null if not foundproperty
- the property name from the referenceinfo
- contains template, line, column details- Returns:
- substitute return value for missing reference, or null if no substitute
-
invalidMethod
public Object invalidMethod(Context context, String reference, Object object, String method, Info info) Description copied from interface:InvalidReferenceEventHandler
Called when object is null or the given method does not exist. invalidMethod() will be called in sequence for each link in the chain until the first non-null value is returned.- Specified by:
invalidMethod
in interfaceInvalidReferenceEventHandler
- Parameters:
context
- the context when the reference was found invalidreference
- string with complete invalid referenceobject
- the object referred to, or null if not foundmethod
- the name of the (non-existent) methodinfo
- contains template, line, column details- Returns:
- substitute return value for missing reference, or null if no substitute
-
invalidSetMethod
public boolean invalidSetMethod(Context context, String leftreference, String rightreference, Info info) Event handler for when the right hand side of a #set() directive is null, which results in a log message. This method gives the application a chance to 'vote' on msg generation- Specified by:
invalidSetMethod
in interfaceInvalidReferenceEventHandler
- Parameters:
context
- the context when the reference was found invalidleftreference
- left reference being assigned torightreference
- invalid reference on the rightinfo
- contains info on template, line, col- Returns:
- if true then stop calling invalidSetMethod along the chain.
-
methodException
Description copied from interface:MethodExceptionEventHandler
Called when a method throws an exception. Only the first registered MethodExceptionEventHandler is called. If none are registered a MethodInvocationException is thrown.- Specified by:
methodException
in interfaceMethodExceptionEventHandler
- Parameters:
context
- current contextclaz
- the class of the object the method is being applied tomethod
- the methode
- the thrown exceptioninfo
- contains template, line, column details- Returns:
- an object to insert in the page
-
log
This just prints the message and level to System.out. -
log
This prints the level, message, and the Throwable's message to System.out. -
formatAndLog
This prints the level and formatted message to System.out. -
isLevelEnabled
public boolean isLevelEnabled(int level) This always returns true because logging levels can't be disabled in this impl. -
getName
Required init methods for Logger interface- Specified by:
getName
in interfaceorg.slf4j.Logger
-
isTraceEnabled
public boolean isTraceEnabled()- Specified by:
isTraceEnabled
in interfaceorg.slf4j.Logger
-
trace
- Specified by:
trace
in interfaceorg.slf4j.Logger
-
trace
- Specified by:
trace
in interfaceorg.slf4j.Logger
-
trace
- Specified by:
trace
in interfaceorg.slf4j.Logger
-
trace
- Specified by:
trace
in interfaceorg.slf4j.Logger
-
trace
- Specified by:
trace
in interfaceorg.slf4j.Logger
-
isDebugEnabled
public boolean isDebugEnabled()- Specified by:
isDebugEnabled
in interfaceorg.slf4j.Logger
-
debug
- Specified by:
debug
in interfaceorg.slf4j.Logger
-
debug
- Specified by:
debug
in interfaceorg.slf4j.Logger
-
debug
- Specified by:
debug
in interfaceorg.slf4j.Logger
-
debug
- Specified by:
debug
in interfaceorg.slf4j.Logger
-
debug
- Specified by:
debug
in interfaceorg.slf4j.Logger
-
isInfoEnabled
public boolean isInfoEnabled()- Specified by:
isInfoEnabled
in interfaceorg.slf4j.Logger
-
info
- Specified by:
info
in interfaceorg.slf4j.Logger
-
info
- Specified by:
info
in interfaceorg.slf4j.Logger
-
info
- Specified by:
info
in interfaceorg.slf4j.Logger
-
info
- Specified by:
info
in interfaceorg.slf4j.Logger
-
info
- Specified by:
info
in interfaceorg.slf4j.Logger
-
isWarnEnabled
public boolean isWarnEnabled()- Specified by:
isWarnEnabled
in interfaceorg.slf4j.Logger
-
warn
- Specified by:
warn
in interfaceorg.slf4j.Logger
-
warn
- Specified by:
warn
in interfaceorg.slf4j.Logger
-
warn
- Specified by:
warn
in interfaceorg.slf4j.Logger
-
warn
- Specified by:
warn
in interfaceorg.slf4j.Logger
-
warn
- Specified by:
warn
in interfaceorg.slf4j.Logger
-
isErrorEnabled
public boolean isErrorEnabled()- Specified by:
isErrorEnabled
in interfaceorg.slf4j.Logger
-
error
- Specified by:
error
in interfaceorg.slf4j.Logger
-
error
- Specified by:
error
in interfaceorg.slf4j.Logger
-
error
- Specified by:
error
in interfaceorg.slf4j.Logger
-
error
- Specified by:
error
in interfaceorg.slf4j.Logger
-
error
- Specified by:
error
in interfaceorg.slf4j.Logger
-
readResolve
- Throws:
ObjectStreamException
-