1 15 16 package javassist.web; 17 18 import java.io.*; 19 import java.net.*; 20 21 50 public class Viewer extends ClassLoader { 51 private String server; 52 private int port; 53 54 57 public static void main(String [] args) throws Throwable { 58 if (args.length >= 3) { 59 Viewer cl = new Viewer(args[0], Integer.parseInt(args[1])); 60 String [] args2 = new String [args.length - 3]; 61 System.arraycopy(args, 3, args2, 0, args.length - 3); 62 cl.run(args[2], args2); 63 } 64 else 65 System.err.println( 66 "Usage: java javassist.web.Viewer <host> <port> class [args ...]"); 67 } 68 69 75 public Viewer(String host, int p) { 76 server = host; 77 port = p; 78 } 79 80 83 public String getServer() { return server; } 84 85 88 public int getPort() { return port; } 89 90 96 public void run(String classname, String [] args) 97 throws Throwable 98 { 99 Class c = loadClass(classname); 100 try { 101 c.getDeclaredMethod("main", new Class [] { String [].class }) 102 .invoke(null, new Object [] { args }); 103 } 104 catch (java.lang.reflect.InvocationTargetException e) { 105 throw e.getTargetException(); 106 } 107 } 108 109 112 protected synchronized Class loadClass(String name, boolean resolve) 113 throws ClassNotFoundException 114 { 115 Class c = findLoadedClass(name); 116 if (c == null) 117 c = findClass(name); 118 119 if (c == null) 120 throw new ClassNotFoundException (name); 121 122 if (resolve) 123 resolveClass(c); 124 125 return c; 126 } 127 128 138 protected Class findClass(String name) throws ClassNotFoundException { 139 Class c = null; 140 if (name.startsWith("java.") || name.startsWith("javax.") 141 || name.equals("javassist.web.Viewer")) 142 c = findSystemClass(name); 143 144 if (c == null) 145 try { 146 byte[] b = fetchClass(name); 147 if (b != null) 148 c = defineClass(name, b, 0, b.length); 149 } 150 catch (Exception e) { 151 } 152 153 return c; 154 } 155 156 160 protected byte[] fetchClass(String classname) throws Exception 161 { 162 byte[] b; 163 URL url = new URL("http", server, port, 164 "/" + classname.replace('.', '/') + ".class"); 165 URLConnection con = url.openConnection(); 166 con.connect(); 167 int size = con.getContentLength(); 168 InputStream s = con.getInputStream(); 169 if (size <= 0) 170 b = readStream(s); 171 else { 172 b = new byte[size]; 173 int len = 0; 174 do { 175 int n = s.read(b, len, size - len); 176 if (n < 0) { 177 s.close(); 178 throw new IOException("the stream was closed: " 179 + classname); 180 } 181 len += n; 182 } while (len < size); 183 } 184 185 s.close(); 186 return b; 187 } 188 189 private byte[] readStream(InputStream fin) throws IOException { 190 byte[] buf = new byte[4096]; 191 int size = 0; 192 int len = 0; 193 do { 194 size += len; 195 if (buf.length - size <= 0) { 196 byte[] newbuf = new byte[buf.length * 2]; 197 System.arraycopy(buf, 0, newbuf, 0, size); 198 buf = newbuf; 199 } 200 201 len = fin.read(buf, size, buf.length - size); 202 } while (len >= 0); 203 204 byte[] result = new byte[size]; 205 System.arraycopy(buf, 0, result, 0, size); 206 return result; 207 } 208 } 209 | Popular Tags |