private String getPort() throws MalformedURLException { if (port == null && url != null) { port = String.valueOf(new JMXServiceURL(getUrl()).getPort()); } return port; }
public PasswordStrategy getPasswordStategy() { if ("PASSWORD_CLEAR".equals(getFactory())) { return PasswordStrategy.PASSWORD_CLEAR; } if ("SASL".equals(getFactory())) { return PasswordStrategy.SASL; } return PasswordStrategy.STANDARD; }
public boolean isLocalConnection() throws MalformedURLException { Objects.requireNonNull(getIpAddress()); Objects.requireNonNull(getPort()); // If we're trying to create a connection to a localhost address... if (getIpAddress().isLoopbackAddress()) { final String jmxPort = System.getProperty(JMX_PORT_SYSTEM_PROPERTY); // returns null if REMOTE JMX is enabled // ... and if the port matches the port of the current JVM... if (getPort().equals(jmxPort) || // ... or if remote JMX RMI is disabled and we're attempting to connect // to the default OpenNMS JMX port... (jmxPort == null && DEFAULT_OPENNMS_JMX_PORT.equals(getPort()))) { return true; } } return false; }
public String[] getCredentials() { if (isSunCacao()) { return new String[]{"com.sun.cacao.user\001" + getUsername(), getPassword()}; } return new String[]{getUsername(), getPassword()}; }
public boolean hasCredentials() { return getUsername() != null && getPassword() != null; }
public JmxServerConnectionWrapper createConnection(JmxConnectionConfig config) throws JmxServerConnectionException { try { if (config.isLocalConnection()) { final String urlString = config.getUrl(); final JMXServiceURL url = new JMXServiceURL(urlString); LOG.debug("JMX: {} - {}", config.getFactory(), url); config.getPasswordStategy().apply(env, config);
public String getUrl() throws MalformedURLException { final Map<String, Object> propertiesMap = new HashMap<>(); if (ipAddress != null) { // Create map to substitute url propertiesMap.put("ipaddr", InetAddressUtils.toUrlIpAddress(getIpAddress())); final String theUrl = PropertiesUtils.substitute(url, propertiesMap); return theUrl; } return url; }
public JmxServerConnectionWrapper createConnection(JmxConnectionConfig config) throws JmxServerConnectionException { try { if (config.isLocalConnection()) { final String urlString = config.getUrl(); final JMXServiceURL url = new JMXServiceURL(urlString); LOG.debug("JMX: {} - {}", config.getFactory(), url); config.getPasswordStategy().apply(env, config);
public String[] getCredentials() { if (isSunCacao()) { return new String[]{"com.sun.cacao.user\001" + getUsername(), getPassword()}; } return new String[]{getUsername(), getPassword()}; }
public boolean hasCredentials() { return getUsername() != null && getPassword() != null; }
public String getUrl() throws MalformedURLException { final Map<String, Object> propertiesMap = new HashMap<>(); if (ipAddress != null) { // Create map to substitute url propertiesMap.put("ipaddr", InetAddressUtils.toUrlIpAddress(getIpAddress())); final String theUrl = PropertiesUtils.substitute(url, propertiesMap); return theUrl; } return url; }
private String getPort() throws MalformedURLException { if (port == null && url != null) { port = String.valueOf(new JMXServiceURL(getUrl()).getPort()); } return port; }
public PasswordStrategy getPasswordStategy() { if ("PASSWORD_CLEAR".equals(getFactory())) { return PasswordStrategy.PASSWORD_CLEAR; } if ("SASL".equals(getFactory())) { return PasswordStrategy.SASL; } return PasswordStrategy.STANDARD; }
public boolean isLocalConnection() throws MalformedURLException { Objects.requireNonNull(getIpAddress()); Objects.requireNonNull(getPort()); // If we're trying to create a connection to a localhost address... if (getIpAddress().isLoopbackAddress()) { final String jmxPort = System.getProperty(JMX_PORT_SYSTEM_PROPERTY); // returns null if REMOTE JMX is enabled // ... and if the port matches the port of the current JVM... if (getPort().equals(jmxPort) || // ... or if remote JMX RMI is disabled and we're attempting to connect // to the default OpenNMS JMX port... (jmxPort == null && DEFAULT_OPENNMS_JMX_PORT.equals(getPort()))) { return true; } } return false; }
public InetAddress getIpAddress() throws MalformedURLException { if (ipAddress == null && url != null) { ipAddress = InetAddressUtils.getInetAddress(new JMXServiceURL(getUrl()).getHost()); } return ipAddress; }
public InetAddress getIpAddress() throws MalformedURLException { if (ipAddress == null && url != null) { ipAddress = InetAddressUtils.getInetAddress(new JMXServiceURL(getUrl()).getHost()); } return ipAddress; }
public static JmxServerConnectionWrapper getMBeanServerConnection(Map<String,String> propertiesMap, InetAddress address) throws IOException { final long timeout = DEFAULT_TIMEOUT; propertiesMap.putIfAbsent("factory", "STANDARD"); propertiesMap.putIfAbsent("port", "1099"); propertiesMap.putIfAbsent("protocol", "rmi"); propertiesMap.putIfAbsent("urlPath", "/jmxrmi"); propertiesMap.putIfAbsent("timeout", Long.toString(timeout)); final Callable<JmxServerConnectionWrapper> task = new Callable<JmxServerConnectionWrapper>() { @Override public JmxServerConnectionWrapper call() throws Exception { return new DefaultJmxConnector().createConnection(address, propertiesMap); } }; final Future<JmxServerConnectionWrapper> future = executor.submit(task); try { final JmxServerConnectionWrapper connectionWrapper = future.get(timeout, TimeUnit.MILLISECONDS); return connectionWrapper; } catch (InterruptedException | ExecutionException | TimeoutException e) { final String url = JmxConnectionConfigBuilder.buildFrom(address, propertiesMap).build().getUrl(); LOG.info("Exception connecting JMXConnectorFactory url {} , Error: {}", url, e.getMessage()); if (!future.isDone()) { future.cancel(true); LOG.info(" the task {}", future.isCancelled() ? "was cancelled" : "could not be cancelled"); } throw new ConnectException("Error connecting JMXConnectionFactory " + url); } } }
public static JmxServerConnectionWrapper getMBeanServerConnection(Map<String,String> propertiesMap, InetAddress address) throws IOException { final long timeout = DEFAULT_TIMEOUT; propertiesMap.putIfAbsent("factory", "STANDARD"); propertiesMap.putIfAbsent("port", "1099"); propertiesMap.putIfAbsent("protocol", "rmi"); propertiesMap.putIfAbsent("urlPath", "/jmxrmi"); propertiesMap.putIfAbsent("timeout", Long.toString(timeout)); final Callable<JmxServerConnectionWrapper> task = new Callable<JmxServerConnectionWrapper>() { @Override public JmxServerConnectionWrapper call() throws Exception { return new DefaultJmxConnector().createConnection(address, propertiesMap); } }; final Future<JmxServerConnectionWrapper> future = executor.submit(task); try { final JmxServerConnectionWrapper connectionWrapper = future.get(timeout, TimeUnit.MILLISECONDS); return connectionWrapper; } catch (InterruptedException | ExecutionException | TimeoutException e) { final String url = JmxConnectionConfigBuilder.buildFrom(address, propertiesMap).build().getUrl(); LOG.info("Exception connecting JMXConnectorFactory url {} , Error: {}", url, e.getMessage()); if (!future.isDone()) { future.cancel(true); LOG.info(" the task {}", future.isCancelled() ? "was cancelled" : "could not be cancelled"); } throw new ConnectException("Error connecting JMXConnectionFactory " + url); } } }
public static MBeanServer getMBeanServer(JmxConfigDao jmxConfigDao, String address, Map<String, String> parameters) { Objects.requireNonNull(address); Objects.requireNonNull(parameters); if (jmxConfigDao != null && jmxConfigDao.getConfig() != null) { try { final JmxConnectionConfig config = JmxConnectionConfigBuilder.buildFrom(address, parameters).build(); final int port = new JMXServiceURL(config.getUrl()).getPort(); final MBeanServer mBeanServer = jmxConfigDao.getConfig().lookupMBeanServer(address, port); return mBeanServer; } catch (MalformedURLException e) { LOG.warn("Unexpected exception: {}", e.getMessage(), e); } } return null; // not found or exception }
@Override public DetectRequest buildRequest(String location, InetAddress address, Integer port, Map<String, String> attributes) { // in case port is null, but url is provided, the port is extracted from the url if (port == null && attributes.containsKey("url")) { try { final JmxConnectionConfig config = JmxConnectionConfigBuilder.buildFrom(address, attributes).build(); port = new JMXServiceURL(config.getUrl()).getPort(); } catch (MalformedURLException e) { throw new IllegalArgumentException("url is not valid", e); } } return new DetectRequestImpl(address, port, getRuntimeAttributes(address, port)); }