Example 10-10 shows the registration of the JMX connector
server service URL in the LDAP directory. The domain name in which the URL
is registered can be passed on the command line through the dn
System property, namely, -Ddn=mydn. If the dn system property is not specified, then you can use cn=name, in which name is the agent name. This is not
mandatory, however.
The location in which the URL is registered is not important, because
the client code never uses that DN directly, but instead performs an LDAP
search to find the nodes that have an auxiliary JMX connector ObjectClass. What is important however, is that each URL is registered
in its own LDAP node. How to name these nodes is left to the LDAP administrator.
In this example, it is assumed that you have configured your LDAP server by
creating a root context under which the node cn=name can
be created, and that this root context has been passed to the LDAP initial
context through the Context.PROVIDER_URL property (see Example 10-9).
The code shown above checks whether the node in which you will register
the server URL already exists. If it does not, you try to create it (this
will fail if the parent node does not exist). Since the ObjectClass is a simple auxiliary class, you can use the javaContainer ObjectClass as structural class if you
need to create a new context. Once again, this is optional.
Any structural class to which the jmxConnector
auxiliary class can be added is acceptable. It then checks whether the node
in which you will register the server already has the jmxConnector auxiliary class. Otherwise, an exception is thrown.
At this point you are sure that the node in which you will register
the URL exists, and has the appropriate jmxConnector
auxiliary class. So you need only to replace the values of the attributes
defined by the LDAP schema for JMX, (see jmx-schema.txt):
jmxServiceUrl: contains the String form
of the server URL, as obtained from server.getAddress()
after the server was started.
jmxAgentName: contains the JMX agent name.
jmxProtocolType: contains the JMX protocol
type, as returned by jmxUrl.getProtocolType().
jmxAgentHost: contains the name of the
agent host.
jmxExpirationDate: contains the date at
which the URL will be considered obsolete.
Example 10-11 Registering the Connector Servers in the LDAP Server
[...]
public JMXConnectorServer rmi(String url)
throws IOException, JMException,
NamingException, ClassNotFoundException {
JMXServiceURL jurl = new JMXServiceURL(url);
final HashMap env = new HashMap();
// Prepare the environment Map
[...]
JMXConnectorServer rmis =
JMXConnectorServerFactory.newJMXConnectorServer(jurl, env, mbs);
final String agentName = System.getProperty("agent.name",
"DefaultAgent");
start(rmis,env,agentName);
return rmis;
}
[...]
|
In Example 10-11, a new RMI connector server named rmis is created with the JMX service URL jurl
and the appropriate LDAP properties passed to its environment map env. The connector server rmis is launched
by calling JMXConnectorServer.start() and is registered
in the LDAP server.
Subsequent code not shown here creates and registers a corresponding
JMXMP connector server named jmxmp.
Example 10-12 Creating the JMX Connector Server
[...]
public void start(JMXConnectorServer server, Map env, String agentName)
throws IOException, NamingException {
server.start();
final DirContext root=getRootContext();
final JMXServiceURL address = server.getAddress();
register(root,address,agentName);
}
[...]
|
Example 10-12 shows the creation of a JMX connector
server server, the obtainment of a pointer to the LDAP
server root directory root and the creation of a URL for server named address. The root directory, the
URL and an agent name are passed as parameters to register()
and are registered in the LDAP server.
10.4.2 Looking up the Connector Servers with the JNDI/LDAP Lookup Service
The following code extract is
taken from the Client class in the Jini Lookup Service
examples directory.
Example 10-13 Looking up the Connector Servers with the JNDI/LDAP Lookup Service
[...]
public class Client {
private static boolean debug = false;
public static void listAttributes(DirContext root, String dn)
throws NamingException {
final Attributes attrs = root.getAttributes(dn);
System.out.println("dn: " + dn);
System.out.println("attributes: " + attrs);
}
public static DirContext getRootContext() throws NamingException {
final Hashtable env = new Hashtable();
// Prepare environment map
[...]
InitialContext root = new InitialLdapContext(env,null);
return (DirContext)(root.lookup(""));
}
// Confirm URL has not expired
[...]
public static List lookup(DirContext root, String protocolType,
String name)
throws IOException, NamingException {
final ArrayList list = new ArrayList();
String queryProtocol =
(protocolType==null)?"":"(jmxProtocolType="+protocolType+")";
String query =
"(&" + "(objectClass=jmxConnector) " +
"(jmxServiceURL=*) " +
queryProtocol +
"(jmxAgentName=" + ((name!=null)?name:"*") + "))";
SearchControls ctrls = new SearchControls();
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
final NamingEnumeration results = root.search("", query, ctrls);
while (results.hasMore()) {
final SearchResult r = (SearchResult) results.nextElement();
debug("Found node: " + r.getName());
final Attributes attrs = r.getAttributes();
final Attribute attr = attrs.get("jmxServiceURL");
if (attr == null) continue;
final Attribute exp = attrs.get("jmxExpirationDate");
if ((exp != null) && hasExpired((String)exp.get())) {
System.out.print(r.getName() + ": ");
System.out.println("URL expired since: " + exp.get());
continue;
}
final String urlStr = (String)attr.get();
if (urlStr.length() == 0) continue;
debug("Found URL: "+ urlStr);
final JMXServiceURL url = new JMXServiceURL(urlStr);
final JMXConnector conn =
JMXConnectorFactory.newJMXConnector(url,null);
list.add(conn);
if (debug) listAttributes(root,r.getName());
}
return list;
}
}
|
|