Skip to content

Commit

Permalink
Better exception message to tell the user why it is not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Maurer committed Jan 30, 2014
1 parent cd5103f commit f7f808c
Showing 1 changed file with 58 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ private static DatagramChannel newSocket(InternetProtocolFamily ipFamily) {
return newSocket();
}

if (PlatformDependent.javaVersion() < 7) {
throw new UnsupportedOperationException();
}
checkJavaVersion();

try {
return DatagramChannel.open(ProtocolFamilyConverter.convert(ipFamily));
Expand All @@ -94,6 +92,12 @@ private static DatagramChannel newSocket(InternetProtocolFamily ipFamily) {
}
}

private static void checkJavaVersion() {
if (PlatformDependent.javaVersion() < 7) {
throw new UnsupportedOperationException("Only supported on java 7+.");
}
}

/**
* Create a new instance which will use the Operation Systems default {@link InternetProtocolFamily}.
*/
Expand Down Expand Up @@ -350,39 +354,39 @@ public ChannelFuture joinGroup(
public ChannelFuture joinGroup(
InetAddress multicastAddress, NetworkInterface networkInterface,
InetAddress source, ChannelPromise promise) {
if (PlatformDependent.javaVersion() >= 7) {
if (multicastAddress == null) {
throw new NullPointerException("multicastAddress");
}

if (networkInterface == null) {
throw new NullPointerException("networkInterface");
}
checkJavaVersion();

try {
MembershipKey key;
if (source == null) {
key = javaChannel().join(multicastAddress, networkInterface);
} else {
key = javaChannel().join(multicastAddress, networkInterface, source);
}
if (multicastAddress == null) {
throw new NullPointerException("multicastAddress");
}

synchronized (this) {
List<MembershipKey> keys = memberships.get(multicastAddress);
if (keys == null) {
keys = new ArrayList<MembershipKey>();
memberships.put(multicastAddress, keys);
}
keys.add(key);
}
if (networkInterface == null) {
throw new NullPointerException("networkInterface");
}

promise.setSuccess();
} catch (Throwable e) {
promise.setFailure(e);
try {
MembershipKey key;
if (source == null) {
key = javaChannel().join(multicastAddress, networkInterface);
} else {
key = javaChannel().join(multicastAddress, networkInterface, source);
}
} else {
throw new UnsupportedOperationException();

synchronized (this) {
List<MembershipKey> keys = memberships.get(multicastAddress);
if (keys == null) {
keys = new ArrayList<MembershipKey>();
memberships.put(multicastAddress, keys);
}
keys.add(key);
}

promise.setSuccess();
} catch (Throwable e) {
promise.setFailure(e);
}

return promise;
}

Expand Down Expand Up @@ -425,9 +429,8 @@ public ChannelFuture leaveGroup(
public ChannelFuture leaveGroup(
InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source,
ChannelPromise promise) {
if (PlatformDependent.javaVersion() < 7) {
throw new UnsupportedOperationException();
}
checkJavaVersion();

if (multicastAddress == null) {
throw new NullPointerException("multicastAddress");
}
Expand Down Expand Up @@ -479,36 +482,34 @@ public ChannelFuture block(
public ChannelFuture block(
InetAddress multicastAddress, NetworkInterface networkInterface,
InetAddress sourceToBlock, ChannelPromise promise) {
if (PlatformDependent.javaVersion() < 7) {
throw new UnsupportedOperationException();
} else {
if (multicastAddress == null) {
throw new NullPointerException("multicastAddress");
}
if (sourceToBlock == null) {
throw new NullPointerException("sourceToBlock");
}
checkJavaVersion();

if (networkInterface == null) {
throw new NullPointerException("networkInterface");
}
synchronized (this) {
if (memberships != null) {
List<MembershipKey> keys = memberships.get(multicastAddress);
for (MembershipKey key: keys) {
if (networkInterface.equals(key.networkInterface())) {
try {
key.block(sourceToBlock);
} catch (IOException e) {
promise.setFailure(e);
}
if (multicastAddress == null) {
throw new NullPointerException("multicastAddress");
}
if (sourceToBlock == null) {
throw new NullPointerException("sourceToBlock");
}

if (networkInterface == null) {
throw new NullPointerException("networkInterface");
}
synchronized (this) {
if (memberships != null) {
List<MembershipKey> keys = memberships.get(multicastAddress);
for (MembershipKey key: keys) {
if (networkInterface.equals(key.networkInterface())) {
try {
key.block(sourceToBlock);
} catch (IOException e) {
promise.setFailure(e);
}
}
}
}
promise.setSuccess();
return promise;
}
promise.setSuccess();
return promise;
}

/**
Expand Down

0 comments on commit f7f808c

Please sign in to comment.