Skip to content

Commit d7aec54

Browse files
committed
fix pmd GuardLogStatement
1 parent 62c44e0 commit d7aec54

File tree

3 files changed

+117
-65
lines changed

3 files changed

+117
-65
lines changed

examples/authentication/src/main/java/cloud/stackit/sdk/authentication/examples/AuthenticationExample.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.FileNotFoundException;
99
import java.io.IOException;
1010
import java.util.Scanner;
11+
import java.util.logging.Level;
1112
import java.util.logging.Logger;
1213

1314
final class AuthenticationExample {
@@ -32,7 +33,9 @@ public static void main(String[] args) {
3233
ListOrganizationsResponse response =
3334
api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null);
3435

35-
LOGGER.info(response.toString());
36+
if (LOGGER.isLoggable(Level.INFO)) {
37+
LOGGER.info(response.toString());
38+
}
3639
} catch (ApiException | IOException e) {
3740
throw new RuntimeException(e);
3841
}
@@ -84,7 +87,9 @@ public static void main(String[] args) {
8487
ListOrganizationsResponse response =
8588
api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null);
8689

87-
LOGGER.info(response.toString());
90+
if (LOGGER.isLoggable(Level.INFO)) {
91+
LOGGER.info(response.toString());
92+
}
8893
} catch (ApiException | IOException e) {
8994
throw new RuntimeException(e);
9095
}
@@ -121,7 +126,9 @@ public static void main(String[] args) {
121126
ListOrganizationsResponse response =
122127
api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null);
123128

124-
LOGGER.info(response.toString());
129+
if (LOGGER.isLoggable(Level.INFO)) {
130+
LOGGER.info(response.toString());
131+
}
125132
} catch (ApiException | IOException e) {
126133
throw new RuntimeException(e);
127134
}

examples/iaas/src/main/java/cloud/stackit/sdk/iaas/examples/IaaSExample.java

Lines changed: 91 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Objects;
1010
import java.util.UUID;
1111
import java.util.concurrent.TimeUnit;
12+
import java.util.logging.Level;
1213
import java.util.logging.Logger;
1314

