make service parser more generic

This commit is contained in:
Doug MacEachern 2006-02-27 00:22:03 +00:00
parent b3fda3e6c1
commit 7d2596e0fe
1 changed files with 50 additions and 16 deletions

View File

@ -4,6 +4,8 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@ -27,37 +29,42 @@ public class NetServices {
SERVICE_FILE = SERVICE_FILE =
System.getProperty("sigar.net.services.file", defaultFile); System.getProperty("sigar.net.services.file", defaultFile);
} }
private static void parseServices(String type, Map services) { private interface EntryReader {
File file = new File(SERVICE_FILE); public void process(String name, String port, List aliases);
}
private static void parse(String fileName, EntryReader entry) {
File file = new File(fileName);
if (!file.exists()) { if (!file.exists()) {
return; return;
} }
BufferedReader reader = null; BufferedReader reader = null;
ArrayList aliases = new ArrayList();
try { try {
reader = new BufferedReader(new FileReader(file)); reader = new BufferedReader(new FileReader(file));
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
String name, protocol;
line = line.trim(); line = line.trim();
if ((line.length() == 0) || (line.charAt(0) == '#')) { if ((line.length() == 0) || (line.charAt(0) == '#')) {
continue; continue;
} }
aliases.clear();
StringTokenizer st = new StringTokenizer(line, " \t/#"); int ix = line.indexOf("#");
if (st.countTokens() < 3) { if (ix != -1) {
line = line.substring(0, ix);
}
StringTokenizer st = new StringTokenizer(line, " \t");
if (st.countTokens() < 2) {
continue; continue;
} }
name = st.nextToken().trim(); String name = st.nextToken().trim();
String pnum = st.nextToken().trim(); String port = st.nextToken().trim();
protocol = st.nextToken().trim(); while (st.hasMoreTokens()) {
if (!type.equals(protocol)) { aliases.add(st.nextToken().trim());
continue;
} }
services.put(Long.valueOf(pnum), name); entry.process(name, port, aliases);
} }
} catch (IOException e) { } catch (IOException e) {
return; return;
@ -70,6 +77,33 @@ public class NetServices {
} }
} }
private static class ServicesReader implements EntryReader {
private String protocol;
private Map services;
private ServicesReader(String protocol, Map services) {
this.protocol = protocol;
this.services = services;
}
public void process(String name, String port, List aliases) {
String pnum, protocol;
int ix = port.indexOf('/');
if (ix == -1) {
return;
}
pnum = port.substring(0, ix);
protocol = port.substring(ix+1);
if (this.protocol.equals(protocol)) {
this.services.put(Long.valueOf(pnum), name);
}
}
}
private static void parseServices(String type, Map services) {
parse(SERVICE_FILE, new ServicesReader(type, services));
}
public static String getName(String protocol, long port) { public static String getName(String protocol, long port) {
if (protocol.equals("tcp")) { if (protocol.equals("tcp")) {
return getTcpName(port); return getTcpName(port);