package slp; import com.sun.slp.ServiceLocationManager; import com.sun.slp.ServiceLocationException; import com.sun.slp.Advertiser; import com.sun.slp.ServiceURL; import com.sun.slp.ServiceLocationAttribute; import javax.management.*; import javax.management.remote.*; import javax.management.remote.rmi.*; import java.util.Map; import java.util.List; import java.util.HashMap; import java.util.ArrayList; import java.util.Locale; import java.util.Vector; import java.io.IOException; import java.io.Serializable; import java.net.MalformedURLException; import javax.naming.Context; import javax.naming.NamingException; /** * This class demonstrates how to use SLP as a lookup service for * JSR 160 connectors. It shows how to register a JMXConnectorServer * with the Service Location Protocol. *
* 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 Server { // The Service URL will remain registered for 300 secs. // This is an intentionally long time for the purpose of this example. // In practice, a shorter lease, periodically refreshed, is preferable. // public final static int JMX_DEFAULT_LEASE = 300; // Default scope. // public final static String JMX_SCOPE = "DEFAULT"; // The local MBeanServer. // private final MBeanServer mbs; private static boolean debug = false; /** * Constructs a Server object. Creates a new MBeanServer. */ public Server() { mbs = MBeanServerFactory.createMBeanServer(); } /** * Registers a JMX Connector URL with the SLP Lookup Service. * * @param jmxUrl A JMX Connector Server URL obtained from * {@link JMXConnectorServer#getAddress() * JMXConnectorServer.getAddress()} * @param name The AgentName with which the URL will be * registered in the SLP Lookup Service. */ public static void register(JMXServiceURL jmxUrl, String name) throws ServiceLocationException { // Create the SLP service URL // // Note: It is recommended that the JMX Agents make use of the // leasing feature of SLP, and periodically renew their lease // ServiceURL serviceURL = new ServiceURL(jmxUrl.toString(), JMX_DEFAULT_LEASE); System.out.println("\nRegistering URL for " + name + ": " + jmxUrl); debug("ServiceType is: " + serviceURL.getServiceType()); // Prepare Lookup Attributes // Vector attributes = new Vector(); Vector attrValues = new Vector(); // Specify default SLP scope // attrValues.add(JMX_SCOPE); ServiceLocationAttribute attr1 = new ServiceLocationAttribute("SCOPE", attrValues); attributes.add(attr1); // Specify AgentName attribute (mandatory) // attrValues.removeAllElements(); attrValues.add(name); ServiceLocationAttribute attr2 = new ServiceLocationAttribute("AgentName", attrValues); attributes.add(attr2); // Register with SLP // ----------------- // Get SLP Advertiser // final Advertiser slpAdvertiser = ServiceLocationManager.getAdvertiser(Locale.US); // Register the service: URL // slpAdvertiser.register(serviceURL, attributes); System.out.println("\nRegistered URL: " + jmxUrl); } /** * Creates an RMI Connector Server, starts it, and registers it * with the SLP Lookup Service. *
* This method will transfer a fixed set of System Properties to * the Map given to the RMIConnectorServer constructor. Some * JNDI properties, if defined, are transfered to the Map so * that they may be used when LDAP is used as external directory * to register the RMI Stub (see {@link javax.management.remote.rmi} * Javadoc). Note that even if LDAP is used as external directory * the {@link Context#INITIAL_CONTEXT_FACTORY * Context.INITIAL_CONTEXT_FACTORY} and * {@link Context#PROVIDER_URL Context.PROVIDER_URL} properties * usually don't need to be passed. *
* The following System properties, if defined, are transfered to * the Map given to the RMIConnectorServer constructor. *
true
.* Creates a server object, gets the JMX Service URL, and calls * the method that will create and register the appropriate JMX * Connector Server for that URL. *
* You may wish to use the following properties on the Java command line: *
-Durl=<jmxServiceURL>
: specifies the URL of
* the JMX Connector Server you wish to use. See README file for more
* details.-Dagent.name=<AgentName>
: specifies the
* AgentName to register with.-Ddebug="true|false"
: switch the Server debug flag
* on/off (default is "false").