KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > iiop > HandleDelegateClassLoader


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.iiop;
24
25 import java.io.*;
26
27 public class HandleDelegateClassLoader
28     extends ClassLoader JavaDoc
29 {
30     
31     public HandleDelegateClassLoader() {
32         super();
33     }
34     
35     protected Class JavaDoc findClass(String JavaDoc name)
36         throws ClassNotFoundException JavaDoc
37     {
38         // This is called only if the class could not be loaded by
39
// the parent class loader (see javadoc for loadClass methods).
40
// Load the class from the current thread's context class loader.
41

42         Class JavaDoc c =Thread.currentThread().getContextClassLoader().loadClass(name);
43         
44         return c;
45     }
46     
47     protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
48         throws ClassNotFoundException JavaDoc
49     {
50         if (!name.equals("com.sun.enterprise.iiop.IIOPHandleDelegate")) {
51             return super.loadClass(name, resolve);
52         }
53         
54         Class JavaDoc handleDelClass = findLoadedClass(name);
55         if (handleDelClass != null) {
56             return handleDelClass;
57         }
58         
59         try {
60             // read the bytes for IIOPHandleDelegate.class
61
ClassLoader JavaDoc resCl = Thread.currentThread().getContextClassLoader();
62             if (Thread.currentThread().getContextClassLoader() == null) {
63                 resCl = getSystemClassLoader();
64             }
65             InputStream is = resCl.getResourceAsStream("com/sun/enterprise/iiop/IIOPHandleDelegate.class");
66             ByteArrayOutputStream baos = new ByteArrayOutputStream();
67             
68             byte[] buf = new byte[4096]; // currently IIOPHandleDelegate is < 4k
69
int nread = 0;
70             while ( (nread = is.read(buf, 0, buf.length)) != -1 ) {
71                 baos.write(buf, 0, nread);
72             }
73             baos.close();
74             is.close();
75
76             byte[] buf2 = baos.toByteArray();
77             
78             handleDelClass = defineClass(
79             "com.sun.enterprise.iiop.IIOPHandleDelegate",
80             buf2, 0, buf2.length);
81             
82         } catch ( Exception JavaDoc ex ) {
83             throw (ClassNotFoundException JavaDoc)new ClassNotFoundException JavaDoc(ex.getMessage()).initCause(ex);
84         }
85         
86         if (resolve) {
87             resolveClass(handleDelClass);
88         }
89         
90         return handleDelClass;
91     }
92 }
93
Popular Tags