1 18 19 package org.apache.struts.tiles; 20 21 import java.util.Map ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.UnavailableException ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.struts.action.ActionServlet; 30 import org.apache.struts.action.PlugIn; 31 import org.apache.struts.action.RequestProcessor; 32 import org.apache.struts.config.ControllerConfig; 33 import org.apache.struts.config.ModuleConfig; 34 import org.apache.struts.config.PlugInConfig; 35 import org.apache.struts.util.RequestUtils; 36 37 60 public class TilesPlugin implements PlugIn { 61 62 65 protected static Log log = LogFactory.getLog(TilesPlugin.class); 66 67 70 protected boolean moduleAware = false; 71 72 76 protected String tilesUtilImplClassname = null; 77 78 81 protected DefinitionsFactory definitionFactory = null; 82 83 87 protected PlugInConfig currentPlugInConfigObject=null; 88 89 94 public boolean isModuleAware() { 95 return moduleAware; 96 } 97 98 105 public void setModuleAware(boolean moduleAware) { 106 this.moduleAware = moduleAware; 107 } 108 109 121 public void init(ActionServlet servlet, ModuleConfig moduleConfig) 122 throws ServletException { 123 124 DefinitionsFactoryConfig factoryConfig = 126 readFactoryConfig(servlet, moduleConfig); 127 128 factoryConfig.setFactoryName(moduleConfig.getPrefix()); 131 132 this.initRequestProcessorClass(moduleConfig); 134 135 this.initTilesUtil(); 136 137 this.initDefinitionsFactory(servlet.getServletContext(), moduleConfig, factoryConfig); 138 } 139 140 147 private void initTilesUtil() throws ServletException { 148 149 if (TilesUtil.isTilesUtilImplSet()) { 150 return; 151 } 152 153 157 if (this.getTilesUtilImplClassname() == null) { 158 159 if (isModuleAware()) { 160 TilesUtil.setTilesUtil(new TilesUtilStrutsModulesImpl()); 161 } else { 162 TilesUtil.setTilesUtil(new TilesUtilStrutsImpl()); 163 } 164 165 } else { try { 167 TilesUtilStrutsImpl impl = 168 (TilesUtilStrutsImpl) RequestUtils 169 .applicationClass(getTilesUtilImplClassname()) 170 .newInstance(); 171 TilesUtil.setTilesUtil(impl); 172 173 } catch (ClassCastException ex) { 174 throw new ServletException ( 175 "Can't set TilesUtil implementation to '" 176 + getTilesUtilImplClassname() 177 + "'. TilesUtil implementation should be a subclass of '" 178 + TilesUtilStrutsImpl.class.getName() 179 + "'"); 180 181 } catch (Exception ex) { 182 throw new ServletException ( 183 "Can't set TilesUtil implementation.", 184 ex); 185 } 186 } 187 188 } 189 190 197 private void initDefinitionsFactory( 198 ServletContext servletContext, 199 ModuleConfig moduleConfig, 200 DefinitionsFactoryConfig factoryConfig) 201 throws ServletException { 202 203 definitionFactory = 205 ((TilesUtilStrutsImpl) TilesUtil.getTilesUtil()).getDefinitionsFactory( 206 servletContext, 207 moduleConfig); 208 209 if (definitionFactory != null) { 210 log.info( 211 "Factory already exists for module '" 212 + moduleConfig.getPrefix() 213 + "'. The factory found is from module '" 214 + definitionFactory.getConfig().getFactoryName() 215 + "'. No new creation."); 216 217 return; 218 } 219 220 try { 222 definitionFactory = 223 TilesUtil.createDefinitionsFactory( 224 servletContext, 225 factoryConfig); 226 227 } catch (DefinitionsFactoryException ex) { 228 log.error( 229 "Can't create Tiles definition factory for module '" 230 + moduleConfig.getPrefix() 231 + "'."); 232 233 throw new ServletException (ex); 234 } 235 236 log.info( 237 "Tiles definition factory loaded for module '" 238 + moduleConfig.getPrefix() 239 + "'."); 240 } 241 242 245 public void destroy() { 246 definitionFactory.destroy(); 247 definitionFactory = null; 248 } 249 250 260 protected DefinitionsFactoryConfig readFactoryConfig( 261 ActionServlet servlet, 262 ModuleConfig config) 263 throws ServletException { 264 265 DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig(); 267 try { 269 DefinitionsUtil.populateDefinitionsFactoryConfig( 270 factoryConfig, 271 servlet.getServletConfig()); 272 273 } catch (Exception ex) { 274 if (log.isDebugEnabled()){ 275 log.debug("", ex); 276 } 277 ex.printStackTrace(); 278 throw new UnavailableException ( 279 "Can't populate DefinitionsFactoryConfig class from 'web.xml': " 280 + ex.getMessage()); 281 } 282 283 try { 285 Map strutsProperties = findStrutsPlugInConfigProperties(servlet, config); 286 factoryConfig.populate(strutsProperties); 287 288 } catch (Exception ex) { 289 if (log.isDebugEnabled()) { 290 log.debug("", ex); 291 } 292 293 throw new UnavailableException ( 294 "Can't populate DefinitionsFactoryConfig class from '" 295 + config.getPrefix() 296 + "/struts-config.xml':" 297 + ex.getMessage()); 298 } 299 300 return factoryConfig; 301 } 302 303 315 protected Map findStrutsPlugInConfigProperties( 316 ActionServlet servlet, 317 ModuleConfig config) 318 throws ServletException { 319 320 return currentPlugInConfigObject.getProperties(); 321 } 322 323 333 protected void initRequestProcessorClass(ModuleConfig config) 334 throws ServletException { 335 336 String tilesProcessorClassname = TilesRequestProcessor.class.getName(); 337 ControllerConfig ctrlConfig = config.getControllerConfig(); 338 String configProcessorClassname = ctrlConfig.getProcessorClass(); 339 340 Class configProcessorClass; 342 try { 343 configProcessorClass = 344 RequestUtils.applicationClass(configProcessorClassname); 345 346 } catch (ClassNotFoundException ex) { 347 log.fatal( 348 "Can't set TilesRequestProcessor: bad class name '" 349 + configProcessorClassname 350 + "'."); 351 throw new ServletException (ex); 352 } 353 354 if (configProcessorClassname.equals(RequestProcessor.class.getName()) 357 || configProcessorClassname.endsWith(tilesProcessorClassname)) { 358 359 ctrlConfig.setProcessorClass(tilesProcessorClassname); 360 return; 361 } 362 363 Class tilesProcessorClass = TilesRequestProcessor.class; 365 if (!tilesProcessorClass.isAssignableFrom(configProcessorClass)) { 366 String msg = 368 "TilesPlugin : Specified RequestProcessor not compatible with TilesRequestProcessor"; 369 if (log.isFatalEnabled()) { 370 log.fatal(msg); 371 } 372 throw new ServletException (msg); 373 } 374 } 375 376 381 public void setTilesUtilImplClassname(String tilesUtilImplClassname) { 382 this.tilesUtilImplClassname = tilesUtilImplClassname; 383 } 384 385 389 public String getTilesUtilImplClassname() { 390 return tilesUtilImplClassname; 391 } 392 393 398 public void setCurrentPlugInConfigObject(PlugInConfig plugInConfigObject) { 399 this.currentPlugInConfigObject = plugInConfigObject; 400 } 401 402 } 403 | Popular Tags |