package jini; import net.jini.core.lookup.ServiceRegistrar; import net.jini.admin.Administrable; import net.jini.core.discovery.LookupLocator; import net.jini.core.lookup.ServiceItem; import net.jini.core.lookup.ServiceRegistration; import net.jini.core.entry.Entry; import net.jini.core.lease.Lease; import net.jini.core.lookup.ServiceTemplate; import net.jini.core.lookup.ServiceMatches; import javax.management.remote.*; import javax.management.*; import java.net.MalformedURLException; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.Iterator; import java.io.IOException; import java.io.Serializable; import java.rmi.RMISecurityManager; /** * This class demonstrates how to use Jini as a lookup service for * JSR 160 connectors. It shows how to lookup a JMXConnector from * the Jini lookup service. *
* See README file and {@link #main(String[])} for more details. *
* Make sure to read the section "Binding with Lookup Services" of * the JMX Remote API 1.0 Specification before looking at this example. */ public class Client { private static boolean debug = false; /** * Get a pointer to the Jini Lookup Service. * (See Jini documentation for more info). *
* The Jini Lookup Service URL is determined as follows: *
* If the System property "jini.lookup.url"
is provided,
* its value is the Jini Lookup Service URL.
*
* Otherwise, the default URL is assumed to be
*
* You may wish to use the following properties on the Java command line:
* "jini://localhost"
*
* @return a pointer to the Jini Lookup Service.
*/
public static ServiceRegistrar getRegistrar()
throws IOException, ClassNotFoundException, MalformedURLException {
final String jurl =
System.getProperty("jini.lookup.url","jini://localhost");
final LookupLocator lookup = new LookupLocator(jurl);
final ServiceRegistrar registrar = lookup.getRegistrar();
if (registrar instanceof Administrable)
debug("Registry is administrable.");
return registrar;
}
/**
* Lookup JMXConnectors in the Jini Lookup Service.
*
* @param registrar A pointer to the Jini Lookup Service,
* returned by {@link #getRegistrar()}.
* @param name the AgentName of the JMXConnectors that should
* be returned. If name 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 Jini Lookup Service.
*/
public static List lookup(ServiceRegistrar registrar,
String name) throws IOException {
final ArrayList list = new ArrayList();
// Returns only JMXConnectors. The filter could be made
// more strict by suplying e.g. RMIConnector.class
// (would only return RMIConnectors).
//
final Class[] classes = new Class[] {JMXConnector.class};
// Will return only those services for which the Name
// attribute was registered. Since JSR 160 specifies that
// the Name attribute is mandatory, this makes it possible
// to filter out all the services that do not conform
// to the spec.
// If
*
*/
public static void main(String[] args) {
try {
// Jini requires a security manager.
//
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
// Get the value of the debug flag.
//
debug = (Boolean.valueOf(System.getProperty("debug","false"))).
booleanValue();
// Get AgentName to lookup. If not defined, all agents
// are looked up.
//
final String agentName = System.getProperty("agent.name");
// Get a pointer to the Jini Lookup Service.
//
final ServiceRegistrar registrar = getRegistrar();
debug("registrar is: " + registrar);
// Lookup all matching agents in the Jini Lookup Service.
//
List l = lookup(registrar,agentName);
// Attempt to connect to retrieved agents
//
System.out.println("Number of agents found : " + l.size());
int j = 1;
for (Iterator i=l.iterator();i.hasNext();j++) {
JMXConnector c1 = (JMXConnector) i.next();
if (c1 != null) {
// Connect
//
System.out.println(
"----------------------------------------------------");
System.out.println("\tConnecting to agent number "+j);
System.out.println(
"----------------------------------------------------");
debug("JMXConnector is: " + c1);
try {
c1.connect(null);
} catch (IOException x) {
System.err.println("Connection failed: " + x);
if (debug) x.printStackTrace(System.err);
continue;
}
// Get MBeanServerConnection
//
MBeanServerConnection conn =
c1.getMBeanServerConnection();
debug("Connection is:" + conn);
System.out.println("Server domain is: " +
conn.getDefaultDomain());
// List all MBeans
//
try {
listMBeans(conn);
} catch (IOException x) {
System.err.println("Failed to list MBeans: " + x);
if (debug) x.printStackTrace(System.err);
}
// Close connector
//
try {
c1.close();
} catch (IOException x) {
System.err.println("Failed to close connection: " + x);
if (debug) x.printStackTrace(System.err);
}
}
}
} catch (Exception x) {
System.err.println("Unexpected exception caught in main: " + x);
x.printStackTrace(System.err);
}
}
}
-Dagent.name=<AgentName>
: specifies an
* AgentName to lookup (default is null, meaning any agent).-Djini.lookup.url=<jini-url>
:
* the Jini Lookup Service URL (default is "jini://localhost"),
* see {@link #getRegistrar()}.-Ddebug="true|false"
: switch the Client debug flag
* on/off (default is "false").