Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / gnu / gcj / runtime / SharedLibHelper.java
blobbdf7c0453ae0562ef6db5554b1698bcd00d118dd
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
7 details. */
9 package gnu.gcj.runtime;
10 import java.lang.ref.WeakReference;
11 import java.net.URL;
12 import java.net.MalformedURLException;
13 import java.util.HashMap;
14 import java.security.*;
15 import gnu.gcj.Core;
16 import java.util.Set;
17 import java.util.Iterator;
18 import java.util.HashSet;
19 import java.util.HashMap;
20 import java.nio.channels.FileChannel;
21 import java.io.*;
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.
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;
43 public static SharedLibHelper findHelper (String libname)
45 synchronized (map)
47 Set s = (Set)map.get(libname);
48 if (s == null)
49 return null;
50 for (Iterator i=s.iterator(); i.hasNext();)
52 WeakReference ref = (WeakReference)i.next();
53 if (ref != null)
54 return (SharedLibHelper) ref.get();
56 return null;
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);
65 source.close();
66 destination.close();
69 public static SharedLibHelper findHelper (ClassLoader loader, String libname,
70 CodeSource source,
71 boolean tryParents)
73 return findHelper (loader, libname, source, null, tryParents);
76 public static SharedLibHelper findHelper (ClassLoader loader, String libname,
77 CodeSource source,
78 ProtectionDomain domain,
79 boolean tryParents)
81 synchronized (map)
83 SharedLibHelper result;
84 Set s = (Set)map.get(libname);
85 if (s == null)
87 s = new HashSet();
88 map.put(libname, s);
90 else
92 for (Iterator i=s.iterator(); i.hasNext();)
94 WeakReference ref = (WeakReference)i.next();
95 if (ref != null)
97 result = (SharedLibHelper) ref.get();
98 if (result != null)
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)
106 return result;
107 l = l.getParent();
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.
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();
126 catch (IOException e)
128 return null;
131 result = new SharedLibHelper(libname, loader, source, domain, 0);
132 s.add(new WeakReference(result));
133 return result;
137 public native void finalize ();
139 public Class findClass(String name)
141 ensureInit();
142 Class result = (Class) classMap.get(name);
143 if (result != null)
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);
150 return result;
153 public URL findResource (String name)
155 ensureInit();
156 if (! hasResource(name))
157 return null;
160 return new URL("gcjlib", "", -1, baseName + "!/" + name);
162 catch (MalformedURLException _)
165 return null;
168 public native Core findCore (String name);
170 void ensureInit()
172 synchronized (classMap)
174 if (initialized)
175 return;
176 init();
177 initialized = true;
181 native boolean hasResource(String name);
182 native void init();
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. */
206 ClassLoader loader;
208 /** Name of base file. */
209 String baseName;
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). */
216 int flags;
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 ();