1 18 19 package org.apache.struts.tiles; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.ServletContext ; 28 import javax.servlet.ServletRequest ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.taglib.tiles.ComponentConstants; 33 34 39 public class DefinitionsUtil extends TilesUtil implements ComponentConstants { 40 41 44 protected static Log log = LogFactory.getLog(DefinitionsUtil.class); 45 46 50 public static int userDebugLevel = 0; 51 52 56 public static final int NO_DEBUG = 0; 57 58 61 public static final String DEFINITIONS_CONFIG_USER_DEBUG_LEVEL = 62 "definitions-debug"; 63 64 67 public static final String DEFINITIONS_FACTORY_CLASSNAME = 68 "definitions-factory-class"; 69 70 73 public static final String DEFINITIONS_FACTORY = 74 "org.apache.struts.tiles.DEFINITIONS_FACTORY"; 75 76 80 public static final String ACTION_DEFINITION = 81 "org.apache.struts.tiles.ACTION_DEFINITION"; 82 83 94 public static DefinitionsFactory createDefinitionsFactory( 95 ServletContext servletContext, 96 Map properties, 97 String classname) 98 throws DefinitionsFactoryException { 99 100 DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig(); 102 try { 104 factoryConfig.populate(properties); 105 106 } catch (Exception ex) { 107 throw new DefinitionsFactoryException( 108 "Error - createDefinitionsFactory : Can't populate config object from properties map", 109 ex); 110 } 111 112 if (classname != null) 114 factoryConfig.setFactoryClassname(classname); 115 116 return createDefinitionsFactory(servletContext, factoryConfig); 118 } 119 120 127 public static DefinitionsFactory createDefinitionsFactory( 128 ServletContext servletContext, 129 Map properties) 130 throws DefinitionsFactoryException { 131 132 return createDefinitionsFactory(servletContext, properties, null); 133 } 134 135 147 public static DefinitionsFactory createDefinitionsFactory( 148 ServletContext servletContext, 149 ServletConfig servletConfig) 150 throws DefinitionsFactoryException { 151 152 DefinitionsFactoryConfig factoryConfig = readFactoryConfig(servletConfig); 153 154 return createDefinitionsFactory(servletContext, factoryConfig); 155 } 156 157 176 public static DefinitionsFactory createDefinitionsFactory( 177 ServletContext servletContext, 178 ServletConfig servletConfig, 179 boolean checkIfExist) 180 throws DefinitionsFactoryException { 181 182 if (checkIfExist) { 183 DefinitionsFactory factory = getDefinitionsFactory(servletContext); 185 if (factory != null) 186 return factory; 187 } 188 189 return createDefinitionsFactory(servletContext, servletConfig); 190 } 191 192 198 public static DefinitionsFactory getDefinitionsFactory(ServletContext servletContext) { 199 return (DefinitionsFactory) servletContext.getAttribute(DEFINITIONS_FACTORY); 200 } 201 202 206 public static ComponentDefinition getActionDefinition(ServletRequest request) { 207 return (ComponentDefinition) request.getAttribute(ACTION_DEFINITION); 208 } 209 210 214 public static void setActionDefinition( 215 ServletRequest request, 216 ComponentDefinition definition) { 217 218 request.setAttribute(ACTION_DEFINITION, definition); 219 } 220 221 225 public static void removeActionDefinition( 226 ServletRequest request, 227 ComponentDefinition definition) { 228 229 request.removeAttribute(ACTION_DEFINITION); 230 } 231 232 243 public static void populateDefinitionsFactoryConfig( 244 DefinitionsFactoryConfig factoryConfig, 245 ServletConfig servletConfig) 246 throws IllegalAccessException , InvocationTargetException { 247 248 Map properties = new DefinitionsUtil.ServletPropertiesMap(servletConfig); 249 factoryConfig.populate(properties); 250 } 251 252 260 protected static DefinitionsFactoryConfig readFactoryConfig(ServletConfig servletConfig) 261 throws DefinitionsFactoryException { 262 263 DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig(); 265 266 try { 268 DefinitionsUtil.populateDefinitionsFactoryConfig( 269 factoryConfig, 270 servletConfig); 271 272 } catch (Exception ex) { 273 ex.printStackTrace(); 274 throw new DefinitionsFactoryException( 275 "Can't populate DefinitionsFactoryConfig class from 'web.xml'.", 276 ex); 277 } 278 279 return factoryConfig; 280 } 281 282 288 static class ServletPropertiesMap extends HashMap { 289 292 ServletPropertiesMap(ServletConfig config) { 293 Enumeration e = config.getInitParameterNames(); 297 while (e.hasMoreElements()) { 298 String key = (String ) e.nextElement(); 299 put(key, config.getInitParameter(key)); 300 } 301 } 302 } 304 } 305 | Popular Tags |