Skip to content

Commit eab01e9

Browse files
committed
spotless
1 parent f6667dd commit eab01e9

File tree

4 files changed

+87
-78
lines changed

4 files changed

+87
-78
lines changed

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/TestApp.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
package io.opentelemetry.contrib.jmxscraper;
27

8+
import java.lang.management.ManagementFactory;
39
import javax.management.MBeanServer;
410
import javax.management.ObjectName;
5-
import java.lang.management.ManagementFactory;
611

712
@SuppressWarnings("all")
813
public class TestApp implements TestAppMXBean {
@@ -14,7 +19,7 @@ public class TestApp implements TestAppMXBean {
1419

1520
public static void main(String[] args) {
1621
TestApp app = TestApp.start();
17-
while(app.isRunning()){
22+
while (app.isRunning()) {
1823
try {
1924
Thread.sleep(100);
2025
} catch (InterruptedException e) {
@@ -23,10 +28,9 @@ public static void main(String[] args) {
2328
}
2429
}
2530

26-
private TestApp() {
27-
}
31+
private TestApp() {}
2832

29-
static TestApp start(){
33+
static TestApp start() {
3034
TestApp app = new TestApp();
3135
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
3236
try {
@@ -40,7 +44,6 @@ static TestApp start(){
4044
return app;
4145
}
4246

43-
4447
@Override
4548
public int getIntValue() {
4649
return 42;

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/TestAppMXBean.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
package io.opentelemetry.contrib.jmxscraper;
27

38
@SuppressWarnings("unused")

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/client/JmxRemoteClientTest.java

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
package io.opentelemetry.contrib.jmxscraper;
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.jmxscraper.client;
27

38
import static org.assertj.core.api.Assertions.assertThat;
49

10+
import io.opentelemetry.contrib.jmxscraper.TestApp;
511
import java.io.Closeable;
612
import java.io.IOException;
713
import java.nio.charset.StandardCharsets;
@@ -53,12 +59,9 @@ static void afterAll() {
5359
}
5460
}
5561

56-
5762
@Test
5863
void noAuth() {
59-
try (AppContainer app = new AppContainer()
60-
.withJmxPort(9990)
61-
.start()) {
64+
try (AppContainer app = new AppContainer().withJmxPort(9990).start()) {
6265
testConnector(() -> JmxRemoteClient.createNew(app.getHost(), app.getPort()).connect());
6366
}
6467
}
@@ -67,15 +70,13 @@ void noAuth() {
6770
void loginPwdAuth() {
6871
String login = "user";
6972
String pwd = "t0p!Secret";
70-
try (AppContainer app = new AppContainer()
71-
.withJmxPort(9999)
72-
.withUserAuth(login, pwd)
73-
.start()) {
74-
testConnector(() -> JmxRemoteClient.createNew(app.getHost(), app.getPort())
75-
.userCredentials(login, pwd)
76-
.connect());
73+
try (AppContainer app = new AppContainer().withJmxPort(9999).withUserAuth(login, pwd).start()) {
74+
testConnector(
75+
() ->
76+
JmxRemoteClient.createNew(app.getHost(), app.getPort())
77+
.userCredentials(login, pwd)
78+
.connect());
7779
}
78-
7980
}
8081

8182
@Test
@@ -95,20 +96,20 @@ private static void testConnector(ConnectorSupplier connectorSupplier) {
9596
try (JMXConnector connector = connectorSupplier.get()) {
9697
assertThat(connector.getMBeanServerConnection())
9798
.isNotNull()
98-
.satisfies(connection -> {
99-
try {
100-
ObjectName name = new ObjectName(TestApp.OBJECT_NAME);
101-
Object value = connection.getAttribute(name, "IntValue");
102-
assertThat(value).isEqualTo(42);
103-
} catch (Exception e) {
104-
throw new RuntimeException(e);
105-
}
106-
});
99+
.satisfies(
100+
connection -> {
101+
try {
102+
ObjectName name = new ObjectName(TestApp.OBJECT_NAME);
103+
Object value = connection.getAttribute(name, "IntValue");
104+
assertThat(value).isEqualTo(42);
105+
} catch (Exception e) {
106+
throw new RuntimeException(e);
107+
}
108+
});
107109

108110
} catch (IOException e) {
109111
throw new RuntimeException(e);
110112
}
111-
112113
}
113114

114115
private interface ConnectorSupplier {
@@ -131,19 +132,18 @@ private AppContainer() {
131132
// SSL registry : com.sun.management.jmxremote.registry.ssl
132133
// client side ssl auth: com.sun.management.jmxremote.ssl.need.client.auth
133134

134-
135135
String appJar = System.getProperty("app.jar.path");
136-
assertThat(Paths.get(appJar))
137-
.isNotEmptyFile()
138-
.isReadable();
139-
140-
this.appContainer = new GenericContainer<>("openjdk:8u272-jre-slim")
141-
.withCopyFileToContainer(MountableFile.forHostPath(appJar), "/app.jar")
142-
.withLogConsumer(new Slf4jLogConsumer(logger))
143-
.withNetwork(network)
144-
.waitingFor(Wait.forLogMessage(TestApp.APP_STARTED_MSG + "\\n", 1)
145-
.withStartupTimeout(Duration.ofSeconds(5)))
146-
.withCommand("java", "-jar", "/app.jar");
136+
assertThat(Paths.get(appJar)).isNotEmptyFile().isReadable();
137+
138+
this.appContainer =
139+
new GenericContainer<>("openjdk:8u272-jre-slim")
140+
.withCopyFileToContainer(MountableFile.forHostPath(appJar), "/app.jar")
141+
.withLogConsumer(new Slf4jLogConsumer(logger))
142+
.withNetwork(network)
143+
.waitingFor(
144+
Wait.forLogMessage(TestApp.APP_STARTED_MSG + "\\n", 1)
145+
.withStartupTimeout(Duration.ofSeconds(5)))
146+
.withCommand("java", "-jar", "/app.jar");
147147
}
148148

149149
@CanIgnoreReturnValue
@@ -177,20 +177,19 @@ AppContainer start() {
177177
properties.put("com.sun.management.jmxremote.access.file", "/jmx.access");
178178
}
179179

180-
String confArgs = properties.entrySet()
181-
.stream()
182-
.map(e -> {
183-
String s = "-D" + e.getKey();
184-
if (!e.getValue().isEmpty()) {
185-
s += "=" + e.getValue();
186-
}
187-
return s;
188-
})
189-
.collect(Collectors.joining(" "));
190-
191-
appContainer
192-
.withEnv("JAVA_TOOL_OPTIONS", confArgs)
193-
.start();
180+
String confArgs =
181+
properties.entrySet().stream()
182+
.map(
183+
e -> {
184+
String s = "-D" + e.getKey();
185+
if (!e.getValue().isEmpty()) {
186+
s += "=" + e.getValue();
187+
}
188+
return s;
189+
})
190+
.collect(Collectors.joining(" "));
191+
192+
appContainer.withEnv("JAVA_TOOL_OPTIONS", confArgs).start();
194193

195194
logger.info("Test application JMX port mapped to {}:{}", getHost(), getPort());
196195

@@ -238,5 +237,4 @@ private static void writeLine(Path path, String line) throws IOException {
238237
Files.write(path, line.getBytes(StandardCharsets.UTF_8));
239238
}
240239
}
241-
242240
}

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/client/JmxRemoteClient.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
package io.opentelemetry.contrib.jmxscraper.client;
27

38
import java.io.IOException;
@@ -6,6 +11,8 @@
611
import java.security.Security;
712
import java.util.HashMap;
813
import java.util.Map;
14+
import java.util.logging.Level;
15+
import java.util.logging.Logger;
916
import javax.management.remote.JMXConnector;
1017
import javax.management.remote.JMXConnectorFactory;
1118
import javax.management.remote.JMXServiceURL;
@@ -15,12 +22,10 @@
1522
import javax.security.auth.callback.PasswordCallback;
1623
import javax.security.auth.callback.UnsupportedCallbackException;
1724
import javax.security.sasl.RealmCallback;
18-
import org.slf4j.Logger;
19-
import org.slf4j.LoggerFactory;
2025

2126
public class JmxRemoteClient {
2227

23-
private static final Logger logger = LoggerFactory.getLogger(JmxRemoteClient.class);
28+
private static final Logger logger = Logger.getLogger(JmxRemoteClient.class.getName());
2429

2530
private final String host;
2631
private final int port;
@@ -78,22 +83,23 @@ public JMXConnector connect() throws IOException {
7883

7984
env.put(
8085
"jmx.remote.sasl.callback.handler",
81-
(CallbackHandler) callbacks -> {
82-
for (Callback callback : callbacks) {
83-
if (callback instanceof NameCallback) {
84-
((NameCallback) callback).setName(userName);
85-
} else if (callback instanceof PasswordCallback) {
86-
char[] pwd = password == null ? null : password.toCharArray();
87-
((PasswordCallback) callback).setPassword(pwd);
88-
} else if (callback instanceof RealmCallback) {
89-
((RealmCallback) callback).setText(realm);
90-
} else {
91-
throw new UnsupportedCallbackException(callback);
92-
}
93-
}
94-
});
86+
(CallbackHandler)
87+
callbacks -> {
88+
for (Callback callback : callbacks) {
89+
if (callback instanceof NameCallback) {
90+
((NameCallback) callback).setName(userName);
91+
} else if (callback instanceof PasswordCallback) {
92+
char[] pwd = password == null ? null : password.toCharArray();
93+
((PasswordCallback) callback).setPassword(pwd);
94+
} else if (callback instanceof RealmCallback) {
95+
((RealmCallback) callback).setText(realm);
96+
} else {
97+
throw new UnsupportedCallbackException(callback);
98+
}
99+
}
100+
});
95101
} catch (final ReflectiveOperationException e) {
96-
logger.warn("SASL unsupported in current environment: " + e.getMessage(), e);
102+
logger.log(Level.WARNING, "SASL unsupported in current environment: " + e.getMessage(), e);
97103
}
98104

99105
JMXServiceURL url = buildUrl(host, port);
@@ -117,15 +123,12 @@ private static JMXServiceURL buildUrl(String host, int port) {
117123
if (host != null) {
118124
sb.append(host);
119125
}
120-
sb.append(":")
121-
.append(port)
122-
.append("/jmxrmi");
126+
sb.append(":").append(port).append("/jmxrmi");
123127

124128
try {
125129
return new JMXServiceURL(sb.toString());
126130
} catch (MalformedURLException e) {
127131
throw new IllegalArgumentException("invalid url", e);
128132
}
129133
}
130-
131134
}

0 commit comments

Comments
 (0)