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