/Users/lyon/j4p/src/classUtils/putils/CustomURLClassLoader.java

1    package classUtils.putils; 
2     
3    import utils.ReplaceString; 
4    import utils.StringUtils; 
5     
6    import java.io.File; 
7    import java.io.IOException; 
8    import java.io.UnsupportedEncodingException; 
9    import java.net.*; 
10   import java.security.CodeSource; 
11   import java.security.PermissionCollection; 
12   import java.util.Vector; 
13   import java.util.Enumeration; 
14   import java.util.jar.JarEntry; 
15   import java.util.jar.JarFile; 
16    
17   public class CustomURLClassLoader extends URLClassLoader { 
18       private URL urls[]; 
19    
20       public static void main(String args[]) { 
21           File f = 
22                   futils.Futil.getReadFile("select a jar file"); 
23           try { 
24               URL urls[] = {f.toURL()}; 
25               CustomURLClassLoader cucl = 
26                       new CustomURLClassLoader(urls); 
27               cucl.printPackages(); 
28               cucl.printClasses(); 
29               System.out.println("==== done ==="); 
30    
31           } catch (MalformedURLException e) { 
32               e.printStackTrace(); 
33    
34           } 
35    
36       } 
37    
38       public JarFile[] getJarFiles() 
39               throws IOException, URISyntaxException { 
40           JarFile jf[] = new JarFile[urls.length]; 
41           for (int i = 0; i < urls.length; i++) 
42               jf[i] = new JarFile( 
43                       new File( 
44                               new URI(urls[i].toString()))); 
45           return jf; 
46       } 
47    
48       public Class[] getClasses() 
49               throws IOException, URISyntaxException { 
50           String s[] = getClassNames(); 
51           Vector v = new Vector(); 
52           for (int i = 0; i < s.length; i++) 
53               try { 
54                   // try standard class loader first 
55                   Class c = Class.forName(s[i]); 
56                   v.addElement(c); 
57               } catch (Exception e1) { 
58                   try { 
59                       // try CustomURLClassLoader Second. 
60                       Class c = Class.forName(s[i], false, this); 
61                       v.addElement(c); 
62                   } catch (Exception e2) { 
63    
64                   } 
65               } 
66           Class ca[] = new Class[v.size()]; 
67           v.copyInto(ca); 
68           return ca; 
69       } 
70    
71       public void printClasses() { 
72           System.out.println("=== Class List ==="); 
73           try { 
74               print(getClasses()); 
75    
76           } catch (Exception e) { 
77               e.printStackTrace(); 
78           } 
79       } 
80    
81       public void print(Object o[]) { 
82           for (int i = 0; i < o.length; i++) 
83               System.out.println(o[i]); 
84       } 
85    
86       public String[] getClassNames() throws IOException, URISyntaxException { 
87           JarEntry je[] = getClassEntries(); 
88           Vector v = new Vector(); 
89           for (int i = 0; i < je.length; i++) 
90               v.addElement(makeClassString(je[i].getName())); 
91           String s[] = new String[v.size()]; 
92           v.copyInto(s); 
93           return s; 
94       } 
95    
96       /** 
97        *  takes a string of the 
98        *  java/lang/String.class form and returns 
99        *  java.lang.String 
100       */ 
101      public static String makeClassString(String s) { 
102          s = StringUtils.sub(s, ".class", ""); 
103          s = s.replace('/', '.'); 
104          s = s.replace('\\', '.'); 
105          return s; 
106      } 
107   
108      public JarEntry[] getClassEntries() throws IOException, URISyntaxException { 
109          JarFile jf[] = getJarFiles(); 
110          Vector v = new Vector(); 
111          for (int i = 0; i < jf.length; i++) { 
112              JarFile jf1 = jf[i]; 
113              Vector v1 = new Vector(); 
114              for (Enumeration e = jf1.entries(); e.hasMoreElements();) { 
115                  Object o = e.nextElement(); 
116                  JarEntry je1 = (JarEntry) o; 
117                  String s = je1.getName(); 
118   
119                  if (s.endsWith(".class") && (-1 == s.indexOf('$'))) 
120                      v1.addElement(je1); 
121              } 
122              JarEntry je1[] = new JarEntry[v1.size()]; 
123              v1.copyInto(je1); 
124              JarEntry je[] = je1; 
125              for (int j = 0; j < je.length; j++) 
126                  v.addElement(je[j]); 
127          } 
128          JarEntry je[] = new JarEntry[v.size()]; 
129          v.copyInto(je); 
130          return je; 
131      } 
132   
133      public void printPackages() { 
134          System.out.println("==== Package List ==="); 
135          Package p[] = getPackages(); 
136          for (int i = 0; i < p.length; i++) 
137              System.out.println(p[i]); 
138      } 
139   
140      public CustomURLClassLoader(URL urls[]) { 
141          super(urls); 
142          this.urls = urls; 
143      } 
144   
145      public final synchronized Class loadClass(String name, boolean resolve) 
146              throws ClassNotFoundException { 
147          // First check if we have permission to access the package. 
148          SecurityManager sm = System.getSecurityManager(); 
149          if (sm != null) { 
150              int i = name.lastIndexOf('.'); 
151              if (i != -1) { 
152                  sm.checkPackageAccess(name.substring(0, i)); 
153              } 
154          } 
155          return super.loadClass(name, resolve); 
156   
157      } 
158   
159      protected Class findClass(final String name) 
160              throws ClassNotFoundException { 
161          // First check if we have permission to access the package. 
162          SecurityManager sm = System.getSecurityManager(); 
163          if (sm != null) { 
164              int i = name.lastIndexOf('.'); 
165              if (i != -1) { 
166                  sm.checkPackageDefinition(name.substring(0, i)); 
167              } 
168          } 
169          return super.findClass(name); 
170      } 
171   
172      protected PermissionCollection getPermissions(CodeSource codesource) { 
173          // Use all the standard permissions, plus allow the code to 
174          // exit the VM. 
175          PermissionCollection pc = super.getPermissions(codesource); 
176          pc.add(new RuntimePermission("exitVM")); 
177          return pc; 
178      } 
179  } 
180