Skip to content

Commit

Permalink
Use generic interfaces for checking socket access (elastic#22753)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Tim-Brooks authored Jan 23, 2017
1 parent 28cfc53 commit 7f20b93
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public static <T> T doPrivileged(PrivilegedAction<T> operation) {
return AccessController.doPrivileged(operation);
}

public static void doPrivilegedVoid(DiscoveryRunnable action) {
public static void doPrivilegedVoid(Runnable action) {
SpecialPermission.check();
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
action.execute();
action.run();
return null;
});
}
Expand All @@ -58,9 +58,4 @@ public static <T> T doPrivilegedIOException(PrivilegedExceptionAction<T> operati
throw (IOException) e.getCause();
}
}

@FunctionalInterface
public interface DiscoveryRunnable {
void execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.common.blobstore.gcs.util;

import org.elasticsearch.SpecialPermission;
import org.elasticsearch.common.CheckedRunnable;

import java.io.IOException;
import java.net.SocketPermission;
Expand All @@ -46,20 +47,15 @@ public static <T> T doPrivilegedIOException(PrivilegedExceptionAction<T> operati
}
}

public static void doPrivilegedVoidIOException(StorageRunnable action) throws IOException {
public static void doPrivilegedVoidIOException(CheckedRunnable<IOException> action) throws IOException {
SpecialPermission.check();
try {
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
action.executeCouldThrow();
action.run();
return null;
});
} catch (PrivilegedActionException e) {
throw (IOException) e.getCause();
}
}

@FunctionalInterface
public interface StorageRunnable {
void executeCouldThrow() throws IOException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,25 @@ public final class SocketAccess {
private SocketAccess() {}

public static <T> T doPrivileged(PrivilegedAction<T> operation) {
checkSpecialPermission();
SpecialPermission.check();
return AccessController.doPrivileged(operation);
}

public static <T> T doPrivilegedIOException(PrivilegedExceptionAction<T> operation) throws IOException {
checkSpecialPermission();
SpecialPermission.check();
try {
return AccessController.doPrivileged(operation);
} catch (PrivilegedActionException e) {
throw (IOException) e.getCause();
}
}

public static void doPrivilegedVoid(StorageRunnable action) {
checkSpecialPermission();
public static void doPrivilegedVoid(Runnable action) {
SpecialPermission.check();
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
action.execute();
action.run();
return null;
});
}

private static void checkSpecialPermission() {
SpecialPermission.check();
}

@FunctionalInterface
public interface StorageRunnable {
void execute();
}

}

0 comments on commit 7f20b93

Please sign in to comment.