File: JRIClassLoader.java

package info (click to toggle)
rjava 1.0-14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,188 kB
  • sloc: java: 13,223; ansic: 5,503; sh: 3,776; xml: 325; makefile: 250; perl: 33
file content (121 lines) | stat: -rw-r--r-- 3,172 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.net.URLClassLoader;
import java.net.URL;
import java.util.HashMap;
import java.util.Vector;
import java.util.Enumeration;
import java.io.File;

public class JRIClassLoader extends URLClassLoader {
	HashMap libMap;

	Vector children;

	static JRIClassLoader mainLoader;

	public static JRIClassLoader getMainLoader() {
		if (mainLoader == null) mainLoader = new JRIClassLoader();
		return mainLoader;
	}

	public JRIClassLoader() {
		super(new URL[]{});
		children = new Vector();
		libMap = new HashMap();
		System.out.println("JRIClassLoader: new loader "+this);
	}

	public void registerLoader(DelegatedClassLoader cl) {
		if (!children.contains(cl))
			children.add(cl);
	}

	public void unregisterLoader(DelegatedClassLoader cl) {
		children.removeElement(cl);
	}

	public void registerLibrary(String name, File f) {
		libMap.put(name, f);
	}

	/** add path to the class path list
		@param path string denoting the path to the file or directory */
	public void addClassPath(String path) {
		try {
			File f = new File(path);
			if (f.exists()) addURL(f.toURL());
		} catch (Exception x) {}
	}

	/** add path to the class path list
		@param f file/directory to add to the list */
	public void addClassPath(File f) {
		try {
			if (f.exists()) addURL(f.toURL());
		} catch (Exception x) {}
	}

	protected String findLibrary(String name) {
		String s = null;
		System.out.println("boot findLibrary(\""+name+"\")");
		try {
			for (Enumeration e = children.elements() ; e.hasMoreElements() ;) {
				DelegatedClassLoader cl = (DelegatedClassLoader)e.nextElement();
				if (cl != null) {
					s = cl.delegatedFindLibrary(name);
					if (s != null) {
						System.out.println(" - found delegated answer "+s+" from "+cl);
						return s;
					}
				}
			}
		} catch (Exception ex) {}

		File u = (File) libMap.get(name);
		if (u!=null && u.exists()) s=u.getAbsolutePath();
		System.out.println(" - mapping to "+((s==null)?"<none>":s));

		return s;
	}

	public Class findAndLinkClass(String name) throws ClassNotFoundException {
		Class c = findClass(name);
		resolveClass(c);
		return c;
	}

	protected Class findClass(String name) throws ClassNotFoundException {
		Class cl = null;
		System.out.println("boot findClass(\""+name+"\")");
		for (Enumeration e = children.elements() ; e.hasMoreElements() ;) {
			DelegatedClassLoader ldr = (DelegatedClassLoader)e.nextElement();
			if (ldr != null) {
				try {
					cl = ldr.delegatedFindClass(name);
					if (cl != null) {
						System.out.println(" - found delegated answer "+cl+" from "+ldr);
						return cl;
					}
				} catch (Exception ex) {}
			}
		}
		return super.findClass(name);
	}

	public URL findResource(String name) {
		URL u = null;
		System.out.println("boot findResource(\""+name+"\")");
		for (Enumeration e = children.elements() ; e.hasMoreElements() ;) {
			DelegatedClassLoader ldr = (DelegatedClassLoader)e.nextElement();
			if (ldr != null) {
				try {
					u = ldr.delegatedFindResource(name);
					if (u != null) {
						System.out.println(" - found delegated answer "+u+" from "+ldr);
						return u;
					}
				} catch (Exception ex) {}
			}
		}
		return super.findResource(name);
	}
}