1 package jp.naist.se.stigmata.reader;
\r
8 import java.util.ArrayList;
\r
9 import java.util.Iterator;
\r
10 import java.util.List;
\r
12 import jp.naist.se.stigmata.utils.WarClassLoader;
\r
15 * @author Haruaki TAMADA
\r
16 * @version $Revision$ $Date$
\r
18 public class ClasspathContext implements Iterable<URL>{
\r
19 private static ClasspathContext DEFAULT_CONTEXT = new ClasspathContext();
\r
21 private ClasspathContext parent;
\r
22 private List<URL> classpath = new ArrayList<URL>();
\r
23 private ClassLoader loader = null;
\r
25 private ClasspathContext(){
\r
28 public ClasspathContext(ClasspathContext parent){
\r
29 this.parent = parent;
\r
32 public static final ClasspathContext getDefaultContext(){
\r
33 return DEFAULT_CONTEXT;
\r
36 public synchronized void addClasspath(URL url){
\r
41 public void clear(){
\r
45 public Iterator<URL> iterator(){
\r
46 return classpath.iterator();
\r
49 public Iterator<URL> classpath(){
\r
54 final Iterator<URL> parentIterator = parent.classpath();
\r
55 final Iterator<URL> thisIterator = iterator();
\r
56 return new Iterator<URL>(){
\r
57 public boolean hasNext(){
\r
58 boolean next = parentIterator.hasNext();
\r
60 next = thisIterator.hasNext();
\r
65 URL nextObject = null;
\r
66 if(parentIterator.hasNext()){
\r
67 nextObject = parentIterator.next();
\r
70 nextObject = thisIterator.next();
\r
74 public void remove(){
\r
80 public synchronized ClassLoader createClassLoader(){
\r
82 List<URL> list = new ArrayList<URL>();
\r
83 for(Iterator<URL> i = classpath(); i.hasNext(); ){
\r
87 loader = new WarClassLoader(list.toArray(new URL[list.size()]), getClass().getClassLoader());
\r
92 public ClassFileEntry find(String className) throws ClassNotFoundException{
\r
93 ClassLoader loader = createClassLoader();
\r
95 URL resource = loader.getResource(className.replace('.', '/') + ".class");
\r
96 if(resource != null){
\r
97 return new ClassFileEntry(className, resource);
\r
102 public Class<?> findClass(String className) throws ClassNotFoundException{
\r
104 ClassLoader loader = createClassLoader();
\r
106 return loader.loadClass(className);
\r
107 } catch(NoClassDefFoundError e){
\r
108 throw new ClassNotFoundException(e.getMessage(), e);
\r