1 18 19 package org.apache.struts.tiles; 20 21 import java.io.IOException ; 22 import java.io.Serializable ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 import javax.servlet.jsp.PageContext ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper; 34 import org.apache.struts.util.RequestUtils; 35 36 41 public class TilesUtilImpl implements Serializable { 42 43 44 protected Log log = LogFactory.getLog(TilesUtil.class); 45 46 47 public static final String DEFINITIONS_FACTORY = 48 "org.apache.struts.tiles.DEFINITIONS_FACTORY"; 49 50 58 public void doForward( 59 String uri, 60 HttpServletRequest request, 61 HttpServletResponse response, 62 ServletContext servletContext) 63 throws IOException , ServletException { 64 65 request.getRequestDispatcher(uri).forward(request, response); 66 } 67 68 78 public void doInclude( 79 String uri, 80 HttpServletRequest request, 81 HttpServletResponse response, 82 ServletContext servletContext) 83 throws IOException , ServletException { 84 85 request.getRequestDispatcher(uri).include(request, response); 86 } 87 88 96 public void doInclude(String uri, PageContext pageContext) 97 throws IOException , ServletException { 98 99 pageContext.include(uri); 100 } 101 102 106 public DefinitionsFactory getDefinitionsFactory( 107 ServletRequest request, 108 ServletContext servletContext) { 109 110 return (DefinitionsFactory) servletContext.getAttribute(DEFINITIONS_FACTORY); 111 } 112 113 127 public DefinitionsFactory createDefinitionsFactory( 128 ServletContext servletContext, 129 DefinitionsFactoryConfig factoryConfig) 130 throws DefinitionsFactoryException { 131 132 DefinitionsFactory factory = 134 createDefinitionFactoryInstance(factoryConfig.getFactoryClassname()); 135 136 factory.init(factoryConfig, servletContext); 137 138 makeDefinitionsFactoryAccessible(factory, servletContext); 140 return factory; 141 } 142 143 152 protected DefinitionsFactory createDefinitionFactoryInstance(String classname) 153 throws DefinitionsFactoryException { 154 155 try { 156 Class factoryClass = RequestUtils.applicationClass(classname); 157 Object factory = factoryClass.newInstance(); 158 159 if (factory instanceof ComponentDefinitionsFactory) { 162 factory = 163 new ComponentDefinitionsFactoryWrapper( 164 (ComponentDefinitionsFactory) factory); 165 } 166 return (DefinitionsFactory) factory; 167 168 } catch (ClassCastException ex) { throw new DefinitionsFactoryException( 170 "Error - createDefinitionsFactory : Factory class '" 171 + classname 172 + " must implement 'TilesDefinitionsFactory'.", 173 ex); 174 175 } catch (ClassNotFoundException ex) { throw new DefinitionsFactoryException( 177 "Error - createDefinitionsFactory : Bad class name '" 178 + classname 179 + "'.", 180 ex); 181 182 } catch (InstantiationException ex) { throw new DefinitionsFactoryException(ex); 184 185 } catch (IllegalAccessException ex) { 186 throw new DefinitionsFactoryException(ex); 187 } 188 } 189 190 196 protected void makeDefinitionsFactoryAccessible( 197 DefinitionsFactory factory, 198 ServletContext servletContext) { 199 200 servletContext.setAttribute(DEFINITIONS_FACTORY, factory); 201 } 202 203 } | Popular Tags |