1 /*
2 * Copyright 1999-2001,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.commons.workflow.core;
18
19
20 import org.apache.commons.workflow.Context;
21 import org.apache.commons.workflow.StepException;
22 import org.apache.commons.workflow.base.BaseStep;
23
24
25 /**
26 * <p>Load a class with the specified name from the specified class loader,
27 * and push the corresponding <code>java.lang.Class</code> object onto the
28 * evaluation stack.
29 *
30 * <p>Supported Attributes:</p>
31 * <ul>
32 * <li><strong>name</strong> - The fully qualified name of the Java class
33 * to be loaded. If not specified, the top of the evaluation stack
34 * is popped, converted to a String (if necessary), and used as the
35 * name of the class to be loaded.</li>
36 * <li><strong>thread</strong> - Should the class be loaded from the current
37 * Thread's context class loader? (Default is to load from the same
38 * class loader that loaded this class).</li>
39 * </ul>
40 *
41 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
42 * @author Craig R. McClanahan
43 */
44
45 public class LoadStep extends BaseStep {
46
47
48 // ----------------------------------------------------------= Constructors
49
50
51 /**
52 * Construct a default instance of this Step.
53 */
54 public LoadStep() {
55
56 super();
57
58 }
59
60
61 /**
62 * Construct an instance of this Step with the specified identifier.
63 *
64 * @param id Step identifier
65 */
66 public LoadStep(String id) {
67
68 super();
69 setId(id);
70
71 }
72
73
74 /**
75 * Construct a fully configured instance of this Step.
76 *
77 * @param id Step identifier
78 * @param name Class name
79 */
80 public LoadStep(String id, String name) {
81
82 super();
83 setId(id);
84 setName(name);
85
86 }
87
88
89 /**
90 * Construct a fully configured instance of this Step.
91 *
92 * @param id Step identifier
93 * @param name Class name
94 * @param thread Load from thread context class loader?
95 */
96 public LoadStep(String id, String name, boolean thread) {
97
98 super();
99 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 }