1 7 package com.inversoft.verge.config; 8 9 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 13 14 import org.apache.log4j.Logger; 15 import org.jdom.Document; 16 import org.jdom.Element; 17 18 import com.inversoft.config.ConfigBuilder; 19 import com.inversoft.config.ConfigRegistry; 20 import com.inversoft.config.ConfigurationException; 21 import com.inversoft.verge.mvc.MVCRegistry; 22 import com.inversoft.verge.mvc.MVCURLTools; 23 import com.inversoft.verge.mvc.controller.ControllerHandler; 24 import com.inversoft.verge.mvc.controller.ControllerParser; 25 import com.inversoft.verge.mvc.model.ModelHandler; 26 import com.inversoft.verge.mvc.model.ModelParser; 27 import com.inversoft.verge.mvc.model.ModelResolver; 28 import com.inversoft.verge.mvc.validator.ValidatorHandler; 29 import com.inversoft.verge.mvc.validator.ValidatorParser; 30 import com.inversoft.util.StringTools; 31 32 33 48 public class VergeBaseConfigBuilder implements ConfigBuilder { 49 50 53 public VergeBaseConfigBuilder() { 54 } 55 56 57 75 public synchronized void build(Document document, ConfigRegistry registry) { 76 Element root = document.getRootElement(); 77 buildMVC(root); 78 } 79 80 85 void buildMVC(Element root) { 86 Logger logger = Logger.getLogger("com.inversoft"); 87 Element mvc = root.getChild(Constants.MVC_ELEMENT); 88 if (mvc != null) { 89 buildMVCConfiguration(mvc, ControllerParser.class, 90 Constants.CONTROLLER_PARSER_ELEMENT, "ControllerParser", logger); 91 buildMVCConfiguration(mvc, ModelParser.class, 92 Constants.MODEL_PARSER_ELEMENT, "ModelParser", logger); 93 buildMVCConfiguration(mvc, ValidatorParser.class, 94 Constants.VALIDATOR_PARSER_ELEMENT, "ValidatorParser", logger); 95 buildMVCConfiguration(mvc, ValidatorHandler.class, 96 Constants.VALIDATOR_HANDLER_ELEMENT, "ValidatorHandler", logger); 97 buildMVCConfiguration(mvc, ControllerHandler.class, 98 Constants.CONTROLLER_HANDLER_ELEMENT, "ControllerHandler", logger); 99 buildMVCConfiguration(mvc, ModelResolver.class, 100 Constants.MODEL_RESOLVER_ELEMENT, "ModelResolver", logger); 101 buildMVCConfiguration(mvc, ModelHandler.class, 102 Constants.MODEL_HANDLER_ELEMENT, "ModelHandler", logger); 103 buildMVCURLEnding(mvc, logger); 104 } else { 105 logger.info("No MVC configuration"); 106 } 107 } 108 109 117 private void buildMVCConfiguration(Element root, Class type, String elemName, 118 String errorName, Logger logger) { 119 List elems = root.getChildren(elemName); 120 Iterator iter = elems.iterator(); 121 Element elem; 122 String name; 123 String className; 124 Class klass; 125 while (iter.hasNext()) { 126 elem = (Element) iter.next(); 127 name = elem.getAttributeValue(Constants.NAME_ATTRIBUTE); 128 className = elem.getTextTrim(); 129 try { 130 klass = Class.forName(className); 131 } catch (ClassNotFoundException cne) { 132 logger.error("Invalid " + errorName + " class: " + className); 133 continue; 134 } 135 136 if (!type.isAssignableFrom(klass)) { 137 logger.error("Invalid " + errorName + " class: " + className); 138 continue; 139 } 140 141 Object obj; 142 try { 143 obj = klass.newInstance(); 144 } catch (InstantiationException ie) { 145 logger.error("Error creating " + errorName + " :" + ie.toString()); 146 continue; 147 } catch (IllegalAccessException iae) { 148 logger.error("Error creating " + errorName + " :" + iae.toString()); 149 continue; 150 } 151 152 if (type == ControllerParser.class) { 153 MVCRegistry.register(name, (ControllerParser) obj); 154 } else if (type == ModelParser.class) { 155 MVCRegistry.register(name, (ModelParser) obj); 156 } else if (type == ValidatorParser.class) { 157 MVCRegistry.register(name, (ValidatorParser) obj); 158 } else if (type == ValidatorHandler.class) { 159 MVCRegistry.register(name, (ValidatorHandler) obj); 160 } else if (type == ControllerHandler.class) { 161 MVCRegistry.register(name, (ControllerHandler) obj); 162 } else if (type == ModelResolver.class) { 163 MVCRegistry.register(name, (ModelResolver) obj); 164 } else if (type == ModelHandler.class) { 165 MVCRegistry.register(name, (ModelHandler) obj); 166 } else { 167 assert (false) : "FATAL: Invalid type"; 168 } 169 } 170 } 171 172 175 private void buildMVCURLEnding(Element mvc, Logger logger) { 176 Element urlElem = mvc.getChild(Constants.URL_BEGINNING_ELEMENT); 177 if (urlElem != null) { 178 String ending = urlElem.getTextTrim(); 179 if (!StringTools.isEmpty(ending)) { 180 MVCURLTools.setURLEnding(ending); 181 } else { 182 logger.error("URL ending value is empty or null"); 183 } 184 } 185 } 186 187 195 public void rebuild(Document document, ConfigRegistry registry) 196 throws ConfigurationException { 197 build(document, registry); 198 } 199 200 203 public void validate(ConfigRegistry registry, Map otherRegistries) { 204 } 206 207 210 public void commit(ConfigRegistry registry, Map otherRegistries) { 211 } 213 } | Popular Tags |