1 18 19 package org.apache.struts.webapp.tiles.channel; 20 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpSession ; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.tiles.DefinitionsFactoryException; 33 import org.apache.struts.tiles.FactoryNotFoundException; 34 import org.apache.struts.tiles.xmlDefinition.DefinitionsFactory; 35 import org.apache.struts.tiles.xmlDefinition.FactorySet; 36 import org.apache.struts.tiles.xmlDefinition.XmlDefinitionsSet; 37 import org.apache.struts.tiles.xmlDefinition.XmlParser; 38 import org.xml.sax.SAXException ; 39 40 50 public class ChannelFactorySet extends FactorySet { 51 52 private static final Log log = LogFactory.getLog(ChannelFactorySet.class); 53 54 58 public static final boolean debug = false; 59 60 63 public static final String DEFAULT_DEFINITIONS_FILE_NAME = 64 "/WEB-INF/templateDefinitions.xml"; 65 66 69 public static final String DEFINITIONS_CONFIG_PARAMETER_NAME = 70 "definitions-config"; 71 72 75 public static final String FACTORY_SELECTOR_KEY = 76 "ChannelFactorySet.factorySelectorKey"; 77 78 81 public static final String FILENAME_EXTENSION = ".xml"; 82 83 86 protected DefinitionsFactory defaultFactory = null; 87 88 91 protected XmlParser xmlParser = null; 92 93 96 private String filename = null; 97 98 101 private Map loaded = null; 102 103 107 public ChannelFactorySet() { 108 super(); 109 } 110 111 116 public ChannelFactorySet(ServletContext servletContext, Map properties) 117 throws DefinitionsFactoryException { 118 119 initFactory(servletContext, properties); 120 } 121 122 132 public void initFactory(ServletContext servletContext, Map properties) 133 throws DefinitionsFactoryException { 134 135 String proposedFilename = 137 (String ) properties.get(DEFINITIONS_CONFIG_PARAMETER_NAME); 138 139 boolean isFileSpecified = true; 141 if (proposedFilename == null) { 142 proposedFilename = DEFAULT_DEFINITIONS_FILE_NAME; 143 isFileSpecified = false; 144 } 145 146 try { 147 initFactory(servletContext, proposedFilename); 148 return; 149 150 } catch (FileNotFoundException ex) { 151 if (isFileSpecified) { 153 throw new FactoryNotFoundException( 154 ex.getMessage() 155 + " : Can't find file '" 156 + proposedFilename 157 + "'"); 158 } 159 } 160 161 } 162 163 172 protected void initFactory( 173 ServletContext servletContext, 174 String proposedFilename) 175 throws DefinitionsFactoryException, FileNotFoundException { 176 177 filename = proposedFilename; 178 loaded = new HashMap (); 179 defaultFactory = createDefaultFactory(servletContext); 180 } 181 182 186 protected DefinitionsFactory getDefaultFactory() { 187 return defaultFactory; 188 } 189 190 197 protected DefinitionsFactory createDefaultFactory(ServletContext servletContext) 198 throws DefinitionsFactoryException, FileNotFoundException { 199 200 XmlDefinitionsSet rootXmlConfig = parseXmlKeyFile(servletContext, "", null); 201 202 if (rootXmlConfig == null) { 203 throw new FileNotFoundException (); 204 } 205 206 rootXmlConfig.resolveInheritances(); 207 return new DefinitionsFactory(rootXmlConfig); 208 } 209 210 218 protected Object getDefinitionsFactoryKey( 219 String name, 220 ServletRequest request, 221 ServletContext servletContext) { 222 Object key = null; 223 224 HttpSession session = ((HttpServletRequest ) request).getSession(false); 225 if (session != null) { 226 key = session.getAttribute(FACTORY_SELECTOR_KEY); 227 } 228 229 return key; 230 } 231 232 240 protected DefinitionsFactory createFactory( 241 Object key, 242 ServletRequest request, 243 ServletContext servletContext) 244 throws DefinitionsFactoryException { 245 246 if (key == null) { 247 return getDefaultFactory(); 248 } 249 250 DefinitionsFactory factory = (DefinitionsFactory) loaded.get(key); 252 if (factory != null) { return factory; 254 } 255 256 XmlDefinitionsSet lastXmlFile = 258 parseXmlKeyFile(servletContext, "_" + (String ) key, null); 259 260 if (lastXmlFile == null) { 261 log.warn( 262 "No definition factory associated to key '" 263 + key 264 + "'. Use default factory instead."); 265 266 factory = getDefaultFactory(); 267 loaded.put(key, factory); 268 return factory; 269 } 270 271 XmlDefinitionsSet rootXmlConfig = parseXmlKeyFile(servletContext, "", null); 273 274 rootXmlConfig.extend(lastXmlFile); 275 rootXmlConfig.resolveInheritances(); 276 277 factory = new DefinitionsFactory(rootXmlConfig); 278 loaded.put(key, factory); 279 280 log.info(factory); 281 282 return factory; 284 } 285 286 301 private XmlDefinitionsSet parseXmlKeyFile( 302 ServletContext servletContext, 303 String postfix, 304 XmlDefinitionsSet xmlDefinitions) 305 throws DefinitionsFactoryException { 306 if (postfix != null && postfix.length() == 0) 307 postfix = null; 308 309 String fullName = concatPostfix(filename, postfix); 310 return parseXmlFile(servletContext, fullName, xmlDefinitions); 311 } 312 313 325 private XmlDefinitionsSet parseXmlFile( 326 ServletContext servletContext, 327 String filename, 328 XmlDefinitionsSet xmlDefinitions) 329 throws DefinitionsFactoryException { 330 331 try { 332 log.debug("Trying to load '" + filename + "'."); 333 334 InputStream input = servletContext.getResourceAsStream(filename); 335 if (input == null) { 336 return xmlDefinitions; 337 } 338 339 xmlParser = new XmlParser(); 340 341 if (xmlDefinitions == null) { 343 xmlDefinitions = new XmlDefinitionsSet(); 344 } 345 346 xmlParser.parse(input, xmlDefinitions); 347 348 } catch (SAXException ex) { 349 log.debug("Error while parsing file '" + filename + "'.", ex); 350 351 throw new DefinitionsFactoryException( 352 "Error while parsing file '" + filename + "'. " + ex.getMessage(), 353 ex); 354 355 } catch (IOException ex) { 356 throw new DefinitionsFactoryException( 357 "IO Error while parsing file '" + filename + "'. " + ex.getMessage(), 358 ex); 359 } 360 361 return xmlDefinitions; 362 } 363 364 369 private String concatPostfix(String name, String postfix) { 370 if (postfix == null) { 371 return name; 372 } 373 374 int dotIndex = name.lastIndexOf("."); 378 int lastNameStart = name.lastIndexOf(java.io.File.pathSeparator); 379 if (dotIndex < 1 || dotIndex < lastNameStart) 380 return name + postfix; 381 382 String ext = name.substring(dotIndex); 383 name = name.substring(0, dotIndex); 384 return name + postfix + ext; 385 } 386 387 } 388 | Popular Tags |