KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > driver > TestClassLoaderFactory


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: TestClassLoaderFactory.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.driver;
25
26 import java.io.File JavaDoc;
27 import java.lang.reflect.Constructor JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLClassLoader JavaDoc;
32
33 import org.enhydra.xml.driver.TestError;
34 import org.enhydra.xml.driver.TestException;
35 import org.enhydra.xml.driver.TestFileOps;
36
37 /**
38  * Create class loaders for the tests. This provides an abstraction for
39  * creating various classloaders that XMLC reloading is expected to work
40  * under. It creates them via reflection, allowing this file to be compiled
41  * even if supported class loaders are not available.
42  */

43 public class TestClassLoaderFactory {
44     /**
45      * Identifiers for valid class loaders.
46      */

47     private static final String JavaDoc LOADER_ENHYDRA = "enhydra";
48     private static final String JavaDoc LOADER_MULTI = "multi";
49     private static final String JavaDoc LOADER_URL = "url";
50
51     /** Load a class using the classloader that loaded this class */
52     private Class JavaDoc loadClass(String JavaDoc className)
53         throws ClassNotFoundException JavaDoc {
54         return getClass().getClassLoader().loadClass(className);
55     }
56
57     /** Create a MultiClassLoader for tests. Used with Enhydra 3.*. */
58     private ClassLoader JavaDoc mkMultiClassLoader(URL JavaDoc[] classPath,
59                                            ClassLoader JavaDoc parent)
60         throws NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc,
61                IllegalAccessException JavaDoc, InstantiationException JavaDoc,
62                ClassNotFoundException JavaDoc {
63         // Need LogChannel object to find constructor
64
Class JavaDoc logClass = loadClass("com.lutris.logging.LogChannel");
65         Class JavaDoc loaderClass
66             = loadClass("com.lutris.classloader.MultiClassLoader");
67
68         // new MultiClassLoader(URL[] path, LogChannel loadLogChannel);
69
Constructor JavaDoc constr
70             = loaderClass.getConstructor(new Class JavaDoc[]{URL JavaDoc[].class, logClass});
71         ClassLoader JavaDoc loader
72             = (ClassLoader JavaDoc)constr.newInstance(new Object JavaDoc[]{classPath, null});
73
74         // setParent(parent)
75
Method JavaDoc setParent = loaderClass.getMethod("setParent",
76                                                  new Class JavaDoc[]{ClassLoader JavaDoc.class});
77         setParent.invoke(loader, new Object JavaDoc[]{parent});
78         return loader;
79     }
80
81     /** Create an EnhydraClassLoader for tests. Used with EAS 4.*. */
82     private ClassLoader JavaDoc mkEnhydraClassLoader(URL JavaDoc[] classPath,
83                                              ClassLoader JavaDoc parent)
84         throws NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc,
85                IllegalAccessException JavaDoc, InstantiationException JavaDoc,
86                ClassNotFoundException JavaDoc {
87         Class JavaDoc loaderClass
88             = loadClass("org.enhydra.classloader.EnhydraClassLoader");
89
90         // new EnhydraClassLoader(ClassLoader parent, URL[] classPath)
91
Constructor JavaDoc constr
92             = loaderClass.getConstructor(new Class JavaDoc[]{ClassLoader JavaDoc.class,
93                                                      URL JavaDoc[].class});
94         return (ClassLoader JavaDoc)constr.newInstance(new Object JavaDoc[]{parent, classPath});
95     }
96
97     /**
98      * Create a URLClassLoader for tests.
99      */

100     private ClassLoader JavaDoc mkURLClassLoader(URL JavaDoc[] classPath,
101                                          ClassLoader JavaDoc parent) {
102         return URLClassLoader.newInstance(classPath, parent);
103     }
104
105     /**
106      * Create a class loader of the specified type.
107      */

108     public ClassLoader JavaDoc mkTestClassLoader(String JavaDoc classLoaderName,
109                                          URL JavaDoc[] classPath,
110                                          ClassLoader JavaDoc parent) {
111         try {
112             if (classLoaderName.equals(LOADER_MULTI)) {
113                 return mkMultiClassLoader(classPath, parent);
114             } else if (classLoaderName.equals(LOADER_ENHYDRA)) {
115                 return mkEnhydraClassLoader(classPath, parent);
116             } else if (classLoaderName.equals(LOADER_URL)) {
117                 return mkURLClassLoader(classPath, parent);
118             } else {
119                 throw new TestError("invalid classloader name \""
120                                     + classLoaderName + "\"");
121             }
122         } catch (NoSuchMethodException JavaDoc except) {
123             throw new TestException(except);
124         } catch (InvocationTargetException JavaDoc except) {
125             throw new TestException(except);
126         } catch (ClassNotFoundException JavaDoc except) {
127             throw new TestException(except);
128         } catch (IllegalAccessException JavaDoc except) {
129             throw new TestException(except);
130         } catch (InstantiationException JavaDoc except) {
131             throw new TestException(except);
132         }
133     }
134
135     /**
136      * Create a class loader of the type associated with this factory
137      */

138     public ClassLoader JavaDoc mkTestClassLoader(URL JavaDoc[] classPath,
139                                          ClassLoader JavaDoc parent) {
140         // FIXME: need to parameterize type
141
return mkTestClassLoader(LOADER_URL, classPath, parent);
142     }
143
144     /**
145      * Create a class loader of the type associated with this factory
146      */

147     public ClassLoader JavaDoc mkTestClassLoader(File JavaDoc[] classPath,
148                                          ClassLoader JavaDoc parent) {
149         URL JavaDoc[] urlClassPath = new URL JavaDoc[classPath.length];
150         for (int idx = 0; idx < classPath.length; idx++) {
151             urlClassPath[idx] = TestFileOps.dirToUrl(classPath[idx]);
152         }
153
154         // FIXME: need to parameterize type
155
return mkTestClassLoader(LOADER_URL, urlClassPath, parent);
156     }
157 }
158
Popular Tags