1 7 package com.inversoft.verge.mvc.controller.actionflow.config; 8 9 10 import java.util.Iterator ; 11 import java.util.Map ; 12 13 import org.apache.log4j.Logger; 14 import org.jdom.Document; 15 import org.jdom.Element; 16 17 import com.inversoft.config.ConfigBuilder; 18 import com.inversoft.config.ConfigRegistry; 19 import com.inversoft.config.ConfigurationException; 20 import com.inversoft.config.component.ComponentConfigBuilderRegistry; 21 import com.inversoft.error.ErrorList; 22 import com.inversoft.util.StringTools; 23 import com.inversoft.verge.config.VergeConfigConstants; 24 import com.inversoft.verge.mvc.config.BaseFormConfig; 25 import com.inversoft.verge.mvc.config.BaseFormConfigBuilder; 26 import com.inversoft.verge.repository.config.RepositoryConfigRegistry; 27 28 29 50 public class ActionFlowConfigBuilder implements ConfigBuilder { 51 52 55 private static final Logger logger = Logger.getLogger(ActionFlowConfigBuilder.class); 56 57 58 61 public static final String BUILDERS_BUNDLE = 62 "com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigBuilders"; 63 64 67 public static final String NAMESPACE_PREFIX = "namespace_"; 68 69 72 public static final String NODE_PREFIX = "node_"; 73 74 78 private ComponentConfigBuilderRegistry builderRegistry; 79 80 81 87 public ActionFlowConfigBuilder() throws ConfigurationException { 88 builderRegistry = new ComponentConfigBuilderRegistry(BUILDERS_BUNDLE); 89 } 90 91 92 116 public synchronized void build(Document document, ConfigRegistry registry) 117 throws ConfigurationException { 118 119 ActionFlowConfigRegistry actionRegistry = (ActionFlowConfigRegistry) registry; 120 ErrorList errors = new ErrorList(); 121 Namespace namespace = buildNamespace(document, errors); 122 123 if (namespace != null) { 124 if (actionRegistry.lookup(namespace.getName()) != null) { 125 logger.error("There are two or more namespaces named: " + 126 namespace.getName()); 127 errors.addError("There are two or more namespaces named: " + 128 namespace.getName()); 129 } else if (logger.isDebugEnabled()) { 130 logger.debug("Stored namespace named '" + namespace.getName() + "'"); 131 } 132 133 actionRegistry.register(namespace); 134 } 135 136 if (!errors.isEmpty()) { 137 throw new ConfigurationException(errors); 138 } 139 } 140 141 149 public void rebuild(Document document, ConfigRegistry registry) 150 throws ConfigurationException { 151 build(document, registry); 152 } 153 154 158 public void validate(ConfigRegistry registry, Map registries) 159 throws ConfigurationException { 160 161 ErrorList errors = new ErrorList(); 163 ActionFlowConfigRegistry actionRegistry = (ActionFlowConfigRegistry) registry; 164 RepositoryConfigRegistry repository = 165 (RepositoryConfigRegistry) registries.get(VergeConfigConstants.REPOSITORY_KEY); 166 assert (repository != null) : "RepositoryConfigRegistry not found"; 167 168 Iterator iter = actionRegistry.iterator(); 169 NamespaceBuilder namespaceBuilder; 170 Namespace namespace; 171 172 while (iter.hasNext()) { 173 namespace = (Namespace) iter.next(); 174 175 namespaceBuilder = 177 (NamespaceBuilder) builderRegistry.lookup(NAMESPACE_PREFIX + 178 namespace.getType()); 179 try { 180 namespaceBuilder.validate(namespace, actionRegistry, registries, 181 builderRegistry); 182 } catch (ConfigurationException ce) { 183 errors.addErrorList(ce.getErrors()); 184 } 185 } 186 187 if (!errors.isEmpty()) { 189 throw new ConfigurationException(errors); 190 } 191 } 192 193 200 public void commit(ConfigRegistry registry, Map otherRegistries) { 201 ActionFlowConfigRegistry.setInstance((ActionFlowConfigRegistry) registry); 202 } 203 204 211 protected Namespace buildNamespace(Document namespace, ErrorList errors) { 212 213 Element root = namespace.getRootElement(); 215 String type = root.getAttributeValue(Constants.TYPE_ATTRIBUTE); 216 if (StringTools.isEmpty(type)) { 217 type = DefaultTypes.NAMESPACE; 218 } 219 220 NamespaceBuilder nsFactory = (NamespaceBuilder) builderRegistry.lookup( 221 NAMESPACE_PREFIX + type); 222 if (nsFactory == null) { 223 errors.addError("Can not locate factory to build namespace of type " + type); 224 return null; 225 } 226 227 Namespace ns = null; 228 try { 229 ns = nsFactory.build(root); 230 } catch (ConfigurationException ce) { 231 errors.addErrorList(ce.getErrors()); 232 return ns; 233 } 234 235 Iterator iter = root.getChildren().iterator(); 237 Element element; 238 String name; 239 Node node; 240 BaseFormConfig form; 241 boolean failed = false; 242 243 while (iter.hasNext()) { 244 element = (Element) iter.next(); 245 name = element.getName(); 246 247 if (name.equals(Constants.NODE_ELEMENT)) { 248 node = buildNode(ns, element, errors); 249 if (node == null) { 250 failed = true; 251 continue; 252 } 253 254 if (logger.isDebugEnabled()) { 255 logger.debug("Adding node named '" + node.getName() + 256 "' to namespace named '" + ns.getName() + "'"); 257 } 258 259 ns.addNode(node); 260 } else if (name.equals(Constants.FORM_ELEMENT)) { 261 form = buildForm(ns, element, errors); 262 if (form == null) { 263 failed = true; 264 continue; 265 } 266 267 if (logger.isDebugEnabled()) { 268 logger.debug("Adding form named '" + form.getName() + 269 "' to namespace named '" + ns.getName() + "'"); 270 } 271 272 ns.addForm(form); 273 } else { 274 errors.addError("Element type: " + name + " not supported"); 275 } 276 } 277 278 if (failed) { 279 ns = null; 280 } 281 282 return ns; 283 } 284 285 293 protected Node buildNode(Namespace namespace, Element element, ErrorList errors) { 294 295 String type = element.getAttributeValue(Constants.TYPE_ATTRIBUTE); 296 if (StringTools.isEmpty(type)) { 297 errors.addError("A node is missing the type attribute"); 298 return null; 299 } 300 301 NodeBuilder factory = (NodeBuilder) builderRegistry.lookup( 302 NODE_PREFIX + type); 303 if (factory == null) { 304 errors.addError("Unable to locate a factory for Node type " + type); 305 return null; 306 } 307 308 Node node = null; 309 try { 310 node = factory.build(namespace, element); 311 } catch (ConfigurationException ce) { 312 errors.addErrorList(ce.getErrors()); 313 } 314 315 return node; 316 } 317 318 326 protected BaseFormConfig buildForm(Namespace namespace, Element element, 327 ErrorList errors) { 328 329 BaseFormConfigBuilder builder = (BaseFormConfigBuilder) 330 builderRegistry.lookup("form"); 331 if (builder == null) { 332 errors.addError("Unable to locate a builder for forms"); 333 return null; 334 } 335 336 BaseFormConfig formConfig = null; 337 try { 338 formConfig = (BaseFormConfig) builder.build(element); 339 } catch (ConfigurationException ce) { 340 errors.addErrorList(ce.getErrors()); 341 } 342 343 return formConfig; 344 } 345 } 346 | Popular Tags |