📄 executelogicinterceptor.java
字号:
package xyz.frame.core;import java.util.List;import org.apache.log4j.Logger;import xyz.frame.Interceptor;import xyz.frame.LogicException;import xyz.frame.LogicFlow;import xyz.frame.component.ComponentInstantiationException;import xyz.frame.component.ComponentType;import xyz.frame.component.LogicMethod;import xyz.frame.converter.ConversionProblems;import xyz.frame.i18n.Message;import xyz.frame.introspector.Introspector;import xyz.frame.util.GettingException;import xyz.frame.util.SettingException;import xyz.frame.validator.ValidationErrors;import xyz.frame.view.ViewException;/** * The last interceptor: calls the logic method * * @author Guilherme Silveira */public class ExecuteLogicInterceptor implements Interceptor { private static final String INVALID = "invalid"; private static final Logger logger = Logger .getLogger(ExecuteLogicInterceptor.class); private XyzExecution engine; private ComponentType componentClass; private LogicMethod logicMethod; public ExecuteLogicInterceptor(XyzExecution engine, ComponentType componentClass, LogicMethod logicMethod) { this.engine = engine; this.componentClass = componentClass; this.logicMethod = logicMethod; } public void intercept(LogicFlow logic) throws LogicException, ViewException { Introspector introspector = engine.getController().getIntrospector(); logger.debug("last interception: ready to execute logic"); try { // populate Object component = instantiateComponent(introspector); // execute String result = executeLogic(component); outjectLogic(introspector, component); logger.debug("last interception: ready to redirect"); if (this.logicMethod.shouldRedirect()) { this.engine.redirect(result); } } catch (ConversionProblems ex) { logger.debug("Some conversion errors were found"); this.engine.getRequest().getRequestContext().setAttribute("errors", ex.getProblems()); this.engine.redirect(INVALID); } } private Object instantiateComponent(Introspector introspector) throws LogicException, ConversionProblems { try { // instantiate Object component = componentClass.newInstance(); // inject introspector.inject(componentClass.getInAnnotations(), component, engine.getRequest()); // read parameters List<Message> problems = introspector.readParameters( componentClass, component, engine.getRequest(), engine .getController().getWebApplication() .getConverterManager()); if (problems.isEmpty()) { return component; } else { throw new ConversionProblems(problems); } } catch (ComponentInstantiationException e) { throw new LogicException(e.getMessage(), e); } catch (SettingException e) { throw new LogicException(e.getMessage(), e); } } /** * @param introspector * @param component * @throws GettingException */ private void outjectLogic(Introspector introspector, Object component) throws LogicException { try { introspector.outject(componentClass.getOutAnnotations(), component, engine.getRequest()); } catch (GettingException e) { throw new LogicException(e.getMessage(), e); } } /** * Executes the validation and logic call * * @param component * the current component * @throws LogicException * logic problem */ private String executeLogic(Object component) throws LogicException { ValidationErrors errors = logicMethod.validate(component, engine .getRequest()); if (errors.size() != 0) { engine.getRequest().getRequestContext().setAttribute("errors", errors); return "invalid"; } else { return logicMethod.execute(component, engine.getRequest()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -