88import  com .google .errorprone .annotations .CanIgnoreReturnValue ;
99import  java .io .IOException ;
1010import  java .net .MalformedURLException ;
11+ import  java .net .URI ;
12+ import  java .rmi .NotBoundException ;
13+ import  java .rmi .registry .LocateRegistry ;
14+ import  java .rmi .registry .Registry ;
1115import  java .security .Provider ;
1216import  java .security .Security ;
1317import  java .util .HashMap ;
1923import  javax .management .remote .JMXConnector ;
2024import  javax .management .remote .JMXConnectorFactory ;
2125import  javax .management .remote .JMXServiceURL ;
26+ import  javax .management .remote .rmi .RMIConnector ;
27+ import  javax .management .remote .rmi .RMIServer ;
28+ import  javax .rmi .ssl .SslRMIClientSocketFactory ;
2229import  javax .security .auth .callback .Callback ;
2330import  javax .security .auth .callback .CallbackHandler ;
2431import  javax .security .auth .callback .NameCallback ;
@@ -37,6 +44,10 @@ public class JmxConnectorBuilder {
3744  @ Nullable  private  String  realm ;
3845  private  boolean  sslRegistry ;
3946
47+   // used only with ssl registry 
48+   private  static  final  SslRMIClientSocketFactory  sslRmiClientSocketFactory  =
49+       new  SslRMIClientSocketFactory ();
50+ 
4051  private  JmxConnectorBuilder (JMXServiceURL  url ) {
4152    this .url  = url ;
4253  }
@@ -91,10 +102,10 @@ public JMXConnector build() throws IOException {
91102    try  {
92103      if  (sslRegistry ) {
93104        return  doConnectSslRegistry (url , env );
105+       } else  {
106+         return  doConnect (url , env );
94107      }
95108
96-       return  doConnect (url , env );
97- 
98109    } catch  (IOException  e ) {
99110      throw  new  IOException ("Unable to connect to "  + url .getHost () + ":"  + url .getPort (), e );
100111    }
@@ -148,7 +159,29 @@ private static JMXConnector doConnect(JMXServiceURL url, Map<String, Object> env
148159  }
149160
150161  public  JMXConnector  doConnectSslRegistry (JMXServiceURL  url , Map <String , Object > env ) {
151-     throw  new  IllegalStateException ("TODO" );
162+ 
163+     logger .info ("Connecting with SSL protected RMI registry to "  + url );
164+     String  hostName ;
165+     int  port ;
166+ 
167+     if  (url .getURLPath ().startsWith ("/jndi/" )) {
168+        String [] components  = url .getURLPath ().split ("/" , 3 );
169+        URI  uri  = URI .create (components [2 ]);
170+       hostName  = uri .getHost ();
171+       port  = uri .getPort ();
172+     } else  {
173+       hostName  = url .getHost ();
174+       port  = url .getPort ();
175+     }
176+ 
177+     try  {
178+       JMXConnector  jmxConn  = new  RMIConnector (getStub (hostName , port ), null );
179+       jmxConn .connect (env );
180+       return  jmxConn ;
181+     } catch  (IOException  e ) {
182+       throw  new  IllegalStateException ("Unable to connect to "  + url , e );
183+     }
184+ 
152185  }
153186
154187  private  static  JMXServiceURL  buildUrl (String  host , int  port ) {
@@ -164,4 +197,13 @@ private static JMXServiceURL buildUrl(String url) {
164197      throw  new  IllegalArgumentException ("invalid url" , e );
165198    }
166199  }
200+ 
201+   private  static  RMIServer  getStub (String  hostName , int  port ) throws  IOException  {
202+     try  {
203+       Registry  registry  = LocateRegistry .getRegistry (hostName , port , sslRmiClientSocketFactory );
204+       return  (RMIServer ) registry .lookup ("jmxrmi" );
205+     } catch  (NotBoundException  nbe ) {
206+       throw  new  IOException (nbe );
207+     }
208+   }
167209}
0 commit comments