Skip to content

Commit 020d333

Browse files
committed
Replaced all occurrences of string concatenation.
1 parent c9f6e15 commit 020d333

11 files changed

+39
-28
lines changed

src/main/java/cd/go/contrib/elasticagent/KubernetesAgentInstances.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
3535
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.SPECIFIED_USING_POD_CONFIGURATION;
3636
import static cd.go.contrib.elasticagent.utils.Util.getSimpleDateFormat;
37+
import static java.text.MessageFormat.format;
3738

3839
public class KubernetesAgentInstances implements AgentInstances<KubernetesInstance> {
3940
private final ConcurrentHashMap<String, KubernetesInstance> instances = new ConcurrentHashMap<>();
@@ -65,7 +66,7 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
6566
if (semaphore.tryAcquire()) {
6667
return createKubernetesInstance(request, settings, pluginRequest);
6768
} else {
68-
LOG.warn(String.format("The number of pending kubernetes pods is currently at the maximum permissible limit (%d). Total kubernetes pods (%d). Not creating any more containers.", maxAllowedContainers, instances.size()));
69+
LOG.warn(format("The number of pending kubernetes pods is currently at the maximum permissible limit ({0}). Total kubernetes pods ({1}). Not creating any more containers.", maxAllowedContainers, instances.size()));
6970
return null;
7071
}
7172
}
@@ -77,10 +78,10 @@ private void doWithLockOnSemaphore(Runnable runnable) {
7778
}
7879
}
7980

80-
private KubernetesInstance createKubernetesInstance(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) throws Exception {
81+
private KubernetesInstance createKubernetesInstance(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) {
8182
JobIdentifier jobIdentifier = request.jobIdentifier();
8283
if (isAgentCreatedForJob(jobIdentifier.getJobId())) {
83-
LOG.warn("[Create Agent Request] Request for creating an agent for Job Identifier [" + jobIdentifier + "] has already been scheduled. Skipping current request.");
84+
LOG.warn(format("[Create Agent Request] Request for creating an agent for Job Identifier [{0}] has already been scheduled. Skipping current request.", jobIdentifier));
8485
return null;
8586
}
8687

@@ -112,7 +113,7 @@ public void terminate(String agentId, PluginSettings settings) throws Exception
112113
KubernetesClient client = factory.client(settings);
113114
instance.terminate(client);
114115
} else {
115-
LOG.warn("Requested to terminate an instance that does not exist " + agentId);
116+
LOG.warn(format("Requested to terminate an instance that does not exist {0}.", agentId));
116117
}
117118
instances.remove(agentId);
118119
}
@@ -124,7 +125,7 @@ public void terminateUnregisteredInstances(PluginSettings settings, Agents agent
124125
return;
125126
}
126127

127-
LOG.warn("Terminating instances that did not register " + toTerminate.instances.keySet());
128+
LOG.warn(format("Terminating instances that did not register {0}.", toTerminate.instances.keySet()));
128129
for (KubernetesInstance container : toTerminate.instances.values()) {
129130
terminate(container.name(), settings);
130131
}
@@ -147,8 +148,8 @@ public Agents instancesCreatedAfterTimeout(PluginSettings settings, Agents agent
147148
}
148149

