33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- package io .opentelemetry .contrib .jmxscraper . client ;
6+ package io .opentelemetry .contrib .jmxscraper ;
77
88import com .google .errorprone .annotations .CanIgnoreReturnValue ;
99import java .io .IOException ;
2626import javax .security .auth .callback .UnsupportedCallbackException ;
2727import javax .security .sasl .RealmCallback ;
2828
29- public class JmxRemoteClient {
29+ public class JmxConnectorBuilder {
3030
31- private static final Logger logger = Logger .getLogger (JmxRemoteClient .class .getName ());
31+ private static final Logger logger = Logger .getLogger (JmxConnectorBuilder .class .getName ());
3232
3333 private final JMXServiceURL url ;
3434 @ Nullable private String userName ;
@@ -37,44 +37,65 @@ public class JmxRemoteClient {
3737 @ Nullable private String realm ;
3838 private boolean sslRegistry ;
3939
40- private JmxRemoteClient (JMXServiceURL url ) {
40+ private JmxConnectorBuilder (JMXServiceURL url ) {
4141 this .url = url ;
4242 }
4343
44- public static JmxRemoteClient createNew (String host , int port ) {
45- return new JmxRemoteClient (buildUrl (host , port ));
44+ public static JmxConnectorBuilder createNew (String host , int port ) {
45+ return new JmxConnectorBuilder (buildUrl (host , port ));
4646 }
4747
48- public static JmxRemoteClient createNew (String url ) {
49- return new JmxRemoteClient (buildUrl (url ));
48+ public static JmxConnectorBuilder createNew (String url ) {
49+ return new JmxConnectorBuilder (buildUrl (url ));
5050 }
5151
5252 @ CanIgnoreReturnValue
53- public JmxRemoteClient userCredentials (String userName , String password ) {
53+ public JmxConnectorBuilder userCredentials (String userName , String password ) {
5454 this .userName = userName ;
5555 this .password = password ;
5656 return this ;
5757 }
5858
5959 @ CanIgnoreReturnValue
60- public JmxRemoteClient withRemoteProfile (String profile ) {
60+ public JmxConnectorBuilder withRemoteProfile (String profile ) {
6161 this .profile = profile ;
6262 return this ;
6363 }
6464
6565 @ CanIgnoreReturnValue
66- public JmxRemoteClient withRealm (String realm ) {
66+ public JmxConnectorBuilder withRealm (String realm ) {
6767 this .realm = realm ;
6868 return this ;
6969 }
7070
7171 @ CanIgnoreReturnValue
72- public JmxRemoteClient withSslRegistry () {
72+ public JmxConnectorBuilder withSslRegistry () {
7373 this .sslRegistry = true ;
7474 return this ;
7575 }
7676
77- public JMXConnector connect () throws IOException {
77+ /**
78+ * Builds JMX connector instance by connecting to the remote JMX endpoint
79+ *
80+ * @return JMX connector
81+ * @throws IOException in case of communication error
82+ */
83+ public JMXConnector build () throws IOException {
84+ Map <String , Object > env = buildEnv ();
85+
86+ try {
87+ if (sslRegistry ) {
88+ return doConnectSslRegistry (url , env );
89+ }
90+
91+ return doConnect (url , env );
92+
93+ } catch (IOException e ) {
94+ throw new IOException ("Unable to connect to " + url .getHost () + ":" + url .getPort (), e );
95+ }
96+ }
97+
98+ private Map <String , Object > buildEnv () {
7899 Map <String , Object > env = new HashMap <>();
79100 if (userName != null && password != null ) {
80101 env .put (JMXConnector .CREDENTIALS , new String [] {userName , password });
@@ -111,16 +132,7 @@ public JMXConnector connect() throws IOException {
111132 } catch (ReflectiveOperationException e ) {
112133 logger .log (Level .WARNING , "SASL unsupported in current environment: " + e .getMessage ());
113134 }
114-
115- try {
116- if (sslRegistry ) {
117- return doConnectSslRegistry (url , env );
118- } else {
119- return doConnect (url , env );
120- }
121- } catch (IOException e ) {
122- throw new IOException ("Unable to connect to " + url .getHost () + ":" + url .getPort (), e );
123- }
135+ return env ;
124136 }
125137
126138 @ SuppressWarnings ("BanJNDI" )
0 commit comments