move etc/services parsing to its own class, NetServices

This commit is contained in:
Doug MacEachern 2005-05-08 16:28:46 +00:00
parent 7851cc3299
commit 6e8e15e4e5
3 changed files with 103 additions and 98 deletions

View File

@ -1,14 +1,8 @@
package net.hyperic.sigar;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
public class NetPortMap {
@ -17,25 +11,6 @@ public class NetPortMap {
private Map outbound;
private int[] states;
private static final String SERVICE_FILE;
private static Map udpServices = null;
private static Map tcpServices = null;
static {
String defaultFile;
if (SigarLoader.IS_WIN32) {
defaultFile = "C:\\windows\\system32\\drivers\\etc\\services";
}
else {
defaultFile = "/etc/services";
}
SERVICE_FILE =
System.getProperty("sigar.net.services.file", defaultFile);
}
public NetPortMap() {
this.sigar = new Sigar();
}
@ -52,77 +27,6 @@ public class NetPortMap {
}
}
private static void parseServices(String type, Map services) {
File file = new File(SERVICE_FILE);
if (!file.exists()) {
return;
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String name, protocol;
Long port;
line = line.trim();
if ((line.length() == 0) || (line.charAt(0) == '#')) {
continue;
}
StringTokenizer st = new StringTokenizer(line, " \t/#");
if (st.countTokens() < 3) {
continue;
}
name = st.nextToken().trim();
String pnum = st.nextToken().trim();
protocol = st.nextToken().trim();
if (!type.equals(protocol)) {
continue;
}
services.put(Long.valueOf(pnum), name);
}
} catch (IOException e) {
return;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
}
public static String getServiceName(String protocol, long port) {
if (protocol.equals("tcp")) {
return getTcpServiceName(port);
}
else if (protocol.equals("udp")) {
return getUdpServiceName(port);
}
else {
return String.valueOf(port);
}
}
public static String getTcpServiceName(long port) {
if (tcpServices == null) {
tcpServices = new HashMap();
parseServices("tcp", tcpServices);
}
return (String)tcpServices.get(new Long(port));
}
public static String getUdpServiceName(long port) {
if (udpServices == null) {
udpServices = new HashMap();
parseServices("udp", udpServices);
}
return (String)udpServices.get(new Long(port));
}
public void stat() throws SigarException {
int flags =
NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT |

View File

@ -0,0 +1,101 @@
package net.hyperic.sigar;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class NetServices {
private static final String SERVICE_FILE;
private static Map udpServices = null;
private static Map tcpServices = null;
static {
String defaultFile;
if (SigarLoader.IS_WIN32) {
defaultFile = "C:\\windows\\system32\\drivers\\etc\\services";
}
else {
defaultFile = "/etc/services";
}
SERVICE_FILE =
System.getProperty("sigar.net.services.file", defaultFile);
}
private static void parseServices(String type, Map services) {
File file = new File(SERVICE_FILE);
if (!file.exists()) {
return;
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String name, protocol;
Long port;
line = line.trim();
if ((line.length() == 0) || (line.charAt(0) == '#')) {
continue;
}
StringTokenizer st = new StringTokenizer(line, " \t/#");
if (st.countTokens() < 3) {
continue;
}
name = st.nextToken().trim();
String pnum = st.nextToken().trim();
protocol = st.nextToken().trim();
if (!type.equals(protocol)) {
continue;
}
services.put(Long.valueOf(pnum), name);
}
} catch (IOException e) {
return;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
}
public static String getName(String protocol, long port) {
if (protocol.equals("tcp")) {
return getTcpName(port);
}
else if (protocol.equals("udp")) {
return getUdpName(port);
}
else {
return String.valueOf(port);
}
}
public static String getTcpName(long port) {
if (tcpServices == null) {
tcpServices = new HashMap();
parseServices("tcp", tcpServices);
}
return (String)tcpServices.get(new Long(port));
}
public static String getUdpName(long port) {
if (udpServices == null) {
udpServices = new HashMap();
parseServices("udp", udpServices);
}
return (String)udpServices.get(new Long(port));
}
}

View File

@ -4,10 +4,10 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import net.hyperic.sigar.NetServices;
import net.hyperic.sigar.SigarException;
import net.hyperic.sigar.NetConnection;
import net.hyperic.sigar.NetFlags;
import net.hyperic.sigar.NetPortMap;
/**
* Display network connections.
@ -101,7 +101,7 @@ public class Netstat extends SigarCommandBase {
return "*";
}
if (!isNumeric) {
String service = NetPortMap.getServiceName(proto, port);
String service = NetServices.getName(proto, port);
if (service != null) {
return service;
}