SharedLibHelper.java [plain text]
package gnu.gcj.runtime;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.security.*;
import gnu.gcj.Core;
import java.util.Set;
import java.util.Iterator;
import java.util.HashSet;
import java.util.HashMap;
import java.nio.channels.FileChannel;
import java.io.*;
public class SharedLibHelper
{
SharedLibHelper(String libname, ClassLoader parent, CodeSource source,
ProtectionDomain domain, int flags)
{
loader = parent;
baseName = libname;
if (domain == null)
domain = new ProtectionDomain(source,
Policy.getPolicy().getPermissions(source));
this.domain = domain;
this.flags = flags;
}
public static SharedLibHelper findHelper (String libname)
{
synchronized (map)
{
Set s = (Set)map.get(libname);
if (s == null)
return null;
for (Iterator i=s.iterator(); i.hasNext();)
{
WeakReference ref = (WeakReference)i.next();
if (ref != null)
return (SharedLibHelper) ref.get();
}
return null;
}
}
static void copyFile (File in, File out) throws IOException
{
FileChannel source = new FileInputStream(in).getChannel();
FileChannel destination = new FileOutputStream(out).getChannel();
source.transferTo(0, source.size(), destination);
source.close();
destination.close();
}
public static SharedLibHelper findHelper (ClassLoader loader, String libname,
CodeSource source,
boolean tryParents)
{
return findHelper (loader, libname, source, null, tryParents);
}
public static SharedLibHelper findHelper (ClassLoader loader, String libname,
CodeSource source,
ProtectionDomain domain,
boolean tryParents)
{
synchronized (map)
{
SharedLibHelper result;
Set s = (Set)map.get(libname);
if (s == null)
{
s = new HashSet();
map.put(libname, s);
}
else
{
for (Iterator i=s.iterator(); i.hasNext();)
{
WeakReference ref = (WeakReference)i.next();
if (ref != null)
{
result = (SharedLibHelper) ref.get();
if (result != null)
{
ClassLoader l = loader;
do
{
if (result.loader == l)
return result;
l = l.getParent();
}
while (tryParents && l != null);
}
}
}
try
{
File copy
= File.createTempFile(new File(libname).getName(),
".so", new File ("/tmp"));
File src = new File(libname);
copyFile (src, copy);
copy.deleteOnExit();
libname = copy.getPath();
}
catch (IOException e)
{
return null;
}
}
result = new SharedLibHelper(libname, loader, source, domain, 0);
s.add(new WeakReference(result));
return result;
}
}
public native void finalize ();
public Class findClass(String name)
{
ensureInit();
Class result = (Class) classMap.get(name);
if (result != null)
{
ensureSupersLinked(result);
}
return result;
}
public URL findResource (String name)
{
ensureInit();
if (! hasResource(name))
return null;
try
{
return new URL("gcjlib", "", -1, baseName + "!/" + name);
}
catch (MalformedURLException _)
{
}
return null;
}
public native Core findCore (String name);
void ensureInit()
{
synchronized (classMap)
{
if (initialized)
return;
init();
initialized = true;
}
}
native boolean hasResource(String name);
native void init();
native void ensureSupersLinked(Class k);
public String toString ()
{
return "shared object " + baseName;
}
void registerClass(String name, Class cls)
{
classMap.put(name, cls);
}
gnu.gcj.RawData handler;
gnu.gcj.RawData core_chain;
HashMap classMap = new HashMap(20);
ClassLoader loader;
String baseName;
ProtectionDomain domain;
int flags;
boolean initialized = false;
static HashMap map = new HashMap ();
}