1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: ProviderLoader.java,v 1.3 2004/02/02 03:50:09 tanderson Exp $
44 */
45 package org.exolab.jmscts.provider;
46
47 import java.io.File;
48 import java.io.IOException;
49 import java.net.URL;
50 import java.net.URLClassLoader;
51 import java.util.ArrayList;
52 import java.util.Iterator;
53
54
55 /***
56 * Loads a {@link Provider} using its configuration
57 *
58 * @version $Revision: 1.3 $ $Date: 2004/02/02 03:50:09 $
59 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
60 * @see Provider
61 */
62 public class ProviderLoader {
63
64 /***
65 * The provider name
66 */
67 private String _name;
68
69 /***
70 * The provider classname
71 */
72 private String _className;
73
74 /***
75 * Determines if the provider should be started prior to running tests
76 */
77 private boolean _start;
78
79 /***
80 * Determines if the provider should be stopped after running tests
81 */
82 private boolean _stop;
83
84 /***
85 * The provider classpath
86 */
87 private Paths _paths;
88
89 /***
90 * The provider
91 */
92 private Provider _provider;
93
94
95 /***
96 * Construct a new <code>ProviderLoader</code>
97 */
98 public ProviderLoader() {
99 }
100
101 /***
102 * Sets the provider name
103 *
104 * @param name the provider name
105 */
106 public void setName(String name) {
107 _name = name;
108 }
109
110 /***
111 * Returns the provider name
112 *
113 * @return the provider name
114 */
115 public String getName() {
116 return _name;
117 }
118
119 /***
120 * Sets the provider implementation class name
121 *
122 * @param className the provider implementation class name
123 */
124 public void setClassName(String className) {
125 _className = className;
126 }
127
128 /***
129 * Returns the provider implementation class name
130 *
131 * @return the provider implementation class name
132 */
133 public String getClassName() {
134 return _className;
135 }
136
137 /***
138 * Sets if the provider should be started prior to running tests
139 *
140 * @param start if <code>true</code>, start the provider
141 */
142 public void setStart(boolean start) {
143 _start = start;
144 }
145
146 /***
147 * Determines if the provider should be started prior to running tests
148 *
149 * @return <code>true</code> if the provider should be started
150 */
151 public boolean getStart() {
152 return _start;
153 }
154
155 /***
156 * Sets if the provider should be stopped after running tests
157 *
158 * @param stop if <code>true</code>, stop the provider
159 */
160 public void setStop(boolean stop) {
161 _stop = stop;
162 }
163
164 /***
165 * Determines if the provider should be stopped after running tests
166 *
167 * @return <code>true</code> if the provider should be stopped
168 */
169 public boolean getStop() {
170 return _stop;
171 }
172
173 /***
174 * Sets the classpath for the provider
175 *
176 * @param paths the classpath
177 */
178 public void setPaths(Paths paths) {
179 _paths = paths;
180 }
181
182 /***
183 * Returns the classpath for the provider
184 *
185 * @return the classpath
186 */
187 public Paths getPaths() {
188 return _paths;
189 }
190
191 /***
192 * Set the provider
193 *
194 * @param provider the provider
195 */
196 public void setProvider(Provider provider) {
197 _provider = provider;
198 }
199
200 /***
201 * Returns the provider
202 *
203 * @return the provider
204 */
205 public Provider getProvider() {
206 return _provider;
207 }
208
209 /***
210 * Create a new provider. This constructs an instance of the class
211 * specified by {@link #getClassName()}, using the classpath specified by
212 * {@link #getPaths()}
213 *
214 * @return the new provider
215 * @throws Exception if the provider can't be created
216 */
217 public Provider createProvider() throws Exception {
218 ArrayList urls = new ArrayList();
219 Iterator iterator = _paths.getPaths().iterator();
220 while (iterator.hasNext()) {
221 String path = (String) iterator.next();
222 urls.add(getURL(path));
223 }
224 URL[] list = (URL[]) urls.toArray(new URL[0]);
225 URLClassLoader loader = new URLClassLoader(list);
226 Class clazz = loader.loadClass(_className);
227 return (Provider) clazz.newInstance();
228 }
229
230 /***
231 * Converts a path to an URL
232 *
233 * @param path the path to convert
234 * @return the path as a URL
235 * @throws IOException if the path cannot be converted
236 */
237 private URL getURL(String path) throws IOException {
238 URL url;
239 File file = new File(path);
240 if (!file.isAbsolute()) {
241 file = file.getCanonicalFile();
242 }
243 if (file.exists() && file.canRead()) {
244 url = file.toURL();
245 } else {
246 url = new URL(path);
247
248 if (url.getProtocol().equals("file")) {
249 file = new File(url.getFile());
250 if (file.exists() && file.canRead() && file.isDirectory()) {
251 url = file.getCanonicalFile().toURL();
252 }
253 }
254 }
255
256 return url;
257 }
258
259 }
This page was automatically generated by Maven