KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > FormData


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.ServletRequest JavaDoc;
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 JavaDoc;
42 import java.lang.reflect.InvocationTargetException JavaDoc;
43 import java.util.Locale JavaDoc;
44 import java.util.HashMap JavaDoc;
45
46
47 /**
48  * Base class for form beans associated with action methods in {@link PageFlowController}s. Note that Page Flow actions
49  * may take form beans of any type.
50  */

51 public class FormData extends ValidatorForm
52 {
53     private static final Logger _log = Logger.getInstance( FormData.class );
54     
55     //
56
// This is used to allow us to run against Validator 1.0 or 1.1. The reflective Method is only used when running
57
// against Validator 1.0 (legacy).
58
//
59
private static Method JavaDoc _legacyInitValidatorMethod = null;
60     
61     static
62     {
63         try
64         {
65             _legacyInitValidatorMethod =
66                     Resources.class.getMethod( "initValidator",
67                                                new Class JavaDoc[]{
68                                                    String JavaDoc.class,
69                                                    Object JavaDoc.class,
70                                                    ServletContext JavaDoc.class,
71                                                    HttpServletRequest JavaDoc.class,
72                                                    ActionErrors.class,
73                                                    int.class
74                                                } );
75         }
76         catch ( NoSuchMethodException JavaDoc e )
77         {
78             // ignore -- we're in Validator 1.1 or later.
79
}
80     }
81     
82     /**
83      * Default constructor.
84      */

85     public FormData()
86     {
87         super();
88     }
89
90     public ActionErrors validate( ActionMapping mapping, HttpServletRequest JavaDoc request )
91     {
92         return validateBean( this, mapping.getAttribute(), mapping, request );
93     }
94
95     /**
96      * MessageResources that conglomerates a primary and backup MessageResources.
97      */

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 JavaDoc getMessage( Locale JavaDoc locale, String JavaDoc key )
112         {
113             String JavaDoc message = _primary.getMessage( locale, key );
114             if ( message == null ) message = _backup.getMessage( locale, key );
115             return message;
116         }
117     }
118     
119     /**
120      * Run all validation (declarative validation from annotations and the result of {@link Validatable#validate}) on
121      * a given bean.
122      *
123      * @param bean the bean to validate.
124      * @param beanName the name of the bean, to be passed to Validator to look up declarative validation rules.
125      * @param mapping the current ActionMapping.
126      * @param request the current HttpServletRequest.
127      * @return an ActionErrors object containing errors that occurred during bean validation.
128      */

129     protected ActionErrors validateBean( Object JavaDoc bean, String JavaDoc beanName, ActionMapping mapping, HttpServletRequest JavaDoc request )
130     {
131         MessageResources messageResources = ( MessageResources ) request.getAttribute( Globals.MESSAGES_KEY );
132         ExpressionAwareMessageResources.update( messageResources, bean );
133         
134         //
135
// See if this action uses a form that defines its own message resources. If so, use those, or combine them
136
// with the message resources from the current module.
137
//
138
if ( mapping instanceof PageFlowActionMapping )
139         {
140             PageFlowActionMapping pfam = ( PageFlowActionMapping ) mapping;
141             String JavaDoc 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         //
162
// If there's no MessageResources for this request, create one that can evaluate expressions.
163
//
164
if ( messageResources == null )
165         {
166             messageResources = new ExpressionAwareMessageResources( bean, request, getServlet().getServletContext() );
167             request.setAttribute( Globals.MESSAGES_KEY, messageResources );
168         }
169         
170         
171         ServletContext JavaDoc servletContext = getServlet().getServletContext();
172         ActionErrors errors = new ActionErrors();
173
174         //
175
// If the ValidatorPlugIn was initialized for this module, run it.
176
//
177
if ( Resources.getValidatorResources( servletContext, request ) != null )
178         {
179             try
180             {
181                 //
182
// Run validations associated with the bean.
183
//
184
Validator beanV = initValidator( beanName, bean, servletContext, request, errors, page );
185                 validatorResults = beanV.validate();
186                 
187                 //
188
// Run validations associated with the action.
189
//
190
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         //
200
// If this bean implements our Validatable interface, run its validate method.
201
//
202
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 JavaDoc beanName, Object JavaDoc bean, ServletContext JavaDoc context,
211                                             HttpServletRequest JavaDoc request, ActionErrors errors, int page )
212     {
213         if ( _legacyInitValidatorMethod != null )
214         {
215             try
216             {
217                 Object JavaDoc[] args = new Object JavaDoc[]{ beanName, bean, context, request, errors, new Integer JavaDoc( page ) };
218                 Validator validator = ( Validator ) _legacyInitValidatorMethod.invoke( Resources.class, args );
219                 
220                 //
221
// The NetUI validator rules work on both 1.1 and 1.2. They take ActionMessages instead of ActionErrors.
222
//
223
validator.addResource( "org.apache.struts.action.ActionMessages", errors );
224                 return validator;
225             }
226             catch ( IllegalAccessException JavaDoc e )
227             {
228                 assert false : e.getMessage();
229                 throw new RuntimeException JavaDoc( e );
230             }
231             catch ( InvocationTargetException JavaDoc e )
232             {
233                 assert false : e.getMessage();
234                 throw new RuntimeException JavaDoc( e );
235             }
236         }
237         else
238         {
239             return Resources.initValidator( beanName, bean, context, request, errors, page );
240         }
241     }
242 }
243
Popular Tags