1 5 6 package org.infohazard.maverick.flow; 7 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 13 import javax.servlet.ServletConfig ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.infohazard.maverick.util.XML; 18 import org.jdom.Element; 19 20 24 class MasterFactory 25 { 26 27 private static Log log = LogFactory.getLog(MasterFactory.class); 28 29 30 protected static final String ATTR_FACTORY_TYPE_NAME = "type"; 31 32 protected static final String ATTR_FACTORY_PROVIDER = "provider"; 33 34 protected static final String ATTR_TYPE_NAME = "type"; 35 36 protected static final String ATTR_TRANSFORM_TYPE_NAME = "transform-type"; 37 38 39 40 public static final String TAG_TRANSFORM = "transform"; 41 42 45 protected Map viewFactories = new HashMap (); 46 47 50 protected Map transformFactories = new HashMap (); 51 52 55 protected String defaultViewType; 56 57 60 protected String defaultTransformType; 61 62 66 protected ServletConfig servletCfg; 67 68 72 public MasterFactory(ServletConfig servletCfg) 73 { 74 this.servletCfg = servletCfg; 75 } 76 77 79 83 public void setDefaultViewType(String type) 84 { 85 this.defaultViewType = type; 86 } 87 88 93 protected View createView(Element viewNode) throws ConfigException 94 { 95 View v = this.createPlainView(viewNode); 96 97 final List transformsList = viewNode.getChildren(TAG_TRANSFORM); 98 99 final String transformPath = viewNode.getAttributeValue("path"); 101 if (transformPath != null) 102 { 103 String typeName = viewNode.getAttributeValue(ATTR_TRANSFORM_TYPE_NAME); 104 if (typeName == null) 105 { 106 typeName = viewNode.getAttributeValue(ATTR_TYPE_NAME); 109 if (typeName == null) 110 typeName = this.defaultViewType; 111 } 112 113 final TransformFactory tf = (TransformFactory)transformFactories.get(typeName); 114 if (tf != null) 115 v = new ViewWithTransforms(v, new Transform[] {tf.createTransform(viewNode)}); 116 } 117 118 if (!transformsList.isEmpty()) 119 { 120 final Transform[] t = this.createTransforms(transformsList); 121 v = new ViewWithTransforms(v, t); 122 } 123 124 final Map params = XML.getParams(viewNode); 125 if (params != null) 126 v = new ViewWithParams(v, params); 127 128 return v; 129 } 130 131 137 protected View createPlainView(Element viewNode) throws ConfigException 138 { 139 String typeName = viewNode.getAttributeValue(ATTR_TYPE_NAME); 140 if (typeName == null) 141 typeName = this.defaultViewType; 142 143 log.debug("Creating view of type " + typeName); 144 145 ViewFactory fact = (ViewFactory)this.viewFactories.get(typeName); 146 147 if (fact == null) 148 throw new ConfigException("No view factory can be found for " + XML.toString(viewNode)); 149 150 return fact.createView(viewNode); 151 } 152 153 159 public void defineViewFactory(String typeName, ViewFactory fact) 160 { 161 log.info("View factory for \"" + typeName + "\" is " + fact.getClass().getName()); 162 163 this.viewFactories.put(typeName, fact); 164 165 if (this.defaultViewType == null) 166 this.defaultViewType = typeName; 167 } 168 169 174 public void defineViewFactories(List viewFactoryNodes) throws ConfigException 175 { 176 Iterator it = viewFactoryNodes.iterator(); 178 while (it.hasNext()) 179 { 180 Element viewFactoryNode = (Element)it.next(); 181 182 String typeName = viewFactoryNode.getAttributeValue(ATTR_FACTORY_TYPE_NAME); 183 String providerName = viewFactoryNode.getAttributeValue(ATTR_FACTORY_PROVIDER); 184 185 if (typeName == null || providerName == null) 186 throw new ConfigException("Not a valid view factory node: " + XML.toString(viewFactoryNode)); 187 188 Class providerClass; 189 ViewFactory instance; 190 try 191 { 192 providerClass = loadClass(providerName); 193 instance = (ViewFactory)providerClass.newInstance(); 194 } 195 catch (Exception ex) 196 { 197 throw new ConfigException("Unable to define view factory for " + typeName, ex); 198 } 199 200 instance.init(viewFactoryNode, this.servletCfg); 202 203 this.defineViewFactory(typeName, instance); 204 } 205 } 206 207 209 213 public void setDefaultTransformType(String type) 214 { 215 this.defaultTransformType = type; 216 } 217 218 224 protected Transform[] createTransforms(List transformNodes) throws ConfigException 225 { 226 Transform[] retVal = new Transform[transformNodes.size()]; 227 228 int index = 0; 229 Iterator it = transformNodes.iterator(); 230 while (it.hasNext()) 231 { 232 Element transNode = (Element)it.next(); 233 234 retVal[index] = this.createTransform(transNode); 235 236 index++; 237 } 238 239 return retVal; 240 } 241 242 248 protected Transform createTransform(Element transformNode) throws ConfigException 249 { 250 Transform t = this.createPlainTransform(transformNode); 251 252 Map params = XML.getParams(transformNode); 253 if (params != null) 254 t = new TransformWithParams(t, params); 255 256 return t; 257 } 258 259 265 protected Transform createPlainTransform(Element transformNode) throws ConfigException 266 { 267 String typeName = transformNode.getAttributeValue(ATTR_TYPE_NAME); 268 if (typeName == null) 269 typeName = this.defaultTransformType; 270 271 log.debug("Creating transform of type " + typeName); 272 273 TransformFactory fact = (TransformFactory)this.transformFactories.get(typeName); 274 275 if (fact == null) 276 throw new ConfigException("No transform factory can be found for " + XML.toString(transformNode)); 277 278 return fact.createTransform(transformNode); 279 } 280 281 287 public void defineTransformFactory(String typeName, TransformFactory fact) 288 { 289 log.info("Transform factory for \"" + typeName + "\" is " + fact.getClass().getName()); 290 291 this.transformFactories.put(typeName, fact); 292 293 if (this.defaultTransformType == null) 294 this.defaultTransformType = typeName; 295 } 296 297 302 public void defineTransformFactories(List transFactoryNodes) throws ConfigException 303 { 304 Iterator it = transFactoryNodes.iterator(); 306 while (it.hasNext()) 307 { 308 Element transFactoryNode = (Element)it.next(); 309 310 String typeName = transFactoryNode.getAttributeValue(ATTR_FACTORY_TYPE_NAME); 311 String providerName = transFactoryNode.getAttributeValue(ATTR_FACTORY_PROVIDER); 312 313 if (typeName == null || providerName == null) 314 throw new ConfigException("Not a valid transform factory node: " + XML.toString(transFactoryNode)); 315 316 Class providerClass; 317 TransformFactory instance; 318 try 319 { 320 providerClass = loadClass(providerName); 321 instance = (TransformFactory)providerClass.newInstance(); 322 } 323 catch (Exception ex) 324 { 325 throw new ConfigException("Unable to define transform factory for " + typeName, ex); 326 } 327 328 instance.init(transFactoryNode, this.servletCfg); 330 331 this.defineTransformFactory(typeName, instance); 332 } 333 } 334 335 337 343 protected Class loadClass(String className) throws ClassNotFoundException 344 { 345 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 346 if (classLoader == null) 347 { 348 classLoader = DefaultControllerFactory.class.getClassLoader(); 349 } 350 Class cls = classLoader.loadClass(className); 351 return cls; 352 } 353 } | Popular Tags |