1 18 19 package org.apache.struts.tiles.definition; 20 21 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import javax.servlet.ServletConfig ; 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.struts.tiles.ComponentDefinition; 31 import org.apache.struts.tiles.ComponentDefinitionsFactory; 32 import org.apache.struts.tiles.DefinitionsFactoryException; 33 import org.apache.struts.tiles.FactoryNotFoundException; 34 import org.apache.struts.tiles.xmlDefinition.I18nFactorySet; 35 import org.apache.struts.util.RequestUtils; 36 37 46 public class ReloadableDefinitionsFactory implements ComponentDefinitionsFactory { 47 48 51 protected ComponentDefinitionsFactory factory = null; 52 53 56 protected Map properties = null; 57 58 61 public static final String DEFINITIONS_FACTORY_CLASSNAME = 62 "definitions-factory-class"; 63 64 71 public ReloadableDefinitionsFactory( 72 ServletContext servletContext, 73 ServletConfig servletConfig) 74 throws DefinitionsFactoryException { 75 76 properties = new ServletPropertiesMap(servletConfig); 77 factory = createFactory(servletContext, properties); 78 } 79 80 87 public ReloadableDefinitionsFactory( 88 ServletContext servletContext, 89 Map properties) 90 throws DefinitionsFactoryException { 91 92 this.properties = properties; 93 factory = createFactory(servletContext, properties); 94 } 95 96 107 public ComponentDefinitionsFactory createFactoryFromClassname( 108 ServletContext servletContext, 109 Map properties, 110 String classname) 111 throws DefinitionsFactoryException { 112 113 if (classname == null) { 114 return createFactory(servletContext, properties); 115 } 116 117 try { 119 Class factoryClass = RequestUtils.applicationClass(classname); 120 ComponentDefinitionsFactory factory = 121 (ComponentDefinitionsFactory) factoryClass.newInstance(); 122 factory.initFactory(servletContext, properties); 123 return factory; 124 125 } catch (ClassCastException ex) { throw new DefinitionsFactoryException( 127 "Error - createDefinitionsFactory : Factory class '" 128 + classname 129 + " must implements 'ComponentDefinitionsFactory'.", 130 ex); 131 132 } catch (ClassNotFoundException ex) { throw new DefinitionsFactoryException( 134 "Error - createDefinitionsFactory : Bad class name '" 135 + classname 136 + "'.", 137 ex); 138 139 } catch (InstantiationException ex) { throw new DefinitionsFactoryException(ex); 141 142 } catch (IllegalAccessException ex) { 143 throw new DefinitionsFactoryException(ex); 144 } 145 146 } 147 148 157 public ComponentDefinitionsFactory createDefaultFactory( 158 ServletContext servletContext, 159 Map properties) 160 throws DefinitionsFactoryException { 161 162 ComponentDefinitionsFactory factory = 163 new I18nFactorySet(servletContext, properties); 164 165 return factory; 166 } 167 168 176 public ComponentDefinitionsFactory createFactory( 177 ServletContext servletContext, 178 Map properties) 179 throws DefinitionsFactoryException { 180 181 String classname = (String ) properties.get(DEFINITIONS_FACTORY_CLASSNAME); 182 183 if (classname != null) { 184 return createFactoryFromClassname(servletContext, properties, classname); 185 } 186 187 return new I18nFactorySet(servletContext, properties); 188 } 189 190 200 public ComponentDefinition getDefinition( 201 String definitionName, 202 ServletRequest request, 203 ServletContext servletContext) 204 throws FactoryNotFoundException, DefinitionsFactoryException { 205 206 return factory.getDefinition( 207 definitionName, 208 (HttpServletRequest ) request, 209 servletContext); 210 } 211 212 219 public void reload(ServletContext servletContext) 220 throws DefinitionsFactoryException { 221 222 ComponentDefinitionsFactory newInstance = 223 createFactory(servletContext, properties); 224 225 factory = newInstance; 226 } 227 228 232 public ComponentDefinitionsFactory getFactory() { 233 return factory; 234 } 235 236 246 public void initFactory(ServletContext servletContext, Map properties) 247 throws DefinitionsFactoryException { 248 } 250 251 255 public String toString() { 256 return factory.toString(); 257 } 258 259 265 class ServletPropertiesMap extends HashMap { 266 269 ServletPropertiesMap(ServletConfig config) { 270 Enumeration e = config.getInitParameterNames(); 274 while (e.hasMoreElements()) { 275 String key = (String ) e.nextElement(); 276 put(key, config.getInitParameter(key)); 277 } 278 } 279 } } 281 | Popular Tags |