1 5 package org.infohazard.maverick.flow; 6 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 11 import javax.servlet.ServletConfig ; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.infohazard.maverick.util.XML; 16 import org.jdom.Document; 17 import org.jdom.Element; 18 19 26 public class Loader 27 { 28 29 protected final static String TAG_MODULES = "modules"; 30 31 protected final static String TAG_VIEWFACTORY = "view-factory"; 32 33 protected final static String TAG_CONTROLLERFACTORY = "controller-factory"; 34 35 protected final static String TAG_TRANSFORMFACTORY = "transform-factory"; 36 37 protected final static String TAG_SHUNTFACTORY = "shunt-factory"; 38 39 protected final static String ATTR_PROVIDER = "provider"; 40 41 42 protected final static String TAG_COMMANDS = "commands"; 43 44 protected final static String TAG_COMMAND = "command"; 45 46 protected final static String ATTR_COMMAND_NAME = "name"; 47 48 49 protected final static String TAG_VIEWS = "views"; 50 51 52 protected final static String PARAM_DEFAULT_VIEW_TYPE = "default-view-type"; 53 54 protected final static String PARAM_DEFAULT_TRANSFORM_TYPE = "default-transform-type"; 55 56 57 private static Log log = LogFactory.getLog(Loader.class); 58 59 60 protected Map commands = new HashMap (); 61 62 63 protected ServletConfig servletCfg; 64 65 66 protected MasterFactory masterFact; 67 68 69 protected ViewRegistry viewReg; 70 71 72 protected CommandFactory commandFact; 73 74 80 public Loader(Document doc, ServletConfig dispatcherConfig) throws ConfigException 81 { 82 this.servletCfg = dispatcherConfig; 83 this.masterFact = new MasterFactory(dispatcherConfig); 84 85 this.viewReg = new ViewRegistrySimple(this.masterFact); 88 this.commandFact = new CommandFactory(this.viewReg); 89 90 this.setupCoreModules(); 91 92 this.loadDocument(doc); 93 } 94 95 99 public Map getCommands() 100 { 101 return this.commands; 102 } 103 104 108 protected void setupCoreModules() throws ConfigException 109 { 110 TransformFactory docTrans = new org.infohazard.maverick.transform.DocumentTransformFactory(); 112 docTrans.init(null, this.servletCfg); 113 this.masterFact.defineTransformFactory("document", docTrans); 114 115 TransformFactory xsltTrans = new org.infohazard.maverick.transform.XSLTransformFactory(); 116 xsltTrans.init(null, this.servletCfg); 117 this.masterFact.defineTransformFactory("xslt", xsltTrans); 118 119 ViewFactory document = new org.infohazard.maverick.view.DocumentViewFactory(); 121 document.init(null, this.servletCfg); 122 this.masterFact.defineViewFactory("document", document); 123 124 ViewFactory redirect = new org.infohazard.maverick.view.RedirectViewFactory(); 125 redirect.init(null, this.servletCfg); 126 this.masterFact.defineViewFactory("redirect", redirect); 127 128 ViewFactory trivial = new org.infohazard.maverick.view.TrivialViewFactory(); 129 trivial.init(null, this.servletCfg); 130 this.masterFact.defineViewFactory("trivial", trivial); 131 132 ViewFactory nullViewFact = new org.infohazard.maverick.view.NullViewFactory(); 133 nullViewFact.init(null, this.servletCfg); 134 this.masterFact.defineViewFactory("null", nullViewFact); 135 } 136 137 142 protected void loadDocument(Document doc) throws ConfigException 143 { 144 Element root = doc.getRootElement(); 145 146 String defaultTransformType = XML.getValue(root, PARAM_DEFAULT_TRANSFORM_TYPE); 148 if (defaultTransformType != null) 149 this.masterFact.setDefaultTransformType(defaultTransformType); 150 151 String defaultViewType = XML.getValue(root, PARAM_DEFAULT_VIEW_TYPE); 153 if (defaultViewType != null) 154 this.masterFact.setDefaultViewType(defaultViewType); 155 156 Iterator allModulesIt = root.getChildren(TAG_MODULES).iterator(); 159 while (allModulesIt.hasNext()) 160 { 161 Element modules = (Element)allModulesIt.next(); 162 this.loadModules(modules); 163 } 164 165 Iterator allViewsIt = root.getChildren(TAG_VIEWS).iterator(); 167 while (allViewsIt.hasNext()) 168 { 169 Element viewsNode = (Element)allViewsIt.next(); 170 this.viewReg.defineGlobalViews(viewsNode); 171 } 172 173 Iterator allCommandsIt = root.getChildren(TAG_COMMANDS).iterator(); 175 while (allCommandsIt.hasNext()) 176 { 177 Element commandsNode = (Element)allCommandsIt.next(); 178 179 Iterator commandIt = commandsNode.getChildren(TAG_COMMAND).iterator(); 180 while (commandIt.hasNext()) 181 { 182 Element commandNode = (Element)commandIt.next(); 183 184 String commandName = commandNode.getAttributeValue(ATTR_COMMAND_NAME); 185 186 log.info("Creating command: " + commandName); 187 188 Command cmd = this.commandFact.createCommand(commandNode); 189 190 this.commands.put(commandName, cmd); 191 } 192 } 193 } 194 195 200 protected void loadModules(Element modulesNode) throws ConfigException 201 { 202 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 203 if (classLoader == null) 204 { 205 classLoader = DefaultControllerFactory.class.getClassLoader(); 206 } 207 208 masterFact.defineTransformFactories(modulesNode.getChildren(TAG_TRANSFORMFACTORY)); 210 masterFact.defineViewFactories(modulesNode.getChildren(TAG_VIEWFACTORY)); 211 212 Element shuntFactoryNode = modulesNode.getChild(TAG_SHUNTFACTORY); 213 if (shuntFactoryNode != null) 214 { 215 String providerName = shuntFactoryNode.getAttributeValue(ATTR_PROVIDER); 216 217 if (providerName == null) 218 throw new ConfigException("Not a valid shunt factory node: " 219 + XML.toString(shuntFactoryNode)); 220 221 Class providerClass; 222 ShuntFactory instance; 223 try 224 { 225 providerClass = classLoader.loadClass(providerName); 226 instance = (ShuntFactory)providerClass.newInstance(); 227 } 228 catch (Exception ex) 229 { 230 throw new ConfigException("Unable to define shunt factory " 231 + providerName, ex); 232 } 233 234 log.info("Using shunt factory " + providerName); 235 instance.init(shuntFactoryNode, this.servletCfg); 237 238 this.viewReg = new ViewRegistryShunted(this.masterFact, instance); 240 this.commandFact.setViewRegistry(this.viewReg); 241 } 242 Element controllerFactoryNode = modulesNode.getChild(TAG_CONTROLLERFACTORY); 243 if (controllerFactoryNode != null) 244 { 245 String providerName = controllerFactoryNode.getAttributeValue(ATTR_PROVIDER); 246 247 if (providerName == null) 248 throw new ConfigException("Not a valid controller factory node: " 249 + XML.toString(controllerFactoryNode)); 250 251 Class providerClass; 252 ControllerFactory instance; 253 try 254 { 255 providerClass = classLoader.loadClass(providerName); 256 instance = (ControllerFactory)providerClass.newInstance(); 257 } 258 catch (Exception ex) 259 { 260 throw new ConfigException("Unable to define controller factory " 261 + providerName, ex); 262 } 263 264 log.info("Using controller factory " + providerName); 265 266 instance.init(controllerFactoryNode, this.servletCfg); 268 269 this.commandFact.setControllerFactory(instance); 270 } 271 } 272 273 } 274 275 | Popular Tags |