nicer loading code

This commit is contained in:
Anton Keks 2013-04-30 01:48:07 +03:00
parent c4b5b537d2
commit 3a8c409acb
3 changed files with 6 additions and 11 deletions

View File

@ -9,17 +9,15 @@ import com.sun.jna.Structure;
* JNA binding for iphlpapi.dll for ICMP and ARP support under Windows
*/
public interface WinIpHlpDll extends Library {
public static WinIpHlpDll dll = Loader.load();
static class Loader {
private static WinIpHlpDll dll;
public static WinIpHlpDll load() {
if (dll != null) return dll;
try {
dll = (WinIpHlpDll) Native.loadLibrary("iphlpapi", WinIpHlpDll.class);
return (WinIpHlpDll) Native.loadLibrary("iphlpapi", WinIpHlpDll.class);
}
catch (UnsatisfiedLinkError e) {
dll = (WinIpHlpDll) Native.loadLibrary("icmp", WinIpHlpDll.class);
return (WinIpHlpDll) Native.loadLibrary("icmp", WinIpHlpDll.class);
}
return dll;
}
}

View File

@ -16,6 +16,7 @@ import java.net.InetAddress;
import java.util.Arrays;
import static java.lang.Thread.currentThread;
import static net.azib.ipscan.core.net.WinIpHlpDll.dll;
/**
* Windows-only pinger that uses Microsoft's ICMP.DLL for its job.
@ -28,11 +29,9 @@ import static java.lang.Thread.currentThread;
*/
public class WindowsPinger implements Pinger {
private int timeout;
private WinIpHlpDll dll;
public WindowsPinger(int timeout) {
this.timeout = timeout;
dll = WinIpHlpDll.Loader.load();
}
public PingResult ping(ScanningSubject subject, int count) throws IOException {

View File

@ -2,16 +2,14 @@ package net.azib.ipscan.fetchers;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import net.azib.ipscan.core.net.WinIpHlpDll;
import net.azib.ipscan.core.net.WinIpHlpDll.IpAddrByVal;
import java.net.InetAddress;
public class WinMACFetcher extends MACFetcher {
private WinIpHlpDll dll;
import static net.azib.ipscan.core.net.WinIpHlpDll.dll;
public class WinMACFetcher extends MACFetcher {
@Override public void init() {
dll = WinIpHlpDll.Loader.load();
}
@Override public String resolveMAC(InetAddress address) {