use of java.rmi.server.RMIClientSocketFactory in project hbase by apache.
the class JMXListener method startConnectorServer.
public void startConnectorServer(int rmiRegistryPort, int rmiConnectorPort) throws IOException {
boolean rmiSSL = false;
boolean authenticate = true;
String passwordFile = null;
String accessFile = null;
System.setProperty("java.rmi.server.randomIDs", "true");
String rmiSSLValue = System.getProperty("com.sun.management.jmxremote.ssl", "false");
rmiSSL = Boolean.parseBoolean(rmiSSLValue);
String authenticateValue = System.getProperty("com.sun.management.jmxremote.authenticate", "false");
authenticate = Boolean.parseBoolean(authenticateValue);
passwordFile = System.getProperty("com.sun.management.jmxremote.password.file");
accessFile = System.getProperty("com.sun.management.jmxremote.access.file");
LOG.info("rmiSSL:" + rmiSSLValue + ",authenticate:" + authenticateValue + ",passwordFile:" + passwordFile + ",accessFile:" + accessFile);
// Environment map
HashMap<String, Object> jmxEnv = new HashMap<>();
RMIClientSocketFactory csf = null;
RMIServerSocketFactory ssf = null;
if (rmiSSL) {
if (rmiRegistryPort == rmiConnectorPort) {
throw new IOException("SSL is enabled. " + "rmiConnectorPort cannot share with the rmiRegistryPort!");
}
csf = new SslRMIClientSocketFactorySecure();
ssf = new SslRMIServerSocketFactorySecure();
}
if (csf != null) {
jmxEnv.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
}
if (ssf != null) {
jmxEnv.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);
}
// Configure authentication
if (authenticate) {
jmxEnv.put("jmx.remote.x.password.file", passwordFile);
jmxEnv.put("jmx.remote.x.access.file", accessFile);
}
// Create the RMI registry
rmiRegistry = LocateRegistry.createRegistry(rmiRegistryPort);
// Retrieve the PlatformMBeanServer.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// Build jmxURL
JMXServiceURL serviceUrl = buildJMXServiceURL(rmiRegistryPort, rmiConnectorPort);
try {
// Start the JMXListener with the connection string
synchronized (JMXListener.class) {
if (JMX_CS != null) {
throw new RuntimeException("Started by another thread?");
}
JMX_CS = JMXConnectorServerFactory.newJMXConnectorServer(serviceUrl, jmxEnv, mbs);
JMX_CS.start();
}
LOG.info("JMXConnectorServer started!");
} catch (IOException e) {
LOG.error("Failed start of JMXConnectorServer!", e);
// deregister the RMI registry
if (rmiRegistry != null) {
UnicastRemoteObject.unexportObject(rmiRegistry, true);
}
}
}
use of java.rmi.server.RMIClientSocketFactory in project karaf by apache.
the class ConnectorServerFactory method init.
public void init() throws Exception {
JMXServiceURL url = new JMXServiceURL(this.serviceUrl);
if (registry == null && locate) {
try {
Registry reg = LocateRegistry.getRegistry(host, getPort());
reg.list();
registry = reg;
} catch (RemoteException e) {
// ignore
}
}
if (registry == null && create) {
registry = new JmxRegistry(getPort(), getBindingName(url));
locallyCreated = true;
}
if (registry != null) {
// register the registry as an OSGi service
Hashtable<String, Object> props = new Hashtable<>();
props.put("port", getPort());
props.put("host", getHost());
bundleContext.registerService(Registry.class, registry, props);
}
if (this.server == null) {
throw new IllegalArgumentException("server must be set");
}
if (isClientAuth()) {
this.secured = true;
}
if (this.secured) {
setupSsl();
} else {
setupKarafRMIServerSocketFactory();
}
if (!AuthenticatorType.PASSWORD.equals(this.authenticatorType)) {
this.environment.remove("jmx.remote.authenticator");
}
MBeanInvocationHandler handler = new MBeanInvocationHandler(server, guard);
MBeanServer guardedServer = (MBeanServer) Proxy.newProxyInstance(server.getClass().getClassLoader(), new Class[] { MBeanServer.class }, handler);
rmiServer = new RMIJRMPServerImpl(url.getPort(), (RMIClientSocketFactory) environment.get(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE), (RMIServerSocketFactory) environment.get(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE), environment);
// Create the connector server now.
this.connectorServer = new RMIConnectorServer(url, environment, rmiServer, guardedServer);
if (this.objectName != null) {
this.server.registerMBean(this.connectorServer, this.objectName);
}
if (jmxmpEnabled) {
Security.addProvider(new PlainSaslServer.SaslPlainProvider());
JMXServiceURL jmxmpUrl = new JMXServiceURL(this.jmxmpServiceUrl);
this.jmxmpConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxmpUrl, this.jmxmpEnvironment, guardedServer);
if (this.jmxmpObjectName != null) {
this.server.registerMBean(this.jmxmpConnectorServer, this.jmxmpObjectName);
}
}
try {
if (this.threaded) {
Thread connectorThread = new Thread(() -> {
try {
Thread.currentThread().setContextClassLoader(ConnectorServerFactory.class.getClassLoader());
connectorServer.start();
remoteServerStub = rmiServer.toStub();
if (jmxmpEnabled && jmxmpConnectorServer != null) {
jmxmpConnectorServer.start();
}
} catch (IOException ex) {
if (ex.getCause() instanceof BindException) {
// we want just the port message
int endIndex = ex.getMessage().indexOf("nested exception is");
// check to make sure we do not get an index out of range
if (endIndex > ex.getMessage().length() || endIndex < 0) {
endIndex = ex.getMessage().length();
}
throw new RuntimeException("\n" + ex.getMessage().substring(0, endIndex) + "\nYou may have started two containers. If you need to start a second container or the default ports are already in use " + "update the config file etc/org.apache.karaf.management.cfg and change the Registry Port and Server Port to unused ports");
}
throw new RuntimeException("Could not start JMX connector server", ex);
}
});
connectorThread.setName("JMX Connector Thread [" + this.serviceUrl + "]");
connectorThread.setDaemon(this.daemon);
connectorThread.start();
} else {
this.connectorServer.start();
remoteServerStub = rmiServer.toStub();
if (jmxmpEnabled && jmxmpConnectorServer != null) {
jmxmpConnectorServer.start();
}
}
} catch (Exception ex) {
if (this.objectName != null) {
doUnregister(this.objectName);
}
if (jmxmpEnabled && this.jmxmpObjectName != null) {
doUnregister(this.jmxmpObjectName);
}
throw ex;
}
}
Aggregations