SharedLibLoader.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright (C) 2001, 2003 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. package gnu.gcj.runtime;
  7. import java.io.IOException;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.security.CodeSource;
  11. import java.util.Enumeration;
  12. import java.util.Vector;
  13. /**
  14. * A ClassLoader backed by a gcj-compiled shared library.
  15. * @author Per Bothner <per@bothner.com>, Brainfood Inc.
  16. */
  17. public class SharedLibLoader extends ClassLoader
  18. {
  19. /** Load a shared library, and associate a ClassLoader with it.
  20. * @param libname named of shared library (passed to dlopen)
  21. * @param parent the parent ClassLoader
  22. * @parem flags passed to dlopen
  23. */
  24. public SharedLibLoader(String libname, ClassLoader parent, int flags)
  25. {
  26. super(parent);
  27. URL url;
  28. try
  29. {
  30. url = new URL("file", "", libname);
  31. }
  32. catch (MalformedURLException _)
  33. {
  34. url = null;
  35. }
  36. helper = SharedLibHelper.findHelper(this, libname,
  37. new CodeSource(url, null), true);
  38. }
  39. /** Load a shared library, and asociate a ClassLoader with it.
  40. * @param libname named of shared library (passed to dlopen)
  41. */
  42. public SharedLibLoader(String libname)
  43. {
  44. this(libname, getSystemClassLoader(), 0);
  45. }
  46. public Class findClass(String name)
  47. throws ClassNotFoundException
  48. {
  49. Class cls = helper.findClass(name);
  50. if (cls == null)
  51. throw new ClassNotFoundException(name);
  52. return cls;
  53. }
  54. public URL findResource (String name)
  55. {
  56. return helper.findResource(name);
  57. }
  58. public Enumeration findResources (String name) throws IOException
  59. {
  60. URL url = findResource(name);
  61. if (url == null)
  62. return null;
  63. Vector v = new Vector(1);
  64. v.add(url);
  65. return v.elements();
  66. }
  67. /** The helper that does the work for us. */
  68. SharedLibHelper helper;
  69. }