This project has retired. For details please refer to its
Attic page.
AbstractDefaultToStringRenderable xref
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.tiles.velocity.template;
23
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.util.Map;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.tiles.velocity.TilesVelocityException;
32 import org.apache.velocity.context.Context;
33 import org.apache.velocity.exception.MethodInvocationException;
34 import org.apache.velocity.exception.ParseErrorException;
35 import org.apache.velocity.exception.ResourceNotFoundException;
36 import org.apache.velocity.runtime.Renderable;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /***
41 * Renderable that provides a default implementation of {@link Renderable#toString()}
42 * and allows access to parameters and context objects.
43 *
44 * @version $Rev: 791161 $ $Date: 2009-07-04 20:53:36 +0200 (sab, 04 lug 2009) $
45 * @since 2.2.0
46 */
47 public abstract class AbstractDefaultToStringRenderable implements Renderable {
48
49
50 /***
51 * The Velocity context.
52 *
53 * @since 2.2.0
54 */
55 protected final Context velocityContext;
56
57 /***
58 * The parameters used in the current tool call.
59 *
60 * @since 2.2.0
61 */
62 protected final Map<String, Object> params;
63
64 /***
65 * The HTTP response.
66 *
67 * @since 2.2.0
68 */
69 protected final HttpServletResponse response;
70
71 /***
72 * The HTTP request.
73 *
74 * @since 2.2.0
75 */
76 protected final HttpServletRequest request;
77
78 /***
79 * The logging object.
80 */
81 private final Logger log = LoggerFactory.getLogger(getClass());
82
83 /***
84 * Constructor.
85 *
86 * @param velocityContext The Velocity context.
87 * @param params The parameters used in the current tool call.
88 * @param response The HTTP response.
89 * @param request The HTTP request.
90 * @since 2.2.0
91 */
92 public AbstractDefaultToStringRenderable(Context velocityContext,
93 Map<String, Object> params, HttpServletResponse response,
94 HttpServletRequest request) {
95 this.velocityContext = velocityContext;
96 this.params = params;
97 this.response = response;
98 this.request = request;
99 }
100
101 /*** {@inheritDoc} */
102 @Override
103 public String toString() {
104 StringWriter writer = new StringWriter();
105 try {
106 render(null, writer);
107 } catch (MethodInvocationException e) {
108 throw new TilesVelocityException("Cannot invoke method when rendering", e);
109 } catch (ParseErrorException e) {
110 throw new TilesVelocityException("Cannot parse when rendering", e);
111 } catch (ResourceNotFoundException e) {
112 throw new TilesVelocityException("Cannot find resource when rendering", e);
113 } catch (IOException e) {
114 throw new TilesVelocityException("I/O exception when rendering", e);
115 } finally {
116 try {
117 writer.close();
118 } catch (IOException e) {
119 log.error("Error when closing a StringWriter, the impossible happened!", e);
120 }
121 }
122 return writer.toString();
123 }
124 }