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 org.apache.commons.workflow.Context;
021    import org.apache.commons.workflow.StepException;
022    import org.apache.commons.workflow.base.BaseStep;
023    
024    
025    /**
026     * <p>Load a class with the specified name from the specified class loader,
027     * and push the corresponding <code>java.lang.Class</code> object onto the
028     * evaluation stack.
029     *
030     * <p>Supported Attributes:</p>
031     * <ul>
032     * <li><strong>name</strong> - The fully qualified name of the Java class
033     *     to be loaded.  If not specified, the top of the evaluation stack
034     *     is popped, converted to a String (if necessary), and used as the
035     *     name of the class to be loaded.</li>
036     * <li><strong>thread</strong> - Should the class be loaded from the current
037     *     Thread's context class loader?  (Default is to load from the same
038     *     class loader that loaded this class).</li>
039     * </ul>
040     *
041     * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
042     * @author Craig R. McClanahan
043     */
044    
045    public class LoadStep extends BaseStep {
046    
047    
048        // ----------------------------------------------------------= Constructors
049    
050    
051        /**
052         * Construct a default instance of this Step.
053         */
054        public LoadStep() {
055    
056            super();
057    
058        }
059    
060    
061        /**
062         * Construct an instance of this Step with the specified identifier.
063         *
064         * @param id Step identifier
065         */
066        public LoadStep(String id) {
067    
068            super();
069            setId(id);
070    
071        }
072    
073    
074        /**
075         * Construct a fully configured instance of this Step.
076         *
077         * @param id Step identifier
078         * @param name Class name
079         */
080        public LoadStep(String id, String name) {
081    
082            super();
083            setId(id);
084            setName(name);
085    
086        }
087    
088    
089        /**
090         * Construct a fully configured instance of this Step.
091         *
092         * @param id Step identifier
093         * @param name Class name
094         * @param thread Load from thread context class loader?
095         */
096        public LoadStep(String id, String name, boolean thread) {
097    
098            super();
099            setId(id);
100            setName(name);
101            setThread(thread);
102    
103        }
104    
105    
106        // ------------------------------------------------------------- Properties
107    
108    
109        /**
110         * The class name to be loaded.
111         */
112        protected String name = null;
113    
114        public String getName() {
115            return (this.name);
116        }
117    
118        public void setName(String name) {
119            this.name = name;
120        }
121    
122    
123        /**
124         * Load from the thread context class loader?
125         */
126        protected boolean thread = false;
127    
128        public boolean getThread() {
129            return (this.thread);
130        }
131    
132        public void setThread(boolean thread) {
133            this.thread = thread;
134        }
135    
136    
137        // --------------------------------------------------------- Public Methods
138    
139    
140        /**
141         * Perform the executable actions related to this Step, in the context of
142         * the specified Context.
143         *
144         * @param context The Context that is tracking our execution state
145         *
146         * @exception StepException if a processing error has occurred
147         */
148        public void execute(Context context) throws StepException {
149    
150            // Acquire the class loader we will be using
151            ClassLoader classLoader = null;
152            if (thread)
153                classLoader = Thread.currentThread().getContextClassLoader();
154            else
155                classLoader = this.getClass().getClassLoader();
156            if (classLoader == null)
157                throw new StepException
158                    ("No thread context class loader is available");
159    
160            // Calculate the name of the class to be loaded
161            String className = getName();
162            if (className == null)
163                className = context.pop().toString();
164    
165            // Load the specified class
166            Class clazz = null;
167            try {
168                clazz = classLoader.loadClass(className);
169            } catch (Throwable t) {
170                throw new StepException
171                    ("Exception from loadClass()", t, this);
172            }
173    
174            // Push the new Class onto the evaluation stack and return
175            context.push(clazz);
176    
177        }
178    
179    
180        /**
181         * Render a string representation of this Step.
182         */
183        public String toString() {
184    
185            StringBuffer sb = new StringBuffer("<core:load");
186            if (getId() != null) {
187                sb.append(" id=\"");
188                sb.append(getId());
189                sb.append("\"");
190            }
191            if (getName() != null) {
192                sb.append(" name=\"");
193                sb.append(getName());
194                sb.append("\"");
195            }
196            sb.append(" thread=\"");
197            sb.append(getThread());
198            sb.append("\"/>");
199            return (sb.toString());
200    
201        }
202    
203    
204    }