KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > config > ManagedBeanBuilder


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 package org.apache.myfaces.config;
17
18 import org.apache.myfaces.config.element.*;
19 import org.apache.myfaces.util.ClassUtils;
20
21 import javax.faces.FacesException;
22 import javax.faces.application.Application;
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.PropertyResolver;
25 import javax.faces.el.ValueBinding;
26 import javax.faces.webapp.UIComponentTag;
27 import java.util.*;
28
29
30 /**
31  * Create and initialize managed beans
32  *
33  * @author <a HREF="mailto:oliver@rossmueller.com">Oliver Rossmueller</a> (latest modification by $Author: bdudney $)
34  * @author Anton Koinov
35  *
36  * $Log: ManagedBeanBuilder.java,v $
37  * Revision 1.5 2005/01/05 16:22:57 bdudney
38  * added fail early code so that if you specify a non-existent property in the managed-bean stuff it will fail early instead of a lame error message that leads down a rabit trail.
39  *
40  * Revision 1.4 2004/10/13 11:50:59 matze
41  * renamed packages to org.apache
42  *
43  * Revision 1.3 2004/10/05 22:34:22 dave0000
44  * bug 1021656 with related improvements
45  *
46  * Revision 1.2 2004/08/10 10:57:38 manolito
47  * fixed StackOverflow in ClassUtils and cleaned up ClassUtils methods
48  *
49  * Revision 1.1 2004/07/07 00:25:05 o_rossmueller
50  * tidy up config/confignew package (moved confignew classes to package config)
51  *
52  */

