/Users/lyon/j4p/src/net/compute/GreeterLoader.java

1    package net.compute; 
2     
3    /** 
4      * Created by 
5      * User: lyon 
6      * Date: Sep 3, 2003 
7      * Time: 11:17:45 AM 
8      * 
9      */ 
10    
11   import java.io.*; 
12    
13   public class GreeterLoader extends ClassLoader { 
14    
15        // basePath gives the path to which this class 
16        // loader appends "/.class" to get the 
17        // full path name of the class file to load 
18        private String basePath; 
19    
20        public GreeterLoader(String basePath) { 
21    
22            this.basePath = basePath; 
23        } 
24    
25        public synchronized Class loadClass(String className, 
26            boolean resolveIt) throws ClassNotFoundException { 
27    
28            Class result; 
29            byte classData[]; 
30    
31            // Check the loaded class cache 
32            result = findLoadedClass(className); 
33            if (result != null) { 
34                // Return a cached class 
35                return result; 
36            } 
37    
38            // If Spoofed, don't delegate 
39            if (className.compareTo("Spoofed") != 0) { 
40    
41                // Check with the system class loader 
42                try { 
43                    result = super.findSystemClass(className); 
44                    // Return a system class 
45                    return result; 
46                } 
47                catch (ClassNotFoundException e) { 
48                } 
49            } 
50    
51            // Don't attempt to load a system file except through 
52            // the primordial class loader 
53            if (className.startsWith("java.")) { 
54                throw new ClassNotFoundException(); 
55            } 
56    
57            // Try to load it from the basePath directory. 
58            classData = getTypeFromBasePath(className); 
59            if (classData == null) { 
60                System.out.println("GCL - Can't load class: " 
61                    + className); 
62                throw new ClassNotFoundException(); 
63            } 
64    
65            // Parse it 
66            result = defineClass(className, classData, 0, 
67                classData.length); 
68            if (result == null) { 
69                System.out.println("GCL - Class format error: " 
70                    + className); 
71                throw new ClassFormatError(); 
72            } 
73    
74            if (resolveIt) { 
75                resolveClass(result); 
76            } 
77    
78            // Return class from basePath directory 
79            return result; 
80        } 
81    
82        private byte[] getTypeFromBasePath(String typeName) { 
83    
84            FileInputStream fis; 
85            String fileName = basePath + File.separatorChar 
86                + typeName.replace('.', File.separatorChar) 
87                + ".class"; 
88    
89            try { 
90                fis = new FileInputStream(fileName); 
91            } 
92            catch (FileNotFoundException e) { 
93                return null; 
94            } 
95    
96            BufferedInputStream bis = new BufferedInputStream(fis); 
97    
98            ByteArrayOutputStream out = new ByteArrayOutputStream(); 
99    
100           try { 
101               int c = bis.read(); 
102               while (c != -1) { 
103                   out.write(c); 
104                   c = bis.read(); 
105               } 
106           } 
107           catch (IOException e) { 
108               return null; 
109           } 
110   
111           return out.toByteArray(); 
112       } 
113        // Arguments to this application: 
114       //     args[0] - path name of directory in which class files 
115       //               for greeters are stored 
116       //     args[1], args[2], ... - class names of greeters to load 
117       //               and invoke the greet() method on. 
118       // 
119       // All greeters must implement the com.artima.greeter.Greeter 
120       // interface. 
121       // 
122       static public void main(String[] args) { 
123   
124           if (args.length <= 1) { 
125               System.out.println( 
126                   "Enter base path and greeter class names as args."); 
127               return; 
128           } 
129   
130           for (int i = 1; i < args.length; ++i) { 
131               try { 
132   
133                   GreeterLoader gcl = 
134                       new GreeterLoader(args[0]); 
135   
136                   // Load the greeter specified on the command line 
137                   Class c = gcl.loadClass(args[i]); 
138   
139                   // Instantiate it into a greeter object 
140                   Object o = c.newInstance(); 
141   
142                   // Cast the Object ref to the Greeter interface type 
143                   // so greet() can be invoked on it 
144                   Runnable greeter = (Runnable) o; 
145   
146                   // Greet the world in this greeter's special way 
147                   greeter.run(); 
148   
149                   // Forget the class loader object, Class 
150                   // instance, and greeter object 
151                   gcl = null; 
152                   c = null; 
153                   o = null; 
154                   greeter = null; 
155   
156                   // At this point, the types loaded through the 
157                   // GreeterClassLoader object created at the top of 
158                   // this for loop are unreferenced and can be unloaded 
159                   // by the virtual machine. 
160               } 
161               catch (Exception e) { 
162                   e.printStackTrace(); 
163               } 
164           } 
165       } 
166  } 
167