return makeClient;
}
};
// Create a Listener responsible for removing the map entries add by the #makeClient entry above.
final NotificationListener mapCleanupListener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
final String connectionId = ((JMXConnectionNotification) notification).getConnectionId();
connectionIdUsernameMap.remove(connectionId);
}
};
String localHost;
try
{
localHost = InetAddress.getLocalHost().getHostName();
}
catch(UnknownHostException ex)
{
localHost="127.0.0.1";
}
final String hostname = localHost;
final JMXServiceURL externalUrl = new JMXServiceURL(
"service:jmx:rmi://"+hostname+":"+(_jmxPortConnectorServer)+"/jndi/rmi://"+hostname+":"+_jmxPortRegistryServer+"/jmxrmi");
final JMXServiceURL internalUrl = new JMXServiceURL("rmi", hostname, _jmxPortConnectorServer);
_cs = new RMIConnectorServer(internalUrl, env, rmiConnectorServerStub, _mbeanServer)
{
@Override
public synchronized void start() throws IOException
{
try
{
//manually bind the connector server to the registry at key 'jmxrmi', like the out-of-the-box agent
_rmiRegistry.bind("jmxrmi", rmiConnectorServerStub);
}
catch (AlreadyBoundException abe)
{
//key was already in use. shouldnt happen here as its a new registry, unbindable by normal means.
//IOExceptions are the only checked type throwable by the method, wrap and rethrow
IOException ioe = new IOException(abe.getMessage());
ioe.initCause(abe);
throw ioe;
}
//now do the normal tasks
super.start();
}
@Override
public synchronized void stop() throws IOException
{
try
{
if (_rmiRegistry != null)
{
_rmiRegistry.unbind("jmxrmi");
}
}
catch (NotBoundException nbe)
{
// TODO consider if we want to keep new logging
_log.error("Failed to unbind jmxrmi", nbe);
//ignore
}
//now do the normal tasks
super.stop();
}
@Override
public JMXServiceURL getAddress()
{
//must return our pre-crafted url that includes the full details, inc JNDI details
return externalUrl;
}
};
//Add the custom invoker as an MBeanServerForwarder, and start the RMIConnectorServer.
MBeanServerForwarder mbsf = MBeanInvocationHandlerImpl.newProxyInstance();
_cs.setMBeanServerForwarder(mbsf);
// Get the handler that is used by the above MBInvocationHandler Proxy.
// which is the MBeanInvocationHandlerImpl and so also a NotificationListener.
final NotificationListener invocationHandler = (NotificationListener) Proxy.getInvocationHandler(mbsf);
// Install a notification listener on OPENED, CLOSED, and FAILED,
// passing the map of connection-ids to usernames as hand-back data.
final NotificationFilterSupport invocationHandlerFilter = new NotificationFilterSupport();
invocationHandlerFilter.enableType(JMXConnectionNotification.OPENED);