001package org.apache.turbine.modules;
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 java.util.List;
023import java.util.function.IntSupplier;
024import java.util.stream.Collectors;
025
026import org.apache.turbine.Turbine;
027import org.apache.turbine.TurbineConstants;
028import org.apache.turbine.pipeline.PipelineData;
029import org.apache.turbine.services.TurbineServices;
030import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
031
032/**
033 * This is the base class for the loaders. It contains code that is
034 * used across all of the loaders. It also specifies the interface
035 * that is required to be called a Loader.
036 *
037 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
038 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
039 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
040 * @param <T> the specialized assembler type
041 */
042public abstract class GenericLoader<T extends Assembler> implements Loader<T>
043{
044    /** The Assembler Broker Service */
045    protected AssemblerBrokerService ab;
046
047    /** Class of loaded assembler */
048    private final Class<T> assemblerClass;
049
050    /** Supplier of configured cache for this assembler Class */
051    private final IntSupplier cacheSizeSupplier;
052
053    /** @serial This can be serialized */
054    private boolean reload = false;
055
056    /** Base packages path for Turbine */
057    private static final String TURBINE_PACKAGE = "org.apache.turbine.modules";
058
059    /** Packages paths for Turbine */
060    private static List<String> TURBINE_PACKAGES = null;
061
062    /**
063     * Basic constructor for creating a loader.
064     *
065     * @param assemblerClass Class of loaded assembler
066     * @param cacheSizeSupplier Supplier of configured cache size for this assembler Class
067     */
068    public GenericLoader(Class<T> assemblerClass, IntSupplier cacheSizeSupplier)
069    {
070        super();
071        this.assemblerClass = assemblerClass;
072        this.cacheSizeSupplier = cacheSizeSupplier;
073        this.ab = (AssemblerBrokerService)TurbineServices.getInstance()
074                .getService(AssemblerBrokerService.SERVICE_NAME);
075    }
076
077    /**
078     * Attempts to load and execute the external action that has been
079     * set.
080     * @param pipelineData the Turbine request
081     * @param name the name of the assembler module
082     * @throws Exception a generic exception.
083     */
084    public abstract void exec(PipelineData pipelineData, String name)
085            throws Exception;
086
087    /**
088     * Returns whether or not this external action is reload itself.
089     * This is in cases where the Next button would be clicked, but
090     * since we are checking for that, we would go into an endless
091     * loop.
092     *
093     * @return True if the action is reload.
094     */
095    public boolean reload()
096    {
097        return this.reload;
098    }
099
100    /**
101     * Sets whether or not this external action is reload itself.
102     * This is in cases where the Next button would be clicked, but
103     * since we are checking for that, we would go into an endless
104     * loop.
105     *
106     * @param reload True if the action must be marked as reload.
107     * @return Itself.
108     */
109    public GenericLoader<T> setReload(boolean reload)
110    {
111        this.reload = reload;
112        return this;
113    }
114
115    /**
116     * Gets the base package where Turbine should find its default
117     * modules.
118     *
119     * @return A String with the base package name.
120     */
121    public static String getBasePackage()
122    {
123        return TURBINE_PACKAGE;
124    }
125
126    /**
127     * Gets the package list where Turbine should find its
128     * modules.
129     *
130     * @return A List with the package names (including the base package).
131     */
132    public static List<String> getPackages()
133    {
134        if (TURBINE_PACKAGES == null)
135        {
136            List<String> turbinePackages = Turbine.getConfiguration()
137            .getList(TurbineConstants.MODULE_PACKAGES).stream().map( o -> (String) o ).collect( Collectors.toList() );
138
139            TURBINE_PACKAGES = turbinePackages;
140        }
141
142        List<String> packages = TURBINE_PACKAGES;
143
144        if (!packages.contains(TURBINE_PACKAGE))
145        {
146            packages.add(TURBINE_PACKAGE);
147        }
148
149        return packages;
150    }
151
152    /**
153     * Pulls out an instance of the object by name.  Name is just the
154     * single name of the object.
155     *
156     * @param name Name of object instance.
157     * @return An Action with the specified name, or null.
158     * @throws Exception a generic exception.
159     */
160    @Override
161    public T getAssembler(String name)
162        throws Exception
163    {
164        return getAssembler(assemblerClass, name);
165    }
166
167    /**
168     * Pulls out an instance of the object by name.  Name is just the
169     * single name of the object.
170     *
171     * @param type Type of the assembler.
172     * @param name Name of object instance.
173     * @return A Screen with the specified name, or null.
174     * @throws Exception a generic exception.
175     */
176    protected T getAssembler(Class<T> type, String name)
177        throws Exception
178    {
179        T asm = null;
180
181        try
182        {
183            if (ab != null)
184            {
185                // Attempt to load the assembler
186                asm = ab.getAssembler(type, name);
187            }
188        }
189        catch (ClassCastException cce)
190        {
191            // This can alternatively let this exception be thrown
192            // So that the ClassCastException is shown in the
193            // browser window.  Like this it shows "Screen not Found"
194            asm = null;
195        }
196
197        if (asm == null)
198        {
199            // If we did not find a screen we should try and give
200            // the user a reason for that...
201            // FIX ME: The AssemblerFactories should each add it's
202            // own string here...
203            List<String> packages = GenericLoader.getPackages();
204
205            throw new ClassNotFoundException(
206                    "\n\n\tRequested " + type + " not found: " + name +
207                    "\n\tTurbine looked in the following " +
208                    "modules.packages path: \n\t" + packages.toString() + "\n");
209        }
210
211        return asm;
212    }
213
214    /**
215     * @see org.apache.turbine.modules.Loader#getCacheSize()
216     */
217    @Override
218    public int getCacheSize()
219    {
220        return cacheSizeSupplier.getAsInt();
221    }
222}