Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractDefaultToStringRenderable |
|
| 5.5;5.5 |
1 | /* | |
2 | * $Id: AbstractDefaultToStringRenderable.java 791161 2009-07-04 18:53:36Z apetrelli $ | |
3 | * | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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-05 04:53:36 +1000 (Sun, 05 Jul 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 | 5 | 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 | 5 | HttpServletRequest request) { |
95 | 5 | this.velocityContext = velocityContext; |
96 | 5 | this.params = params; |
97 | 5 | this.response = response; |
98 | 5 | this.request = request; |
99 | 5 | } |
100 | ||
101 | /** {@inheritDoc} */ | |
102 | @Override | |
103 | public String toString() { | |
104 | 1 | StringWriter writer = new StringWriter(); |
105 | try { | |
106 | 1 | render(null, writer); |
107 | 0 | } catch (MethodInvocationException e) { |
108 | 0 | throw new TilesVelocityException("Cannot invoke method when rendering", e); |
109 | 0 | } catch (ParseErrorException e) { |
110 | 0 | throw new TilesVelocityException("Cannot parse when rendering", e); |
111 | 0 | } catch (ResourceNotFoundException e) { |
112 | 0 | throw new TilesVelocityException("Cannot find resource when rendering", e); |
113 | 0 | } catch (IOException e) { |
114 | 0 | throw new TilesVelocityException("I/O exception when rendering", e); |
115 | } finally { | |
116 | 0 | try { |
117 | 1 | writer.close(); |
118 | 0 | } catch (IOException e) { |
119 | 0 | log.error("Error when closing a StringWriter, the impossible happened!", e); |
120 | 1 | } |
121 | 0 | } |
122 | 1 | return writer.toString(); |
123 | } | |
124 | } |