1 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 53 public class ManagedBeanBuilder 54 { 55 public Object buildManagedBean(FacesContext facesContext, ManagedBean beanConfiguration) throws FacesException 56 { 57 Object 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 e) { 66 throw new IllegalArgumentException ( 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 ("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 ("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 break; 99 100 default: 101 throw new IllegalStateException ("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 bean) 110 { 111 while (managedProperties.hasNext()) 112 { 113 ManagedProperty property = (ManagedProperty) managedProperties.next(); 114 Object 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 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 ("unable to find the type of property " + property.getPropertyName()); 149 } 150 Object 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 keyClass = (mapEntries.getKeyClass() == null) 161 ? String .class : ClassUtils.simpleJavaTypeToClass(mapEntries.getKeyClass()); 162 Class valueClass = (mapEntries.getValueClass() == null) 163 ? String .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 key = entry.getKey(); 170 171 if (UIComponentTag.isValueReference((String ) key)) 172 { 173 valueBinding = application.createValueBinding((String ) 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 value = entry.getValue(); 184 if (UIComponentTag.isValueReference((String ) value)) 185 { 186 valueBinding = application.createValueBinding((String ) 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 valueClass = listEntries.getValueClass() == null ? String .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 value = entry.getValue(); 211 if (UIComponentTag.isValueReference((String ) value)) 212 { 213 valueBinding = application.createValueBinding((String ) value); 214 value = valueBinding.getValue(facesContext); 215 } 216 list.add(ClassUtils.convertToType(value, valueClass)); 217 } 218 } 219 } 220 } 221 | Popular Tags |