    001package org.apache.turbine.util.velocity;
    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.fulcrum.parser.ParameterParser;
    025import org.apache.turbine.Turbine;
    026import org.apache.turbine.annotation.TurbineService;
    027import org.apache.turbine.modules.ActionEvent;
    028import org.apache.turbine.pipeline.PipelineData;
    029import org.apache.turbine.services.velocity.VelocityService;
    030import org.apache.velocity.context.Context;
    031
    032/**
    033 * If you are using VelocitySite stuff, then your Action's should
    034 * extend this class instead of extending the ActionEvent class.  The
    035 * difference between this class and the ActionEvent class is that
    036 * this class will first attempt to execute one of your doMethod's
    037 * with a constructor like this:
    038 *
    039 * <p><code>doEvent(RunData data, Context context)</code></p>
    040 *
    041 * <p>It gets the context from the TemplateInfo.getTemplateContext()
    042 * method. If it can't find a method like that, then it will try to
    043 * execute the method without the Context in it.</p>
    044 *
    045 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    046 * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
    047 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    048 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    049 * @version $Id: VelocityActionEvent.java 1773378 2016-12-09 13:19:59Z tv $
    050 */
    051public abstract class VelocityActionEvent extends ActionEvent
    052{
    053    /** Injected velocity service */
    054    @TurbineService
    055    protected VelocityService velocity;
    056
    057    /** Indicates whether or not this module has been initialized. */
    058    protected boolean initialized = false;
    059
    060    /**
    061     * Provides a means of initializing the module.
    062     *
    063     * @throws Exception a generic exception.
    064     */
    065    protected abstract void initialize()
    066        throws Exception;
    067
    068    /**
    069     * This overrides the default Action.doPerform() to execute the
    070     * doEvent() method.  If that fails, then it will execute the
    071     * doPerform() method instead.
    072     *
    073     * @param pipelineData A Turbine RunData object.
    074     * @throws Exception a generic exception.
    075     */
    076    @Override
    077    public void doPerform(PipelineData pipelineData)
    078            throws Exception
    079    {
    080        if (!initialized)
    081        {
    082            initialize();
    083        }
    084
    085        ParameterParser pp = pipelineData.get(Turbine.class, ParameterParser.class);
    086        Context context = velocity.getContext(pipelineData);
    087        executeEvents(pp, new Class<?>[]{ PipelineData.class, Context.class },
    088                new Object[]{ pipelineData, context });
    089    }
    090}