開発メモ

開発用のメモです。

実行したアクション情報を取得する Interceptor

import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapping;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ActionInfoInterceptor extends AbstractInterceptor {

    Logger logger = Logger.getLogger(getClass());

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        String ret = invocation.invoke();
        if (logger.isDebugEnabled() == false) {
            return ret;
        }
        ActionMapping map = ServletActionContext.getActionMapping();

        if (map != null) {
            logger.debug(String.format("%20s : %s", "Namespace", map.getNamespace()));
            logger.debug(String.format("%20s : %s", "+Action", map.getName()));
            logger.debug(String.format("%20s : %s", "+Method", map.getMethod()));
            logger.debug(String.format("%20s : %s", "+Class", map.getClass().getName()));
            for (Entry<String, Object> param : map.getParams().entrySet()) {
                logger.debug(String.format("%20s : %s", "+param[" + param.getKey() + "]", param.getValue()));
            }
            logger.debug(String.format("%20s : %s", "+Result", ret));
        }
        return ret;
    }

}
Twitter: @asahina_alice