LUCENE-6238: Remove access to sun.misc package from static <clinit> code.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1662317 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2015-02-25 22:16:12 +00:00
parent 0096d32bc2
commit 563c9da606
2 changed files with 22 additions and 30 deletions

View File

@ -384,6 +384,7 @@ abstract class ByteBufferIndexInput extends IndexInput implements RandomAccessIn
* Pass in an implementation of this interface to cleanup ByteBuffers. * Pass in an implementation of this interface to cleanup ByteBuffers.
* MMapDirectory implements this to allow unmapping of bytebuffers with private Java APIs. * MMapDirectory implements this to allow unmapping of bytebuffers with private Java APIs.
*/ */
@FunctionalInterface
static interface BufferCleaner { static interface BufferCleaner {
void freeBuffer(ByteBufferIndexInput parent, ByteBuffer b) throws IOException; void freeBuffer(ByteBufferIndexInput parent, ByteBuffer b) throws IOException;
} }

View File

@ -25,6 +25,7 @@ import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedActionException; import java.security.PrivilegedActionException;
import java.util.Locale; import java.util.Locale;
@ -158,18 +159,16 @@ public class MMapDirectory extends FSDirectory {
/** /**
* <code>true</code>, if this platform supports unmapping mmapped files. * <code>true</code>, if this platform supports unmapping mmapped files.
*/ */
public static final boolean UNMAP_SUPPORTED; public static final boolean UNMAP_SUPPORTED =
static { AccessController.doPrivileged((PrivilegedAction<Boolean>) MMapDirectory::checkUnmapSupported);
boolean v;
private static boolean checkUnmapSupported() {
try { try {
Class.forName("sun.misc.Cleaner"); Class.forName("java.nio.DirectByteBuffer").getMethod("cleaner");
Class.forName("java.nio.DirectByteBuffer") return true;
.getMethod("cleaner");
v = true;
} catch (Exception e) { } catch (Exception e) {
v = false; return false;
} }
UNMAP_SUPPORTED = v;
} }
/** /**
@ -280,27 +279,19 @@ public class MMapDirectory extends FSDirectory {
return newIoe; return newIoe;
} }
private static final BufferCleaner CLEANER = new BufferCleaner() { private static final BufferCleaner CLEANER = (ByteBufferIndexInput parent, ByteBuffer buffer) -> {
@Override try {
public void freeBuffer(final ByteBufferIndexInput parent, final ByteBuffer buffer) throws IOException { AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
try { final Method getCleanerMethod = buffer.getClass().getMethod("cleaner");
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { getCleanerMethod.setAccessible(true);
@Override final Object cleaner = getCleanerMethod.invoke(buffer);
public Void run() throws Exception { if (cleaner != null) {
final Method getCleanerMethod = buffer.getClass() cleaner.getClass().getMethod("clean").invoke(cleaner);
.getMethod("cleaner"); }
getCleanerMethod.setAccessible(true); return null;
final Object cleaner = getCleanerMethod.invoke(buffer); });
if (cleaner != null) { } catch (PrivilegedActionException e) {
cleaner.getClass().getMethod("clean") throw new IOException("Unable to unmap the mapped buffer: " + parent.toString(), e.getCause());
.invoke(cleaner);
}
return null;
}
});
} catch (PrivilegedActionException e) {
throw new IOException("Unable to unmap the mapped buffer: " + parent.toString(), e.getCause());
}
} }
}; };
} }