001    package 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    
024    import org.apache.ecs.ConcreteElement;
025    import org.apache.turbine.Turbine;
026    import org.apache.turbine.pipeline.PipelineData;
027    import org.apache.turbine.util.RunData;
028    
029    /**
030     * The purpose of this class is to allow one to load and execute
031     * Screen modules.
032     *
033     * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
034     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
035     * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
036     * @version $Id: ScreenLoader.java 1078552 2011-03-06 19:58:46Z tv $
037     */
038    public class ScreenLoader
039        extends GenericLoader<Screen>
040        implements Loader<Screen>
041    {
042        /** The single instance of this class. */
043        private static ScreenLoader instance = new ScreenLoader();
044    
045        /**
046         * These ctor's are private to force clients to use getInstance()
047         * to access this class.
048         */
049        private ScreenLoader()
050        {
051            super();
052        }
053    
054        /**
055         * Attempts to load and execute the external Screen. This is used
056         * when you want to execute a Screen which returns its output via
057         * a MultiPartElement instead of out the data.getPage() value.
058         * This allows you to easily chain the execution of Screen modules
059         * together.
060         *
061         * @deprecated Use PipelineData version instead.
062         * @param data Turbine information.
063         * @param name Name of object that will execute the screen.
064         * @exception Exception a generic exception.
065         */
066        @Deprecated
067        public ConcreteElement eval(RunData data, String name)
068                throws Exception
069        {
070            // Execute screen
071            return getAssembler(name).build(data);
072        }
073    
074        /**
075         * Attempts to load and execute the external Screen. This is used
076         * when you want to execute a Screen which returns its output via
077         * a MultiPartElement instead of out the data.getPage() value.
078         * This allows you to easily chain the execution of Screen modules
079         * together.
080         *
081         * @param data Turbine information.
082         * @param name Name of object that will execute the screen.
083         * @exception Exception a generic exception.
084         */
085        public ConcreteElement eval(PipelineData pipelineData, String name)
086                throws Exception
087        {
088            // Execute screen
089            return getAssembler(name).build(pipelineData);
090        }
091    
092        /**
093         * Attempts to load and execute the Screen. This is used when you
094         * want to execute a Screen which returns its output via the
095         * data.getPage() object.
096         * @deprecated Use PipelineData version instead.
097         * @param data Turbine information.
098         * @param name Name of object that will execute the screen.
099         * @exception Exception a generic exception.
100         */
101        @Deprecated
102        @Override
103        public void exec(RunData data, String name)
104                throws Exception
105        {
106            this.eval(data, name);
107        }
108    
109        /**
110         * Attempts to load and execute the Screen. This is used when you
111         * want to execute a Screen which returns its output via the
112         * data.getPage() object.
113         *
114         * @param data Turbine information.
115         * @param name Name of object that will execute the screen.
116         * @exception Exception a generic exception.
117         */
118        @Override
119        public void exec(PipelineData pipelineData, String name)
120            throws Exception
121            {
122            this.eval(pipelineData, name);
123            }
124    
125        /**
126         * Pulls out an instance of the object by name.  Name is just the
127         * single name of the object. This is equal to getInstance but
128         * returns an Assembler object and is needed to fulfil the Loader
129         * interface.
130         *
131         * @param name Name of object instance.
132         * @return A Screen with the specified name, or null.
133         * @exception Exception a generic exception.
134         */
135        public Screen getAssembler(String name)
136            throws Exception
137        {
138            return getAssembler(Screen.NAME, name);
139        }
140    
141        /**
142         * @see org.apache.turbine.modules.Loader#getCacheSize()
143         */
144        public int getCacheSize()
145        {
146            return ScreenLoader.getConfiguredCacheSize();
147        }
148    
149        /**
150         * The method through which this class is accessed.
151         *
152         * @return The single instance of this class.
153         */
154        public static ScreenLoader getInstance()
155        {
156            return instance;
157        }
158    
159        /**
160         * Helper method to get the configured cache size for this module
161         *
162         * @return the configure cache size
163         */
164        private static int getConfiguredCacheSize()
165        {
166            return Turbine.getConfiguration().getInt(Screen.CACHE_SIZE_KEY,
167                    Screen.CACHE_SIZE_DEFAULT);
168        }
169    }