Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LoadStep |
|
| 1.8;1.8 |
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 | 0 | super(); |
57 | ||
58 | 0 | } |
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 | 0 | super(); |
69 | 0 | setId(id); |
70 | ||
71 | 0 | } |
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 | 0 | super(); |
83 | 0 | setId(id); |
84 | 0 | setName(name); |
85 | ||
86 | 0 | } |
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 | 0 | super(); |
99 | 0 | setId(id); |
100 | 0 | setName(name); |
101 | 0 | setThread(thread); |
102 | ||
103 | 0 | } |
104 | ||
105 | ||
106 | // ------------------------------------------------------------- Properties | |
107 | ||
108 | ||
109 | /** | |
110 | * The class name to be loaded. | |
111 | */ | |
112 | 0 | protected String name = null; |
113 | ||
114 | public String getName() { | |
115 | 0 | return (this.name); |
116 | } | |
117 | ||
118 | public void setName(String name) { | |
119 | 0 | this.name = name; |
120 | 0 | } |
121 | ||
122 | ||
123 | /** | |
124 | * Load from the thread context class loader? | |
125 | */ | |
126 | 0 | protected boolean thread = false; |
127 | ||
128 | public boolean getThread() { | |
129 | 0 | return (this.thread); |
130 | } | |
131 | ||
132 | public void setThread(boolean thread) { | |
133 | 0 | this.thread = thread; |
134 | 0 | } |
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 | 0 | ClassLoader classLoader = null; |
152 | 0 | if (thread) |
153 | 0 | classLoader = Thread.currentThread().getContextClassLoader(); |
154 | else | |
155 | 0 | classLoader = this.getClass().getClassLoader(); |
156 | 0 | if (classLoader == null) |
157 | 0 | throw new StepException |
158 | ("No thread context class loader is available"); | |
159 | ||
160 | // Calculate the name of the class to be loaded | |
161 | 0 | String className = getName(); |
162 | 0 | if (className == null) |
163 | 0 | className = context.pop().toString(); |
164 | ||
165 | // Load the specified class | |
166 | 0 | Class clazz = null; |
167 | try { | |
168 | 0 | clazz = classLoader.loadClass(className); |
169 | 0 | } catch (Throwable t) { |
170 | 0 | throw new StepException |
171 | ("Exception from loadClass()", t, this); | |
172 | 0 | } |
173 | ||
174 | // Push the new Class onto the evaluation stack and return | |
175 | 0 | context.push(clazz); |
176 | ||
177 | 0 | } |
178 | ||
179 | ||
180 | /** | |
181 | * Render a string representation of this Step. | |
182 | */ | |
183 | public String toString() { | |
184 | ||
185 | 0 | StringBuffer sb = new StringBuffer("<core:load"); |
186 | 0 | if (getId() != null) { |
187 | 0 | sb.append(" id=\""); |
188 | 0 | sb.append(getId()); |
189 | 0 | sb.append("\""); |
190 | } | |
191 | 0 | if (getName() != null) { |
192 | 0 | sb.append(" name=\""); |
193 | 0 | sb.append(getName()); |
194 | 0 | sb.append("\""); |
195 | } | |
196 | 0 | sb.append(" thread=\""); |
197 | 0 | sb.append(getThread()); |
198 | 0 | sb.append("\"/>"); |
199 | 0 | return (sb.toString()); |
200 | ||
201 | } | |
202 | ||
203 | ||
204 | } |