View Javadoc

1   package org.apache.turbine.modules.screens;
2   
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  import org.apache.commons.lang.StringUtils;
23  import org.apache.commons.lang.exception.ExceptionUtils;
24  
25  import org.apache.ecs.ConcreteElement;
26  
27  import org.apache.velocity.context.Context;
28  
29  import org.apache.turbine.Turbine;
30  import org.apache.turbine.TurbineConstants;
31  import org.apache.turbine.services.template.TurbineTemplate;
32  import org.apache.turbine.services.velocity.TurbineVelocity;
33  import org.apache.turbine.util.RunData;
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   * @version $Id: VelocityDirectScreen.java 534527 2007-05-02 16:10:59Z tv $
44   */
45  public class VelocityDirectScreen
46      extends VelocityScreen
47  {
48      /*** The prefix for lookup up screen pages */
49      private String prefix = TurbineConstants.SCREEN_PREFIX + "/";
50  
51      /***
52       * This builds the Velocity template.
53       *
54       * @param data Turbine information.
55       * @return A ConcreteElement.
56       * @exception Exception, a generic exception.
57       */
58      public ConcreteElement buildTemplate(RunData data)
59          throws Exception
60      {
61          Context context = TurbineVelocity.getContext(data);
62  
63          String screenTemplate = data.getTemplateInfo().getScreenTemplate();
64          String templateName
65              = TurbineTemplate.getScreenTemplateName(screenTemplate);
66  
67          // The Template Service could not find the Screen
68          if (StringUtils.isEmpty(templateName))
69          {
70              log.error("Screen " + screenTemplate + " not found!");
71              throw new Exception("Could not find screen for " + screenTemplate);
72          }
73  
74          try
75          {
76              TurbineVelocity.handleRequest(context,
77                                            prefix + templateName,
78                                            data.getOut());
79  
80          }
81          catch (Exception e)
82          {
83              // If there is an error, build a $processingException and
84              // attempt to call the error.vm template in the screens
85              // directory.
86              context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
87              context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
88  
89              templateName = Turbine.getConfiguration()
90                  .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
91                             TurbineConstants.TEMPLATE_ERROR_VM);
92  
93              TurbineVelocity.handleRequest(context,
94                      prefix + templateName,
95                      data.getOut());
96          }
97  
98          return null;
99      }
100 }