1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.tiles;
23
24 import java.io.IOException;
25 import java.io.Serializable;
26
27 import java.lang.reflect.Method;
28 import java.lang.reflect.InvocationTargetException;
29
30 import javax.servlet.ServletContext;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import javax.servlet.jsp.PageContext;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper;
40 import org.apache.struts.util.RequestUtils;
41
42
43
44
45
46
47 public class TilesUtilImpl implements Serializable {
48
49
50 protected static final Log log = LogFactory.getLog(TilesUtil.class);
51
52
53 public static final String DEFINITIONS_FACTORY =
54 "org.apache.struts.tiles.DEFINITIONS_FACTORY";
55
56
57
58
59 private static Method include = null;
60
61
62
63
64
65 static {
66
67 try {
68
69 Class[] args = new Class[]{String.class, boolean.class};
70 include = PageContext.class.getMethod("include", args);
71 } catch (NoSuchMethodException e) {
72 log.debug("Could not find JSP 2.0 include method. Using old one that doesn't support " +
73 "configurable flushing.", e);
74 }
75 }
76
77
78
79
80
81
82
83
84
85 public void doForward(
86 String uri,
87 HttpServletRequest request,
88 HttpServletResponse response,
89 ServletContext servletContext)
90 throws IOException, ServletException {
91
92 request.getRequestDispatcher(uri).forward(request, response);
93 }
94
95
96
97
98
99
100
101
102
103
104
105 public void doInclude(
106 String uri,
107 HttpServletRequest request,
108 HttpServletResponse response,
109 ServletContext servletContext)
110 throws IOException, ServletException {
111
112 request.getRequestDispatcher(uri).include(request, response);
113 }
114
115
116
117
118
119
120
121
122
123
124 public void doInclude(String uri, PageContext pageContext, boolean flush)
125 throws IOException, ServletException {
126 try {
127
128 if (include != null) {
129 include.invoke(pageContext, new Object[]{uri, Boolean.valueOf(flush)});
130 return;
131 }
132 } catch (IllegalAccessException e) {
133 log.debug("Could not find JSP 2.0 include method. Using old one.", e);
134 } catch (InvocationTargetException e) {
135 if (e.getCause() instanceof ServletException){
136 throw ((ServletException)e.getCause());
137 } else if (e.getCause() instanceof IOException){
138 throw ((IOException)e.getCause());
139 } else {
140 throw new ServletException(e);
141 }
142 }
143
144 pageContext.include(uri);
145 }
146
147
148
149
150
151 public DefinitionsFactory getDefinitionsFactory(
152 ServletRequest request,
153 ServletContext servletContext) {
154
155 return (DefinitionsFactory) servletContext.getAttribute(DEFINITIONS_FACTORY);
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 public DefinitionsFactory createDefinitionsFactory(
173 ServletContext servletContext,
174 DefinitionsFactoryConfig factoryConfig)
175 throws DefinitionsFactoryException {
176
177
178 DefinitionsFactory factory =
179 createDefinitionFactoryInstance(factoryConfig.getFactoryClassname());
180
181 factory.init(factoryConfig, servletContext);
182
183
184 makeDefinitionsFactoryAccessible(factory, servletContext);
185 return factory;
186 }
187
188
189
190
191
192
193
194
195
196
197 protected DefinitionsFactory createDefinitionFactoryInstance(String classname)
198 throws DefinitionsFactoryException {
199
200 try {
201 Class factoryClass = RequestUtils.applicationClass(classname);
202 Object factory = factoryClass.newInstance();
203
204
205
206 if (factory instanceof ComponentDefinitionsFactory) {
207 factory =
208 new ComponentDefinitionsFactoryWrapper(
209 (ComponentDefinitionsFactory) factory);
210 }
211 return (DefinitionsFactory) factory;
212
213 } catch (ClassCastException ex) {
214 throw new DefinitionsFactoryException(
215 "Error - createDefinitionsFactory : Factory class '"
216 + classname
217 + " must implement 'TilesDefinitionsFactory'.",
218 ex);
219
220 } catch (ClassNotFoundException ex) {
221 throw new DefinitionsFactoryException(
222 "Error - createDefinitionsFactory : Bad class name '"
223 + classname
224 + "'.",
225 ex);
226
227 } catch (InstantiationException ex) {
228 throw new DefinitionsFactoryException(ex);
229
230 } catch (IllegalAccessException ex) {
231 throw new DefinitionsFactoryException(ex);
232 }
233 }
234
235
236
237
238
239
240
241 protected void makeDefinitionsFactoryAccessible(
242 DefinitionsFactory factory,
243 ServletContext servletContext) {
244
245 servletContext.setAttribute(DEFINITIONS_FACTORY, factory);
246 }
247
248 }