149150
@Override
150-
public void refreshAll(PluginRequest pluginRequest) throws Exception {
151-
LOG.debug("[Refresh Instances]. Syncing k8s elastic agent pod information");
151+
public void refreshAll(PluginRequest pluginRequest) {
152+
LOG.debug("[Refresh Instances]. Syncing k8s elastic agent pod information.");
152153
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
153154
PodList list = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).list();
154155

src/main/java/cd/go/contrib/elasticagent/KubernetesInstanceFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.PRIVILEGED;
4242
import static cd.go.contrib.elasticagent.utils.Util.GSON;
4343
import static cd.go.contrib.elasticagent.utils.Util.getSimpleDateFormat;
44-
import static java.lang.String.format;
4544
import static java.lang.String.valueOf;
45+
import static java.text.MessageFormat.format;
4646
import static org.apache.commons.lang3.StringUtils.isBlank;
4747

4848
public class KubernetesInstanceFactory {
@@ -55,7 +55,7 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
5555
}
5656

5757
private KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, KubernetesClient client, PluginRequest pluginRequest) {
58-
String containerName = format("%s-%s", KUBERNETES_POD_NAME_PREFIX, UUID.randomUUID().toString());
58+
String containerName = format("{0}-{1}", KUBERNETES_POD_NAME_PREFIX, UUID.randomUUID().toString());
5959

6060
Container container = new Container();
6161
container.setName(containerName);
@@ -101,13 +101,13 @@ private ResourceRequirements getPodResources(CreateAgentRequest request) {
101101
String maxMemory = request.properties().get("MaxMemory");
102102
if (StringUtils.isNotBlank(maxMemory)) {
103103
Size mem = Size.parse(maxMemory);
104-
LOG.debug(format("[Create Agent] Setting memory resource limit on k8s pod:%s", new Quantity(valueOf(mem.toMegabytes()), "M")));
104+
LOG.debug(format("[Create Agent] Setting memory resource limit on k8s pod: {0}.", new Quantity(valueOf(mem.toMegabytes()), "M")));
105105
limits.put("memory", new Quantity(valueOf(mem.toBytes())));
106106
}
107107

108108
String maxCPU = request.properties().get("MaxCPU");
109109
if (StringUtils.isNotBlank(maxCPU)) {
110-
LOG.debug(format("[Create Agent] Setting cpu resource limit on k8s pod:%s", new Quantity(maxCPU)));
110+
LOG.debug(format("[Create Agent] Setting cpu resource limit on k8s pod: {0}.", new Quantity(maxCPU)));
111111
limits.put("cpu", new Quantity(maxCPU));
112112
}
113113

@@ -130,7 +130,7 @@ private static void setAnnotations(Pod pod, CreateAgentRequest request) {
130130
}
131131

132132
private KubernetesInstance createKubernetesPod(KubernetesClient client, Pod elasticAgentPod) {
133-
LOG.info(format("[Create Agent] Creating K8s pod with spec:%s", elasticAgentPod.toString()));
133+
LOG.info(format("[Create Agent] Creating K8s pod with spec: {0}.", elasticAgentPod.toString()));
134134
Pod pod = client.pods().inNamespace(KUBERNETES_NAMESPACE).create(elasticAgentPod);
135135
return fromKubernetesPod(pod);
136136
}

src/main/java/cd/go/contrib/elasticagent/PluginRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import static cd.go.contrib.elasticagent.Constants.*;
2727
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
28+
import static java.text.MessageFormat.format;
2829

2930
public class PluginRequest {
3031
private final GoApplicationAccessor accessor;
@@ -72,7 +73,7 @@ public Agents listAgents() throws ServerRequestFailedException {
7273
}
7374

7475
public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFailedException {
75-
LOG.debug("[Server Ping] Disabling Agents:" + toBeDisabled.toString());
76+
LOG.debug(format("[Server Ping] Disabling Agents: {0}", toBeDisabled.toString()));
7677
if (toBeDisabled.isEmpty()) {
7778
return;
7879
}
@@ -88,7 +89,7 @@ public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFa
8889
}
8990

9091
public void deleteAgents(Collection<Agent> toBeDeleted) throws ServerRequestFailedException {
91-
LOG.debug("[Server Ping] Deleting Agents:" + toBeDeleted.toString());
92+
LOG.debug(format("[Server Ping] Deleting Agents: {0}", toBeDeleted.toString()));
9293
if (toBeDeleted.isEmpty()) {
9394
return;
9495
}

src/main/java/cd/go/contrib/elasticagent/ServerRequestFailedException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
import com.thoughtworks.go.plugin.api.response.GoApiResponse;
2020

21-
import static java.lang.String.format;
21+
import static java.text.MessageFormat.format;
2222

2323
public class ServerRequestFailedException extends RuntimeException {
2424

2525
private ServerRequestFailedException(GoApiResponse response, String request) {
2626
super(format(
27-
"The server sent an unexpected status code %d with the response body %s when it was sent a %s message",
27+
"The server sent an unexpected status code {0} with the response body {1} when it was sent a {2} message",
2828
response.responseCode(), response.responseBody(), request
2929
));
3030
}

src/main/java/cd/go/contrib/elasticagent/executors/AgentStatusReportExecutor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818

1919
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
20+
import static java.text.MessageFormat.format;
2021

2122
public class AgentStatusReportExecutor {
2223
private final AgentStatusReportRequest request;
@@ -38,7 +39,7 @@ public AgentStatusReportExecutor(AgentStatusReportRequest request, PluginRequest
3839
public GoPluginApiResponse execute() throws Exception {
3940
String elasticAgentId = request.getElasticAgentId();
4041
JobIdentifier jobIdentifier = request.getJobIdentifier();
41-
LOG.info(String.format("[status-report] Generating status report for agent: %s with job: %s", elasticAgentId, jobIdentifier));
42+
LOG.info(format("[status-report] Generating status report for agent: {0} with job: {1}", elasticAgentId, jobIdentifier));
4243
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
4344

4445
try {
@@ -73,7 +74,7 @@ private Pod findPodUsingJobIdentifier(JobIdentifier jobIdentifier, KubernetesCli
7374
.withLabel(Constants.JOB_ID_LABEL_KEY, String.valueOf(jobIdentifier.getJobId()))
7475
.list().getItems().get(0);
7576
} catch (Exception e) {
76-
throw new RuntimeException(String.format("Can not find a running Pod for the provided job identifier '%s'", jobIdentifier));
77+
throw new RuntimeException(format("Can not find a running Pod for the provided job identifier ''{0}", jobIdentifier));
7778
}
7879
}
7980

@@ -85,6 +86,6 @@ private Pod findPodUsingElasticAgentId(String elasticAgentId, KubernetesClient c
8586
}
8687
}
8788

88-
throw new RuntimeException(String.format("Can not find a running Pod for the provided elastic agent id '%s'", elasticAgentId));
89+
throw new RuntimeException(format("Can not find a running Pod for the provided elastic agent id ''{0}", elasticAgentId));
8990
}
9091
}

src/main/java/cd/go/contrib/elasticagent/executors/CreateAgentRequestExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
2626

2727
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
28+
import static java.text.MessageFormat.format;
2829

2930
public class CreateAgentRequestExecutor implements RequestExecutor {
3031
private final AgentInstances<KubernetesInstance> agentInstances;
@@ -39,7 +40,7 @@ public CreateAgentRequestExecutor(CreateAgentRequest request, AgentInstances<Kub
3940

4041
@Override
4142
public GoPluginApiResponse execute() throws Exception {
42-
LOG.debug(String.format("[Create Agent] creating elastic agent for profile %s", request.properties()));
43+
LOG.debug(format("[Create Agent] creating elastic agent for profile {0}", request.properties()));
4344
agentInstances.create(request, pluginRequest.getPluginSettings(), pluginRequest);
4445
return new DefaultGoPluginApiResponse(200);
4546
}

src/main/java/cd/go/contrib/elasticagent/executors/ProfileValidateRequestExecutor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
3434
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.*;
3535
import static cd.go.contrib.elasticagent.utils.Util.GSON;
36+
import static java.text.MessageFormat.format;
3637

3738
public class ProfileValidateRequestExecutor implements RequestExecutor {
3839
private final ProfileValidateRequest request;
@@ -42,7 +43,7 @@ public ProfileValidateRequestExecutor(ProfileValidateRequest request) {
4243
}
4344

4445
@Override
45-
public GoPluginApiResponse execute() throws Exception {
46+
public GoPluginApiResponse execute() {
4647
LOG.debug("Validating elastic profile.");
4748
ArrayList<Map<String, String>> result = new ArrayList<>();
4849
List<String> knownFields = new ArrayList<>();
@@ -104,7 +105,7 @@ private void validatePodYaml(HashMap<String, String> properties, ArrayList<Map<S
104105
}
105106

106107
private void addNotBlankError(ArrayList<Map<String, String>> result, String key, String value) {
107-
addError(result, key, String.format("%s must not be blank.", value));
108+
addError(result, key, format("{0} must not be blank.", value));
108109
}
109110

110111
private void addError(ArrayList<Map<String, String>> result, String key, String message) {

src/main/java/cd/go/contrib/elasticagent/executors/ServerPingRequestExecutor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Collection;
2424

2525
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
26+
import static java.text.MessageFormat.format;
2627

2728
public class ServerPingRequestExecutor implements RequestExecutor {
2829

@@ -43,14 +44,14 @@ public GoPluginApiResponse execute() throws Exception {
4344

4445
for (Agent agent : allAgents.agents()) {
4546
if (agentInstances.find(agent.elasticAgentId()) == null) {
46-
LOG.warn(String.format("Was expecting a container with name %s, but it was missing!", agent.elasticAgentId()));
47+
LOG.warn(format("Was expecting a container with name {0}, but it was missing!", agent.elasticAgentId()));
4748
missingAgents.add(agent);
4849
}
4950
}
5051

51-
LOG.debug(String.format("[Server Ping] Missing Agents:%s", missingAgents.agentIds()));
52+
LOG.debug(format("[Server Ping] Missing Agents:{0}", missingAgents.agentIds()));
5253
Agents agentsToDisable = agentInstances.instancesCreatedAfterTimeout(pluginSettings, allAgents);
53-
LOG.debug(String.format("[Server Ping] Agent Created After Timeout:%s", agentsToDisable.agentIds()));
54+
LOG.debug(format("[Server Ping] Agent Created After Timeout:{0}", agentsToDisable.agentIds()));
5455
agentsToDisable.addAll(missingAgents);
5556

5657
disableIdleAgents(agentsToDisable);

src/main/java/cd/go/contrib/elasticagent/executors/ShouldAssignWorkRequestExecutor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*
23
* Copyright 2017 ThoughtWorks, Inc.
34
*

src/main/java/cd/go/contrib/elasticagent/model/reports/agent/GoCDContainerDetails.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.ArrayList;
99
import java.util.List;
1010

11+
import static java.text.MessageFormat.format;
12+
1113
public class GoCDContainerDetails {
1214
private String name;
1315
private String image;
@@ -25,7 +27,7 @@ public static GoCDContainerDetails fromContainer(Container container, ContainerS
2527
containerDetails.imagePullPolicy = container.getImagePullPolicy();
2628

2729
containerDetails.command = container.getCommand();
28-
containerDetails.env = new ArrayList<EnvironmentVariable>();
30+
containerDetails.env = new ArrayList<>();
2931
for (EnvVar var : container.getEnv()) {
3032
containerDetails.env.add(new EnvironmentVariable(var.getName(), var.getValue()));
3133
}
@@ -83,7 +85,7 @@ public String getValue() {
8385

8486
@Override
8587
public String toString() {
86-
return String.format("%s: %s", getName(), getValue());
88+
return format("{0}: {1}", getName(), getValue());
8789
}
8890
}
8991
}

0 commit comments

Comments
 (0)