001package org.apache.turbine.modules;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import org.apache.turbine.Turbine;
025import org.apache.turbine.pipeline.PipelineData;
026
027/**
028 * The purpose of this class is to allow one to load and execute
029 * Screen modules.
030 *
031 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
032 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
033 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
034 * @version $Id: ScreenLoader.java 1709648 2015-10-20 17:08:10Z tv $
035 */
036public class ScreenLoader
037    extends GenericLoader<Screen>
038    implements Loader<Screen>
039{
040    /** The single instance of this class. */
041    private static ScreenLoader instance = new ScreenLoader();
042
043    /**
044     * These ctor's are private to force clients to use getInstance()
045     * to access this class.
046     */
047    private ScreenLoader()
048    {
049        super();
050    }
051
052    /**
053     * Attempts to load and execute the external Screen. This is used
054     * when you want to execute a Screen which returns its output via
055     * a String instead of out the data.getResponse() value.
056     * This allows you to easily chain the execution of Screen modules
057     * together.
058     *
059     * @param pipelineData Turbine information.
060     * @param name Name of object that will execute the screen.
061     * @return the output of the screen module
062     * @exception Exception a generic exception.
063     */
064    public String eval(PipelineData pipelineData, String name)
065            throws Exception
066    {
067        // Execute screen
068        return getAssembler(name).build(pipelineData);
069    }
070
071    /**
072     * Attempts to load and execute the Screen. This is used when you
073     * want to execute a Screen which returns its output via the
074     * data.getResponse() object.
075     *
076     * @param pipelineData Turbine information.
077     * @param name Name of object that will execute the screen.
078     * @exception Exception a generic exception.
079     */
080    @Override
081    public void exec(PipelineData pipelineData, String name)
082        throws Exception
083        {
084        this.eval(pipelineData, name);
085        }
086
087    /**
088     * Pulls out an instance of the object by name.  Name is just the
089     * single name of the object.
090     *
091     * @param name Name of object instance.
092     * @return A Screen with the specified name, or null.
093     * @exception Exception a generic exception.
094     */
095    @Override
096    public Screen getAssembler(String name)
097        throws Exception
098    {
099        return getAssembler(Screen.class, name);
100    }
101
102    /**
103     * @see org.apache.turbine.modules.Loader#getCacheSize()
104     */
105    @Override
106    public int getCacheSize()
107    {
108        return ScreenLoader.getConfiguredCacheSize();
109    }
110
111    /**
112     * The method through which this class is accessed.
113     *
114     * @return The single instance of this class.
115     */
116    public static ScreenLoader getInstance()
117    {
118        return instance;
119    }
120
121    /**
122     * Helper method to get the configured cache size for this module
123     *
124     * @return the configure cache size
125     */
126    private static int getConfiguredCacheSize()
127    {
128        return Turbine.getConfiguration().getInt(Screen.CACHE_SIZE_KEY,
129                Screen.CACHE_SIZE_DEFAULT);
130    }
131}