| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.source.async; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
import java.nio.channels.SelectableChannel; |
| 15 |
import java.nio.channels.SelectionKey; |
| 16 |
import java.util.concurrent.Future; |
| 17 |
import java.util.logging.Level; |
| 18 |
import java.util.logging.Logger; |
| 19 |
|
| 20 |
abstract class ChannelSelectedHandler { |
| 21 |
|
| 22 |
private static final Logger LOGGER = Logger.getLogger(ChannelSelectedHandler.class.getName()); |
| 23 |
|
| 24 |
final Future<?> future; |
| 25 |
|
| 26 |
ChannelSelectedHandler(Future<?> future) { |
| 27 |
this.future = future; |
| 28 |
} |
| 29 |
|
| 30 |
void handleChannelSelected(SelectableChannel channel, SelectionKey selectionKey) { |
| 31 |
if (future.isCancelled()) { |
| 32 |
try { |
| 33 |
channel.close(); |
| 34 |
} catch (IOException e) { |
| 35 |
LOGGER.log(Level.INFO, "Could not close channel", e); |
| 36 |
} |
| 37 |
return; |
| 38 |
} |
| 39 |
handleChannelSelectedAndNotCancelled(channel, selectionKey); |
| 40 |
} |
| 41 |
|
| 42 |
protected abstract void handleChannelSelectedAndNotCancelled(SelectableChannel channel, SelectionKey selectionKey); |
| 43 |
|
| 44 |
} |