📄 eventhandlerutil.java
字号:
{
EventHandlerMethodExecutor methodExecutor =
new IncludeEventHandler.IncludeEventExecutor(
context, includeResourcePath,
currentResourcePath, directiveName);
callEventHandlers(
applicationEventHandlerIterator,
contextEventHandlerIterator, methodExecutor);
return (String) methodExecutor.getReturnValue();
}
catch (RuntimeException e)
{
throw e;
}
catch (Exception e)
{
throw ExceptionUtils.createRuntimeException("Exception in event handler.",e);
}
}
/**
* Called when an invalid get method is encountered.
*
* @param rsvc current instance of RuntimeServices
* @param context the context when the reference was found invalid
* @param reference complete invalid reference
* @param object object from reference, or null if not available
* @param property name of property, or null if not relevant
* @param info contains info on template, line, col
* @return substitute return value for missing reference, or null if no substitute
*/
public static Object invalidGetMethod(RuntimeServices rsvc,
InternalContextAdapter context, String reference,
Object object, String property, Info info)
{
return
invalidReferenceHandlerCall (
new InvalidReferenceEventHandler.InvalidGetMethodExecutor
(context, reference, object, property, info),
rsvc,
context);
}
/**
* Called when an invalid set method is encountered.
*
* @param rsvc current instance of RuntimeServices
* @param context the context when the reference was found invalid
* @param leftreference left reference being assigned to
* @param rightreference invalid reference on the right
* @param info contains info on template, line, col
*/
public static void invalidSetMethod(RuntimeServices rsvc,
InternalContextAdapter context, String leftreference,
String rightreference, Info info)
{
/**
* ignore return value
*/
invalidReferenceHandlerCall (
new InvalidReferenceEventHandler.InvalidSetMethodExecutor
(context, leftreference, rightreference, info),
rsvc,
context);
}
/**
* Called when an invalid method is encountered.
*
* @param rsvc current instance of RuntimeServices
* @param context the context when the reference was found invalid
* @param reference complete invalid reference
* @param object object from reference, or null if not available
* @param method name of method, or null if not relevant
* @param info contains info on template, line, col
* @return substitute return value for missing reference, or null if no substitute
*/
public static Object invalidMethod(RuntimeServices rsvc,
InternalContextAdapter context, String reference,
Object object, String method, Info info)
{
return
invalidReferenceHandlerCall (
new InvalidReferenceEventHandler.InvalidMethodExecutor
(context, reference, object, method, info),
rsvc,
context);
}
/**
* Calls event handler method with appropriate chaining across event handlers.
*
* @param methodExecutor
* @param rsvc current instance of RuntimeServices
* @param context The current context
* @return return value from method, or null if no return value
*/
public static Object invalidReferenceHandlerCall(
EventHandlerMethodExecutor methodExecutor,
RuntimeServices rsvc,
InternalContextAdapter context)
{
// app level cartridges have already been initialized
EventCartridge ev1 = rsvc.getApplicationEventCartridge();
Iterator applicationEventHandlerIterator =
(ev1 == null) ? null: ev1.getInvalidReferenceEventHandlers();
EventCartridge ev2 = context.getEventCartridge();
initializeEventCartridge(rsvc, ev2);
Iterator contextEventHandlerIterator =
(ev2 == null) ? null: ev2.getInvalidReferenceEventHandlers();
try
{
callEventHandlers(
applicationEventHandlerIterator,
contextEventHandlerIterator, methodExecutor);
return methodExecutor.getReturnValue();
}
catch (RuntimeException e)
{
throw e;
}
catch (Exception e)
{
throw ExceptionUtils.createRuntimeException("Exception in event handler.",e);
}
}
/**
* Initialize the event cartridge if appropriate.
*
* @param rsvc current instance of RuntimeServices
* @param eventCartridge the event cartridge to be initialized
*/
private static void initializeEventCartridge(RuntimeServices rsvc, EventCartridge eventCartridge)
{
if (eventCartridge != null)
{
try
{
eventCartridge.initialize(rsvc);
}
catch (Exception e)
{
throw ExceptionUtils.createRuntimeException("Couldn't initialize event cartridge : ", e);
}
}
}
/**
* Loop through both the application level and context-attached event handlers.
*
* @param applicationEventHandlerIterator Iterator that loops through all global event handlers declared at application level
* @param contextEventHandlerIterator Iterator that loops through all global event handlers attached to context
* @param eventExecutor Strategy object that executes event handler method
* @exception Exception generic exception potentially thrown by event handlers
*/
private static void callEventHandlers(
Iterator applicationEventHandlerIterator,
Iterator contextEventHandlerIterator,
EventHandlerMethodExecutor eventExecutor)
throws Exception
{
/**
* First loop through the event handlers configured at the app level
* in the properties file.
*/
iterateOverEventHandlers(applicationEventHandlerIterator, eventExecutor);
/**
* Then loop through the event handlers attached to the context.
*/
iterateOverEventHandlers(contextEventHandlerIterator, eventExecutor);
}
/**
* Loop through a given iterator of event handlers.
*
* @param handlerIterator Iterator that loops through event handlers
* @param eventExecutor Strategy object that executes event handler method
* @exception Exception generic exception potentially thrown by event handlers
*/
private static void iterateOverEventHandlers(
Iterator handlerIterator,
EventHandlerMethodExecutor eventExecutor)
throws Exception
{
if (handlerIterator != null)
{
for (Iterator i = handlerIterator; i.hasNext();)
{
EventHandler eventHandler = (EventHandler) i.next();
if (!eventExecutor.isDone())
{
eventExecutor.execute(eventHandler);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -