3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- package io .opentelemetry .contrib .jmxscraper . client ;
6
+ package io .opentelemetry .contrib .jmxscraper ;
7
7
8
8
import com .google .errorprone .annotations .CanIgnoreReturnValue ;
9
9
import java .io .IOException ;
26
26
import javax .security .auth .callback .UnsupportedCallbackException ;
27
27
import javax .security .sasl .RealmCallback ;
28
28
29
- public class JmxRemoteClient {
29
+ public class JmxConnectorBuilder {
30
30
31
- private static final Logger logger = Logger .getLogger (JmxRemoteClient .class .getName ());
31
+ private static final Logger logger = Logger .getLogger (JmxConnectorBuilder .class .getName ());
32
32
33
33
private final JMXServiceURL url ;
34
34
@ Nullable private String userName ;
@@ -37,44 +37,65 @@ public class JmxRemoteClient {
37
37
@ Nullable private String realm ;
38
38
private boolean sslRegistry ;
39
39
40
- private JmxRemoteClient (JMXServiceURL url ) {
40
+ private JmxConnectorBuilder (JMXServiceURL url ) {
41
41
this .url = url ;
42
42
}
43
43
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 ));
46
46
}
47
47
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 ));
50
50
}
51
51
52
52
@ CanIgnoreReturnValue
53
- public JmxRemoteClient userCredentials (String userName , String password ) {
53
+ public JmxConnectorBuilder userCredentials (String userName , String password ) {
54
54
this .userName = userName ;
55
55
this .password = password ;
56
56
return this ;
57
57
}
58
58
59
59
@ CanIgnoreReturnValue
60
- public JmxRemoteClient withRemoteProfile (String profile ) {
60
+ public JmxConnectorBuilder withRemoteProfile (String profile ) {
61
61
this .profile = profile ;
62
62
return this ;
63
63
}
64
64
65
65
@ CanIgnoreReturnValue
66
- public JmxRemoteClient withRealm (String realm ) {
66
+ public JmxConnectorBuilder withRealm (String realm ) {
67
67
this .realm = realm ;
68
68
return this ;
69
69
}
70
70
71
71
@ CanIgnoreReturnValue
72
- public JmxRemoteClient withSslRegistry () {
72
+ public JmxConnectorBuilder withSslRegistry () {
73
73
this .sslRegistry = true ;
74
74
return this ;
75
75
}
76
76
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 () {
78
99
Map <String , Object > env = new HashMap <>();
79
100
if (userName != null && password != null ) {
80
101
env .put (JMXConnector .CREDENTIALS , new String [] {userName , password });
@@ -111,16 +132,7 @@ public JMXConnector connect() throws IOException {
111
132
} catch (ReflectiveOperationException e ) {
112
133
logger .log (Level .WARNING , "SASL unsupported in current environment: " + e .getMessage ());
113
134
}
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 ;
124
136
}
125
137
126
138
@ SuppressWarnings ("BanJNDI" )
0 commit comments