View Javadoc

1   package org.apache.turbine.modules.screens;
2   
3   
4   /*
5    * Copyright 2001-2004 The Apache Software Foundation.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License")
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.commons.lang.exception.ExceptionUtils;
23  
24  import org.apache.ecs.ConcreteElement;
25  
26  import org.apache.turbine.Turbine;
27  import org.apache.turbine.TurbineConstants;
28  import org.apache.turbine.pipeline.PipelineData;
29  import org.apache.turbine.services.template.TurbineTemplate;
30  import org.apache.turbine.services.velocity.TurbineVelocity;
31  import org.apache.turbine.util.RunData;
32  
33  import org.apache.velocity.context.Context;
34  
35  /***
36   * VelocityDirectScreen is a screen class which returns its output
37   * directly to the output stream. It can be used if buffering an
38   * output screen isn't possible or the result doesn't fit in the
39   * memory.
40   * <p>
41   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
42   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
44   * @version $Id: VelocityDirectScreen.java 222043 2004-12-06 17:47:33Z painter $
45   */
46  public class VelocityDirectScreen
47      extends VelocityScreen
48  {
49      /*** The prefix for lookup up screen pages */
50      private String prefix = TurbineConstants.SCREEN_PREFIX + "/";
51  
52      /***
53       * This builds the Velocity template.
54       *
55       * @deprecated Use PipelineData version instead.
56       * @param data Turbine information.
57       * @return A ConcreteElement.
58       * @exception Exception, a generic exception.
59       */
60      public ConcreteElement buildTemplate(RunData data)
61          throws Exception
62      {
63          Context context = TurbineVelocity.getContext(data);
64  
65          String screenTemplate = data.getTemplateInfo().getScreenTemplate();
66          String templateName
67              = TurbineTemplate.getScreenTemplateName(screenTemplate);
68  
69          // The Template Service could not find the Screen
70          if (StringUtils.isEmpty(templateName))
71          {
72              log.error("Screen " + screenTemplate + " not found!");
73              throw new Exception("Could not find screen for " + screenTemplate);
74          }
75  
76          try
77          {
78              TurbineVelocity.handleRequest(context,
79                                            prefix + templateName,
80                                            data.getOut());
81  
82          }
83          catch (Exception e)
84          {
85              // If there is an error, build a $processingException and
86              // attempt to call the error.vm template in the screens
87              // directory.
88              context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
89              context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
90  
91              templateName = Turbine.getConfiguration()
92                  .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
93                             TurbineConstants.TEMPLATE_ERROR_VM);
94  
95              TurbineVelocity.handleRequest(context,
96                      prefix + templateName,
97                      data.getOut());
98          }
99  
100         return null;
101     }
102     
103     /***
104      * This builds the Velocity template.
105      *
106      * @param data Turbine information.
107      * @return A ConcreteElement.
108      * @exception Exception, a generic exception.
109      */
110     public ConcreteElement buildTemplate(PipelineData pipelineData)
111         throws Exception
112     {
113             RunData data = (RunData) getRunData(pipelineData);
114             Context context = TurbineVelocity.getContext(pipelineData);
115 
116             String screenTemplate = data.getTemplateInfo().getScreenTemplate();
117             String templateName
118                 = TurbineTemplate.getScreenTemplateName(screenTemplate);
119 
120             // The Template Service could not find the Screen
121             if (StringUtils.isEmpty(templateName))
122             {
123                 log.error("Screen " + screenTemplate + " not found!");
124                 throw new Exception("Could not find screen for " + screenTemplate);
125             }
126 
127             try
128             {
129                 TurbineVelocity.handleRequest(context,
130                                               prefix + templateName,
131                                               data.getOut());
132 
133             }
134             catch (Exception e)
135             {
136                 // If there is an error, build a $processingException and
137                 // attempt to call the error.vm template in the screens
138                 // directory.
139                 context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
140                 context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
141 
142                 templateName = Turbine.getConfiguration()
143                     .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
144                                TurbineConstants.TEMPLATE_ERROR_VM);
145 
146                 TurbineVelocity.handleRequest(context,
147                         prefix + templateName,
148                         data.getOut());
149             }
150 
151             return null;
152     }
153 }