1 18 19 package org.apache.struts.tiles.definition; 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletRequest ; 26 27 import org.apache.struts.tiles.ComponentDefinition; 28 import org.apache.struts.tiles.ComponentDefinitionsFactory; 29 import org.apache.struts.tiles.DefinitionsFactory; 30 import org.apache.struts.tiles.DefinitionsFactoryConfig; 31 import org.apache.struts.tiles.DefinitionsFactoryException; 32 import org.apache.struts.tiles.NoSuchDefinitionException; 33 import org.apache.struts.util.RequestUtils; 34 35 40 public class ComponentDefinitionsFactoryWrapper implements DefinitionsFactory { 41 42 45 private ComponentDefinitionsFactory factory = null; 46 47 50 private DefinitionsFactoryConfig config = null; 51 52 57 public ComponentDefinitionsFactoryWrapper(ComponentDefinitionsFactory factory) { 58 this.factory = factory; 59 } 60 61 67 public ComponentDefinitionsFactoryWrapper() { 68 super(); 69 } 70 71 78 public ComponentDefinition getDefinition( 79 String name, 80 ServletRequest request, 81 ServletContext servletContext) 82 throws NoSuchDefinitionException, DefinitionsFactoryException { 83 84 return factory.getDefinition(name, request, servletContext); 85 } 86 87 92 public void init(DefinitionsFactoryConfig config, ServletContext servletContext) 93 throws DefinitionsFactoryException { 94 95 this.config = config; 96 97 if (factory == null) { 99 factory = createFactoryInstance(config.getFactoryClassname()); 100 } 101 102 factory.initFactory(servletContext, createConfigMap(config)); 103 } 104 105 108 public void destroy() { 109 factory = null; 110 } 111 112 118 public void setConfig( 119 DefinitionsFactoryConfig config, 120 ServletContext servletContext) 121 throws DefinitionsFactoryException { 122 123 ComponentDefinitionsFactory newFactory = 124 createFactoryInstance(config.getFactoryClassname()); 125 126 newFactory.initFactory(servletContext, createConfigMap(config)); 127 factory = newFactory; 128 } 129 130 134 public DefinitionsFactoryConfig getConfig() { 135 return config; 136 } 137 138 142 public ComponentDefinitionsFactory getInternalFactory() { 143 return factory; 144 } 145 146 153 protected ComponentDefinitionsFactory createFactoryInstance(String classname) 154 throws DefinitionsFactoryException { 155 156 try { 157 Class factoryClass = RequestUtils.applicationClass(classname); 158 Object factory = factoryClass.newInstance(); 159 return (ComponentDefinitionsFactory) factory; 160 161 } catch (ClassCastException ex) { throw new DefinitionsFactoryException( 163 "Error - createDefinitionsFactory : Factory class '" 164 + classname 165 + " must implement 'DefinitionsFactory'.", 166 ex); 167 168 } catch (ClassNotFoundException ex) { throw new DefinitionsFactoryException( 170 "Error - createDefinitionsFactory : Bad class name '" 171 + classname 172 + "'.", 173 ex); 174 175 } catch (InstantiationException ex) { throw new DefinitionsFactoryException(ex); 177 178 } catch (IllegalAccessException ex) { 179 throw new DefinitionsFactoryException(ex); 180 } 181 182 } 183 184 189 public String toString() { 190 return getInternalFactory().toString(); 191 } 192 193 199 public static Map createConfigMap(DefinitionsFactoryConfig config) { 200 Map map = new HashMap (config.getAttributes()); 201 map.put( 203 DefinitionsFactoryConfig.DEFINITIONS_CONFIG_PARAMETER_NAME, 204 config.getDefinitionConfigFiles()); 205 206 map.put( 207 DefinitionsFactoryConfig.PARSER_VALIDATE_PARAMETER_NAME, 208 (config.getParserValidate() ? Boolean.TRUE.toString() : Boolean.FALSE.toString())); 209 210 if (!"org.apache.struts.tiles.xmlDefinition.I18nFactorySet" 211 .equals(config.getFactoryClassname())) { 212 213 map.put( 214 DefinitionsFactoryConfig.FACTORY_CLASSNAME_PARAMETER_NAME, 215 config.getFactoryClassname()); 216 } 217 218 return map; 219 } 220 221 } 222 | Popular Tags |