28import java.util.HashMap;
30import org.jau.sec.SecurityUtil;
31import org.jau.sys.dl.DynamicLinker;
33 abstract class DynamicLinkerImpl
implements DynamicLinker {
41 private final Object secSync =
new Object();
42 private boolean allLinkPermissionGranted =
false;
48public final void claimAllLinkPermission() throws SecurityException {
49 synchronized( secSync ) {
50 allLinkPermissionGranted =
true;
58public final void releaseAllLinkPermission() throws SecurityException {
59 synchronized( secSync ) {
60 allLinkPermissionGranted =
false;
64 private final void checkLinkPermission(
final String pathname)
throws SecurityException {
65 synchronized( secSync ) {
66 if( !allLinkPermissionGranted ) {
67 SecurityUtil.checkLinkPermission(pathname);
71 private final void checkLinkPermission(
final long libraryHandle)
throws SecurityException {
72 synchronized( secSync ) {
73 if( !allLinkPermissionGranted ) {
74 final LibRef libRef = getLibRef( libraryHandle );
75 if(
null == libRef ) {
76 throw new IllegalArgumentException(
"Library handle 0x"+Long.toHexString(libraryHandle)+
" unknown.");
78 SecurityUtil.checkLinkPermission(libRef.getName());
83 private final void checkAllLinkPermission() throws SecurityException {
84 synchronized( secSync ) {
85 if( !allLinkPermissionGranted ) {
86 SecurityUtil.checkAllLinkPermission();
92 public final long openLibraryGlobal(
final String pathname,
final boolean debug)
throws SecurityException {
93 checkLinkPermission(pathname);
94 final long handle = openLibraryGlobalImpl(pathname);
96 final LibRef libRef = incrLibRefCount(handle, pathname);
97 if( DEBUG || debug ) {
98 System.err.println(
"DynamicLinkerImpl.openLibraryGlobal \""+pathname+
"\": 0x"+Long.toHexString(handle)+
" -> "+libRef+
")");
100 }
else if ( DEBUG || debug ) {
101 System.err.println(
"DynamicLinkerImpl.openLibraryGlobal \""+pathname+
"\" failed, error: "+
getLastError());
105 protected abstract long openLibraryGlobalImpl(
final String pathname)
throws SecurityException;
108 public final long openLibraryLocal(
final String pathname,
final boolean debug)
throws SecurityException {
109 checkLinkPermission(pathname);
110 final long handle = openLibraryLocalImpl(pathname);
112 final LibRef libRef = incrLibRefCount(handle, pathname);
113 if( DEBUG || debug ) {
114 System.err.println(
"DynamicLinkerImpl.openLibraryLocal \""+pathname+
"\": 0x"+Long.toHexString(handle)+
" -> "+libRef+
")");
116 }
else if ( DEBUG || debug ) {
117 System.err.println(
"DynamicLinkerImpl.openLibraryLocal \""+pathname+
"\" failed, error: "+
getLastError());
121 protected abstract long openLibraryLocalImpl(
final String pathname)
throws SecurityException;
124 public final long lookupSymbolGlobal(
final String symbolName)
throws SecurityException {
125 checkAllLinkPermission();
126 final long addr = lookupSymbolGlobalImpl(symbolName);
128 System.err.println(
"DynamicLinkerImpl.lookupSymbolGlobal("+symbolName+
") -> 0x"+Long.toHexString(addr));
132 protected abstract long lookupSymbolGlobalImpl(
final String symbolName)
throws SecurityException;
135 public final long lookupSymbol(
final long libraryHandle,
final String symbolName)
throws SecurityException, IllegalArgumentException {
136 checkLinkPermission(libraryHandle);
137 final long addr = lookupSymbolLocalImpl(libraryHandle, symbolName);
139 System.err.println(
"DynamicLinkerImpl.lookupSymbol(0x"+Long.toHexString(libraryHandle)+
", "+symbolName+
") -> 0x"+Long.toHexString(addr));
143 protected abstract long lookupSymbolLocalImpl(
final long libraryHandle,
final String symbolName)
throws SecurityException;
146 public final void closeLibrary(
final long libraryHandle,
final boolean debug)
throws SecurityException, IllegalArgumentException {
147 final LibRef libRef = decrLibRefCount( libraryHandle );
148 if(
null != libRef ) {
149 checkLinkPermission(libRef.getName());
151 if( DEBUG || debug ) {
152 System.err.println(
"DynamicLinkerImpl.closeLibrary(0x"+Long.toHexString(libraryHandle)+
" -> "+libRef+
")");
154 if( 0 != libraryHandle ) {
155 closeLibraryImpl(libraryHandle);
158 protected abstract void closeLibraryImpl(
final long libraryHandle)
throws SecurityException;
160 private static final HashMap<Long,Object> libHandle2Name =
new HashMap<Long,Object>( 16 );
162 static final class LibRef {
163 LibRef(
final String name) {
167 final int incrRefCount() {
return ++refCount; }
168 final int decrRefCount() {
return --refCount; }
169 final int getRefCount() {
return refCount; }
171 final String getName() {
return name; }
173 public final String toString() {
return "LibRef["+name+
", refCount "+refCount+
"]"; }
175 private final String name;
176 private int refCount;
179 private final LibRef getLibRef(
final long handle) {
180 synchronized( libHandle2Name ) {
181 return (LibRef) libHandle2Name.get(handle);
185 private final LibRef incrLibRefCount(
final long handle,
final String libName) {
186 synchronized( libHandle2Name ) {
187 LibRef libRef = getLibRef(handle);
188 if(
null == libRef ) {
189 libRef =
new LibRef(libName);
190 libHandle2Name.put(handle, libRef);
192 libRef.incrRefCount();
195 System.err.println(
"DynamicLinkerImpl.incrLibRefCount 0x"+Long.toHexString(handle)+
" -> "+libRef+
", libs loaded "+libHandle2Name.size());
201 private final LibRef decrLibRefCount(
final long handle) {
202 synchronized( libHandle2Name ) {
203 final LibRef libRef = getLibRef(handle);
204 if(
null != libRef ) {
205 if( 0 == libRef.decrRefCount() ) {
206 libHandle2Name.remove(handle);
210 System.err.println(
"DynamicLinkerImpl.decrLibRefCount 0x"+Long.toHexString(handle)+
" -> "+libRef+
", libs loaded "+libHandle2Name.size());
String getLastError()
Returns a string containing the last error.