1 16 package org.springframework.webflow.engine.builder; 17 18 import org.springframework.beans.BeansException; 19 import org.springframework.beans.factory.BeanFactory; 20 import org.springframework.binding.convert.ConversionService; 21 import org.springframework.binding.convert.support.DefaultConversionService; 22 import org.springframework.binding.convert.support.GenericConversionService; 23 import org.springframework.binding.convert.support.TextToExpression; 24 import org.springframework.binding.expression.ExpressionParser; 25 import org.springframework.binding.method.TextToMethodSignature; 26 import org.springframework.core.io.DefaultResourceLoader; 27 import org.springframework.core.io.ResourceLoader; 28 import org.springframework.util.Assert; 29 import org.springframework.webflow.action.BeanInvokingActionFactory; 30 import org.springframework.webflow.core.DefaultExpressionParserFactory; 31 import org.springframework.webflow.engine.Flow; 32 import org.springframework.webflow.engine.FlowAttributeMapper; 33 import org.springframework.webflow.engine.FlowExecutionExceptionHandler; 34 import org.springframework.webflow.engine.State; 35 import org.springframework.webflow.engine.TargetStateResolver; 36 import org.springframework.webflow.engine.TransitionCriteria; 37 import org.springframework.webflow.engine.ViewSelector; 38 import org.springframework.webflow.execution.Action; 39 40 49 public class BaseFlowServiceLocator implements FlowServiceLocator { 50 51 55 private FlowArtifactFactory flowArtifactFactory = new FlowArtifactFactory(); 56 57 61 private BeanInvokingActionFactory beanInvokingActionFactory = new BeanInvokingActionFactory(); 62 63 67 private ExpressionParser expressionParser = DefaultExpressionParserFactory.getExpressionParser(); 68 69 72 private ConversionService conversionService = createConversionService(null); 73 74 77 private ResourceLoader resourceLoader = new DefaultResourceLoader(); 78 79 83 public void setFlowArtifactFactory(FlowArtifactFactory flowArtifactFactory) { 84 Assert.notNull(flowArtifactFactory, "The flow artifact factory is required"); 85 this.flowArtifactFactory = flowArtifactFactory; 86 } 87 88 92 public void setBeanInvokingActionFactory(BeanInvokingActionFactory beanInvokingActionFactory) { 93 Assert.notNull(beanInvokingActionFactory, "The bean invoking action factory is required"); 94 this.beanInvokingActionFactory = beanInvokingActionFactory; 95 } 96 97 101 public void setExpressionParser(ExpressionParser expressionParser) { 102 Assert.notNull(expressionParser, "The expression parser is required"); 103 this.expressionParser = expressionParser; 104 } 105 106 110 public void setConversionService(ConversionService conversionService) { 111 Assert.notNull(conversionService, "The conversion service is required"); 112 this.conversionService = createConversionService(conversionService); 113 } 114 115 119 public void setResourceLoader(ResourceLoader resourceLoader) { 120 this.resourceLoader = resourceLoader; 121 } 122 123 public Flow getSubflow(String id) throws FlowArtifactLookupException { 124 throw new FlowArtifactLookupException(id, Flow.class, 125 "Subflow lookup is not supported by this service locator"); 126 } 127 128 public Action getAction(String id) throws FlowArtifactLookupException { 129 return (Action)getBean(id, Action.class); 130 } 131 132 public FlowAttributeMapper getAttributeMapper(String id) throws FlowArtifactLookupException { 133 return (FlowAttributeMapper)getBean(id, FlowAttributeMapper.class); 134 } 135 136 public TransitionCriteria getTransitionCriteria(String id) throws FlowArtifactLookupException { 137 return (TransitionCriteria)getBean(id, TransitionCriteria.class); 138 } 139 140 public TargetStateResolver getTargetStateResolver(String id) throws FlowArtifactLookupException { 141 return (TargetStateResolver)getBean(id, TargetStateResolver.class); 142 } 143 144 public ViewSelector getViewSelector(String id) throws FlowArtifactLookupException { 145 return (ViewSelector)getBean(id, ViewSelector.class); 146 } 147 148 public FlowExecutionExceptionHandler getExceptionHandler(String id) throws FlowArtifactLookupException { 149 return (FlowExecutionExceptionHandler)getBean(id, FlowExecutionExceptionHandler.class); 150 } 151 152 public FlowArtifactFactory getFlowArtifactFactory() { 153 return flowArtifactFactory; 154 } 155 156 public BeanInvokingActionFactory getBeanInvokingActionFactory() { 157 return beanInvokingActionFactory; 158 } 159 160 public BeanFactory getBeanFactory() throws UnsupportedOperationException { 161 throw new UnsupportedOperationException ("Bean factory lookup is not supported by this service locator"); 162 } 163 164 public ResourceLoader getResourceLoader() { 165 return resourceLoader; 166 } 167 168 public ExpressionParser getExpressionParser() { 169 return expressionParser; 170 } 171 172 public ConversionService getConversionService() { 173 return conversionService; 174 } 175 176 178 184 protected boolean containsBean(String id) { 185 return getBeanFactory().containsBean(id); 186 } 187 188 196 protected Object getBean(String id, Class artifactType) throws FlowArtifactLookupException { 197 try { 198 return getBeanFactory().getBean(id, artifactType); 199 } 200 catch (BeansException e) { 201 throw new FlowArtifactLookupException(id, artifactType, e); 202 } 203 } 204 205 212 protected Class getBeanType(String id, Class artifactType) throws FlowArtifactLookupException { 213 try { 214 return getBeanFactory().getType(id); 215 } 216 catch (BeansException e) { 217 throw new FlowArtifactLookupException(id, artifactType, e); 218 } 219 } 220 221 227 protected ConversionService createConversionService(ConversionService parent) { 228 if (parent != null) { 229 GenericConversionService conversionService = new GenericConversionService(); 230 addWebFlowConverters(conversionService); 231 conversionService.setParent(parent); 232 return conversionService; 233 } 234 else { 235 DefaultConversionService conversionService = new DefaultConversionService(); 236 addWebFlowConverters(conversionService); 237 return conversionService; 238 } 239 } 240 241 244 protected void addWebFlowConverters(GenericConversionService conversionService) { 245 conversionService.addConverter(new TextToTransitionCriteria(this)); 246 conversionService.addConverter(new TextToTargetStateResolver(this)); 247 conversionService.addConverter(new TextToViewSelector(this)); 248 conversionService.addConverter(new TextToExpression(getExpressionParser())); 249 conversionService.addConverter(new TextToMethodSignature(conversionService)); 250 } 251 } | Popular Tags |