jp.terasoluna.fw.service.thin.BLogicMapper.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.service.thin.BLogicMapper.java

Source

/*
 * Copyright (c) 2007 NTT DATA Corporation
 *
 * 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 jp.terasoluna.fw.service.thin;

import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.terasoluna.fw.util.BeanUtil;
import jp.terasoluna.fw.util.PropertyAccessException;
import jp.terasoluna.fw.web.struts.form.FormEx;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * rWlX?WbN?o?fNX?B
 *
 * <p>
 *  BLogicIOPlugIn??BLogicResources?A
 *  WebwIuWFNg?ArWlX?WbNf?[^}bsO?s?B<br>
 *  WebwNGXg?(request)?AZbV?(session)?A
 *  T?[ubgReLXg(application)??B<br>
 *  rWlX?WbN?oNGXg?(request)?AZbV?(session)
 *  ??B?rWlX?WbNT?[ubgReLXg?o
 *  T|?[g?B
 * </p>
 * <p>
 *  AbstractBLogicMapperTuNX?A
 *  ftHgNX?A
 *  struts-config.xmlBLogicIOPlugIn??A
 *  @\u?B
 *  ??AAbstractBLogicMapperBLogicMapper
 *  p?rWlX?WbN?o?fNX??Kv?B
 *  grWlX?WbN?o?fNX?A
 *  blogic-io.xmlsource?request?Asession?Aapplication?A
 *  dest?request?AsessionO
 *  Cw??l???A?olf???B<br>
 *  l???\bh?A
 *  "getValueFrom" + source?w?B
 *  source?"factory"w???A?\bhgetValueFromFactory?B
 *  ?Sl?\bh?A
 *  getValueFromForm()?\bh??B<br>
 *  ?olf???\bh?A
 *  "setValueTo" + dest?w?B
 *  dest?"factory"w???A?\bhgetValueToFactory?B
 *  ?S?olf?\bh?A
 *  getValueToForm()?\bh??B<br>
 *  ??Al??AActionForm??lXgv?peBw
 *  \?B
 * </p>
 * <p>
 *  rWlX?WbN?o?fNX?E
 *  struts-config.xmlL?q@?ABLogicIOPlugInQ??B
 * </p>
 *
 * @see jp.terasoluna.fw.service.thin.AbstractBLogicMapper
 * @see jp.terasoluna.fw.web.struts.plugins.BLogicIOPlugIn
 *
 */
public class BLogicMapper extends AbstractBLogicMapper {

    /**
     * ?ONX?B
     */
    private static Log log = LogFactory.getLog(BLogicMapper.class);

    /**
     * \?[Xt@C??G?[R?[h?B
     */
    private static final String ERROR_RESOURCES_FILE = "errors.resources.file";

    /**
     * RXgN^?B
     */
    public BLogicMapper() {

    }

    /**
     * RXgN^?B
     * @param resources \?[XpX
     */
    public BLogicMapper(String resources) {
        if (resources == null || "".equals(resources)) {
            log.error("resources file location is not specified");
            throw new IllegalArgumentException(ERROR_RESOURCES_FILE);
        }
    }

    /**
     * NGXgwv?peBl?B
     *
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @return v?peBl
     */
    @Override
    public Object getValueFromRequest(String propName, HttpServletRequest request, HttpServletResponse response) {

        if (propName == null || "".equals(propName)) {
            log.error("illegal argument:propName = [" + propName + "]");
            throw new IllegalArgumentException();
        }

        // NGXgl
        Object value = request.getAttribute(propName);

        if (log.isDebugEnabled()) {
            if (request.getAttribute(propName) == null) {
                log.debug("Request's attribute is null:key =[" + propName + "]");
            }
        }

        return value;
    }

    /**
     * tH?[wv?peBl?B
     *
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @return v?peBl
     */
    public Object getValueFromForm(String propName, HttpServletRequest request,
            @SuppressWarnings("unused") HttpServletResponse response) throws PropertyAccessException {
        // tH?[
        ActionForm form = getActionForm(request);

        // Forml
        Object value = null;
        try {
            value = BeanUtil.getBeanProperty(form, propName);
        } catch (PropertyAccessException e) {
            log.error("illegal argument:propName = [" + propName + "]");
            throw e;
        }
        return value;
    }

