Fix a problem where DefaultChannelId prevents Netty 5 from running on Android
- Fixes #2109 - Use reflection to find the current PID
This commit is contained in:
parent
a389d69ba8
commit
6df3bb5a79
@ -22,7 +22,7 @@ import io.netty.util.internal.ThreadLocalRandom;
|
|||||||
import io.netty.util.internal.logging.InternalLogger;
|
import io.netty.util.internal.logging.InternalLogger;
|
||||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory;
|
import java.lang.reflect.Method;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -228,7 +228,21 @@ final class DefaultChannelId implements ChannelId {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static int defaultProcessId() {
|
private static int defaultProcessId() {
|
||||||
String value = ManagementFactory.getRuntimeMXBean().getName();
|
String value;
|
||||||
|
try {
|
||||||
|
ClassLoader loader = ClassLoader.getSystemClassLoader();
|
||||||
|
Class<?> mgmtFactoryType = Class.forName("java.lang.management.ManagementFactory", true, loader);
|
||||||
|
Class<?> runtimeMxBeanType = Class.forName("java.lang.management.RuntimeMXBean", true, loader);
|
||||||
|
|
||||||
|
Method getRuntimeMXBean = mgmtFactoryType.getMethod("getRuntimeMXBean", null);
|
||||||
|
Object bean = getRuntimeMXBean.invoke(null, null);
|
||||||
|
Method getName = runtimeMxBeanType.getDeclaredMethod("getName");
|
||||||
|
value = (String) getName.invoke(bean, null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.debug("Could not invoke ManagementFactory.getRuntimeMXBean().getName(); Android?", e);
|
||||||
|
value = "";
|
||||||
|
}
|
||||||
|
|
||||||
int atIndex = value.indexOf('@');
|
int atIndex = value.indexOf('@');
|
||||||
if (atIndex >= 0) {
|
if (atIndex >= 0) {
|
||||||
value = value.substring(0, atIndex);
|
value = value.substring(0, atIndex);
|
||||||
@ -238,12 +252,13 @@ final class DefaultChannelId implements ChannelId {
|
|||||||
try {
|
try {
|
||||||
pid = Integer.parseInt(value);
|
pid = Integer.parseInt(value);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
|
// value did not contain an integer.
|
||||||
pid = -1;
|
pid = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pid < 0 || pid > MAX_PROCESS_ID) {
|
if (pid < 0 || pid > MAX_PROCESS_ID) {
|
||||||
pid = ThreadLocalRandom.current().nextInt(MAX_PROCESS_ID + 1);
|
pid = ThreadLocalRandom.current().nextInt(MAX_PROCESS_ID + 1);
|
||||||
logger.warn("Failed to find the current process ID; using a random value: {}", pid);
|
logger.warn("Failed to find the current process ID from '{}'; using a random value: {}", value, pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
return pid;
|
return pid;
|
||||||
@ -255,8 +270,6 @@ final class DefaultChannelId implements ChannelId {
|
|||||||
private transient String shortValue;
|
private transient String shortValue;
|
||||||
private transient String longValue;
|
private transient String longValue;
|
||||||
|
|
||||||
public DefaultChannelId() { }
|
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user