001package org.apache.turbine.modules.layouts;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.apache.turbine.TurbineConstants;
025import org.apache.turbine.annotation.TurbineService;
026import org.apache.turbine.modules.Layout;
027import org.apache.turbine.pipeline.PipelineData;
028import org.apache.turbine.services.velocity.VelocityService;
029import org.apache.turbine.util.RunData;
030import org.apache.turbine.util.template.TemplateNavigation;
031import org.apache.turbine.util.template.TemplateScreen;
032import org.apache.velocity.context.Context;
033
034/**
035 * This Layout module is Turbine 2.3.3 VelocityDirectLayout (same package)
036 * with methods added for {@link PipelineData}. It is used in Jetspeed-1 portal.
037 *
038 * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
039 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
040 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
041 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
042 * @version $Id$
043 */
044public class VelocityCachedLayout
045    extends Layout
046{
047    /** Logging */
048    private static Log log = LogFactory.getLog(VelocityCachedLayout.class);
049
050    /** The prefix for lookup up layout pages */
051    private String prefix = getPrefix() + "/";
052
053    /** Injected service instance */
054    @TurbineService
055    private VelocityService velocityService;
056
057    /**
058     * Method called by LayoutLoader.
059     *
060     *
061     * @param pipelineData PipelineData
062     * @throws Exception generic exception
063     */
064    @Override
065    public void doBuild(PipelineData pipelineData)
066        throws Exception
067    {
068        RunData data = getRunData(pipelineData);
069        // Get the context needed by Velocity.
070        Context context = velocityService.getContext(pipelineData);
071
072        // variable for the screen in the layout template
073        context.put(TurbineConstants.SCREEN_PLACEHOLDER,
074                    new TemplateScreen(data));
075
076        // variable to reference the navigation screen in the layout template
077        context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
078                    new TemplateNavigation(data));
079
080        // Grab the layout template set in the VelocityPage.
081        // If null, then use the default layout template
082        // (done by the TemplateInfo object)
083        String templateName = data.getTemplateInfo().getLayoutTemplate();
084
085        // Set the locale and content type
086        data.getResponse().setLocale(data.getLocale());
087        data.getResponse().setContentType(data.getContentType());
088
089        log.debug("Now trying to render layout " + templateName);
090
091        // Finally, generate the layout template and send it to the browser
092        velocityService.handleRequest(context,
093                prefix + templateName, data.getOut());
094    }
095}
096