View Javadoc

1   package org.apache.turbine.modules.layouts;
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.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.turbine.TurbineConstants;
25  import org.apache.turbine.annotation.TurbineService;
26  import org.apache.turbine.modules.Layout;
27  import org.apache.turbine.pipeline.PipelineData;
28  import org.apache.turbine.services.velocity.VelocityService;
29  import org.apache.turbine.util.RunData;
30  import org.apache.turbine.util.template.TemplateNavigation;
31  import org.apache.turbine.util.template.TemplateScreen;
32  import org.apache.velocity.context.Context;
33  
34  /**
35   * This Layout module is Turbine 2.3.3 VelocityDirectLayout (same package)
36   * with methods added for {@link PipelineData}. It is used in Jetspeed-1 portal.
37   *
38   * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
39   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
40   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
41   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
42   * @version $Id$
43   */
44  public class VelocityCachedLayout
45      extends Layout
46  {
47      /** Logging */
48      private static Log log = LogFactory.getLog(VelocityCachedLayout.class);
49  
50      /** The prefix for lookup up layout pages */
51      private String prefix = getPrefix() + "/";
52  
53      /** Injected service instance */
54      @TurbineService
55      private VelocityService velocityService;
56  
57      /**
58       * Method called by LayoutLoader.
59       *
60       *
61       * @param pipelineData PipelineData
62       * @throws Exception generic exception
63       */
64      @Override
65      public void doBuild(PipelineData pipelineData)
66          throws Exception
67      {
68          RunData data = getRunData(pipelineData);
69          // Get the context needed by Velocity.
70          Context context = velocityService.getContext(pipelineData);
71  
72          // variable for the screen in the layout template
73          context.put(TurbineConstants.SCREEN_PLACEHOLDER,
74                      new TemplateScreen(data));
75  
76          // variable to reference the navigation screen in the layout template
77          context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
78                      new TemplateNavigation(data));
79  
80          // Grab the layout template set in the VelocityPage.
81          // If null, then use the default layout template
82          // (done by the TemplateInfo object)
83          String templateName = data.getTemplateInfo().getLayoutTemplate();
84  
85          // Set the locale and content type
86          data.getResponse().setLocale(data.getLocale());
87          data.getResponse().setContentType(data.getContentType());
88  
89          log.debug("Now trying to render layout " + templateName);
90  
91          // Finally, generate the layout template and send it to the browser
92          velocityService.handleRequest(context,
93                  prefix + templateName, data.getOut());
94      }
95  }
96