    /**
     * ZbVwv?peBL?[l?B
     *
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @return v?peBl
     */
    @Override
    public Object getValueFromSession(String propName, HttpServletRequest request, HttpServletResponse response) {

        if (propName == null || "".equals(propName)) {
            log.error("illegal argument:propName = [" + propName + "]");
            throw new IllegalArgumentException();
        }

        // ZbV
        HttpSession session = request.getSession(true);
        // ZbVl
        Object value = session.getAttribute(propName);

        if (log.isDebugEnabled()) {
            if (session.getAttribute(propName) == null) {
                log.debug("Session's attribute is null:key =[" + propName + "]");
            }
        }
        return value;
    }

    /**
     * NGXgwv?peBli[?B
     *
     * @param value ?ol
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     */
    @SuppressWarnings("unchecked")
    @Override
    public void setValueToRequest(Object value, String propName, HttpServletRequest request,
            HttpServletResponse response) {

        if (propName == null || "".equals(propName)) {
            log.error("illegal argument:propName = [" + propName + "]");
            throw new IllegalArgumentException();
        }

        if (log.isDebugEnabled()) {
            Enumeration<String> enumeration = request.getAttributeNames();
            int existFlag = 0;
            while (enumeration.hasMoreElements()) {
                String key = enumeration.nextElement();
                if (key.equals(propName)) {
                    existFlag = 1;
                    break;
                }
            }
            if (existFlag != 1) {
                log.debug("Request's key does not exist:key =[" + propName + "]");
            }
        }
        // NGXgli[
        request.setAttribute(propName, value);
    }

    /**
     * tH?[wv?peBli[?B<br>
     * tH?[FormExCX^X?AmodifiedtOtrue
     * ??B
     *
     * @param value ?ol
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     */
    public void setValueToForm(Object value, String propName, HttpServletRequest request,
            HttpServletResponse response) throws PropertyAccessException {
        // tH?[
        ActionForm form = getActionForm(request);

        // tH?[li[
        try {
            BeanUtil.setBeanProperty(form, propName, value);
            if (form instanceof FormEx) {
                FormEx formEx = (FormEx) form;
                formEx.setModified(true);
            }
        } catch (PropertyAccessException e) {
            log.error("illegal argument:propName = [" + propName + "]");
            throw e;
        }
    }

    /**
     * ZbVwv?peBL?[li[?B
     *
     * @param value ?ol
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     */
    @SuppressWarnings("unchecked")
    @Override
    public void setValueToSession(Object value, String propName, HttpServletRequest request,
            HttpServletResponse response) {

        if (propName == null || "".equals(propName)) {
            log.error("illegal argument:propName = [" + propName + "]");
            throw new IllegalArgumentException();
        }

        // ZbV
        HttpSession session = request.getSession(true);

        if (log.isDebugEnabled()) {
            Enumeration<String> enumeration = session.getAttributeNames();
            int existFlag = 0;
            while (enumeration.hasMoreElements()) {
                String key = enumeration.nextElement();
                if (key.equals(propName)) {
                    existFlag = 1;
                    break;
                }
            }
            if (existFlag != 1) {
                log.debug("Session's key does not exist:key =[" + propName + "]");
            }
        }
        // ZbVli[
        session.setAttribute(propName, value);
    }

    /**
     * NGXgZbVi[ActionForm
     * CX^X?B
     *
     * @param request HTTPNGXg
     * @return ActionFormCX^X
     */
    protected ActionForm getActionForm(HttpServletRequest request) {

        ActionMapping mapping = (ActionMapping) request.getAttribute(Globals.MAPPING_KEY);
        ActionForm form = null;
        if ("request".equals(mapping.getScope())) {
            form = (ActionForm) request.getAttribute(mapping.getAttribute());
        } else {
            HttpSession session = request.getSession();
            form = (ActionForm) session.getAttribute(mapping.getAttribute());
        }
        // ActionFormp
        return form;
    }

    /**
     * T?[ubgReLXgwv?peBL?[l?B
     *
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @return v?peBl
     */
    @Override
    public Object getValueFromApplication(String propName, HttpServletRequest request,
            HttpServletResponse response) {

        if (propName == null || "".equals(propName)) {
            log.error("illegal argument:propName = [" + propName + "]");
            throw new IllegalArgumentException();
        }

        // T?[ubgReLXg
        HttpSession session = request.getSession(true);
        ServletContext context = session.getServletContext();
        // ZbVl
        Object value = context.getAttribute(propName);

        if (log.isDebugEnabled()) {
            if (value == null) {
                log.debug("ServletContext's attribute is null:key =[" + propName + "]");
            }
        }
        return value;
    }
}