KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > genmodel > GenActionModel


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.compiler.genmodel;
19
20 import org.apache.beehive.netui.compiler.CompilerUtils;
21 import org.apache.beehive.netui.compiler.JpfLanguageConstants;
22 import org.apache.beehive.netui.compiler.model.ActionModel;
23 import org.apache.beehive.netui.compiler.model.ForwardModel;
24 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
25 import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
26 import org.apache.beehive.netui.compiler.typesystem.declaration.Declaration;
27 import org.apache.beehive.netui.compiler.typesystem.declaration.MethodDeclaration;
28 import org.apache.beehive.netui.compiler.typesystem.declaration.ParameterDeclaration;
29 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
30 import org.apache.beehive.netui.compiler.typesystem.type.DeclaredType;
31 import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
32
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36
37
38 public class GenActionModel
39         extends ActionModel
40         implements JpfLanguageConstants
41 {
42     public GenActionModel( Declaration sourceElement, GenStrutsApp parentApp, ClassDeclaration jclass )
43     {
44         super( parentApp );
45         
46         init( getActionName( sourceElement ), getActionAnnotation( sourceElement ), parentApp, jclass );
47         
48         // Get the form class from the method argument.
49
setFormBeanName( getFormBean( sourceElement, parentApp ) );
50     }
51     
52     protected GenActionModel( String JavaDoc actionName, AnnotationInstance ann, GenStrutsApp parentApp, ClassDeclaration jclass )
53     {
54         super( parentApp );
55         init( actionName, ann, parentApp, jclass );
56     }
57     
58     private void init( String JavaDoc actionName, AnnotationInstance annotation, GenStrutsApp parentApp, ClassDeclaration jclass )
59     {
60         setPath( '/' + actionName );
61         
62         //
63
// loginRequired
64
//
65
Boolean JavaDoc loginRequired = CompilerUtils.getBoolean( annotation, LOGIN_REQUIRED_ATTR, true );
66         if ( loginRequired == null )
67         {
68             loginRequired = parentApp.getFlowControllerInfo().getMergedControllerAnnotation().isLoginRequired();
69         }
70         if ( loginRequired != null ) setLoginRequired( loginRequired.booleanValue() );
71         
72         //
73
// prevent-double-submit
74
//
75
Boolean JavaDoc preventDoubleSubmit = CompilerUtils.getBoolean( annotation, PREVENT_DOUBLE_SUBMIT_ATTR, false );
76         setPreventDoubleSubmit( preventDoubleSubmit.booleanValue() );
77         
78         //
79
// readOnly
80
//
81
Boolean JavaDoc readOnly = CompilerUtils.getBoolean( annotation, READONLY_ATTR, true );
82         if ( readOnly == null )
83         {
84             readOnly = Boolean.valueOf( parentApp.getFlowControllerInfo().getMergedControllerAnnotation().isReadOnly() );
85         }
86         setReadonly( readOnly.booleanValue() );
87         
88         //
89
// rolesAllowed -- avoid setting this if loginRequired is explicitly false.
90
//
91
if ( loginRequired == null || loginRequired.booleanValue() ) setRolesAllowed( annotation, jclass, parentApp );
92         
93         //
94
// type (delegating Action class, with the FlowController as parameter)
95
//
96
setType( FLOW_CONTROLLER_ACTION_CLASS );
97         setParameter( jclass.getQualifiedName() );
98         
99         //
100
// form bean member -- the page-flow-scoped form referenced by the action (a member variable)
101
//
102
setFormMember( CompilerUtils.getString( annotation, USE_FORM_BEAN_ATTR, true ) );
103         
104         //
105
// forwards
106
//
107
getForwards( annotation, jclass, parentApp );
108         
109         //
110
// validationErrorForward -- the forward used when validation fails
111
//
112
AnnotationInstance validateErrFwd = CompilerUtils.getAnnotation( annotation, VALIDATION_ERROR_FORWARD_ATTR, true );
113         boolean doValidation = false;
114         if ( validateErrFwd != null )
115         {
116             ForwardModel fwd = new GenForwardModel( parentApp, validateErrFwd, jclass, " (validationErrorForward)" );
117             addForward( fwd );
118             setInput( fwd.getName() );
119             doValidation = true;
120         }
121         
122         //
123
// validate
124
//
125
Boolean JavaDoc explicitDoValidation = CompilerUtils.getBoolean( annotation, DO_VALIDATION_ATTR, true );
126         setValidate( explicitDoValidation != null ? explicitDoValidation.booleanValue() : doValidation );
127
128         //
129
// exception-catches
130
//
131
GenExceptionModel.addCatches( annotation, this, jclass, parentApp, this );
132     }
133     
134     private void setRolesAllowed( AnnotationInstance annotation, ClassDeclaration jclass, GenStrutsApp parentApp )
135     {
136         List JavaDoc rolesAllowed = CompilerUtils.getStringArray( annotation, ROLES_ALLOWED_ATTR, true );
137         List JavaDoc classLevelRA = parentApp.getFlowControllerInfo().getMergedControllerAnnotation().getRolesAllowed();
138         Iterator JavaDoc it = null;
139         
140         if ( rolesAllowed != null && classLevelRA != null )
141         {
142             HashSet JavaDoc merged = new HashSet JavaDoc();
143             for ( Iterator JavaDoc ii = rolesAllowed.iterator(); ii.hasNext(); )
144             {
145                 String JavaDoc role = ( String JavaDoc ) ii.next();
146                 merged.add( role );
147             }
148             for ( Iterator JavaDoc ii = classLevelRA.iterator(); ii.hasNext(); )
149             {
150                 String JavaDoc classLevelRole = ( String JavaDoc ) ii.next();
151                 merged.add( classLevelRole );
152             }
153             it = merged.iterator();
154         }
155         else if ( rolesAllowed != null )
156         {
157             it = rolesAllowed.iterator();
158         }
159         else if ( classLevelRA != null )
160         {
161             it = classLevelRA.iterator();
162         }
163         
164         if ( it != null && it.hasNext() )
165         {
166             StringBuffer JavaDoc rolesAllowedStr = new StringBuffer JavaDoc( ( String JavaDoc ) it.next() );
167             
168             while ( it.hasNext() )
169             {
170                 rolesAllowedStr.append( ',' ).append( ( ( String JavaDoc ) it.next() ).trim() );
171             }
172             
173             setRoles( rolesAllowedStr.toString() );
174         }
175     }
176     
177     protected static String JavaDoc getActionName( Declaration sourceElement )
178     {
179         return sourceElement.getSimpleName();
180     }
181     
182     /**
183      * @return the Struts name of the form bean.
184      */

185     protected String JavaDoc getFormBean( Declaration sourceElement, GenStrutsApp parentApp )
186     {
187         assert sourceElement instanceof MethodDeclaration : sourceElement.getClass().getName();
188         ParameterDeclaration[] params = ( ( MethodDeclaration ) sourceElement ).getParameters();
189         String JavaDoc formBeanName = null;
190         
191         if ( params.length > 0 )
192         {
193             assert params.length == 1 : params.length; // checker should catch this
194
TypeInstance paramType = CompilerUtils.getGenericBoundsType( params[0].getType() );
195             formBeanName = addFormBean( paramType, parentApp );
196         }
197         
198         return formBeanName;
199     }
200     
201     protected String JavaDoc addFormBean( TypeInstance paramType, GenStrutsApp parentApp )
202     {
203         paramType = CompilerUtils.getGenericBoundsType( paramType );
204         assert paramType instanceof DeclaredType : paramType.getClass().getName(); // checker should enforce this
205
TypeDeclaration decl = CompilerUtils.getDeclaration( ( DeclaredType ) paramType );
206         String JavaDoc formBeanName = parentApp.addFormBean( decl, this );
207         
208         //
209
// If this isn't an ActionForm-derived argument, keep track of the classname for the runtime.
210
//
211
if ( ! CompilerUtils.isAssignableFrom( PAGEFLOW_FORM_CLASS_NAME, decl, parentApp.getEnv() ) )
212         {
213             setFormClass( CompilerUtils.getLoadableName( decl ) );
214         }
215         
216         return formBeanName;
217     }
218     
219     protected AnnotationInstance getActionAnnotation( Declaration sourceElement )
220     {
221         assert sourceElement instanceof MethodDeclaration : sourceElement.getClass().getName();
222         return CompilerUtils.getAnnotation( sourceElement, ACTION_TAG_NAME );
223     }
224     
225     protected void getForwards( AnnotationInstance annotation, ClassDeclaration jclass, GenStrutsApp parentApp )
226     {
227         GenForwardModel.addForwards( annotation, this, jclass, parentApp, null );
228     }
229 }
230
Popular Tags