in management/server/src/main/java/org/apache/karaf/management/ConnectorServerFactory.java [352:460]
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;
}
}