001package org.apache.turbine.services.jsp.util;
002
003
004import org.apache.logging.log4j.LogManager;
005
006/*
007 * Licensed to the Apache Software Foundation (ASF) under one
008 * or more contributor license agreements.  See the NOTICE file
009 * distributed with this work for additional information
010 * regarding copyright ownership.  The ASF licenses this file
011 * to you under the Apache License, Version 2.0 (the
012 * "License"); you may not use this file except in compliance
013 * with the License.  You may obtain a copy of the License at
014 *
015 *   http://www.apache.org/licenses/LICENSE-2.0
016 *
017 * Unless required by applicable law or agreed to in writing,
018 * software distributed under the License is distributed on an
019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020 * KIND, either express or implied.  See the License for the
021 * specific language governing permissions and limitations
022 * under the License.
023 */
024
025
026import org.apache.logging.log4j.Logger;
027import org.apache.turbine.modules.Screen;
028import org.apache.turbine.modules.ScreenLoader;
029import org.apache.turbine.services.TurbineServices;
030import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
031import org.apache.turbine.services.template.TemplateService;
032import org.apache.turbine.util.RunData;
033
034/**
035 * Returns output of a Screen module. An instance of this is placed in the
036 * request by the JspLayout. This allows template authors to
037 * place the screen template within the layout.<br>
038 * Here's how it's used in a JSP template:<br>
039 * <code>
040 * &lt;%useBean id="screen_placeholder" class="JspScreenPlaceholder" scope="request" %&gt;
041 * ...
042 * &lt;%= screen_placeholder %&gt;
043 * </code>
044 *
045 * @author <a href="john.mcnally@clearink.com">John D. McNally</a>
046 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
047 * @version $Id$
048 */
049public class JspScreenPlaceholder
050{
051    /** Logging */
052    private static Logger log = LogManager.getLogger(JspNavigation.class);
053
054    /* The RunData object */
055    private final RunData data;
056
057    private final ScreenLoader screenLoader;
058
059    /**
060     * Constructor
061     *
062     * @param data A Rundata Object
063     */
064    public JspScreenPlaceholder(RunData data)
065    {
066        this.data = data;
067        AssemblerBrokerService abs = (AssemblerBrokerService)TurbineServices.getInstance()
068            .getService(AssemblerBrokerService.SERVICE_NAME);
069        this.screenLoader = (ScreenLoader)abs.getLoader(Screen.class);
070    }
071
072    /**
073     * builds the output of the navigation template
074     */
075    public void exec()
076    {
077        String template = null;
078        String module = null;
079        try
080        {
081            template = data.getTemplateInfo().getScreenTemplate();
082            TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
083            module = templateService.getScreenName(template);
084            screenLoader.exec(data, module);
085        }
086        catch (Exception e)
087        {
088            String message = "Error processing screen template:" +
089                    template + " using module: " + module;
090            log.error(message, e);
091            try
092            {
093                data.getResponse().getWriter().print(message);
094            }
095            catch (java.io.IOException ioe)
096            {
097                // ignore
098            }
099        }
100    }
101}