Source for gnu.gcj.runtime.SharedLibLoader

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