public void destroy() throws SystemInfoException { if (this.sigar instanceof Sigar) { try { ((Sigar) this.sigar).close(); } catch (RuntimeException e) { throw new SystemInfoException(e); } } }
public void destroy() throws SystemInfoException { if (this.sigar instanceof Sigar) { try { ((Sigar) this.sigar).close(); } catch (RuntimeException e) { throw new SystemInfoException(e); } } }
public String getHostname() throws SystemInfoException { try { return sigar.getNetInfo().getHostName(); } catch (Exception e) { // For some reason, the native layer failed to get the hostname. // Let's fallback and ask Java for help. But if that fails too, // let's wrap the native layer's exception, since we'll want to // see its cause, which will probably have a more descriptive error message. try { return InetAddress.getLocalHost().getCanonicalHostName(); } catch (UnknownHostException uhe) { throw new SystemInfoException(e); } } }
public String getHostname() throws SystemInfoException { try { return sigar.getNetInfo().getHostName(); } catch (Exception e) { // For some reason, the native layer failed to get the hostname. // Let's fallback and ask Java for help. But if that fails too, // let's wrap the native layer's exception, since we'll want to // see its cause, which will probably have a more descriptive error message. try { return InetAddress.getLocalHost().getCanonicalHostName(); } catch (UnknownHostException uhe) { throw new SystemInfoException(e); } } }
public NetworkAdapterStats getNetworkAdapterStats(String interfaceName) { try { NetInterfaceStat interfaceStat = sigar.getNetInterfaceStat(interfaceName); return new NetworkAdapterStats(interfaceStat); } catch (SigarException e) { throw new SystemInfoException(e); } }
public List<NetConnection> getNetworkConnections(String addressName, int port) { try { int flags = NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT | NetFlags.CONN_TCP; NetConnection[] conns = sigar.getNetConnectionList(flags); InetAddress matchAddress = (addressName != null) ? InetAddress.getByName(addressName) : null; List<NetConnection> list = new ArrayList<NetConnection>(); for (NetConnection conn : conns) { if (port > 0 && (conn.getLocalPort() != port)) { continue; // does not match the port we are looking for } if (matchAddress != null && !matchAddress.equals(InetAddress.getByName(conn.getLocalAddress()))) { continue; // does not match the address we are looking for } list.add(conn); // matches our criteria, add it to the list to be returned to the caller } return list; } catch (SigarException e) { throw new SystemInfoException(e); } catch (UnknownHostException e) { throw new SystemInfoException(e); } }
public List<NetConnection> getNetworkConnections(String addressName, int port) { try { int flags = NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT | NetFlags.CONN_TCP; NetConnection[] conns = sigar.getNetConnectionList(flags); InetAddress matchAddress = (addressName != null) ? InetAddress.getByName(addressName) : null; List<NetConnection> list = new ArrayList<NetConnection>(); for (NetConnection conn : conns) { if (port > 0 && (conn.getLocalPort() != port)) { continue; // does not match the port we are looking for } if (matchAddress != null && !matchAddress.equals(InetAddress.getByName(conn.getLocalAddress()))) { continue; // does not match the address we are looking for } list.add(conn); // matches our criteria, add it to the list to be returned to the caller } return list; } catch (SigarException e) { throw new SystemInfoException(e); } catch (UnknownHostException e) { throw new SystemInfoException(e); } }
private void createFileSystemObject(SigarProxy sigar) { try { // this only needs to be loaded once - it will never change during the lifetime of the file system if (this.fs == null) { this.fs = sigar.getFileSystemMap().getFileSystem(this.mountPoint); } } catch (Exception e) { throw new SystemInfoException("Cannot refresh file system mounted at [" + this.mountPoint + "]", e); } }
public NetworkAdapterStats getNetworkAdapterStats(String interfaceName) { try { NetInterfaceStat interfaceStat = sigar.getNetInterfaceStat(interfaceName); return new NetworkAdapterStats(interfaceStat); } catch (SigarException e) { throw new SystemInfoException(e); } }
/** * Provides the caller with direct access to the Sigar native API via a proxy to Sigar. * This provides the caller with low-level access to platform functionality. * If the Sigar native layer is not available on the platform we are running on, or if * it has been disabled by the user, an exception will be thrown. * * @return the proxy used to access the native layer * * @throws SystemInfoException if native layer is unavailable or has been disabled */ public synchronized static SigarProxy getSigar() { if (isSigarAvailable()) { if (sigarProxy == null) { invocationHandler = new SigarAccessHandler(); sigarProxy = (SigarProxy) Proxy.newProxyInstance(SigarAccess.class.getClassLoader(), new Class[] { SigarProxy.class }, invocationHandler); } return sigarProxy; } else { throw new SystemInfoException("Native layer is not available or has been disabled"); } }
/** * Provides the caller with direct access to the Sigar native API via a proxy to Sigar. * This provides the caller with low-level access to platform functionality. * If the Sigar native layer is not available on the platform we are running on, or if * it has been disabled by the user, an exception will be thrown. * * @return the proxy used to access the native layer * * @throws SystemInfoException if native layer is unavailable or has been disabled */ public synchronized static SigarProxy getSigar() { if (isSigarAvailable()) { if (sigarProxy == null) { invocationHandler = new SigarAccessHandler(); sigarProxy = (SigarProxy) Proxy.newProxyInstance(SigarAccess.class.getClassLoader(), new Class[] { SigarProxy.class }, invocationHandler); } return sigarProxy; } else { throw new SystemInfoException("Native layer is not available or has been disabled"); } }
/** * Gets a list of all the value entries of the given registry key. * * @param root * @param key * * @return list of registry entries for the given registry key * * @throws SystemInfoException */ public List<RegistryEntry> getRegistryEntries(Root root, String key) throws SystemInfoException { try { List<String> names = getRegistryValueNames(root, key); List<RegistryEntry> entries = new ArrayList<RegistryEntry>(); for (String name : names) { RegistryEntry entry = getRegistryEntry(root, key, name); entries.add(entry); } return entries; } catch (Exception e) { throw new SystemInfoException(e); } }
/** * Gets a list of all the value entries of the given registry key. * * @param root * @param key * * @return list of registry entries for the given registry key * * @throws SystemInfoException */ public List<RegistryEntry> getRegistryEntries(Root root, String key) throws SystemInfoException { try { List<String> names = getRegistryValueNames(root, key); List<RegistryEntry> entries = new ArrayList<RegistryEntry>(); for (String name : names) { RegistryEntry entry = getRegistryEntry(root, key, name); entries.add(entry); } return entries; } catch (Exception e) { throw new SystemInfoException(e); } }
public List<NetworkAdapterInfo> getAllNetworkAdapters() throws SystemInfoException { List<NetworkAdapterInfo> adapters = new ArrayList<NetworkAdapterInfo>(); try { String[] interfaceNames = sigar.getNetInterfaceList(); if (interfaceNames != null) { for (String interfaceName : interfaceNames) { if (interfaceName.indexOf(':') != -1) { continue; //filter out virtual IPs } adapters.add(new NetworkAdapterInfo(sigar.getNetInterfaceConfig(interfaceName))); } } } catch (Exception e) { throw new SystemInfoException(e); } return adapters; }
/** * Returns a registry value where the key is located under CURRENT_USER. * * @param key the name of the registry key * @param name the name of the key entry value * * @return the string value of the registry entry * * @throws SystemInfoException */ private RegistryValue getCurrentUserRegistryValue(String key, String name) throws SystemInfoException { RegistryKey registry; try { registry = RegistryKey.CurrentUser.openSubKey(key); } catch (Exception e) { throw new SystemInfoException(e); } return getRegistryValue(registry, name); }
/** * Returns a registry value where the key is located under LOCAL_MACHINE. * * @param key the name of the registry key * @param name the name of the key entry value * * @return the value of the registry entry * * @throws SystemInfoException */ private RegistryValue getLocalMachineRegistryValue(String key, String name) throws SystemInfoException { RegistryKey registry; try { registry = RegistryKey.LocalMachine.openSubKey(key); } catch (Exception e) { throw new SystemInfoException(e); } return getRegistryValue(registry, name); }
/** * Returns a registry value where the key is located under LOCAL_MACHINE. * * @param key the name of the registry key * @param name the name of the key entry value * * @return the value of the registry entry * * @throws SystemInfoException */ private RegistryValue getLocalMachineRegistryValue(String key, String name) throws SystemInfoException { RegistryKey registry; try { registry = RegistryKey.LocalMachine.openSubKey(key); } catch (Exception e) { throw new SystemInfoException(e); } return getRegistryValue(registry, name); }
/** * Returns a registry value where the key is located under CURRENT_USER. * * @param key the name of the registry key * @param name the name of the key entry value * * @return the string value of the registry entry * * @throws SystemInfoException */ private RegistryValue getCurrentUserRegistryValue(String key, String name) throws SystemInfoException { RegistryKey registry; try { registry = RegistryKey.CurrentUser.openSubKey(key); } catch (Exception e) { throw new SystemInfoException(e); } return getRegistryValue(registry, name); }
private RegistryValue.Type guessType(RegistryKey registry, String name) { // TODO: SIGAR does not have a nice API to tell me what type a registry key name has - I have to try to guess. // For now, I can only guess at the few types SIGAR knows about. Need to enhance SIGAR to understand // all the types that Windows supports (like QWORD, BINARY, etc). try { registry.getIntValue(name); return RegistryValue.Type.DWORD; } catch (Exception e) { } try { registry.getMultiStringValue(name, new ArrayList<String>()); return RegistryValue.Type.MULTI_SZ; } catch (Exception e) { } try { registry.getStringValue(name); return RegistryValue.Type.SZ; } catch (Exception e) { } throw new SystemInfoException("Unknown registry value type for [" + name + "]"); } }
private RegistryValue getRegistryValue(RegistryKey registry, String name) throws SystemInfoException { RegistryValue.Type valueType = guessType(registry, name); try { switch (valueType) { // case BINARY: return new BinaryRegistryValue(registry.getByteArrayValue(name)); // case QWORD: return new QWordRegistryValue(registry.getLongValue(name)); // case EXPAND_SZ: return new ExpandSzRegistryValue(registry.getStringValue(name)); case DWORD: { return new DWordRegistryValue(registry.getIntValue(name)); } case SZ: { return new SzRegistryValue(registry.getStringValue(name)); } case MULTI_SZ: { List<String> values = new ArrayList<String>(); registry.getMultiStringValue(name, values); return new MultiSzRegistryValue(values.toArray(new String[0])); } } } catch (Exception e) { throw new SystemInfoException(e); } throw new IllegalStateException("Bad registry value type: " + valueType); }