1 /* Copyright (C) 2001, 2003, 2004, 2005 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 package gnu
.gcj
.runtime
;
10 import java
.lang
.ref
.WeakReference
;
12 import java
.net
.MalformedURLException
;
13 import java
.util
.HashMap
;
14 import java
.security
.*;
17 import java
.util
.Iterator
;
18 import java
.util
.HashSet
;
19 import java
.util
.HashMap
;
20 import java
.nio
.channels
.FileChannel
;
23 public class SharedLibHelper
25 /** Load a shared library, and associate a ClassLoader with it.
26 * @param libname named of shared library (passed to dlopen)
27 * @param parent the parent ClassLoader
28 * @parem flags passed to dlopen
30 SharedLibHelper(String libname
, ClassLoader parent
, CodeSource source
,
31 ProtectionDomain domain
, int flags
)
33 // FIXME: ask security manager first.
37 domain
= new ProtectionDomain(source
,
38 Policy
.getPolicy().getPermissions(source
));
43 public static SharedLibHelper
findHelper (String libname
)
47 Set s
= (Set
)map
.get(libname
);
50 for (Iterator i
=s
.iterator(); i
.hasNext();)
52 WeakReference ref
= (WeakReference
)i
.next();
54 return (SharedLibHelper
) ref
.get();
60 static void copyFile (File in
, File out
) throws IOException
62 FileChannel source
= new FileInputStream(in
).getChannel();
63 FileChannel destination
= new FileOutputStream(out
).getChannel();
64 source
.transferTo(0, source
.size(), destination
);
69 public static SharedLibHelper
findHelper (ClassLoader loader
, String libname
,
73 return findHelper (loader
, libname
, source
, null, tryParents
);
76 public static SharedLibHelper
findHelper (ClassLoader loader
, String libname
,
78 ProtectionDomain domain
,
83 SharedLibHelper result
;
84 Set s
= (Set
)map
.get(libname
);
92 for (Iterator i
=s
.iterator(); i
.hasNext();)
94 WeakReference ref
= (WeakReference
)i
.next();
97 result
= (SharedLibHelper
) ref
.get();
100 // A match succeeds if the library is already
101 // loaded by LOADER or any of its ancestors.
102 ClassLoader l
= loader
;
105 if (result
.loader
== l
)
109 while (tryParents
&& l
!= null);
114 // Oh dear. We've already mapped this shared library, but
115 // with a different class loader. We need to copy it.
119 = File
.createTempFile(new File(libname
).getName(),
120 ".so", new File ("/tmp"));
121 File src
= new File(libname
);
122 copyFile (src
, copy
);
124 libname
= copy
.getPath();
126 catch (IOException e
)
131 result
= new SharedLibHelper(libname
, loader
, source
, domain
, 0);
132 s
.add(new WeakReference(result
));
137 public native void finalize ();
139 public Class
findClass(String name
)
142 Class result
= (Class
) classMap
.get(name
);
145 // We never want to return a class without its supers linked.
146 // It isn't clear from the spec, but this is what other
147 // implementations do in practice.
148 ensureSupersLinked(result
);
153 public URL
findResource (String name
)
156 if (! hasResource(name
))
160 return new URL("gcjlib", "", -1, baseName
+ "!/" + name
);
162 catch (MalformedURLException _
)
168 public native Core
findCore (String name
);
172 synchronized (classMap
)
181 native boolean hasResource(String name
);
183 native void ensureSupersLinked(Class k
);
185 public String
toString ()
187 return "shared object " + baseName
;
190 /** Called during dlopen's processing of the init section. */
191 void registerClass(String name
, Class cls
)
193 classMap
.put(name
, cls
);
196 /** The handle returned by dlopen. */
197 gnu
.gcj
.RawData handler
;
199 /** Holds a _Jv_core_chain for the loader. */
200 gnu
.gcj
.RawData core_chain
;
202 /** Map classnames to Classes. */
203 HashMap classMap
= new HashMap(20);
205 /** Class loader we're helping. */
208 /** Name of base file. */
211 /** Protection domain for loaded classes. */
212 ProtectionDomain domain
;
214 /** Flags to pass to dlopen. FIXME: platform dependent.
215 0 is always "sensible" (defined by us). */
218 /** True if we've been initialized. */
219 boolean initialized
= false;
221 /** Map shared library names to a helper object. This uses weak
222 references in the values so we don't prevent collection. */
223 static HashMap map
= new HashMap ();