1 36 package org.ungoverned.moduleloader; 37 38 import java.net.URL ; 39 import java.security.CodeSource ; 40 import java.security.SecureClassLoader ; 41 import java.security.cert.Certificate ; 42 import java.util.Enumeration ; 43 import java.util.Vector ; 44 45 114 public class ModuleClassLoader extends SecureClassLoader 115 { 116 private ModuleManager m_mgr = null; 117 private Module m_module = null; 118 119 128 protected ModuleClassLoader(ModuleManager mgr, Module module) 129 { 130 super(ModuleClassLoader.class.getClassLoader()); 131 m_mgr = mgr; 132 m_module = module; 133 } 134 135 152 protected Class loadClass(String name, boolean resolve) 153 throws ClassNotFoundException 154 { 155 Class c = findLoadedClass(name); 157 if (c == null) 158 { 159 try 160 { 161 if (getParent() != null) 164 { 165 c = getParent().loadClass(name); 166 } 167 179 180 } 181 catch (ClassNotFoundException ex) 182 { 183 if (m_mgr.getSearchPolicy() != null) 186 { 187 c = m_mgr.getSearchPolicy().findClass(m_module, name); 188 } 189 190 if (c == null) 191 { 192 c = findClass(name); 193 } 194 } 195 } 196 197 if (resolve) 198 { 199 resolveClass(c); 200 } 201 202 return c; 203 } 204 205 215 protected Class findClass(String name) throws ClassNotFoundException 217 { 218 Class clazz = findLoadedClass(name); 219 220 if (clazz == null) 221 { 222 String actual = name.replace('.', '/') + ".class"; 223 ResourceSource[] sources = m_module.getResourceSources(); 224 for (int i = 0; 225 (clazz == null) && (sources != null) && (i < sources.length); 226 i++) 227 { 228 byte[] bytes = sources[i].getBytes(actual); 229 if (bytes != null) 230 { 231 URL url = m_mgr.getURLPolicy().createCodeSourceURL( 238 m_mgr, m_module); 239 240 if (url != null) 244 { 245 CodeSource cs = new CodeSource (url, (Certificate []) null); 246 clazz = defineClass(name, bytes, 0, bytes.length, cs); 247 } 248 else 249 { 250 clazz = defineClass(name, bytes, 0, bytes.length); 251 } 252 } 253 } 254 } 255 256 if (clazz != null) 257 { 258 return clazz; 259 } 260 261 throw new ClassNotFoundException (name); 262 } 263 264 279 public Class searchForClass(String name) 280 { 281 try 282 { 283 return findClass(name); 284 } catch (Throwable th) { 285 } 289 return null; 290 } 291 292 302 public URL getResource(String name) 303 { 304 URL url = null; 305 306 if (getParent() != null) 308 { 309 url = getParent().getResource(name); 310 } 311 316 if (url == null) 318 { 319 if (m_mgr.getSearchPolicy() != null) 322 { 323 try 324 { 325 url = m_mgr.getSearchPolicy().findResource(m_module, name); 326 } 327 catch (ResourceNotFoundException ex) 328 { 329 return null; 333 } 334 } 335 } 336 337 if (url == null) 339 { 340 url = findResource(name); 341 } 342 343 return url; 344 } 345 346 355 protected URL findResource(String name) 356 { 357 if (name.startsWith("/")) 359 { 360 name = name.substring(1); 361 } 362 363 URL url = null; 364 365 if (url == null) 368 { 369 ResourceSource[] sources = m_module.getResourceSources(); 370 for (int i = 0; 371 (url == null) && (sources != null) && (i < sources.length); 372 i++) 373 { 374 if (sources[i].hasResource(name)) 375 { 376 url = m_mgr.getURLPolicy().createResourceURL(m_mgr, m_module, i, name); 377 } 378 } 379 } 380 381 return url; 382 } 383 384 399 public URL searchForResource(String name) 400 { 401 try 402 { 403 URL url = findResource(name); 404 return url; 405 } 406 catch (Throwable th) 407 { 408 } 410 return null; 411 } 412 413 protected Enumeration findResources(String name) 414 { 415 Vector v = new Vector (); 416 417 if (name.startsWith("/")) 419 { 420 name = name.substring(1); 421 } 422 423 426 ResourceSource[] sources = m_module.getResourceSources(); 427 for (int i = 0; (sources != null) && (i < sources.length); i++) 428 { 429 if (sources[i].hasResource(name)) 430 { 431 v.addElement(m_mgr.getURLPolicy().createResourceURL(m_mgr, m_module, i, name)); 432 } 433 } 434 435 return v.elements(); 436 } 437 438 447 protected String findLibrary(String name) 448 { 449 if (name.startsWith("/")) 451 { 452 name = name.substring(1); 453 } 454 455 LibrarySource[] sources = m_module.getLibrarySources(); 456 for (int i = 0; 457 (sources != null) && (i < sources.length); 458 i++) 459 { 460 String path = sources[i].getPath(name); 461 if (path != null) 462 { 463 return path; 464 } 465 } 466 467 return null; 468 } 469 } | Popular Tags |