1 26 27 28 package org.objectweb.mobilitools.smi.lib; 29 30 31 import org.objectweb.mobilitools.smi.lib.bytearray.ByteArrayURLStreamHandler; 32 import org.omg.CfMAF.*; 33 import org.omg.CORBA.*; 34 import java.net.*; 35 import java.io.*; 36 import java.util.*; 37 import java.util.jar.*; 38 39 40 82 public class SMIClassLoader extends ClassLoader 83 { 84 String my_codebase; 85 AgentProfile my_profile; 86 MAFAgentSystem my_provider; 87 volatile ClassCache my_cache; 88 89 90 101 static public ClassLoader getClassLoader( 102 ClassLoader parent, 103 String codebase, 104 AgentProfile profile, 105 MAFAgentSystem provider) 106 { 107 return new SMIClassLoader(parent, codebase, profile, provider); 108 } 109 110 111 121 public byte[] getBytes(String name) 122 { 123 synchronized (my_cache) 124 { 125 byte[] result = null; 126 result = (byte[])my_cache.get(name); 128 if (result == null && my_provider != null) 129 { 130 try 132 { 133 ClassName[] className = { new ClassName(name, new byte[0]) }; 134 result = my_provider.fetch_class(className, my_codebase, my_profile)[0]; 135 } 136 catch (Exception ex) 137 { 138 System.err.println("warning: could not get bytes from class provider (" + ex + ") => trying codebase interpretation"); 139 } 140 } 141 if (result == null) 142 { 143 try 145 { 146 URL url = null; 148 try 149 { 150 url = new URL(my_codebase); 151 } 152 catch (MalformedURLException e) 153 { 154 } 155 InputStream is = null; 156 boolean is_jar = false; 157 JarInputStream jis = null; 158 JarEntry je = null; 159 if (url == null) 160 { 162 if (my_codebase.endsWith("/")) 163 { 165 is = new FileInputStream(my_codebase + name); 166 } 167 else 168 { 170 try 171 { 172 is = new FileInputStream(my_codebase); 173 jis = new JarInputStream(is); 174 je = jis.getNextJarEntry(); 175 } 176 catch (FileNotFoundException e) 177 { 178 } 179 if (je == null) 180 { 182 is = new FileInputStream(my_codebase + "/" + name); 183 } 184 else 185 { 187 is_jar = true; 188 } 189 } 190 } 191 else 192 { 194 if (my_codebase.endsWith("/")) 195 { 197 url = new URL(url, name); 198 is = url.openConnection().getInputStream(); 199 } 200 else 201 { 203 try 204 { 205 is = url.openConnection().getInputStream(); 206 jis = new JarInputStream(is); 207 je = jis.getNextJarEntry(); 208 } 209 catch (IOException e) 210 { 211 } 212 if (je == null) 213 { 215 url = new URL(my_codebase + "/" + name); 216 is = url.openConnection().getInputStream(); 217 } 218 else 219 { 221 is_jar = true; 222 } 223 } 224 } 225 BufferedInputStream bis = null; 227 if (is_jar) 228 { 230 while (je != null && !je.getName().equals(name)) 231 { 232 je = jis.getNextJarEntry(); 233 } 234 if (je != null) 235 { 236 bis = new BufferedInputStream(jis); 237 } 238 } 239 else 240 { 241 bis = new BufferedInputStream(is); 242 } 243 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 245 int n; 246 while ((n = bis.read()) != -1) 247 { 248 baos.write(n); 249 } 250 result = baos.toByteArray(); 251 } 252 catch (Exception e) 253 { 254 } 255 } 256 if (result != null) 257 { 258 my_cache.put(name, result); 259 } 260 return result; 261 } 262 } 263 264 265 276 SMIClassLoader(ClassLoader parent, String codebase, AgentProfile profile, MAFAgentSystem provider) 277 { 278 super(parent); 279 my_codebase = codebase; 280 my_profile = profile; 281 my_provider = provider; 282 my_cache = ClassCache.getCache(codebase); 283 ClassCache.inc(codebase); 284 } 285 286 287 299 protected Class findClass(String name) 300 throws ClassNotFoundException 301 { 302 synchronized (my_cache) 303 { 304 Class result = null; 305 String pathname = name.replace('.', '/') + ".class"; 306 try 308 { 309 ClassLoader parent = getParent(); 310 if (parent == null) 311 { 312 parent = getSystemClassLoader(); 313 } 314 result = parent.loadClass(name); 315 } 316 catch (ClassNotFoundException e) 317 { 318 result = (Class )my_cache.get(name); 320 if (result == null) 321 { 322 byte[] code = getBytes(pathname); 324 if (code != null) 325 { 326 result = defineClass(name, code, 0, code.length); 327 my_cache.put(name, result); 328 } 329 else 330 { 331 throw new ClassNotFoundException (name); 332 } 333 } 334 } 335 return result; 336 } 337 } 338 339 340 347 public URL findResource(String name) 348 { 349 try 350 { 351 return new URL(null, "bytearray:" + my_codebase + "#" + name, new ByteArrayURLStreamHandler(this)); 352 } 353 catch (Exception ex) 354 { 355 ex.printStackTrace(); 356 return null; 357 } 358 } 359 360 361 369 public Enumeration findResources(String name) 370 { 371 Vector result = new Vector(1); 372 URL url = findResource(name); 373 if (url != null) 374 { 375 result.add(url); 376 } 377 return result.elements(); 378 } 379 380 381 public String toString() 382 { 383 return "SMIClassLoader for codebase " + my_codebase; 384 } 385 386 387 public void finalize() 388 { 389 ClassCache.dec(my_codebase); 390 } 391 } 392 | Popular Tags |