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.assemblerbroker.AssemblerBrokerService;
029import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
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 = TurbineAssemblerBroker.getService();
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    }
062
063    /**
064     * Attempts to load and execute the external action that has been
065     * set.
066     * @param pipelineData the Turbine request
067     * @param name the name of the assembler module
068     * @exception Exception a generic exception.
069     */
070    public abstract void exec(PipelineData pipelineData, String name)
071            throws Exception;
072
073    /**
074     * Returns whether or not this external action is reload itself.
075     * This is in cases where the Next button would be clicked, but
076     * since we are checking for that, we would go into an endless
077     * loop.
078     *
079     * @return True if the action is reload.
080     */
081    public boolean reload()
082    {
083        return this.reload;
084    }
085
086    /**
087     * Sets whether or not this external action is reload itself.
088     * This is in cases where the Next button would be clicked, but
089     * since we are checking for that, we would go into an endless
090     * loop.
091     *
092     * @param reload True if the action must be marked as reload.
093     * @return Itself.
094     */
095    public GenericLoader<T> setReload(boolean reload)
096    {
097        this.reload = reload;
098        return this;
099    }
100
101    /**
102     * Gets the base package where Turbine should find its default
103     * modules.
104     *
105     * @return A String with the base package name.
106     */
107    public static String getBasePackage()
108    {
109        return TURBINE_PACKAGE;
110    }
111
112    /**
113     * Gets the package list where Turbine should find its
114     * modules.
115     *
116     * @return A List with the package names (including the base package).
117     */
118    public static List<String> getPackages()
119    {
120        if (TURBINE_PACKAGES == null)
121        {
122            List<String> turbinePackages = new ArrayList<String>();
123            List<Object> configTurbinePackages =
124                Turbine.getConfiguration()
125                        .getList(TurbineConstants.MODULE_PACKAGES);
126            for (Object o : configTurbinePackages)
127            {
128                turbinePackages.add((String)o);
129            }
130
131            TURBINE_PACKAGES = turbinePackages;
132        }
133
134        List<String> packages = TURBINE_PACKAGES;
135
136        if (!packages.contains(TURBINE_PACKAGE))
137        {
138            packages.add(TURBINE_PACKAGE);
139        }
140
141        return packages;
142    }
143
144    /**
145     * Pulls out an instance of the object by name.  Name is just the
146     * single name of the object.
147     *
148     * @param type Type of the assembler.
149     * @param name Name of object instance.
150     * @return A Screen with the specified name, or null.
151     * @exception Exception a generic exception.
152     */
153    protected T getAssembler(Class<T> type, String name)
154        throws Exception
155    {
156        T asm = null;
157
158        try
159        {
160            if (ab != null)
161            {
162                // Attempt to load the assembler
163                asm = ab.getAssembler(type, name);
164            }
165        }
166        catch (ClassCastException cce)
167        {
168            // This can alternatively let this exception be thrown
169            // So that the ClassCastException is shown in the
170            // browser window.  Like this it shows "Screen not Found"
171            asm = null;
172        }
173
174        if (asm == null)
175        {
176            // If we did not find a screen we should try and give
177            // the user a reason for that...
178            // FIX ME: The AssemblerFactories should each add it's
179            // own string here...
180            List<String> packages = GenericLoader.getPackages();
181
182            throw new ClassNotFoundException(
183                    "\n\n\tRequested " + type + " not found: " + name +
184                    "\n\tTurbine looked in the following " +
185                    "modules.packages path: \n\t" + packages.toString() + "\n");
186        }
187
188        return asm;
189    }
190}