1 25 26 package net.yagga.util; 27 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.net.JarURLConnection ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Modifier ; 33 import java.lang.reflect.InvocationTargetException ; 34 import java.util.jar.Attributes ; 35 import java.io.IOException ; 36 import java.util.jar.*; 38 import java.util.*; 39 import java.io.*; 40 43 class JarClassLoader extends ClassLoader 44 { 45 private String jarFile; 46 private Hashtable htSizes=new Hashtable(); 47 48 public JarClassLoader(String jarF) { 49 super(); 50 this.jarFile = jarF; 51 } 52 53 57 public String getMainClassName() throws IOException 58 { 59 JarFile jf=new JarFile(jarFile); 60 Manifest mf = jf.getManifest(); 61 return mf != null ? mf.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS) : null; 62 } 63 64 70 public void invokeClass(String name, String [] args) 71 throws ClassNotFoundException , 72 NoSuchMethodException , 73 InvocationTargetException 74 { 75 Class c = loadClass(name); 76 77 Method m = c.getMethod("main", new Class [] { args.getClass() }); 79 m.setAccessible(true); 81 int mods = m.getModifiers(); 82 if (m.getReturnType() != void.class || !Modifier.isStatic(mods) || 83 !Modifier.isPublic(mods)) { 84 throw new NoSuchMethodException ("main"); 85 } 86 try { 87 m.invoke(null, new Object [] { args }); 88 } catch (IllegalAccessException e) { 89 } 91 } 92 93 94 protected Class findClass(String className) throws ClassNotFoundException 95 { 96 String urlName=className.replace('.', '/') + ".class"; 97 Ut.infoln("findClass1 :"+className); 98 byte[] b1=getClassFromJar(urlName); 99 try{ 100 return defineClass(className,b1,0,b1.length); 101 }catch(ClassFormatError cfe){ 102 Ut.error("'"+className+"':"+cfe); 103 } 104 return null; 105 } 106 107 private void initSizes() 108 { 109 try{ 110 JarFile zf=new JarFile(jarFile); 111 Enumeration e=zf.entries(); 112 while (e.hasMoreElements()) 113 { 114 JarEntry ze=(JarEntry)e.nextElement(); 115 htSizes.put(ze.getName(),new Integer ((int)ze.getSize())); 116 } 117 zf.close(); 118 } 119 catch(IOException ioe){ 120 Ut.error("Error reading sizes in '"+jarFile+"':"+ioe); 121 } 122 } 123 124 public byte[] getClassFromJar(String cl) 125 { 126 initSizes(); 127 try 128 { 129 JarInputStream zis=getJIS(jarFile); 131 JarEntry ze=null; 132 while ((ze=zis.getNextJarEntry())!=null) 133 { 134 if (ze.isDirectory()) 135 continue; 136 137 int size=(int)ze.getSize(); 138 if (size==-1) 139 size=((Integer )htSizes.get(ze.getName())).intValue(); 140 141 if(ze.getName().equals(cl)) 142 { 143 byte[] b=new byte[(int)size]; 144 int rb=0; 145 int chunk=0; 146 while (((int)size - rb) > 0) 147 { 148 chunk=zis.read(b,rb,(int)size - rb); 149 if (chunk==-1) 150 break; 151 rb+=chunk; 152 } 153 return b; 154 } 155 } 156 } 157 catch (NullPointerException e) { 158 Ut.error("'"+jarFile+"':(done)"+e); 159 } catch (FileNotFoundException e) { 160 Ut.error("'"+jarFile+"':"+e); 161 } catch (IOException e) { 162 Ut.error("'"+jarFile+"':"+e); 163 } 164 return null; 165 } 166 167 168 private JarInputStream getJIS(String jarFile) 169 { 170 try{ 171 InputStream is=ResourceMgr.openResource(jarFile); 172 return new JarInputStream(is); 173 }catch(IOException io){ 174 Ut.error("IOE..."+io); 175 } 176 return null; 177 } 178 179 private InputStream openResource(String filename) 180 { 181 try 182 { 183 InputStream is=ResourceMgr.class.getResourceAsStream("/"+filename); 184 is.available(); return is; 186 } 187 catch (java.io.IOException e) 188 { 189 Ut.error("RM2: file not found:"+filename); 190 System.exit(1); 191 } 192 catch(NullPointerException e2){ 193 Ut.error("RM2: file not found!"+filename); 194 System.exit(1); 195 } 196 return null; 197 } 198 199 } 200 201 204 330 331 | Popular Tags |