1 16 package org.springframework.webflow.executor.jsf; 17 18 import javax.faces.context.FacesContext; 19 import javax.faces.el.EvaluationException; 20 import javax.faces.el.PropertyNotFoundException; 21 import javax.faces.el.PropertyResolver; 22 import javax.faces.el.ReferenceSyntaxException; 23 24 import org.springframework.util.Assert; 25 import org.springframework.web.context.WebApplicationContext; 26 import org.springframework.web.jsf.FacesContextUtils; 27 import org.springframework.webflow.execution.FlowExecution; 28 29 39 public class FlowPropertyResolver extends PropertyResolver { 40 41 44 private final PropertyResolver resolverDelegate; 45 46 55 public FlowPropertyResolver(PropertyResolver resolverDelegate) { 56 this.resolverDelegate = resolverDelegate; 57 } 58 59 public Class getType(Object base, int index) throws EvaluationException, PropertyNotFoundException { 60 if (!(base instanceof FlowExecution)) { 61 return resolverDelegate.getType(base, index); 62 } 63 else { 64 return null; 67 } 68 } 69 70 public Class getType(Object base, Object property) throws EvaluationException, PropertyNotFoundException { 71 if (!(base instanceof FlowExecution)) { 72 return resolverDelegate.getType(base, property); 73 } 74 if (property == null) { 75 throw new PropertyNotFoundException("Unable to get value from Flow, as property (key) is null"); 76 } 77 if (!(property instanceof String )) { 78 throw new PropertyNotFoundException("Unable to get value from Flow map, as key is non-String"); 79 } 80 FlowExecution execution = (FlowExecution)base; 81 Object value = execution.getActiveSession().getScope().get((String )property); 83 return (value == null) ? null : value.getClass(); 88 } 89 90 public Object getValue(Object base, int index) throws EvaluationException, PropertyNotFoundException { 91 if (!(base instanceof FlowExecution)) { 92 return resolverDelegate.getValue(base, index); 93 } 94 else { 95 throw new ReferenceSyntaxException("Cannot apply an index value to Flow map"); 96 } 97 } 98 99 public Object getValue(Object base, Object property) throws EvaluationException, PropertyNotFoundException { 100 if (!(base instanceof FlowExecution)) { 101 return resolverDelegate.getValue(base, property); 102 } 103 if (!(property instanceof String )) { 104 throw new PropertyNotFoundException("Unable to get value from Flow map, as key is non-String"); 105 } 106 FlowExecution execution = (FlowExecution)base; 107 String attributeName = (String )property; 108 Object value = execution.getActiveSession().getScope().get(attributeName); 109 if (value == null) { 110 FacesContext context = FacesContext.getCurrentInstance(); 111 Assert.notNull(context, "FacesContext must exist during property resolution stage"); 112 WebApplicationContext wac = getWebApplicationContext(context); 113 if (wac.containsBean(attributeName)) { 114 value = wac.getBean(attributeName); 118 execution.getActiveSession().getScope().put(attributeName, value); 119 } 120 } 121 return value; 122 } 123 124 public boolean isReadOnly(Object base, int index) throws EvaluationException, PropertyNotFoundException { 125 if (!(base instanceof FlowExecution)) { 126 return resolverDelegate.isReadOnly(base, index); 127 } 128 return false; 129 } 130 131 public boolean isReadOnly(Object base, Object property) throws EvaluationException, PropertyNotFoundException { 132 if (!(base instanceof FlowExecution)) { 133 return resolverDelegate.isReadOnly(base, property); 134 } 135 return false; 136 } 137 138 public void setValue(Object base, int index, Object value) throws EvaluationException, PropertyNotFoundException { 139 if (!(base instanceof FlowExecution)) { 140 resolverDelegate.setValue(base, index, value); 141 } 142 throw new ReferenceSyntaxException("Can not apply an index value to Flow map"); 143 } 144 145 public void setValue(Object base, Object property, Object value) throws EvaluationException, 146 PropertyNotFoundException { 147 if (!(base instanceof FlowExecution)) { 148 resolverDelegate.setValue(base, property, value); 149 return; 150 } 151 if (property == null || !(property instanceof String ) 152 || (property instanceof String && ((String )property).length() == 0)) { 153 throw new PropertyNotFoundException( 154 "Attempt to set Flow attribute with null name, empty name, or non-String name"); 155 } 156 FlowExecution execution = (FlowExecution)base; 157 execution.getActiveSession().getScope().put((String )property, value); 158 } 159 160 169 protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) { 170 return FacesContextUtils.getRequiredWebApplicationContext(facesContext); 171 } 172 } | Popular Tags |