Java tutorial
/* * 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.web.struts.actions; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import jp.terasoluna.fw.exception.SystemException; import jp.terasoluna.fw.service.thin.AbstractBLogicMapper; import jp.terasoluna.fw.service.thin.BLogicIO; import jp.terasoluna.fw.service.thin.BLogicMessage; import jp.terasoluna.fw.service.thin.BLogicMessages; import jp.terasoluna.fw.service.thin.BLogicResources; import jp.terasoluna.fw.service.thin.BLogicResult; import jp.terasoluna.fw.web.struts.ModuleUtil; import jp.terasoluna.fw.web.struts.action.ActionMappingEx; import jp.terasoluna.fw.web.struts.plugins.BLogicIOPlugIn; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; /** * rWlX?WbNN?NX?B * * <p> * rWlX?WbNN?sANVNX@\ * ?W?NX?BSrWlX?WbNNANVNX * NXp??B * BLogicActionNXp??B * @\L?B * <ol> * <li>struts-config.xmlANV? * rWlX?WbNNX?B</li> * <li>rWlX?WbN? JavaBean ???B</li> * <li>p??NXGg|CgdoExecuteBLogic()?s?B</li> * <li>rWlX?WbN???I???ABLogicResult???B</li> * </ol> * * <p> * rWlX?WbNNANVNX?A * Bean`t@CNrWlX?WbN???A * ?s?B * L?ArWlX?WbNSampleBLogicSampleAction * N??B * struts-config.xml??B * </p> * <p> * <strong>Bean`t@C?</strong> * <code><pre> * <bean name="/SampleAction" scope="prototype" * <b>class="jp.terasoluna.sample1.actions.SampleAction"</b>> * <b><property name="sampleBLogic"> * <ref bean="SampleBLogic"></ref> * </property></b> * </bean> * <bean id="SampleBLogic" * <b>class="jp.terasoluna.sample1.blogic.SampleBLogic"</b>> * </bean> * </pre></code> * </p> * <p> * <strong>struts-config.xml?</strong> * <code><pre> * <action path="/SampleAction" * name="_sampleForm" * validate="true" * scope="session" * input="/sample.jsp"> * <forward name="success" path="/sampleSCR.do"/> * <forward name="failure" path="/errorSCR.do"/> * </action> * </pre></code> * </p> * BLogicResultp?A??A * BLogicIOPlugIn?ABLogicResult?AAbstractBLogicMapperQ??B * </p> * * <p> * ?ArWlX?WbN?ss???A * ?bZ?[W???ABLogicResult?bZ?[Wi[?B * </p> * <p> * <code><pre> * public BLogicResult doExecuteBLogic(ParamsBean params) { * * // BLogicResult???B * BLogicResult result = new BLogicResult(); * ?E?E?E * //rWlX?WbN * ?E?E?E * //G?[ * if (// G?[?? ) { * // Webwf???B * ?E?E?E * return result; * } else { * // rWlX?WbNG?[? * // BLogicMessages?? * BLogicMessages messages = new BLogicMessages(); * // GROUP_ERRORO?[v?bZ?[W?ABLogicMessagei[ * messages.add("GROUP_ERROR", new BLogicMessage("message.error.sample", "sample")); * // G?[pBLogicMessagesBLogicResult? * result.setErrors(messages); * // ?s"failure"w * result.setResultString("failure"); * return result; * } * } * </pre></code> * </p> * <p> * L?A<property>vf"saveMessageScope"?A * rWlX?WbN??BLogicMessages? * "request""session"w?B * v?peB`???"request"?B * * <code><pre> * <bean name="/SampleAction" scope="prototype" * class="jp.terasoluna.sample1.actions.SampleAction"> * <property name="sampleBLogic"> * <ref bean="SampleBLogic"></ref> * </property> * <b><property name="saveMessageScope" value="session"/></b> * </bean> * </pre></code> * </p> * * @see jp.terasoluna.fw.web.struts.actions.BLogicAction * @see jp.terasoluna.fw.web.struts.plugins.BLogicIOPlugIn * @see jp.terasoluna.fw.service.thin.BLogicResult * @see jp.terasoluna.fw.service.thin.AbstractBLogicMapper * @see jp.terasoluna.fw.service.thin.BLogicMapper * @see jp.terasoluna.fw.service.thin.BLogicMessage * @see jp.terasoluna.fw.service.thin.BLogicMessages * * @param <P> rWlX?WbNlJavaBeanw * */ public abstract class AbstractBLogicAction<P> extends ActionEx { /** * ?ONX?B */ private static Log log = LogFactory.getLog(AbstractBLogicAction.class); /** * gANVtH?[?G?[G?[R?[h?B */ protected static final String BLOGIC_FORM_ILLEGAL_ERROR = "errors.blogic.form"; /** * gANV}bsO?G?[G?[R?[h?B */ protected static final String BLOGIC_MAPPING_ILLEGAL_ERROR = "errors.blogic.mapping"; /** * gANV\?[X?G?[G?[R?[h?B */ protected static final String BLOGIC_RESOURCES_ILLEGAL_ERROR = "errors.blogic.resources"; /** * BLogicResultnullpG?[R?[h?B */ protected static final String BLOGIC_RESULT_NULL_ERROR = "errors.blogic.result.null"; /** * AbstractBLogicMappernull??G?[R?[h?B */ protected static final String NULL_MAPPER_KEY = "errors.blogic.mapper.null"; /** * ?bZ?[W?XR?[v?B * * rWlX?WbN??BLogicMessages? * requestsessionw?B */ private String saveMessageScope = null; /** * ?bZ?[W?XR?[v??B * * @param saveMessageScope ?bZ?[W?XR?[v */ public void setSaveMessageScope(String saveMessageScope) { this.saveMessageScope = saveMessageScope; } /** * rWlX?WbN?s?B * <p> * rWlX?WbN?sKvL???s?B<br> * <ul> * <li>rWlX?WbN?JavaBean??</li> * <li>rWlX?WbN?o?BLogicResult?</li> * <li>tH??[h?</li> * </ul> * </p> * * @param mapping ANV}bsO * @param form tH?[ * @param request NGXg * @param response X|X * * @return J?ANVtH??[h * @throws Exception TuNXX??[?A\O */ @Override public ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { if (log.isDebugEnabled()) { log.debug("*** doExecute() called. action path=[" + mapping.getPath() + "] ***"); } ActionMappingEx mappingEx = null; try { mappingEx = (ActionMappingEx) mapping; } catch (ClassCastException e) { log.error("Illegal ActionMapping."); throw new SystemException(e, BLOGIC_MAPPING_ILLEGAL_ERROR); } P params = getBLogicParams(mappingEx, request, response); if (log.isDebugEnabled()) { log.debug("*** BLogicParams is prepared. ***"); if (params != null) { // params??_v?B log.debug("BLogicParams:" + params.toString()); } else { // paramsnull?? log.debug("BLogicParams:null"); } } if (log.isDebugEnabled()) { log.debug("*** starting blogic action[" + this.getClass().getName() + "] ***"); } BLogicResult result = null; // O?? preDoExecuteBLogic(request, response, params); // rWlX?WbNN result = doExecuteBLogic(params); // ?? postDoExecuteBLogic(request, response, params, result); if (log.isDebugEnabled()) { log.debug("*** finished blogic action[" + this.getClass().getName() + "] ***"); } if (result != null) { // BLogicResultnullp?? // ]?s?B evaluateBLogicResult(result, request, response, mappingEx); // ActionForward return mapping.findForward(result.getResultString()); } log.error("BLogicResult is null."); // nullp??SystemExceptionX??[?B throw new SystemException(new NullPointerException(), BLOGIC_RESULT_NULL_ERROR); } /** * rWlX?WbN?sO???B * * @param request NGXg * @param response X|X * @param params p??[^?iJavaBean?j * @throws Exception \O */ protected void preDoExecuteBLogic(HttpServletRequest request, HttpServletResponse response, P params) throws Exception { } /** * rWlX?WbN?s???B * * <p>rWlX?WbNO????A?s?B</p> * * @param request NGXg * @param response X|X * @param params p??[^?iJavaBean?j * @param result rWlX?WbN?s * @throws Exception \O */ protected void postDoExecuteBLogic(HttpServletRequest request, HttpServletResponse response, P params, BLogicResult result) throws Exception { } /** * BLogicResult]?AWebwIuWFNgf?s?B * * @param result BLogicResultCX^X * @param request HTTPNGXg * @param response HTTPX|X * @param mappingEx gANV}bsO */ protected void evaluateBLogicResult(BLogicResult result, HttpServletRequest request, HttpServletResponse response, ActionMappingEx mappingEx) { // BLogicResultnull`FbN if (result == null) { log.error("BLogicResult is null."); throw new SystemException(new NullPointerException(), BLOGIC_RESULT_NULL_ERROR); } // BLogicResult??_v?B if (log.isDebugEnabled()) { log.debug("*** BLogicResult is prepared. ***"); log.debug("BLogicResult:" + result.toString()); } ActionMessages errors = convertMessages(result.getErrors()); ActionMessages messages = convertMessages(result.getMessages()); // ANV}bsOwG?[?E?bZ?[W? // ??B if ("session".equalsIgnoreCase(saveMessageScope)) { // ?ZbVw???A // ZbVG?[?E?bZ?[W??B // ZbV HttpSession session = request.getSession(true); addErrors(session, errors); addMessages(session, messages); } else { // O???ANGXg?B addErrors(request, errors); addMessages(request, messages); } if (isErrorsEmpty(result)) { processBLogicResult(result, request, response, mappingEx); } } /** * BLogicResultWebwIuWFNgf?s?B * * @param result BLogicResultCX^X * @param request HTTPNGXg * @param response HTTPX|X * @param mappingEx gANV}bsO */ protected void processBLogicResult(BLogicResult result, HttpServletRequest request, HttpServletResponse response, ActionMappingEx mappingEx) { // G?[??AWebwf if (log.isDebugEnabled()) { log.debug("*** setting result into web layer. ***"); } getBLogicMapper(request).mapBLogicResult(request, response, getBLogicIO(mappingEx, request), result); } /** * BLogicMessageseActionMessagesi[?B * * @param blogicMessages BLogicMessagesCX^X * @return ActionMessages */ @SuppressWarnings("unchecked") protected ActionMessages convertMessages(BLogicMessages blogicMessages) { if (blogicMessages == null) { // blogicMessagesnull???Anullp return null; } ActionMessages messages = new ActionMessages(); // BLogicMessagespCe?[^ Iterator itr = blogicMessages.get(); // ?bZ?[WO?[vpCe?[^ Iterator groupItr = blogicMessages.getGroup(); while (itr.hasNext()) { // BLogicMessage BLogicMessage blogicMessage = (BLogicMessage) itr.next(); // ActionMessage?? ActionMessage actionMessage = null; if (blogicMessage.isResource()) { actionMessage = new ActionMessage(blogicMessage.getKey(), blogicMessage.getValues()); } else { actionMessage = new ActionMessage(blogicMessage.getKey(), blogicMessage.isResource()); } // ?bZ?[WO?[v String group = (String) groupItr.next(); // ?bZ?[W?i[ messages.add(group, actionMessage); } return messages; } /** * rWlX?WbN?s??\bh?B * TuNX?B * * @param param rWlX?WbN? * @return rWlX?WbN?o? * @throws Exception \O */ public abstract BLogicResult doExecuteBLogic(P param) throws Exception; /** * BLogicResulti[G?[? * null?Atruep?B * * @param result rWlX?WbN?s * @return G?[pBLogicMessagesnull?Atrue */ protected boolean isErrorsEmpty(BLogicResult result) { // BLogicResult null`FbN if (result == null) { log.error("BLogicResult is null."); throw new SystemException(new NullPointerException(), BLOGIC_RESULT_NULL_ERROR); } BLogicMessages errors = result.getErrors(); if (errors == null) { return true; } return errors.isEmpty(); } /** * BLogicMapperCX^X?B * * @param req HTTPNGXg * @return BLogicMapperCX^X */ protected AbstractBLogicMapper getBLogicMapper(HttpServletRequest req) { String moduleName = ModuleUtil.getPrefix(req); AbstractBLogicMapper mapper = null; try { mapper = (AbstractBLogicMapper) servlet.getServletContext() .getAttribute(BLogicIOPlugIn.BLOGIC_MAPPER_KEY + moduleName); } catch (ClassCastException e) { // AbstractBLogicMapperCX^X???AON?B log.error("Cannot cast BLogicMapper : " + e.getMessage()); throw new SystemException(e, BLOGIC_MAPPING_ILLEGAL_ERROR); } // BLogicMappernull???AO? if (mapper == null) { log.error("BLogicMapper is null."); throw new SystemException(new NullPointerException(), NULL_MAPPER_KEY); } return mapper; } /** * params???Ap?B * * @param mapping ANV}bsO * @param request HTTPNGXg * @param response HTTPX|X * @return rWlX?WbN? * @throws Exception \O */ @SuppressWarnings("unchecked") protected P getBLogicParams(ActionMappingEx mapping, HttpServletRequest request, HttpServletResponse response) throws Exception { BLogicIO io = getBLogicIO(mapping, request); // tH?[?AZbV?l P bean = (P) getBLogicMapper(request).mapBLogicParams(request, response, io); return bean; } /** * BLogicIO?B * * @param mapping ANV}bsO * @param request HTTPNGXg * @return rWlX?WbN?o? */ protected BLogicIO getBLogicIO(ActionMapping mapping, HttpServletRequest request) { String moduleName = ModuleUtil.getPrefix(request); BLogicResources resource = null; try { resource = (BLogicResources) servlet.getServletContext() .getAttribute(BLogicResources.BLOGIC_RESOURCES_KEY + moduleName); } catch (ClassCastException e) { // BLogicResourcesCX^X???AON?B log.error("Cannot cast BLogicResources : " + e.getMessage()); throw new SystemException(e, BLOGIC_RESOURCES_ILLEGAL_ERROR); } if (resource != null) { return resource.getBLogicIO(mapping.getPath()); } return null; } }