NetServices move to native code

This commit is contained in:
Doug MacEachern 2007-09-16 18:35:25 +00:00
parent db8b0124d2
commit 58f8755835
3 changed files with 34 additions and 102 deletions

View File

@ -960,6 +960,20 @@ JNIEXPORT jstring SIGAR_JNIx(getNetListenAddress)
return jnet_address_to_string(env, sigar, &address); return jnet_address_to_string(env, sigar, &address);
} }
JNIEXPORT jstring SIGAR_JNIx(getNetServicesName)
(JNIEnv *env, jobject sigar_obj, jint protocol, jlong port)
{
char *name;
dSIGAR(NULL);
if ((name = sigar_net_services_name_get(sigar, protocol, port))) {
return JENV->NewStringUTF(env, name);
}
else {
return NULL;
}
}
JNIEXPORT jstring SIGAR_JNI(NetConnection_getTypeString) JNIEXPORT jstring SIGAR_JNI(NetConnection_getTypeString)
(JNIEnv *env, jobject obj) (JNIEnv *env, jobject obj)
{ {

View File

@ -18,108 +18,32 @@
package org.hyperic.sigar; package org.hyperic.sigar;
import java.io.BufferedReader; /**
import java.io.File; * @deprecated
import java.io.FileReader; * @see org.hyperic.sigar.Sigar#getNetServicesName
import java.io.IOException; */
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class NetServices { public class NetServices {
private static final String SERVICE_FILE; private static NetServices instance;
private static Map udpServices = null; private Sigar sigar;
private static Map tcpServices = null;
static { private NetServices() {
String defaultFile; this.sigar = new Sigar();
if (SigarLoader.IS_WIN32) {
defaultFile = "C:\\windows\\system32\\drivers\\etc\\services";
}
else {
defaultFile = "/etc/services";
} }
SERVICE_FILE = protected void finalize() {
System.getProperty("sigar.net.services.file", defaultFile); this.sigar.close();
} }
interface EntryReader { private static NetServices getInstance() {
public void process(String name, String port, List aliases); if (instance == null) {
instance = new NetServices();
}
return instance;
} }
static void parse(String fileName, EntryReader entry) { private static String getServiceName(int protocol, long port) {
File file = new File(fileName); return getInstance().sigar.getNetServicesName(protocol, port);
if (!file.exists()) {
return;
}
BufferedReader reader = null;
ArrayList aliases = new ArrayList();
try {
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if ((line.length() == 0) || (line.charAt(0) == '#')) {
continue;
}
aliases.clear();
int ix = line.indexOf("#");
if (ix != -1) {
line = line.substring(0, ix);
}
StringTokenizer st = new StringTokenizer(line, " \t");
if (st.countTokens() < 2) {
continue;
}
String name = st.nextToken().trim();
String port = st.nextToken().trim();
while (st.hasMoreTokens()) {
aliases.add(st.nextToken().trim());
}
entry.process(name, port, aliases);
}
} catch (IOException e) {
return;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
}
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) {
@ -135,18 +59,10 @@ public class NetServices {
} }
public static String getTcpName(long port) { public static String getTcpName(long port) {
if (tcpServices == null) { return getServiceName(NetFlags.CONN_TCP, port);
tcpServices = new HashMap();
parseServices("tcp", tcpServices);
}
return (String)tcpServices.get(new Long(port));
} }
public static String getUdpName(long port) { public static String getUdpName(long port) {
if (udpServices == null) { return getServiceName(NetFlags.CONN_UDP, port);
udpServices = new HashMap();
parseServices("udp", udpServices);
}
return (String)udpServices.get(new Long(port));
} }
} }

View File

@ -760,6 +760,8 @@ public class Sigar implements SigarProxy {
return getNetListenAddress(Long.parseLong(port)); return getNetListenAddress(Long.parseLong(port));
} }
public native String getNetServicesName(int protocol, long port);
public NetStat getNetStat() public NetStat getNetStat()
throws SigarException { throws SigarException {
NetStat netstat = new NetStat(); NetStat netstat = new NetStat();