/** * Lookup JMXConnectors in the LDAP directory. * * @param root A pointer to the LDAP directory, returned by {@link #getRootContext()}. * @param protocolType The protocol type of the JMX Connectors we want to retrieve. If * <var>protocolType</var> is null, then the jmxProtocolType attribute is ignored. Otherwise, * only those agents that have registered a matching jmxProtocolType attribute will be * returned. * @param name the AgentName of the JMXConnectors that should be returned. If <var>name</var> is * null, then the JMXConnectors for all agents are returned (null is an equivalent for a * wildcard). * @return The list of matching JMXConnectors retrieved from the LDAP directory. */ public static List lookup(DirContext root, String protocolType, String name) throws IOException, NamingException { final ArrayList list = new ArrayList(); // If protocolType is not null, include it in the filter. // String queryProtocol = (protocolType == null) ? "" : "(jmxProtocolType=" + protocolType + ")"; // Set the LDAPv3 query string // // Only those node that have the jmxConnector object class are // of interest to us, so we specify (objectClass=jmxConnector) // in the filter. // // We specify the jmxAgentName attribute in the filter so that the // query will return only those services for which the AgentName // attribute was registered. Since JSR 160 specifies that // the AgentName attribute is mandatory, this makes it possible // to filter out all the services that do not conform // to the spec. // If <name> is null, it is replaced by "*", so that all // services for which the AgentName attribute was specified match, // regardless of the value of that attribute. // Otherwise, only those services for which AgentName matches the // name or pattern specified by <name> will be returned. // // We also specify (jmxServiceURL=*) so that only those node // for which the jmxServiceURL attribute is present will be // returned. Thus, we filter out all those node corresponding // to agents that are not currently available. // String query = "(&" + "(objectClass=jmxConnector) " + "(jmxServiceURL=*) " + queryProtocol + "(jmxAgentName=" + ((name != null) ? name : "*") + "))"; System.out.println("Looking up JMX Agents with filter: " + query); SearchControls ctrls = new SearchControls(); // Want to get all jmxConnector objects, wherever they've been // registered. // ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Want to get only the jmxServiceUrl and jmxExpirationDate // (comment these lines and all attributes will be returned). // // ctrls.setReturningAttributes(new String[] { // "jmxServiceURL", // "jmxExpirationDate" // }); // Search... // final NamingEnumeration results = root.search("", query, ctrls); // Get the URL... // while (results.hasMore()) { // Get node... // final SearchResult r = (SearchResult) results.nextElement(); debug("Found node: " + r.getName()); // Get attributes // final Attributes attrs = r.getAttributes(); // Get jmxServiceURL attribute // final Attribute attr = attrs.get("jmxServiceURL"); if (attr == null) continue; // Get jmxExpirationDate // final Attribute exp = attrs.get("jmxExpirationDate"); // Check that URL has not expired. // if ((exp != null) && hasExpired((String) exp.get())) { System.out.print(r.getName() + ": "); System.out.println("URL expired since: " + exp.get()); continue; } // Get the URL string // final String urlStr = (String) attr.get(); if (urlStr.length() == 0) continue; debug("Found URL: " + urlStr); // Create a JMXServiceURL // final JMXServiceURL url = new JMXServiceURL(urlStr); // Create a JMXConnector // final JMXConnector conn = JMXConnectorFactory.newJMXConnector(url, null); // Add the connector to the result list // list.add(conn); if (debug) listAttributes(root, r.getName()); } return list; }
/** * Returns a client stub for this connector server. A client stub is a serializable object whose * {@link JMXConnector#connect(Map) connect} method can be used to make one new connection to this * connector server. * * <p>A given connector need not support the generation of client stubs. However, the connectors * specified by the JMX Remote API do (JMXMP Connector and RMI Connector). * * <p>The default implementation of this method uses {@link #getAddress} and {@link * JMXConnectorFactory} to generate the stub, with code equivalent to the following: * * <pre> * JMXServiceURL addr = {@link #getAddress() getAddress()}; * return {@link JMXConnectorFactory#newJMXConnector(JMXServiceURL, Map) * JMXConnectorFactory.newJMXConnector(addr, env)}; * </pre> * * <p>A connector server for which this is inappropriate must override this method so that it * either implements the appropriate logic or throws {@link UnsupportedOperationException}. * * @param env client connection parameters of the same sort that could be provided to {@link * JMXConnector#connect(Map) JMXConnector.connect(Map)}. Can be null, which is equivalent to * an empty map. * @return a client stub that can be used to make a new connection to this connector server. * @exception UnsupportedOperationException if this connector server does not support the * generation of client stubs. * @exception IllegalStateException if the JMXConnectorServer is not started (see {@link * JMXConnectorServerMBean#isActive()}). * @exception IOException if a communications problem means that a stub cannot be created. */ public JMXConnector toJMXConnector(Map<String, ?> env) throws IOException { if (!isActive()) throw new IllegalStateException("Connector is not active"); JMXServiceURL addr = getAddress(); return JMXConnectorFactory.newJMXConnector(addr, env); }