Use generic interfaces for checking socket access (#22753)

This commit replaces specialized functional interfaces in various
plugins with generic options. Instead of creating `StorageRunnable`
interfaces in every plugin we can just use `Runnable` or `CheckedRunnable`.
This commit is contained in:
Tim Brooks 2017-01-23 16:34:24 -06:00 committed by GitHub
parent 28cfc533e2
commit 7f20b93051
3 changed files with 10 additions and 28 deletions

View File

@ -42,10 +42,10 @@ public final class Access {
return AccessController.doPrivileged(operation); return AccessController.doPrivileged(operation);
} }
public static void doPrivilegedVoid(DiscoveryRunnable action) { public static void doPrivilegedVoid(Runnable action) {
SpecialPermission.check(); SpecialPermission.check();
AccessController.doPrivileged((PrivilegedAction<Void>) () -> { AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
action.execute(); action.run();
return null; return null;
}); });
} }
@ -58,9 +58,4 @@ public final class Access {
throw (IOException) e.getCause(); throw (IOException) e.getCause();
} }
} }
@FunctionalInterface
public interface DiscoveryRunnable {
void execute();
}
} }

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.blobstore.gcs.util; package org.elasticsearch.common.blobstore.gcs.util;
import org.elasticsearch.SpecialPermission; import org.elasticsearch.SpecialPermission;
import org.elasticsearch.common.CheckedRunnable;
import java.io.IOException; import java.io.IOException;
import java.net.SocketPermission; import java.net.SocketPermission;
@ -46,20 +47,15 @@ public final class SocketAccess {
} }
} }
public static void doPrivilegedVoidIOException(StorageRunnable action) throws IOException { public static void doPrivilegedVoidIOException(CheckedRunnable<IOException> action) throws IOException {
SpecialPermission.check(); SpecialPermission.check();
try { try {
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> { AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
action.executeCouldThrow(); action.run();
return null; return null;
}); });
} catch (PrivilegedActionException e) { } catch (PrivilegedActionException e) {
throw (IOException) e.getCause(); throw (IOException) e.getCause();
} }
} }
@FunctionalInterface
public interface StorageRunnable {
void executeCouldThrow() throws IOException;
}
} }

View File

@ -38,12 +38,12 @@ public final class SocketAccess {
private SocketAccess() {} private SocketAccess() {}
public static <T> T doPrivileged(PrivilegedAction<T> operation) { public static <T> T doPrivileged(PrivilegedAction<T> operation) {
checkSpecialPermission(); SpecialPermission.check();
return AccessController.doPrivileged(operation); return AccessController.doPrivileged(operation);
} }
public static <T> T doPrivilegedIOException(PrivilegedExceptionAction<T> operation) throws IOException { public static <T> T doPrivilegedIOException(PrivilegedExceptionAction<T> operation) throws IOException {
checkSpecialPermission(); SpecialPermission.check();
try { try {
return AccessController.doPrivileged(operation); return AccessController.doPrivileged(operation);
} catch (PrivilegedActionException e) { } catch (PrivilegedActionException e) {
@ -51,21 +51,12 @@ public final class SocketAccess {
} }
} }
public static void doPrivilegedVoid(StorageRunnable action) { public static void doPrivilegedVoid(Runnable action) {
checkSpecialPermission(); SpecialPermission.check();
AccessController.doPrivileged((PrivilegedAction<Void>) () -> { AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
action.execute(); action.run();
return null; return null;
}); });
} }
private static void checkSpecialPermission() {
SpecialPermission.check();
}
@FunctionalInterface
public interface StorageRunnable {
void execute();
}
} }