1 18 package org.apache.beehive.netui.pageflow; 19 20 import org.apache.struts.validator.ValidatorForm; 21 import org.apache.struts.validator.Resources; 22 import org.apache.struts.action.ActionErrors; 23 import org.apache.struts.action.ActionMapping; 24 import org.apache.struts.util.MessageResources; 25 import org.apache.struts.util.MessageResourcesFactory; 26 import org.apache.struts.config.ModuleConfig; 27 import org.apache.struts.Globals; 28 import org.apache.commons.validator.Validator; 29 import org.apache.commons.validator.ValidatorException; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.ServletContext ; 33 import javax.servlet.ServletRequest ; 34 35 import org.apache.beehive.netui.util.logging.Logger; 36 import org.apache.beehive.netui.pageflow.config.PageFlowActionMapping; 37 import org.apache.beehive.netui.pageflow.internal.InternalExpressionUtils; 38 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 39 import org.apache.beehive.netui.pageflow.internal.ExpressionAwareMessageResources; 40 41 import java.lang.reflect.Method ; 42 import java.lang.reflect.InvocationTargetException ; 43 import java.util.Locale ; 44 import java.util.HashMap ; 45 46 47 51 public class FormData extends ValidatorForm 52 { 53 private static final Logger _log = Logger.getInstance( FormData.class ); 54 55 private static Method _legacyInitValidatorMethod = null; 60 61 static 62 { 63 try 64 { 65 _legacyInitValidatorMethod = 66 Resources.class.getMethod( "initValidator", 67 new Class []{ 68 String .class, 69 Object .class, 70 ServletContext .class, 71 HttpServletRequest .class, 72 ActionErrors.class, 73 int.class 74 } ); 75 } 76 catch ( NoSuchMethodException e ) 77 { 78 } 80 } 81 82 85 public FormData() 86 { 87 super(); 88 } 89 90 public ActionErrors validate( ActionMapping mapping, HttpServletRequest request ) 91 { 92 return validateBean( this, mapping.getAttribute(), mapping, request ); 93 } 94 95 98 private static class MergedMessageResources 99 extends MessageResources 100 { 101 private MessageResources _primary; 102 private MessageResources _backup; 103 104 public MergedMessageResources( MessageResources primary, MessageResources backup ) 105 { 106 super( primary.getFactory(), primary.getConfig(), primary.getReturnNull() ); 107 _primary = primary; 108 _backup = backup; 109 } 110 111 public String getMessage( Locale locale, String key ) 112 { 113 String message = _primary.getMessage( locale, key ); 114 if ( message == null ) message = _backup.getMessage( locale, key ); 115 return message; 116 } 117 } 118 119 129 protected ActionErrors validateBean( Object bean, String beanName, ActionMapping mapping, HttpServletRequest request ) 130 { 131 MessageResources messageResources = ( MessageResources ) request.getAttribute( Globals.MESSAGES_KEY ); 132 ExpressionAwareMessageResources.update( messageResources, bean ); 133 134 if ( mapping instanceof PageFlowActionMapping ) 139 { 140 PageFlowActionMapping pfam = ( PageFlowActionMapping ) mapping; 141 String bundle = pfam.getFormBeanMessageResourcesKey(); 142 143 if ( bundle != null ) 144 { 145 MessageResources formBeanResources = ( MessageResources ) request.getAttribute( bundle ); 146 ExpressionAwareMessageResources.update( formBeanResources, bean ); 147 148 if ( formBeanResources != null ) 149 { 150 if ( messageResources != null ) 151 { 152 formBeanResources = new MergedMessageResources( messageResources, formBeanResources ); 153 } 154 155 request.setAttribute( Globals.MESSAGES_KEY, formBeanResources ); 156 messageResources = formBeanResources; 157 } 158 } 159 } 160 161 if ( messageResources == null ) 165 { 166 messageResources = new ExpressionAwareMessageResources( bean, request, getServlet().getServletContext() ); 167 request.setAttribute( Globals.MESSAGES_KEY, messageResources ); 168 } 169 170 171 ServletContext servletContext = getServlet().getServletContext(); 172 ActionErrors errors = new ActionErrors(); 173 174 if ( Resources.getValidatorResources( servletContext, request ) != null ) 178 { 179 try 180 { 181 Validator beanV = initValidator( beanName, bean, servletContext, request, errors, page ); 185 validatorResults = beanV.validate(); 186 187 Validator actionV = initValidator( mapping.getPath(), bean, servletContext, request, errors, page ); 191 validatorResults.merge( actionV.validate() ); 192 } 193 catch ( ValidatorException e ) 194 { 195 _log.error( e.getMessage(), e ); 196 } 197 } 198 199 if ( bean instanceof Validatable ) 203 { 204 ( ( Validatable ) bean ).validate( mapping, request, errors ); 205 } 206 207 return errors; 208 } 209 210 private static Validator initValidator( String beanName, Object bean, ServletContext context, 211 HttpServletRequest request, ActionErrors errors, int page ) 212 { 213 if ( _legacyInitValidatorMethod != null ) 214 { 215 try 216 { 217 Object [] args = new Object []{ beanName, bean, context, request, errors, new Integer ( page ) }; 218 Validator validator = ( Validator ) _legacyInitValidatorMethod.invoke( Resources.class, args ); 219 220 validator.addResource( "org.apache.struts.action.ActionMessages", errors ); 224 return validator; 225 } 226 catch ( IllegalAccessException e ) 227 { 228 assert false : e.getMessage(); 229 throw new RuntimeException ( e ); 230 } 231 catch ( InvocationTargetException e ) 232 { 233 assert false : e.getMessage(); 234 throw new RuntimeException ( e ); 235 } 236 } 237 else 238 { 239 return Resources.initValidator( beanName, bean, context, request, errors, page ); 240 } 241 } 242 } 243 | Popular Tags |