1 23 24 package org.objectweb.clif.util; 25 26 import org.objectweb.clif.util.bytearray.ByteArrayURLStreamHandler; 27 import java.net.URL ; 28 import java.net.Socket ; 29 import java.io.IOException ; 30 import java.io.DataInputStream ; 31 import java.io.DataOutputStream ; 32 import java.util.Enumeration ; 33 import java.util.Vector ; 34 import java.util.Map ; 35 import java.util.HashMap ; 36 37 41 public class ClifClassLoader extends ClassLoader 42 { 43 static private ClifClassLoader cl = new ClifClassLoader(); 44 45 46 static public ClifClassLoader getClassLoader() 47 { 48 return cl; 49 } 50 51 52 static public void clear() 53 { 54 cl.my_cache.clear(); 55 cl = new ClifClassLoader(); 56 } 57 58 59 Map my_cache = new HashMap (); 60 Socket sock = null; 61 DataInputStream dins; 62 DataOutputStream douts; 63 64 65 private ClifClassLoader() 66 { 67 super(); 68 } 69 70 71 public synchronized byte[] getBytes(String name) 72 throws IOException 73 { 74 if (sock == null) 75 { 76 sock = new Socket ( 77 System.getProperty("clif.codeserver.host"), 78 Integer.parseInt(System.getProperty("clif.codeserver.port"))); 79 dins = new DataInputStream (sock.getInputStream()); 80 douts = new DataOutputStream (sock.getOutputStream()); 81 } 82 byte[] result = null; 83 result = (byte[])my_cache.get(name); 85 if (result == null && sock != null) 86 { 87 douts.writeUTF(name); 88 douts.flush(); 89 int length = dins.readInt(); 90 if (length >= 0) 91 { 92 result = new byte[length]; 93 int offset = 0; 94 while (length > 0) 95 { 96 int n = dins.read(result, offset, length); 97 length -= n; 98 offset += n; 99 } 100 my_cache.put(name, result); 101 } 102 } 103 return result; 104 } 105 106 107 protected synchronized Class findClass(String name) 108 throws ClassNotFoundException 109 { 110 if (name.startsWith("org.objectweb.fractal.julia.generated.")) 111 { 112 throw new ClassNotFoundException (name); 113 } 114 Class result = null; 115 String pathname = name.replace('.', '/') + ".class"; 116 try 118 { 119 ClassLoader parent = getParent(); 120 if (parent == null) 121 { 122 parent = getSystemClassLoader(); 123 } 124 result = parent.loadClass(name); 125 } 126 catch (ClassNotFoundException e) 127 { 128 result = (Class )my_cache.get(name); 130 if (result == null) 131 { 132 byte[] code = null; 133 try 135 { 136 code = getBytes(pathname); 137 } 138 catch (IOException ex) 139 { 140 throw new ClassNotFoundException (name, ex); 141 } 142 if (code != null) 143 { 144 result = defineClass(name, code, 0, code.length); 145 my_cache.put(name, result); 146 } 147 else 148 { 149 throw new ClassNotFoundException (name); 150 } 151 } 152 } 153 return result; 154 } 155 156 157 public URL findResource(String name) 158 { 159 try 160 { 161 return new URL (null, "bytearray:" + name, new ByteArrayURLStreamHandler(this)); 162 } 163 catch (Exception ex) 164 { 165 ex.printStackTrace(); 166 return null; 167 } 168 } 169 170 171 176 public Enumeration findResources(String name) 177 { 178 Vector result = new Vector (1); 179 URL url = findResource(name); 180 if (url != null) 181 { 182 result.add(url); 183 } 184 return result.elements(); 185 } 186 } 187 | Popular Tags |