From 3a8c409acb9fb1bd29be22f5fc85ed7ec828eb7e Mon Sep 17 00:00:00 2001 From: Anton Keks Date: Tue, 30 Apr 2013 01:48:07 +0300 Subject: [PATCH] nicer loading code --- src/net/azib/ipscan/core/net/WinIpHlpDll.java | 8 +++----- src/net/azib/ipscan/core/net/WindowsPinger.java | 3 +-- src/net/azib/ipscan/fetchers/WinMACFetcher.java | 6 ++---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/net/azib/ipscan/core/net/WinIpHlpDll.java b/src/net/azib/ipscan/core/net/WinIpHlpDll.java index d5a853a1..c4b16fd7 100644 --- a/src/net/azib/ipscan/core/net/WinIpHlpDll.java +++ b/src/net/azib/ipscan/core/net/WinIpHlpDll.java @@ -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; } } diff --git a/src/net/azib/ipscan/core/net/WindowsPinger.java b/src/net/azib/ipscan/core/net/WindowsPinger.java index 1f5bc98c..21fe0373 100644 --- a/src/net/azib/ipscan/core/net/WindowsPinger.java +++ b/src/net/azib/ipscan/core/net/WindowsPinger.java @@ -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 { diff --git a/src/net/azib/ipscan/fetchers/WinMACFetcher.java b/src/net/azib/ipscan/fetchers/WinMACFetcher.java index a5dc9e35..6a24c42f 100644 --- a/src/net/azib/ipscan/fetchers/WinMACFetcher.java +++ b/src/net/azib/ipscan/fetchers/WinMACFetcher.java @@ -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) {