/* * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle or the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 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.remote.rmi.*; import javax.management.*; import java.util.Map; import java.util.List; import java.util.HashMap; import java.util.Hashtable; import java.util.ArrayList; import java.io.IOException; import java.net.MalformedURLException; import java.io.Serializable; import java.rmi.RMISecurityManager; import javax.naming.Context; /** * This class demonstrates how to use Jini as a lookup service for * JSR 160 connectors. It shows how to register a JMXConnectorServer * with 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 Server { /** * 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(); } /** * Creates an RMI Connector Server, starts it, and registers it * with the Jini 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
.* 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
* "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;
}
/**
* Register a JMXConnector proxy with the Jini Lookup Service.
*
* @param registrar A pointer to the Jini Lookup Service, as returned
* by {@link #getRegistrar()}.
* @param proxy A JMXConnector server proxy, that should have
* been obtained from
* {@link JMXConnectorServer#toJMXConnector(Map)
* JMXConnectorServer.toJMXConnector(Map)};
* @param name The AgentName with which the proxy must be registered
* in the Jini Lookup Service.
*
* @return The ServiceRegistration object returned by the Jini Lookup
* Service.
*/
public static ServiceRegistration register(ServiceRegistrar registrar,
JMXConnector proxy, String name)
throws IOException {
// Prepare Service's attributes entry
//
Entry[] serviceAttrs = new Entry[] {
new net.jini.lookup.entry.Name(name)
// Add here the lookup attributes you want to specify.
};
System.out.println("Registering proxy: AgentName=" + name );
debug("\t\t" + proxy);
// Create a ServiceItem from the service instance
//
ServiceItem srvcItem = new ServiceItem(null, proxy, serviceAttrs);
// Register the Service with the Lookup Service
//
ServiceRegistration srvcRegistration =
registrar.register(srvcItem, Lease.ANY);
debug("Registered ServiceID: " +
srvcRegistration.getServiceID().toString());
return srvcRegistration;
}
/**
* Trace a debug message.
*/
private static void debug(String msg) {
if (debug) System.out.println(msg);
}
/**
* Program Main
*
* 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 an
* AgentName to register with.-Djini.lookup.url=<jini-url>
:
* the Jini Lookup Service URL (default is "jini://localhost"),
* see {@link #getRegistrar()}.-Ddebug="true|false"
: switch the Server debug flag
* on/off (default is "false").