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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intLOG_LEVEL_DEBUGstatic intLOG_LEVEL_ERRORstatic intLOG_LEVEL_INFOstatic intLOG_LEVEL_TRACEstatic intLOG_LEVEL_WARNprotected Stringname
-
Constructor Summary
Constructors Constructor Description EventExample()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddebug(String msg)voiddebug(String format, Object param1)voiddebug(String format, Object... argArray)voiddebug(String format, Object param1, Object param2)voiddebug(String msg, Throwable t)voiderror(String msg)voiderror(String format, Object arg)voiderror(String format, Object... argArray)voiderror(String format, Object arg1, Object arg2)voiderror(String msg, Throwable t)voidformatAndLog(int level, String format, Object... arguments)This prints the level and formatted message to System.out.StringgetName()Required init methods for Logger interfacevoidinfo(String msg)voidinfo(String format, Object arg)voidinfo(String format, Object... argArray)voidinfo(String format, Object arg1, Object arg2)voidinfo(String msg, Throwable t)ObjectinvalidGetMethod(Context context, String reference, Object object, String property, Info info)Called when object is null or there is no getter for the given property.ObjectinvalidMethod(Context context, String reference, Object object, String method, Info info)Called when object is null or the given method does not exist.booleaninvalidSetMethod(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.booleanisDebugEnabled()booleanisErrorEnabled()booleanisInfoEnabled()booleanisLevelEnabled(int level)This always returns true because logging levels can't be disabled in this impl.booleanisTraceEnabled()booleanisWarnEnabled()voidlog(int level, String message)This just prints the message and level to System.out.voidlog(int level, String message, Throwable t)This prints the level, message, and the Throwable's message to System.out.static voidmain(String[] args)ObjectmethodException(Context context, Class claz, String method, Exception e, Info info)Called when a method throws an exception.protected ObjectreadResolve()ObjectreferenceInsert(Context context, String reference, Object value)Event handler for when a reference is inserted into the output stream.voidthrowException()silly method to throw an exception to demonstrate the method invocation exception event handlingvoidtrace(String msg)voidtrace(String format, Object param1)voidtrace(String format, Object... argArray)voidtrace(String format, Object param1, Object param2)voidtrace(String msg, Throwable t)voidwarn(String msg)voidwarn(String format, Object arg)voidwarn(String format, Object... argArray)voidwarn(String format, Object arg1, Object arg2)voidwarn(String msg, Throwable t)-
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 Detail
-
LOG_LEVEL_TRACE
public static final int LOG_LEVEL_TRACE
- See Also:
- Constant Field Values
-
LOG_LEVEL_DEBUG
public static final int LOG_LEVEL_DEBUG
- See Also:
- Constant Field Values
-
LOG_LEVEL_INFO
public static final int LOG_LEVEL_INFO
- See Also:
- Constant Field Values
-
LOG_LEVEL_WARN
public static final int LOG_LEVEL_WARN
- See Also:
- Constant Field Values
-
LOG_LEVEL_ERROR
public static final int LOG_LEVEL_ERROR
- See Also:
- Constant Field Values
-
name
protected String name
-
-
Method Detail
-
main
public static void main(String[] args)
-
throwException
public void throwException() throws Exceptionsilly method to throw an exception to demonstrate the method invocation exception event handling- Throws:
Exception
-
referenceInsert
public Object referenceInsert(Context context, String reference, Object value)
Event handler for when a reference is inserted into the output stream.- Specified by:
referenceInsertin 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:InvalidReferenceEventHandlerCalled 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:
invalidGetMethodin 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:InvalidReferenceEventHandlerCalled 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:
invalidMethodin 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:
invalidSetMethodin 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
public Object methodException(Context context, Class claz, String method, Exception e, Info info)
Description copied from interface:MethodExceptionEventHandlerCalled when a method throws an exception. Only the first registered MethodExceptionEventHandler is called. If none are registered a MethodInvocationException is thrown.- Specified by:
methodExceptionin 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
public void log(int level, String message)This just prints the message and level to System.out.
-
log
public void log(int level, String message, Throwable t)This prints the level, message, and the Throwable's message to System.out.
-
formatAndLog
public void formatAndLog(int level, String format, Object... arguments)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
public String getName()
Required init methods for Logger interface- Specified by:
getNamein interfaceorg.slf4j.Logger
-
isTraceEnabled
public boolean isTraceEnabled()
- Specified by:
isTraceEnabledin interfaceorg.slf4j.Logger
-
trace
public void trace(String msg)
- Specified by:
tracein interfaceorg.slf4j.Logger
-
trace
public void trace(String format, Object param1)
- Specified by:
tracein interfaceorg.slf4j.Logger
-
trace
public void trace(String format, Object param1, Object param2)
- Specified by:
tracein interfaceorg.slf4j.Logger
-
trace
public void trace(String format, Object... argArray)
- Specified by:
tracein interfaceorg.slf4j.Logger
-
isDebugEnabled
public boolean isDebugEnabled()
- Specified by:
isDebugEnabledin interfaceorg.slf4j.Logger
-
debug
public void debug(String msg)
- Specified by:
debugin interfaceorg.slf4j.Logger
-
debug
public void debug(String format, Object param1)
- Specified by:
debugin interfaceorg.slf4j.Logger
-
debug
public void debug(String format, Object param1, Object param2)
- Specified by:
debugin interfaceorg.slf4j.Logger
-
debug
public void debug(String format, Object... argArray)
- Specified by:
debugin interfaceorg.slf4j.Logger
-
isInfoEnabled
public boolean isInfoEnabled()
- Specified by:
isInfoEnabledin interfaceorg.slf4j.Logger
-
info
public void info(String msg)
- Specified by:
infoin interfaceorg.slf4j.Logger
-
info
public void info(String format, Object arg1, Object arg2)
- Specified by:
infoin interfaceorg.slf4j.Logger
-
info
public void info(String format, Object... argArray)
- Specified by:
infoin interfaceorg.slf4j.Logger
-
isWarnEnabled
public boolean isWarnEnabled()
- Specified by:
isWarnEnabledin interfaceorg.slf4j.Logger
-
warn
public void warn(String msg)
- Specified by:
warnin interfaceorg.slf4j.Logger
-
warn
public void warn(String format, Object arg1, Object arg2)
- Specified by:
warnin interfaceorg.slf4j.Logger
-
warn
public void warn(String format, Object... argArray)
- Specified by:
warnin interfaceorg.slf4j.Logger
-
isErrorEnabled
public boolean isErrorEnabled()
- Specified by:
isErrorEnabledin interfaceorg.slf4j.Logger
-
error
public void error(String msg)
- Specified by:
errorin interfaceorg.slf4j.Logger
-
error
public void error(String format, Object arg)
- Specified by:
errorin interfaceorg.slf4j.Logger
-
error
public void error(String format, Object arg1, Object arg2)
- Specified by:
errorin interfaceorg.slf4j.Logger
-
error
public void error(String format, Object... argArray)
- Specified by:
errorin interfaceorg.slf4j.Logger
-
readResolve
protected Object readResolve() throws ObjectStreamException
- Throws:
ObjectStreamException
-
-