1 package org.apache.turbine.modules.screens;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.configuration.Configuration;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.lang.exception.ExceptionUtils;
25 import org.apache.turbine.TurbineConstants;
26 import org.apache.turbine.annotation.TurbineConfiguration;
27 import org.apache.turbine.annotation.TurbineService;
28 import org.apache.turbine.pipeline.PipelineData;
29 import org.apache.turbine.services.template.TemplateService;
30 import org.apache.turbine.services.velocity.VelocityService;
31 import org.apache.turbine.util.RunData;
32 import org.apache.velocity.context.Context;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class VelocityScreen
50 extends TemplateScreen
51 {
52
53 private final String prefix = getPrefix() + "/";
54
55
56 @TurbineService
57 protected VelocityService velocity;
58
59
60 @TurbineService
61 protected TemplateService templateService;
62
63
64 @TurbineConfiguration
65 protected Configuration conf;
66
67
68
69
70
71
72
73
74
75
76 protected void doBuildTemplate(PipelineData pipelineData,
77 Context context)
78 throws Exception
79 {
80
81 }
82
83
84
85
86
87
88
89
90
91 @Override
92 protected void doBuildTemplate(PipelineData pipelineData)
93 throws Exception
94 {
95 doBuildTemplate(pipelineData, velocity.getContext(pipelineData));
96 }
97
98
99
100
101
102
103
104
105 @Override
106 public String buildTemplate(PipelineData pipelineData)
107 throws Exception
108 {
109 RunData data = getRunData(pipelineData);
110 String screenData = null;
111
112 Context context = velocity.getContext(pipelineData);
113
114 String screenTemplate = data.getTemplateInfo().getScreenTemplate();
115 String templateName
116 = templateService.getScreenTemplateName(screenTemplate);
117
118
119 if (StringUtils.isEmpty(templateName))
120 {
121 log.error("Screen " + screenTemplate + " not found!");
122 throw new Exception("Could not find screen for " + screenTemplate);
123 }
124
125 try
126 {
127
128
129 if (getLayout(pipelineData) == null)
130 {
131 velocity.handleRequest(context,
132 prefix + templateName,
133 data.getResponse().getOutputStream());
134 }
135 else
136 {
137 screenData =
138 velocity.handleRequest(context, prefix + templateName);
139 }
140 }
141 catch (Exception e)
142 {
143
144
145
146 context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
147 context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
148
149 templateName = conf.getString(TurbineConstants.TEMPLATE_ERROR_KEY,
150 TurbineConstants.TEMPLATE_ERROR_VM);
151
152 screenData = velocity.handleRequest(context, prefix + templateName);
153 }
154
155 return screenData;
156 }
157 }