private String findAgentUrl(Configuration pConfig) { // System property has precedence String url = System.getProperty("jolokia." + ConfigKey.DISCOVERY_AGENT_URL.getKeyValue()); if (url == null) { url = System.getenv("JOLOKIA_DISCOVERY_AGENT_URL"); if (url == null) { url = pConfig.get(ConfigKey.DISCOVERY_AGENT_URL); } } return NetworkUtil.replaceExpression(url); }
private Dictionary<String,String> getConfiguration() { Dictionary<String,String> config = new Hashtable<String,String>(); for (ConfigKey key : ConfigKey.values()) { String value = getConfiguration(key); if (value != null) { config.put(key.getKeyValue(),value); } } String jolokiaId = NetworkUtil.replaceExpression(config.get(ConfigKey.AGENT_ID.getKeyValue())); if (jolokiaId == null) { config.put(ConfigKey.AGENT_ID.getKeyValue(), NetworkUtil.getAgentId(hashCode(),"osgi")); } config.put(ConfigKey.AGENT_TYPE.getKeyValue(),"osgi"); return config; }
private static InetAddress findLocalAddressListeningOnPort(String pHost, int pPort) throws UnknownHostException, SocketException { InetAddress address = InetAddress.getByName(pHost); if (address.isLoopbackAddress()) { // First check local address InetAddress localAddress = getLocalAddress(); if (!localAddress.isLoopbackAddress() && isPortOpen(localAddress, pPort)) { return localAddress; } // Then try all addresses attache to all interfaces localAddress = getLocalAddressFromNetworkInterfacesListeningOnPort(pPort); if (localAddress != null) { return localAddress; } } return address; }
public static String getAgentId(int objectId, String type) { String address; try { address = getLocalAddress().getHostAddress(); } catch (IOException exp) { address = "local"; } return address + "-" + getProcessId() + "-" + Integer.toHexString(objectId) + "-" + type; }
private static InetAddress getLocalAddressFromNetworkInterfacesListeningOnPort(int pPort) { try { Enumeration<NetworkInterface> networkInterfaces; networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface nif = networkInterfaces.nextElement(); for (Enumeration<InetAddress> addrEnum = nif.getInetAddresses(); addrEnum.hasMoreElements(); ) { InetAddress interfaceAddress = addrEnum.nextElement(); if (!interfaceAddress.isLoopbackAddress() && checkMethod(nif, isUp) && isPortOpen(interfaceAddress, pPort)) { return interfaceAddress; } } } } catch (SocketException e) { return null; } return null; }
private void addJolokiaId(Map<String, String> pFinalCfg) { if (!pFinalCfg.containsKey(ConfigKey.AGENT_ID.getKeyValue())) { pFinalCfg.put(ConfigKey.AGENT_ID.getKeyValue(), NetworkUtil.getAgentId(hashCode(),"jvm")); } pFinalCfg.put(ConfigKey.AGENT_TYPE.getKeyValue(), "jvm"); }
String value; if (var.equalsIgnoreCase("host")) { value = getLocalAddress().getHostName(); } else if (var.equalsIgnoreCase("ip")) { value = getLocalAddress().getHostAddress(); } else { String key = extractKey(var,"env"); if (key != null) { value = System.getenv(key); } else { key = extractKey(var,"prop"); if (key != null) { value = System.getProperty(key);
/** * Description including agent URL * * @return agent url */ @Override public String getDescription() { String hostDescr = host; try { if (hostDescr == null) { hostDescr = NetworkUtil.getLocalAddress().getHostName(); } } catch (IOException e) { hostDescr = "localhost"; } return "Jolokia Agent: http://" + hostDescr + ":" + getPort() + "/jolokia"; }
/** * Examine the given URL and replace the host with a non-loopback host if possible. It is checked, * whether the port is open as well. * <p/> * A replaced host uses the IP address instead of a (possibly non resolvable) name. * * @param pRequestURL url to examine and to update * @return the 'sane' URL (or the original one if no san */ public static String sanitizeLocalUrl(String pRequestURL) { try { URL url = new URL(pRequestURL); String host = url.getHost(); int port = getPort(url); InetAddress address = findLocalAddressListeningOnPort(host, port); return new URL(url.getProtocol(), address.getHostAddress(), port, url.getFile()).toExternalForm(); } catch (IOException e) { // Best effort, we at least tried it return pRequestURL; } }
/** * Get the local network info as a string * * @return return a description of the current network setup of the local host. * @throws UnknownHostException * @throws SocketException */ public static String dumpLocalNetworkInfo() throws UnknownHostException, SocketException { StringBuffer buffer = new StringBuffer(); InetAddress addr = InetAddress.getLocalHost(); buffer.append("Localhost: " + getAddrInfo(addr) + "\n"); Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces(); buffer.append("Network interfaces:\n"); while (nifs.hasMoreElements()) { NetworkInterface nif = nifs.nextElement(); buffer.append(" - " + getNetworkInterfaceInfo(nif) + "\n"); Enumeration<InetAddress> addresses = nif.getInetAddresses(); while (addresses.hasMoreElements()) { addr = addresses.nextElement(); buffer.append(" " + getAddrInfo(addr) + "\n"); } } return buffer.toString(); }
/** * Get a local address which supports multicast. A loopback adress is returned, but only if not * another is available * * @return a multicast enabled address of null if none could be found * @throws UnknownHostException * @throws SocketException */ public static InetAddress getLocalAddressWithMulticast() throws UnknownHostException, SocketException { InetAddress addr = InetAddress.getLocalHost(); NetworkInterface nif = NetworkInterface.getByInetAddress(addr); if (addr.isLoopbackAddress() || addr instanceof Inet6Address || !isMulticastSupported(nif)) { // Find local address that isn't a loopback address InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface(); // If a local, multicast enabled address can be found, use it. Otherwise // we are using the local address, which might not be what you want if (lookedUpAddr != null) { return lookedUpAddr; } addr = InetAddress.getByName("127.0.0.1"); } if (isMulticastSupported(addr)) { return addr; } else { throw new UnknownHostException("Cannot find address of local host which can be used for multicasting"); } }
/** * Check whether the given interface supports multicast and is up * * @param pNif check whether the given interface supports multicast * @return true if multicast is supported and the interface is up */ public static boolean isMulticastSupported(NetworkInterface pNif) { return pNif != null && checkMethod(pNif, isUp) && checkMethod(pNif, supportsMulticast); }
private static String getNetworkInterfaceInfo(NetworkInterface pNif) throws SocketException { if (pNif == null) { return "[null]"; } return pNif.getDisplayName() + " [up: " + pNif.isUp() + ", mc: " + pNif.supportsMulticast() + ", lb: " + pNif.isLoopback() + ", hw: " + formatHwAddress(pNif.getHardwareAddress()) + "]"; }
public static void main(String[] args) throws UnknownHostException, SocketException { System.out.println(dumpLocalNetworkInfo()); // NOSONAR }
/** * Get a local, IP4 Address, preferable a non-loopback address which is bound to an interface. * * @return * @throws UnknownHostException * @throws SocketException */ public static InetAddress getLocalAddress() throws UnknownHostException, SocketException { InetAddress addr = InetAddress.getLocalHost(); NetworkInterface nif = NetworkInterface.getByInetAddress(addr); if (addr.isLoopbackAddress() || addr instanceof Inet6Address || nif == null) { // Find local address that isn't a loopback address InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface(); // If a local, multicast enabled address can be found, use it. Otherwise // we are using the local address, which might not be what you want addr = lookedUpAddr != null ? lookedUpAddr : InetAddress.getByName("127.0.0.1"); } return addr; }
private Map<String, String> getInitParameters(MuleAgentConfig pConfig) { Map<String,String> ret = new HashMap<String,String>(); ret.put("debugMaxEntries", "" + pConfig.getDebugMaxEntries()); ret.put("historyMaxEntries", "" + pConfig.getHistoryMaxEntries()); ret.put("maxCollectionsSize", "" + pConfig.getMaxCollectionSize()); ret.put("maxDepth", "" + pConfig.getMaxDepth()); ret.put("maxObjects", "" + pConfig.getMaxObjects()); ret.put("debug", "" + pConfig.isDebug()); ret.put("agentType", "mule"); ret.put("agentId", NetworkUtil.getAgentId(hashCode(), "mule")); return ret; }
public static String getAgentId(int objectId, String type) { String address; try { address = getLocalAddress().getHostAddress(); } catch (IOException exp) { address = "local"; } return address + "-" + getProcessId() + "-" + Integer.toHexString(objectId) + "-" + type; }
String value; if (var.equalsIgnoreCase("host")) { value = getLocalAddress().getHostName(); } else if (var.equalsIgnoreCase("ip")) { value = getLocalAddress().getHostAddress(); } else { String key = extractKey(var,"env"); if (key != null) { value = System.getenv(key); } else { key = extractKey(var,"prop"); if (key != null) { value = System.getProperty(key);
private String detectAgentUrl(HttpServer pServer, JolokiaServerConfig pConfig, String pContextPath) { serverAddress= pServer.getAddress(); InetAddress realAddress; int port; if (serverAddress != null) { realAddress = serverAddress.getAddress(); if (realAddress.isAnyLocalAddress()) { try { realAddress = NetworkUtil.getLocalAddress(); } catch (IOException e) { try { realAddress = InetAddress.getLocalHost(); } catch (UnknownHostException e1) { // Ok, ok. We take the original one realAddress = serverAddress.getAddress(); } } } port = serverAddress.getPort(); } else { realAddress = pConfig.getAddress(); port = pConfig.getPort(); } return String.format("%s://%s:%d%s", pConfig.getProtocol(),realAddress.getHostAddress(),port, pContextPath); }
private static InetAddress getLocalAddressFromNetworkInterfacesListeningOnPort(int pPort) { try { Enumeration<NetworkInterface> networkInterfaces; networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface nif = networkInterfaces.nextElement(); for (Enumeration<InetAddress> addrEnum = nif.getInetAddresses(); addrEnum.hasMoreElements(); ) { InetAddress interfaceAddress = addrEnum.nextElement(); if (!interfaceAddress.isLoopbackAddress() && checkMethod(nif, isUp) && isPortOpen(interfaceAddress, pPort)) { return interfaceAddress; } } } } catch (SocketException e) { return null; } return null; }