List<Filter> arounds = new ArrayList<Filter>(); // 已执行的Around过滤器
List<Filter> chains = FilterUtils.getFilterChain(controller);
boolean chainBroken = false;
try {
for (int i = 0; i < chains.size(); i++) {
Filter filter = chains.get(i);
if (filter.canInvoke(controller.getActionName())) {
boolean ret = filter.beforeInvoke(controller);
if (ret == true && filter.getAroundFilter() != null) {
arounds.add(filter);
}
if (!ret) {
chainBroken = true;
break;
}
}
}
} catch (Exception ex) {
try {
afterInvokeAroundFilters(controller, arounds);
} catch (Exception ex2) {
}
throw ex;
}
if (chainBroken == true) { // 过滤器链执行中断
afterInvokeAroundFilters(controller, arounds);
return;
}
// 执行控制器方法
Object actionResult = null;
try {
actionResult = controller.invoke();
} catch (Exception ex) {
controller.exception = ex;
}
// Around过滤器after方法,反序执行
if (afterInvokeAroundFilters(controller, arounds) == false) {
return; // 不继续执行后续代码
}
if (controller.exception != null) {
throw controller.exception;
}
// After过滤器,顺序执行
for (int i = 0; i < chains.size(); i++) {
Filter filter = chains.get(i);
if (filter.canInvoke(controller.getActionName())
&& filter.getAfterFilter() != null) {
boolean ret = filter.afterInvoke(controller);
if (!ret) {
return;
}
}
}