39 |
package java.lang; |
package java.lang; |
40 |
|
|
41 |
import gnu.classpath.SystemProperties; |
import gnu.classpath.SystemProperties; |
42 |
|
import gnu.classpath.VMStackWalker; |
43 |
|
|
44 |
import java.io.File; |
import java.io.File; |
45 |
import java.io.IOException; |
import java.io.IOException; |
629 |
* before the final ".so" if the VM was invoked by the name "java_g". There |
* before the final ".so" if the VM was invoked by the name "java_g". There |
630 |
* may be a security check, of <code>checkLink</code>. |
* may be a security check, of <code>checkLink</code>. |
631 |
* |
* |
632 |
|
* <p> |
633 |
|
* The library is loaded using the class loader associated with the |
634 |
|
* class associated with the invoking method. |
635 |
|
* |
636 |
* @param filename the file to load |
* @param filename the file to load |
637 |
* @throws SecurityException if permission is denied |
* @throws SecurityException if permission is denied |
638 |
* @throws UnsatisfiedLinkError if the library is not found |
* @throws UnsatisfiedLinkError if the library is not found |
639 |
*/ |
*/ |
640 |
public void load(String filename) |
public void load(String filename) |
641 |
{ |
{ |
642 |
|
load(filename, VMStackWalker.getCallingClassLoader()); |
643 |
|
} |
644 |
|
|
645 |
|
/** |
646 |
|
* Same as <code>load(String)</code> but using the given loader. |
647 |
|
* |
648 |
|
* @param filename the file to load |
649 |
|
* @param loader class loader, or <code>null</code> for the boot loader |
650 |
|
* @throws SecurityException if permission is denied |
651 |
|
* @throws UnsatisfiedLinkError if the library is not found |
652 |
|
*/ |
653 |
|
void load(String filename, ClassLoader loader) |
654 |
|
{ |
655 |
SecurityManager sm = SecurityManager.current; // Be thread-safe! |
SecurityManager sm = SecurityManager.current; // Be thread-safe! |
656 |
if (sm != null) |
if (sm != null) |
657 |
sm.checkLink(filename); |
sm.checkLink(filename); |
658 |
if (loadLib(filename) == 0) |
if (loadLib(filename, loader) == 0) |
659 |
throw new UnsatisfiedLinkError("Could not load library " + filename); |
throw new UnsatisfiedLinkError("Could not load library " + filename); |
660 |
} |
} |
661 |
|
|
663 |
* Do a security check on the filename and then load the native library. |
* Do a security check on the filename and then load the native library. |
664 |
* |
* |
665 |
* @param filename the file to load |
* @param filename the file to load |
666 |
|
* @param loader class loader, or <code>null</code> for the boot loader |
667 |
* @return 0 on failure, nonzero on success |
* @return 0 on failure, nonzero on success |
668 |
* @throws SecurityException if file read permission is denied |
* @throws SecurityException if file read permission is denied |
669 |
*/ |
*/ |
670 |
private static int loadLib(String filename) |
private static int loadLib(String filename, ClassLoader loader) |
671 |
{ |
{ |
672 |
SecurityManager sm = SecurityManager.current; // Be thread-safe! |
SecurityManager sm = SecurityManager.current; // Be thread-safe! |
673 |
if (sm != null) |
if (sm != null) |
674 |
sm.checkRead(filename); |
sm.checkRead(filename); |
675 |
return VMRuntime.nativeLoad(filename); |
return VMRuntime.nativeLoad(filename, loader); |
676 |
} |
} |
677 |
|
|
678 |
/** |
/** |
687 |
* <code>System.mapLibraryName(libname)</code>. There may be a security |
* <code>System.mapLibraryName(libname)</code>. There may be a security |
688 |
* check, of <code>checkLink</code>. |
* check, of <code>checkLink</code>. |
689 |
* |
* |
690 |
|
* <p> |
691 |
|
* The library is loaded using the class loader associated with the |
692 |
|
* class associated with the invoking method. |
693 |
|
* |
694 |
* @param libname the library to load |
* @param libname the library to load |
695 |
* |
* |
696 |
* @throws SecurityException if permission is denied |
* @throws SecurityException if permission is denied |
701 |
*/ |
*/ |
702 |
public void loadLibrary(String libname) |
public void loadLibrary(String libname) |
703 |
{ |
{ |
704 |
|
loadLibrary(libname, VMStackWalker.getCallingClassLoader()); |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
|
* Same as <code>loadLibrary(String)</code> but using the given loader. |
709 |
|
* |
710 |
|
* @param libname the library to load |
711 |
|
* @param loader class loader, or <code>null</code> for the boot loader |
712 |
|
* @throws SecurityException if permission is denied |
713 |
|
* @throws UnsatisfiedLinkError if the library is not found |
714 |
|
*/ |
715 |
|
void loadLibrary(String libname, ClassLoader loader) |
716 |
|
{ |
717 |
SecurityManager sm = SecurityManager.current; // Be thread-safe! |
SecurityManager sm = SecurityManager.current; // Be thread-safe! |
718 |
if (sm != null) |
if (sm != null) |
719 |
sm.checkLink(libname); |
sm.checkLink(libname); |
|
|
|
720 |
String filename; |
String filename; |
721 |
ClassLoader cl = VMSecurityManager.currentClassLoader(); |
if (loader != null && (filename = loader.findLibrary(libname)) != null) |
|
if (cl != null) |
|
722 |
{ |
{ |
723 |
filename = cl.findLibrary(libname); |
if (loadLib(filename, loader) != 0) |
724 |
if (filename != null) |
return; |
|
{ |
|
|
if (loadLib(filename) != 0) |
|
|
return; |
|
|
else |
|
|
throw new UnsatisfiedLinkError("Could not load library " + filename); |
|
|
} |
|
725 |
} |
} |
726 |
|
else |
727 |
filename = VMRuntime.mapLibraryName(libname); |
{ |
728 |
for (int i = 0; i < libpath.length; i++) |
filename = VMRuntime.mapLibraryName(libname); |
729 |
if (loadLib(libpath[i] + filename) != 0) |
for (int i = 0; i < libpath.length; i++) |
730 |
return; |
if (loadLib(libpath[i] + filename, loader) != 0) |
731 |
|
return; |
732 |
throw new UnsatisfiedLinkError("Could not find library " + libname + "."); |
} |
733 |
|
throw new UnsatisfiedLinkError("Native library `" + libname |
734 |
|
+ "' not found (as file `" + filename + "')"); |
735 |
} |
} |
736 |
|
|
737 |
/** |
/** |