1:
8:
9: package ;
10: import ;
11: import ;
12: import ;
13: import ;
14: import ;
15: import ;
16: import ;
17: import ;
18: import ;
19: import ;
20: import ;
21: import ;
22:
23: public class SharedLibHelper
24: {
25:
30: SharedLibHelper(String libname, ClassLoader parent, CodeSource source,
31: ProtectionDomain domain, int flags)
32: {
33:
34: loader = parent;
35: baseName = libname;
36: if (domain == null)
37: domain = new ProtectionDomain(source,
38: Policy.getPolicy().getPermissions(source));
39: this.domain = domain;
40: this.flags = flags;
41: }
42:
43: public static SharedLibHelper findHelper (String libname)
44: {
45: synchronized (map)
46: {
47: Set s = (Set)map.get(libname);
48: if (s == null)
49: return null;
50: for (Iterator i=s.iterator(); i.hasNext();)
51: {
52: WeakReference ref = (WeakReference)i.next();
53: if (ref != null)
54: return (SharedLibHelper) ref.get();
55: }
56: return null;
57: }
58: }
59:
60: static void copyFile (File in, File out) throws IOException
61: {
62: FileChannel source = new FileInputStream(in).getChannel();
63: FileChannel destination = new FileOutputStream(out).getChannel();
64: source.transferTo(0, source.size(), destination);
65: source.close();
66: destination.close();
67: }
68:
69: public static SharedLibHelper findHelper (ClassLoader loader, String libname,
70: CodeSource source,
71: boolean tryParents)
72: {
73: return findHelper (loader, libname, source, null, tryParents);
74: }
75:
76: public static SharedLibHelper findHelper (ClassLoader loader, String libname,
77: CodeSource source,
78: ProtectionDomain domain,
79: boolean tryParents)
80: {
81: synchronized (map)
82: {
83: SharedLibHelper result;
84: Set s = (Set)map.get(libname);
85: if (s == null)
86: {
87: s = new HashSet();
88: map.put(libname, s);
89: }
90: else
91: {
92: for (Iterator i=s.iterator(); i.hasNext();)
93: {
94: WeakReference ref = (WeakReference)i.next();
95: if (ref != null)
96: {
97: result = (SharedLibHelper) ref.get();
98: if (result != null)
99: {
100:
101:
102: ClassLoader l = loader;
103: do
104: {
105: if (result.loader == l)
106: return result;
107: l = l.getParent();
108: }
109: while (tryParents && l != null);
110: }
111: }
112: }
113:
114:
115:
116: try
117: {
118: File copy
119: = File.createTempFile(new File(libname).getName(),
120: ".so", new File ("/tmp"));
121: File src = new File(libname);
122: copyFile (src, copy);
123: copy.deleteOnExit();
124: libname = copy.getPath();
125: }
126: catch (IOException e)
127: {
128: return null;
129: }
130: }
131: result = new SharedLibHelper(libname, loader, source, domain, 0);
132: s.add(new WeakReference(result));
133: return result;
134: }
135: }
136:
137: public native void finalize ();
138:
139: public Class findClass(String name)
140: {
141: ensureInit();
142: Class result = (Class) classMap.get(name);
143: if (result != null)
144: {
145:
146:
147:
148: ensureSupersLinked(result);
149: }
150: return result;
151: }
152:
153: public URL findResource (String name)
154: {
155: ensureInit();
156: if (! hasResource(name))
157: return null;
158: try
159: {
160: return new URL("gcjlib", "", -1, baseName + "!/" + name);
161: }
162: catch (MalformedURLException _)
163: {
164: }
165: return null;
166: }
167:
168: public native Core findCore (String name);
169:
170: void ensureInit()
171: {
172: synchronized (classMap)
173: {
174: if (initialized)
175: return;
176: init();
177: initialized = true;
178: }
179: }
180:
181: native boolean hasResource(String name);
182: native void init();
183: native void ensureSupersLinked(Class k);
184:
185: public String toString ()
186: {
187: return "shared object " + baseName;
188: }
189:
190:
191: void registerClass(String name, Class cls)
192: {
193: classMap.put(name, cls);
194: }
195:
196:
197: gnu.gcj.RawData handler;
198:
199:
200: gnu.gcj.RawData core_chain;
201:
202:
203: HashMap classMap = new HashMap(20);
204:
205:
206: ClassLoader loader;
207:
208:
209: String baseName;
210:
211:
212: ProtectionDomain domain;
213:
214:
216: int flags;
217:
218:
219: boolean initialized = false;
220:
221:
223: static HashMap map = new HashMap ();
224: }