KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > JWSClassLoader


1 /*
2  * Copyright 2002-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 package org.apache.axis.utils;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 /**
25  * Class loader for JWS files. There is one of these per JWS class, and
26  * we keep a static Hashtable of them, indexed by class name. When we want
27  * to reload a JWS, we replace the ClassLoader for that class and let the
28  * old one get GC'ed.
29  *
30  * @author Glen Daniels (gdaniels@apache.org)
31  * @author Doug Davis (dug@us.ibm.com)
32  */

33 public class JWSClassLoader extends ClassLoader JavaDoc {
34
35     private String JavaDoc classFile = null;
36     private String JavaDoc name = null;
37
38     /**
39      * Construct a JWSClassLoader with a class name, a parent ClassLoader,
40      * and a filename of a .class file containing the bytecode for the class.
41      * The constructor will load the bytecode, define the class, and register
42      * this JWSClassLoader in the static registry.
43      *
44      * @param name the name of the class which will be created/loaded
45      * @param cl the parent ClassLoader
46      * @param classFile filename of the .class file
47      * @exception FileNotFoundException
48      * @exception IOException
49      */

50     public JWSClassLoader(String JavaDoc name, ClassLoader JavaDoc cl, String JavaDoc classFile)
51         throws FileNotFoundException JavaDoc, IOException JavaDoc
52     {
53         super(cl);
54
55         this.name = name + ".class";
56         this.classFile = classFile;
57
58         FileInputStream JavaDoc fis = new FileInputStream JavaDoc( classFile );
59         ByteArrayOutputStream baos = new ByteArrayOutputStream();
60         byte buf[] = new byte[1024];
61         for(int i = 0; (i = fis.read(buf)) != -1; )
62             baos.write(buf, 0, i);
63         fis.close();
64         baos.close();
65
66         /* Create a new Class object from it */
67         /*************************************/
68         byte[] data = baos.toByteArray();
69         defineClass( name, data, 0, data.length );
70
71         ClassUtils.setClassLoader(name,this);
72     }
73
74     /**
75      * Overloaded getResourceAsStream() so we can be sure to return the
76      * correct class file regardless of where it might live on our hard
77      * drive.
78      *
79      * @param resourceName the resource to load (should be "classname.class")
80      * @return an InputStream of the class bytes, or null
81      */

82     public InputStream JavaDoc getResourceAsStream(String JavaDoc resourceName) {
83         try {
84             if (resourceName.equals(name))
85                 return new FileInputStream JavaDoc( classFile );
86         } catch (FileNotFoundException JavaDoc e) {
87             // Fall through, return null.
88
}
89         return null;
90     }
91 }
92
Popular Tags