KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > DriverClassLoader


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella.core;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23
24 /**
25  * A classloader for drivers specified by connection element.
26  * <p>This class loader is used only if classpath connection attribute is not empty.
27  * @see #loadClass(String) delegation model description.
28  *
29  * @author Fyodor Kupolov
30  * @version 1.0
31  */

32 class DriverClassLoader extends URLClassLoader JavaDoc {
33     public DriverClassLoader(URL JavaDoc[] urls) {
34         super(urls, DriverClassLoader.class.getClassLoader());
35     }
36
37     /**
38      * Loads a class specified by name.
39      * <p>This class loader has a specific delegation model:
40      * <ul>
41      * <li>If a class package is a drivers package (jdbc or drivers), the
42      * class is loaded and created using this classloader without delegating to the parent.
43      * This solution is used to overcome limitations of cross-loaders interaction and to simplify built-in drivers development.
44      * <li>In other cases the semantics is the same as in {@link URLClassLoader}.
45      * </ul>
46      * @param name class name.
47      * @return the loaded class
48      * @throws ClassNotFoundException If the class was not found
49      */

50     public Class JavaDoc<?> loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
51         if (name.startsWith("scriptella.jdbc.") || name.startsWith("scriptella.driver.")) {
52             byte[] b = getClassBytes(name);
53             if (b != null) {
54                 definePackage(name);
55                 return defineClass(name, b, 0, b.length);
56             }
57         }
58         return super.loadClass(name);
59     }
60
61     /**
62      * Defines a package for a class name.
63      * @param className class name to define a package.
64      * @see #definePackage(String, java.util.jar.Manifest, java.net.URL)
65      */

66     private void definePackage(String JavaDoc className) {
67         if (className==null) {
68             return;
69         }
70         int ind = className.lastIndexOf('.');
71         if (ind<0) {
72             return;
73         }
74         String JavaDoc pName = className.substring(0, ind);
75         if (getPackage(pName)==null) {
76             definePackage(pName, null ,null, null ,null, null, null, null);
77         }
78     }
79
80     /**
81      * Loads a class content using a parent class loader.
82      * <p>Please note that we load class bytes even if it has already been loaded by the parent class loader.
83      * @param name class name.
84      * @return class file content.
85      */

86     private static byte[] getClassBytes(final String JavaDoc name) {
87         String JavaDoc path = '/' + name.replace('.', '/') + ".class";
88         InputStream JavaDoc is = DriverClassLoader.class.getResourceAsStream(path);
89         if (is == null) {
90             return null;
91         }
92         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(1024);
93         byte[] b = new byte[1024];
94         try {
95             for (int c; (c = is.read(b)) >= 0;) {
96                 baos.write(b, 0, c);
97             }
98         } catch (IOException JavaDoc e) {
99             throw new IllegalStateException JavaDoc(e);
100         }
101         return baos.toByteArray();
102
103     }
104
105
106 }
107
Popular Tags