53 public class ManagedBeanBuilder
54 {
55     public Object JavaDoc buildManagedBean(FacesContext facesContext, ManagedBean beanConfiguration) throws FacesException
56     {
57         Object JavaDoc bean = ClassUtils.newInstance(beanConfiguration.getManagedBeanClassName());
58
59         switch (beanConfiguration.getInitMode())
60         {
61             case ManagedBean.INIT_MODE_PROPERTIES:
62                 try {
63                   initializeProperties(facesContext, beanConfiguration
64                       .getManagedProperties(), bean);
65                 } catch (IllegalArgumentException JavaDoc e) {
66                   throw new IllegalArgumentException JavaDoc(
67                           e.getMessage()
68                               + " for bean '"
69                               + beanConfiguration.getManagedBeanName()
70                               + "' check the configuration to make sure all properties correspond with get/set methods");
71                 }
72                 break;
73
74             case ManagedBean.INIT_MODE_MAP:
75                 if (!(bean instanceof Map))
76                 {
77                     throw new IllegalArgumentException JavaDoc("Class " + bean.getClass().getName()
78                         + " of managed bean "
79                         + beanConfiguration.getManagedBeanName()
80                         + " is not a Map.");
81                 }
82                 initializeMap(facesContext, beanConfiguration.getMapEntries(), (Map) bean);
83                 break;
84
85             case ManagedBean.INIT_MODE_LIST:
86                 if (!(bean instanceof List))
87                 {
88                     throw new IllegalArgumentException JavaDoc("Class " + bean.getClass().getName()
89                         + " of managed bean "
90                         + beanConfiguration.getManagedBeanName()
91                         + " is not a List.");
92                 }
93                 initializeList(facesContext, beanConfiguration.getListEntries(), (List) bean);
94                 break;
95
96             case ManagedBean.INIT_MODE_NO_INIT:
97                 // no init values
98
break;
99
100             default:
101                 throw new IllegalStateException JavaDoc("Unknown managed bean type "
102                     + bean.getClass().getName() + " for managed bean "
103                     + beanConfiguration.getManagedBeanName() + '.');
104         }
105         return bean;
106     }
107
108
109     private void initializeProperties(FacesContext facesContext, Iterator managedProperties, Object JavaDoc bean)
110     {
111         while (managedProperties.hasNext())
112         {
113             ManagedProperty property = (ManagedProperty) managedProperties.next();
114             Object JavaDoc value = null;
115
116             switch (property.getType())
117             {
118                 case ManagedProperty.TYPE_LIST:
119                     value = new ArrayList();
120                     initializeList(facesContext, property.getListEntries(), (List) value);
121                     break;
122                 case ManagedProperty.TYPE_MAP:
123                     value = new HashMap();
124                     initializeMap(facesContext, property.getMapEntries(), (Map) value);
125                     break;
126                 case ManagedProperty.TYPE_NULL:
127                     value = null;
128                     break;
129                 case ManagedProperty.TYPE_VALUE:
130                     value = property.getRuntimeValue(facesContext);
131                     break;
132             }
133             PropertyResolver propertyResolver =
134                 facesContext.getApplication().getPropertyResolver();
135             Class JavaDoc propertyClass = null;
136
137             if (property.getPropertyClass() == null)
138             {
139                 propertyClass = propertyResolver
140                     .getType(bean, property.getPropertyName());
141             }
142             else
143             {
144                 propertyClass = ClassUtils
145                     .simpleJavaTypeToClass(property.getPropertyClass());
146             }
147             if(null == propertyClass) {
148               throw new IllegalArgumentException JavaDoc("unable to find the type of property " + property.getPropertyName());
149             }
150             Object JavaDoc coercedValue = ClassUtils.convertToType(value, propertyClass);
151             propertyResolver.setValue(
152                 bean, property.getPropertyName(), coercedValue);
153         }
154     }
155
156
157     private void initializeMap(FacesContext facesContext, MapEntries mapEntries, Map map)
158     {
159         Application application = facesContext.getApplication();
160         Class JavaDoc keyClass = (mapEntries.getKeyClass() == null)
161             ? String JavaDoc.class : ClassUtils.simpleJavaTypeToClass(mapEntries.getKeyClass());
162         Class JavaDoc valueClass = (mapEntries.getValueClass() == null)
163             ? String JavaDoc.class : ClassUtils.simpleJavaTypeToClass(mapEntries.getValueClass());
164         ValueBinding valueBinding;
165
166         for (Iterator iterator = mapEntries.getMapEntries(); iterator.hasNext();)
167         {
168             MapEntry entry = (MapEntry) iterator.next();
169             Object JavaDoc key = entry.getKey();
170
171             if (UIComponentTag.isValueReference((String JavaDoc) key))
172             {
173                 valueBinding = application.createValueBinding((String JavaDoc) key);
174                 key = valueBinding.getValue(facesContext);
175             }
176
177             if (entry.isNullValue())
178             {
179                 map.put(ClassUtils.convertToType(key, keyClass), null);
180             }
181             else
182             {
183                 Object JavaDoc value = entry.getValue();
184                 if (UIComponentTag.isValueReference((String JavaDoc) value))
185                 {
186                     valueBinding = application.createValueBinding((String JavaDoc) value);
187                     value = valueBinding.getValue(facesContext);
188                 }
189                 map.put(ClassUtils.convertToType(key, keyClass), ClassUtils.convertToType(value, valueClass));
190             }
191         }
192     }
193
194
195     private void initializeList(FacesContext facesContext, ListEntries listEntries, List list)
196     {
197         Application application = facesContext.getApplication();
198         Class JavaDoc valueClass = listEntries.getValueClass() == null ? String JavaDoc.class : ClassUtils.simpleJavaTypeToClass(listEntries.getValueClass());
199         ValueBinding valueBinding;
200
201         for (Iterator iterator = listEntries.getListEntries(); iterator.hasNext();)
202         {
203             ListEntry entry = (ListEntry) iterator.next();
204             if (entry.isNullValue())
205             {
206                 list.add(null);
207             }
208             else
209             {
210                 Object JavaDoc value = entry.getValue();
211                 if (UIComponentTag.isValueReference((String JavaDoc) value))
212                 {
213                     valueBinding = application.createValueBinding((String JavaDoc) value);
214                     value = valueBinding.getValue(facesContext);
215                 }
216                 list.add(ClassUtils.convertToType(value, valueClass));
217             }
218         }
219     }
220 }
221
Popular Tags