net.ymate.platform.mvc.support.RequestExecutor.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.mvc.support.RequestExecutor.java

Source

/*
 * Copyright 2007-2107 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.mvc.support;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import net.ymate.platform.base.YMP;
import net.ymate.platform.commons.i18n.I18N;
import net.ymate.platform.commons.lang.PairObject;
import net.ymate.platform.mvc.filter.IFilterChain;
import net.ymate.platform.mvc.view.IView;
import net.ymate.platform.mvc.web.WebMVC;
import net.ymate.platform.validation.ValidateResult;
import net.ymate.platform.validation.Validates;
import net.ymate.platform.validation.ValidationException;
import net.ymate.platform.validation.annotation.ValidateRule;
import net.ymate.platform.validation.annotation.Validation;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>
 * RequestExecutor
 * </p>
 * <p>
 * MVC
 * </p>
 * 
 * @author (suninformation@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">?</th><th width="100px"></th><th
 *          width="100px"></th><th width="100px"></th>
 *          </tr>
 *          <!--  Table ?? -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td></td>
 *          <td></td>
 *          <td>2012-12-14?4:30:27</td>
 *          </tr>
 *          </table>
 */
public class RequestExecutor {

    private static final Log _LOG = LogFactory.getLog(RequestExecutor.class);

    protected final RequestMeta requestMeta;
    protected final IFilterChain chain;

    /**
     * ?????
     */
    protected PairObject<Validation, Map<String, ValidateRule[]>> validateRuleConf;

    /**
     * ?????????nullString?String[]IUploadFileWrapper?IUploadFileWrapper[]
     */
    protected Map<String, Object> validateFieldValues = new HashMap<String, Object>();

    /**
     * 
     * 
     * @param meta MVC???
     */
    public RequestExecutor(RequestMeta meta) {
        this(meta, null);
    }

    /**
     * 
     * 
     * @param meta MVC???
     * @param chain 
     */
    public RequestExecutor(RequestMeta meta, IFilterChain chain) {
        this.requestMeta = meta;
        this.chain = chain;
        if (meta.handler == null) {
            validateRuleConf = Validates.loadValidateRule(meta.getMethod(), meta.getMethodParamNames());
        } else {
            validateRuleConf = new PairObject<Validation, Map<String, ValidateRule[]>>(
                    meta.getMethod().getAnnotation(Validation.class));
        }
    }

    /**
     * @return ?invoke??
     */
    protected Object[] getMethodParams() {
        return new Object[this.requestMeta.getParameterTypes().length];
    }

    /**
     * @param result 
     * @return ?IView
     * @throws Exception ?
     */
    protected IView processMethodResultToView(Object result) throws Exception {
        return (IView) result;
    }

    /**
     * @return 
     * @throws Exception ?
     */
    public IView execute() throws Exception {
        _LOG.info(I18N.formatMessage(YMP.__LSTRING_FILE, null, null, "ymp.mvc.request_executor_startup",
                this.requestMeta.getRequestMapping()));
        IView _view = null;
        if (chain != null) {
            _view = chain.doChain(this.requestMeta);
        }
        if (_view == null) {
            try {
                Object[] _params = null;
                if (this.requestMeta.getRequestMethodHandler() != null) {
                    //               _LOG.info("?????");
                    _params = this.requestMeta.getRequestMethodHandler().getMethodParams();
                } else {
                    _params = this.getMethodParams();
                }
                if (hasValidation()) {
                    Set<ValidateResult> _results = null;
                    if (this.requestMeta.getRequestMethodHandler() != null) {
                        //                  _LOG.info("????");
                        _results = this.requestMeta.getRequestMethodHandler()
                                .doValidation(this.requestMeta.getMethod(), _params);
                    } else {
                        _results = Validates.execute(validateRuleConf.getKey(), validateRuleConf.getValue(),
                                validateFieldValues);
                    }
                    if (_results != null && !_results.isEmpty()) {
                        if (WebMVC.getConfig().getErrorHandlerClassImpl() != null) {
                            _view = WebMVC.getConfig().getErrorHandlerClassImpl().onValidation(_results);
                        }
                        if (_view == null) {
                            throw new ValidationException(_results.toString());
                        }
                    }
                }
                if (_view == null) {
                    Object _result = this.requestMeta.getMethod().invoke(this.requestMeta.getTarget(), _params);
                    _view = this.processMethodResultToView(_result);
                }
            } finally {
                validateFieldValues.clear();
            }
        }
        _LOG.info(I18N.formatMessage(YMP.__LSTRING_FILE, null, null, "ymp.mvc.request_executor_stop",
                this.requestMeta.getRequestMapping()));
        return _view;
    }

    /**
     * @return ?????
     */
    protected boolean hasValidation() {
        return validateRuleConf.getKey() != null;
    }

}