001: /*
002: * $Id: TilesUtilImpl.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.tiles;
023:
024: import java.io.IOException;
025: import java.io.Serializable;
026:
027: import java.lang.reflect.Method;
028: import java.lang.reflect.InvocationTargetException;
029:
030: import javax.servlet.ServletContext;
031: import javax.servlet.ServletException;
032: import javax.servlet.ServletRequest;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.servlet.jsp.PageContext;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039: import org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper;
040: import org.apache.struts.util.RequestUtils;
041:
042: /**
043: * Default implementation of TilesUtil.
044: * This class contains default implementation of utilities. This implementation
045: * is intended to be used without Struts.
046: */
047: public class TilesUtilImpl implements Serializable {
048:
049: /** Commons Logging instance.*/
050: protected static final Log log = LogFactory.getLog(TilesUtil.class);
051:
052: /** Constant name used to store factory in servlet context */
053: public static final String DEFINITIONS_FACTORY = "org.apache.struts.tiles.DEFINITIONS_FACTORY";
054:
055: /**
056: * JSP 2.0 include method to use which supports configurable flushing.
057: */
058: private static Method include = null;
059:
060: /**
061: * Initialize the include variable with the
062: * JSP 2.0 method if available.
063: */
064: static {
065:
066: try {
067: // get version of include method with flush argument
068: Class[] args = new Class[] { String.class, boolean.class };
069: include = PageContext.class.getMethod("include", args);
070: } catch (NoSuchMethodException e) {
071: log.debug(
072: "Could not find JSP 2.0 include method. Using old one that doesn't support "
073: + "configurable flushing.", e);
074: }
075: }
076:
077: /**
078: * Do a forward using request dispatcher.
079: *
080: * This method is used by the Tiles package anytime a forward is required.
081: * @param uri Uri or Definition name to forward.
082: * @param request Current page request.
083: * @param servletContext Current servlet context.
084: */
085: public void doForward(String uri, HttpServletRequest request,
086: HttpServletResponse response, ServletContext servletContext)
087: throws IOException, ServletException {
088:
089: request.getRequestDispatcher(uri).forward(request, response);
090: }
091:
092: /**
093: * Do an include using request dispatcher.
094: *
095: * This method is used by the Tiles package when an include is required.
096: * The Tiles package can use indifferently any form of this method.
097: * @param uri Uri or Definition name to forward.
098: * @param request Current page request.
099: * @param response Current page response.
100: * @param servletContext Current servlet context.
101: */
102: public void doInclude(String uri, HttpServletRequest request,
103: HttpServletResponse response, ServletContext servletContext)
104: throws IOException, ServletException {
105:
106: request.getRequestDispatcher(uri).include(request, response);
107: }
108:
109: /**
110: * Do an include using PageContext.include().
111: *
112: * This method is used by the Tiles package when an include is required.
113: * The Tiles package can use indifferently any form of this method.
114: * @param uri Uri or Definition name to forward.
115: * @param pageContext Current page context.
116: * @param flush If the writer should be flushed before the include
117: */
118: public void doInclude(String uri, PageContext pageContext,
119: boolean flush) throws IOException, ServletException {
120: try {
121: // perform include with new JSP 2.0 method that supports flushing
122: if (include != null) {
123: include.invoke(pageContext, new Object[] { uri,
124: Boolean.valueOf(flush) });
125: return;
126: }
127: } catch (IllegalAccessException e) {
128: log
129: .debug(
130: "Could not find JSP 2.0 include method. Using old one.",
131: e);
132: } catch (InvocationTargetException e) {
133: log
134: .debug(
135: "Unable to execute JSP 2.0 include method. Trying old one.",
136: e);
137: }
138:
139: pageContext.include(uri);
140: }
141:
142: /**
143: * Get definition factory from appropriate servlet context.
144: * @return Definitions factory or <code>null</code> if not found.
145: */
146: public DefinitionsFactory getDefinitionsFactory(
147: ServletRequest request, ServletContext servletContext) {
148:
149: return (DefinitionsFactory) servletContext
150: .getAttribute(DEFINITIONS_FACTORY);
151: }
152:
153: /**
154: * Create Definition factory from specified configuration object.
155: * Create an instance of the factory with the class specified in the config
156: * object. Then, initialize this factory and finally store the factory in
157: * appropriate context by calling
158: * {@link #makeDefinitionsFactoryAccessible(DefinitionsFactory, ServletContext)}.
159: * Factory creation is done by {@link #createDefinitionFactoryInstance(String)}.
160: * <p>
161: *
162: * @param servletContext Servlet Context passed to newly created factory.
163: * @param factoryConfig Configuration object passed to factory.
164: * @return newly created factory of type specified in the config object.
165: * @throws DefinitionsFactoryException If an error occur while initializing factory
166: */
167: public DefinitionsFactory createDefinitionsFactory(
168: ServletContext servletContext,
169: DefinitionsFactoryConfig factoryConfig)
170: throws DefinitionsFactoryException {
171:
172: // Create configurable factory
173: DefinitionsFactory factory = createDefinitionFactoryInstance(factoryConfig
174: .getFactoryClassname());
175:
176: factory.init(factoryConfig, servletContext);
177:
178: // Make factory accessible from jsp tags (push it in appropriate context)
179: makeDefinitionsFactoryAccessible(factory, servletContext);
180: return factory;
181: }
182:
183: /**
184: * Create Definition factory of specified classname.
185: * Factory class must extend the {@link DefinitionsFactory} class.
186: * The factory is wrapped appropriately with {@link ComponentDefinitionsFactoryWrapper}
187: * if it is an instance of the deprecated ComponentDefinitionsFactory class.
188: * @param classname Class name of the factory to create.
189: * @return newly created factory.
190: * @throws DefinitionsFactoryException If an error occur while initializing factory
191: */
192: protected DefinitionsFactory createDefinitionFactoryInstance(
193: String classname) throws DefinitionsFactoryException {
194:
195: try {
196: Class factoryClass = RequestUtils
197: .applicationClass(classname);
198: Object factory = factoryClass.newInstance();
199:
200: // Backward compatibility : if factory classes implements old interface,
201: // provide appropriate wrapper
202: if (factory instanceof ComponentDefinitionsFactory) {
203: factory = new ComponentDefinitionsFactoryWrapper(
204: (ComponentDefinitionsFactory) factory);
205: }
206: return (DefinitionsFactory) factory;
207:
208: } catch (ClassCastException ex) { // Bad classname
209: throw new DefinitionsFactoryException(
210: "Error - createDefinitionsFactory : Factory class '"
211: + classname
212: + " must implement 'TilesDefinitionsFactory'.",
213: ex);
214:
215: } catch (ClassNotFoundException ex) { // Bad classname
216: throw new DefinitionsFactoryException(
217: "Error - createDefinitionsFactory : Bad class name '"
218: + classname + "'.", ex);
219:
220: } catch (InstantiationException ex) { // Bad constructor or error
221: throw new DefinitionsFactoryException(ex);
222:
223: } catch (IllegalAccessException ex) {
224: throw new DefinitionsFactoryException(ex);
225: }
226: }
227:
228: /**
229: * Make definition factory accessible to Tags.
230: * Factory is stored in servlet context.
231: * @param factory Factory to be made accessible.
232: * @param servletContext Current servlet context.
233: */
234: protected void makeDefinitionsFactoryAccessible(
235: DefinitionsFactory factory, ServletContext servletContext) {
236:
237: servletContext.setAttribute(DEFINITIONS_FACTORY, factory);
238: }
239:
240: }
|