1415
final class IaaSExample {
@@ -69,19 +70,22 @@ public static void main(String[] args) throws IOException {
6970

7071
/* fetch the network we just created */
7172
Network fetchedNetwork = iaasApi.getNetwork(projectId, newNetwork.getNetworkId());
72-
LOGGER.info("\nFetched network: ");
73-
LOGGER.info("* Network name: " + fetchedNetwork.getName());
74-
LOGGER.info("* Id: " + fetchedNetwork.getNetworkId());
75-
LOGGER.info(
76-
"* DHCP: " + (Boolean.TRUE.equals(fetchedNetwork.getDhcp()) ? "YES" : "NO"));
77-
LOGGER.info("* Gateway: " + fetchedNetwork.getGateway());
78-
LOGGER.info("* Public IP: " + fetchedNetwork.getPublicIp());
73+
if (LOGGER.isLoggable(Level.INFO)) {
74+
LOGGER.info("\nFetched network: ");
75+
LOGGER.info("* Network name: " + fetchedNetwork.getName());
76+
LOGGER.info("* Id: " + fetchedNetwork.getNetworkId());
77+
LOGGER.info("* DHCP: " + (Boolean.TRUE.equals(fetchedNetwork.getDhcp()) ? "YES" : "NO"));
78+
LOGGER.info("* Gateway: " + fetchedNetwork.getGateway());
79+
LOGGER.info("* Public IP: " + fetchedNetwork.getPublicIp());
80+
}
7981

8082
/* list all available networks in the project */
8183
NetworkListResponse networks = iaasApi.listNetworks(projectId, null);
82-
LOGGER.info("\nAvailable networks: ");
83-
for (Network network : networks.getItems()) {
84-
LOGGER.info("* " + network.getName());
84+
if (LOGGER.isLoggable(Level.INFO)) {
85+
LOGGER.info("\nAvailable networks: ");
86+
for (Network network : networks.getItems()) {
87+
LOGGER.info("* " + network.getName());
88+
}
8589
}
8690

8791
/*
@@ -92,9 +96,11 @@ public static void main(String[] args) throws IOException {
9296

9397
/* list all available images */
9498
ImageListResponse images = iaasApi.listImages(projectId, false, null);
95-
LOGGER.info("\nAvailable images: ");
96-
for (Image image : images.getItems()) {
97-
LOGGER.info(image.getId() + " | " + image.getName());
99+
if (LOGGER.isLoggable(Level.INFO)) {
100+
LOGGER.info("\nAvailable images: ");
101+
for (Image image : images.getItems()) {
102+
LOGGER.info(image.getId() + " | " + image.getName());
103+
}
98104
}
99105

100106
/* get an image */
@@ -104,12 +110,14 @@ public static void main(String[] args) throws IOException {
104110
.getId(); // we just use a random image id in our example
105111
assert imageId != null;
106112
Image fetchedImage = iaasApi.getImage(projectId, imageId);
107-
LOGGER.info("\nFetched image:");
108-
LOGGER.info("* Image name: " + fetchedImage.getName());
109-
LOGGER.info("* Image id: " + fetchedImage.getId());
110-
LOGGER.info("* Checksum: " + fetchedImage.getChecksum());
111-
LOGGER.info("* Created at: " + fetchedImage.getCreatedAt());
112-
LOGGER.info("* Updated at: " + fetchedImage.getUpdatedAt());
113+
if (LOGGER.isLoggable(Level.INFO)) {
114+
LOGGER.info("\nFetched image:");
115+
LOGGER.info("* Image name: " + fetchedImage.getName());
116+
LOGGER.info("* Image id: " + fetchedImage.getId());
117+
LOGGER.info("* Checksum: " + fetchedImage.getChecksum());
118+
LOGGER.info("* Created at: " + fetchedImage.getCreatedAt());
119+
LOGGER.info("* Updated at: " + fetchedImage.getUpdatedAt());
120+
}
113121

114122
/*
115123
* ///////////////////////////////////////////////////////
@@ -119,9 +127,11 @@ public static void main(String[] args) throws IOException {
119127

120128
/* list all available keypairs */
121129
KeyPairListResponse keypairs = iaasApi.listKeyPairs(null);
122-
LOGGER.info("\nAvailable keypairs: ");
123-
for (Keypair keypair : keypairs.getItems()) {
124-
LOGGER.info("* " + keypair.getName());
130+
if (LOGGER.isLoggable(Level.INFO)) {
131+
LOGGER.info("\nAvailable keypairs: ");
132+
for (Keypair keypair : keypairs.getItems()) {
133+
LOGGER.info("* " + keypair.getName());
134+
}
125135
}
126136

127137
/* create a keypair */
@@ -132,7 +142,9 @@ public static void main(String[] args) throws IOException {
132142
new CreateKeyPairPayload()
133143
.name("java-sdk-example-keypair-01")
134144
.publicKey(publicKey));
135-
LOGGER.info("\nKeypair created: " + newKeypair.getName());
145+
if (LOGGER.isLoggable(Level.INFO)) {
146+
LOGGER.info("\nKeypair created: " + newKeypair.getName());
147+
}
136148

137149
/* update the keypair */
138150
assert newKeypair.getName() != null;
@@ -143,13 +155,15 @@ public static void main(String[] args) throws IOException {
143155

144156
/* fetch the keypair we just created / updated */
145157
Keypair fetchedKeypair = iaasApi.getKeyPair(newKeypair.getName());
146-
LOGGER.info("\nFetched key pair: ");
147-
LOGGER.info("* Name: " + fetchedKeypair.getName());
148-
if (fetchedKeypair.getLabels() != null) {
149-
LOGGER.info("* Labels: " + fetchedKeypair.getLabels().toString());
158+
if (LOGGER.isLoggable(Level.INFO)) {
159+
LOGGER.info("\nFetched key pair: ");
160+
LOGGER.info("* Name: " + fetchedKeypair.getName());
161+
if (fetchedKeypair.getLabels() != null) {
162+
LOGGER.info("* Labels: " + fetchedKeypair.getLabels().toString()); // NOPMD GuardLogStatement
163+
}
164+
LOGGER.info("* Fingerprint: " + fetchedKeypair.getFingerprint());
165+
LOGGER.info("* Public key: " + fetchedKeypair.getPublicKey());
150166
}
151-
LOGGER.info("* Fingerprint: " + fetchedKeypair.getFingerprint());
152-
LOGGER.info("* Public key: " + fetchedKeypair.getPublicKey());
153167

154168
/*
155169
* ///////////////////////////////////////////////////////
@@ -159,21 +173,25 @@ public static void main(String[] args) throws IOException {
159173

160174
/* list all available machine types */
161175
MachineTypeListResponse machineTypes = iaasApi.listMachineTypes(projectId, null);
162-
LOGGER.info("\nAvailable machine types: ");
163-
for (MachineType machineType : machineTypes.getItems()) {
164-
LOGGER.info("* " + machineType.getName());
176+
if (LOGGER.isLoggable(Level.INFO)) {
177+
LOGGER.info("\nAvailable machine types: ");
178+
for (MachineType machineType : machineTypes.getItems()) {
179+
LOGGER.info("* " + machineType.getName());
180+
}
165181
}
166182

167183
/* fetch details about a machine type */
168184
MachineType fetchedMachineType =
169185
iaasApi.getMachineType(projectId, machineTypes.getItems().get(0).getName());
170-
LOGGER.info("\nFetched machine type: ");
171-
LOGGER.info("* Machine type name: " + fetchedMachineType.getName());
172-
LOGGER.info("* Description: " + fetchedMachineType.getDescription());
173-
LOGGER.info("* Disk size: " + fetchedMachineType.getDisk());
174-
LOGGER.info("* RAM: " + fetchedMachineType.getRam());
175-
LOGGER.info("* vCPUs: " + fetchedMachineType.getVcpus());
176-
LOGGER.info("* Extra specs: " + fetchedMachineType.getExtraSpecs());
186+
if (LOGGER.isLoggable(Level.INFO)) {
187+
LOGGER.info("\nFetched machine type: ");
188+
LOGGER.info("* Machine type name: " + fetchedMachineType.getName());
189+
LOGGER.info("* Description: " + fetchedMachineType.getDescription());
190+
LOGGER.info("* Disk size: " + fetchedMachineType.getDisk());
191+
LOGGER.info("* RAM: " + fetchedMachineType.getRam());
192+
LOGGER.info("* vCPUs: " + fetchedMachineType.getVcpus());
193+
LOGGER.info("* Extra specs: " + fetchedMachineType.getExtraSpecs());
194+
}
177195

178196
/*
179197
* create a server
@@ -216,30 +234,36 @@ public static void main(String[] args) throws IOException {
216234

217235
/* list all servers */
218236
ServerListResponse servers = iaasApi.listServers(projectId, false, null);
219-
LOGGER.info("\nAvailable servers: ");
220-
for (Server server : servers.getItems()) {
221-
LOGGER.info("* " + server.getId() + " | " + server.getName());
237+
if (LOGGER.isLoggable(Level.INFO)) {
238+
LOGGER.info("\nAvailable servers: ");
239+
for (Server server : servers.getItems()) {
240+
LOGGER.info("* " + server.getId() + " | " + server.getName());
241+
}
222242
}
223243

224244
/* fetch the server we just created */
225245
Server fetchedServer = iaasApi.getServer(projectId, serverId, false);
226-
LOGGER.info("\nFetched server:");
227-
LOGGER.info("* Name: " + fetchedServer.getName());
228-
LOGGER.info("* Id: " + fetchedServer.getId());
229-
if (fetchedServer.getLabels() != null) {
230-
LOGGER.info("* Labels: " + fetchedServer.getLabels().toString());
246+
if (LOGGER.isLoggable(Level.INFO)) {
247+
LOGGER.info("\nFetched server:");
248+
LOGGER.info("* Name: " + fetchedServer.getName());
249+
LOGGER.info("* Id: " + fetchedServer.getId());
250+
if (fetchedServer.getLabels() != null) {
251+
LOGGER.info("* Labels: " + fetchedServer.getLabels().toString()); // NOPMD GuardLogStatement
252+
}
253+
LOGGER.info("* Machine type: " + fetchedServer.getMachineType());
254+
LOGGER.info("* Created at: " + fetchedServer.getCreatedAt());
255+
LOGGER.info("* Updated at: " + fetchedServer.getUpdatedAt());
256+
LOGGER.info("* Launched at: " + fetchedServer.getLaunchedAt());
231257
}
232-
LOGGER.info("* Machine type: " + fetchedServer.getMachineType());
233-
LOGGER.info("* Created at: " + fetchedServer.getCreatedAt());
234-
LOGGER.info("* Updated at: " + fetchedServer.getUpdatedAt());
235-
LOGGER.info("* Launched at: " + fetchedServer.getLaunchedAt());
236258

237259
/* stop the server we just created */
238260
iaasApi.stopServer(projectId, serverId);
239261
/* wait for the server to stop */
240262
while (!Objects.equals(
241263
iaasApi.getServer(projectId, serverId, false).getPowerStatus(), "STOPPED")) {
242-
LOGGER.info("Waiting for server " + serverId + " to stop...");
264+
if (LOGGER.isLoggable(Level.INFO)) {
265+
LOGGER.info("Waiting for server " + serverId + " to stop...");
266+
}
243267
TimeUnit.SECONDS.sleep(5);
244268
}
245269

@@ -248,7 +272,9 @@ public static void main(String[] args) throws IOException {
248272
/* wait for the server to boot */
249273
while (!Objects.equals(
250274
iaasApi.getServer(projectId, serverId, false).getPowerStatus(), "RUNNING")) {
251-
LOGGER.info("Waiting for server " + serverId + " to boot...");
275+
if (LOGGER.isLoggable(Level.INFO)) {
276+
LOGGER.info("Waiting for server " + serverId + " to boot...");
277+
}
252278
TimeUnit.SECONDS.sleep(5);
253279
}
254280

@@ -263,13 +289,17 @@ public static void main(String[] args) throws IOException {
263289

264290
/* delete the server we just created */
265291
iaasApi.deleteServer(projectId, serverId);
266-
LOGGER.info("Deleted server: " + serverId);
292+
if (LOGGER.isLoggable(Level.INFO)) {
293+
LOGGER.info("Deleted server: " + serverId);
294+
}
267295

268296
/* wait for server deletion to complete */
269297
while (true) {
270298
try {
271299
iaasApi.getServer(projectId, serverId, false);
272-
LOGGER.info("Waiting for server deletion to complete...");
300+
if (LOGGER.isLoggable(Level.INFO)) {
301+
LOGGER.info("Waiting for server deletion to complete...");
302+
}
273303
TimeUnit.SECONDS.sleep(5);
274304
} catch (ApiException e) {
275305
if (e.getCode() == HttpURLConnection.HTTP_NOT_FOUND) {
@@ -280,11 +310,15 @@ public static void main(String[] args) throws IOException {
280310

281311
/* delete the keypair we just created */
282312
iaasApi.deleteKeyPair(newKeypair.getName());
283-
LOGGER.info("Deleted key pair: " + newKeypair.getName());
313+
if (LOGGER.isLoggable(Level.INFO)) {
314+
LOGGER.info("Deleted key pair: " + newKeypair.getName());
315+
}
284316

285317
/* delete the network we just created */
286318
iaasApi.deleteNetwork(projectId, newNetwork.getNetworkId());
287-
LOGGER.info("Deleted network: " + newNetwork.getNetworkId());
319+
if (LOGGER.isLoggable(Level.INFO)) {
320+
LOGGER.info("Deleted network: " + newNetwork.getNetworkId());
321+
}
288322

289323
} catch (ApiException | InterruptedException e) {
290324
throw new RuntimeException(e);

examples/resourcemanager/src/main/java/cloud/stackit/sdk/resourcemanager/examples/ResourcemanagerExample.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Arrays;
1717
import java.util.Collections;
1818
import java.util.UUID;
19+
import java.util.logging.Level;
1920
import java.util.logging.Logger;
2021

2122
final class ResourcemanagerExample {
@@ -55,13 +56,17 @@ public static void main(String[] args) throws IOException {
5556
.labels(
5657
Collections.singletonMap(
5758
"some-project-label", "foo-bar")));
58-
LOGGER.info("Project:\n" + project.toString());
59+
if (LOGGER.isLoggable(Level.INFO)) {
60+
LOGGER.info("Project:\n" + project.toString());
61+
}
5962

6063
/* list projects */
6164
ListProjectsResponse responseListProject =
6265
resourceManagerApi.listProjects(
6366
organizationIdString, null, null, null, null, null);
64-
LOGGER.info("Project List:\n" + responseListProject.toString());
67+
if (LOGGER.isLoggable(Level.INFO)) {
68+
LOGGER.info("Project List:\n" + responseListProject.toString());
69+
}
6570

6671
/* create a folder */
6772
FolderResponse folder =
@@ -70,13 +75,17 @@ public static void main(String[] args) throws IOException {
7075
.containerParentId(containerParentId.toString())
7176
.name("java-testing-folder")
7277
.labels(Collections.singletonMap("foo", "bar")));
73-
LOGGER.info("Folder: \n" + folder.toString());
78+
if (LOGGER.isLoggable(Level.INFO)) {
79+
LOGGER.info("Folder: \n" + folder.toString());
80+
}
7481

7582
/* list folders */
7683
ListFoldersResponse responseListFolders =
7784
resourceManagerApi.listFolders(
7885
organizationIdString, null, null, null, null, null);
79-
LOGGER.info("Folder List:\n" + responseListFolders.toString());
86+
if (LOGGER.isLoggable(Level.INFO)) {
87+
LOGGER.info("Folder List:\n" + responseListFolders.toString());
88+
}
8089

8190
/* delete a project label */
8291
resourceManagerApi.deleteProjectLabels(project.getContainerId(), Arrays.asList("foo"));
@@ -98,7 +107,9 @@ public static void main(String[] args) throws IOException {
98107
/* get organization details */
99108
OrganizationResponse organizationResponse =
100109
resourceManagerApi.getOrganization(organizationIdString);
101-
LOGGER.info("Organization List:\n" + organizationResponse.toString());
110+
if (LOGGER.isLoggable(Level.INFO)) {
111+
LOGGER.info("Organization List:\n" + organizationResponse.toString());
112+
}
102113

103114
/* since you cannot delete a folder when a project is present we need to move the project out again */
104115
resourceManagerApi.partialUpdateProject(

0 commit comments

Comments
 (0)