001    /*
002     * Copyright 1999-2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */ 
016    
017    package org.apache.commons.workflow.core;
018    
019    
020    import java.util.EmptyStackException;
021    import org.apache.commons.workflow.Activity;
022    import org.apache.commons.workflow.Context;
023    import org.apache.commons.workflow.StepException;
024    import org.apache.commons.workflow.base.BaseStep;
025    
026    
027    /**
028     * <p>Pop the top value from the evaluation stack, which must be an
029     * <code>Activity</code>, and initiate a "subroutine call" to execute
030     * this Activity before resuming the current one.</p>
031     *
032     * <p><strong>NOTE</strong> - The means by which the Activity on the top
033     * of the stack was acquired is NOT mandated by this Step implementation.</p>
034     *
035     * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
036     * @author Craig R. McClanahan
037     */
038    
039    public class CallStep extends BaseStep {
040    
041    
042        // ----------------------------------------------------------= Constructors
043    
044    
045        /**
046         * Construct a default instance of this Step.
047         */
048        public CallStep() {
049    
050            super();
051    
052        }
053    
054    
055        /**
056         * Construct an instance of this Step with the specified identifier.
057         *
058         * @param id Step identifier
059         */
060        public CallStep(String id) {
061    
062            super();
063            setId(id);
064    
065        }
066    
067    
068        // --------------------------------------------------------- Public Methods
069    
070    
071        /**
072         * Perform the executable actions related to this Step, in the context of
073         * the specified Context.
074         *
075         * @param context The Context that is tracking our execution state
076         *
077         * @exception StepException if a processing error has occurred
078         */
079        public void execute(Context context) throws StepException {
080    
081            // Pop the evaluation stack
082            Object value = null;
083            try {
084                value = context.pop();
085            } catch (EmptyStackException e) {
086                throw new StepException("Evaluation stack is empty", e, this);
087            }
088    
089            // Call the requested Activity 
090            if (!(value instanceof Activity))
091                throw new StepException("Top of stack is not an Activity", this);
092            context.call((Activity) value);
093    
094        }
095    
096    
097    }