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