Skip to content

Commit 315bb4f

Browse files
committed
Fixed #395 - Remove google guava lib
- Replaced many things with base JDK8 constructs. - In JDK9 we can improve some more parts. - We do not generat shaded artifact 'stash' anymore. - Upgrade version number based on the change.
1 parent 23e120e commit 315bb4f

File tree

23 files changed

+331
-312
lines changed

23 files changed

+331
-312
lines changed

ReleaseNotes.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Release Notes
22

3-
## Release 0.3.9 (NOT RELEASED YET)
3+
## Release 0.4.0 (NOT RELEASED YET)
4+
5+
* [Fixed Issue 395][issue-395]
6+
7+
* Remove google guava lib
8+
* Removed also the creation of the shaded artifact `stash`
9+
cause we do not rely on Guava anymore. So you
10+
can use the original artifact directly.
11+
* This results in a bumping of the version
12+
number cause it a change which is breaking
13+
with previous version 0.3.8.
414

515
* [Fixed Issue 405][issue-405]
616

@@ -1113,6 +1123,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
11131123
[issue-298]: https://github.com/jenkinsci/java-client-api/issues/298
11141124
[issue-301]: https://github.com/jenkinsci/java-client-api/issues/301
11151125
[issue-394]: https://github.com/jenkinsci/java-client-api/issues/394
1126+
[issue-395]: https://github.com/jenkinsci/java-client-api/issues/395
11161127
[issue-396]: https://github.com/jenkinsci/java-client-api/issues/396
11171128
[issue-397]: https://github.com/jenkinsci/java-client-api/issues/397
11181129
[issue-399]: https://github.com/jenkinsci/java-client-api/issues/399

jenkins-client-it-docker/pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.offbytwo.jenkins</groupId>
1313
<artifactId>jenkins-client-parent</artifactId>
14-
<version>0.3.9-SNAPSHOT</version>
14+
<version>0.4.0-SNAPSHOT</version>
1515
</parent>
1616

1717
<artifactId>jenkins-client-it-docker</artifactId>
@@ -43,11 +43,6 @@
4343
<artifactId>assertj-core</artifactId>
4444
<scope>test</scope>
4545
</dependency>
46-
<dependency>
47-
<groupId>com.google.guava</groupId>
48-
<artifactId>guava</artifactId>
49-
<scope>test</scope>
50-
</dependency>
5146
<dependency>
5247
<groupId>org.apache.httpcomponents</groupId>
5348
<artifactId>httpclient</artifactId>

jenkins-client-it-docker/src/test/java/com/offbytwo/jenkins/integration/NoExecutorStartedGetJobIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.testng.annotations.BeforeMethod;
88
import org.testng.annotations.Test;
99

10-
import com.google.common.base.Joiner;
1110
import com.offbytwo.jenkins.model.Build;
1211
import com.offbytwo.jenkins.model.BuildResult;
1312
import com.offbytwo.jenkins.model.BuildWithDetails;
@@ -50,7 +49,7 @@ private void checkJob(BuildWithDetails details) throws IOException {
5049
"Building in workspace /var/jenkins_home/jobs/test/workspace",
5150
"[workspace] $ /bin/sh -xe /tmp/hudson2556403647634111927.sh", "+ echo test", "test",
5251
"Finished: SUCCESS", "" };
53-
String expectedOutput = Joiner.on("\r\n").join(expectedOutputLines);
52+
String expectedOutput = String.join("\r\n", expectedOutputLines);
5453
// Hint: It looks like the consoleOutputText contains CR+LF
5554
String resultingOutput = details.getConsoleOutputText();
5655
assertThat(resultingOutput).isEqualTo(expectedOutput);

jenkins-client-it-docker/src/test/java/com/offbytwo/jenkins/integration/NoExecutorStartedGetJobXmlIT.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import org.testng.annotations.BeforeMethod;
88
import org.testng.annotations.Test;
99

10-
import com.google.common.base.Joiner;
11-
1210
@Test(groups = { Groups.NO_EXECUTOR_GROUP })
1311
public class NoExecutorStartedGetJobXmlIT extends AbstractJenkinsIntegrationCase {
1412

@@ -47,7 +45,7 @@ public void beforeMethod() throws IOException {
4745

4846
@Test
4947
public void getJobXmlShouldReturnTheExpectedConfigXml() {
50-
String expectedXml = Joiner.on("\n").join(CONFIG_XML);
48+
String expectedXml = String.join("\n", CONFIG_XML);
5149
assertThat(jobXml).isEqualTo(expectedXml);
5250
}
5351

jenkins-client/pom.xml

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.offbytwo.jenkins</groupId>
1313
<artifactId>jenkins-client-parent</artifactId>
14-
<version>0.3.9-SNAPSHOT</version>
14+
<version>0.4.0-SNAPSHOT</version>
1515
</parent>
1616

1717
<artifactId>jenkins-client</artifactId>
@@ -51,11 +51,6 @@
5151
</dependency>
5252

5353
<!-- Musties -->
54-
<dependency>
55-
<groupId>com.google.guava</groupId>
56-
<artifactId>guava</artifactId>
57-
</dependency>
58-
5954
<dependency>
6055
<groupId>org.apache.commons</groupId>
6156
<artifactId>commons-lang3</artifactId>
@@ -127,28 +122,6 @@
127122
<groupId>org.apache.maven.plugins</groupId>
128123
<artifactId>maven-shade-plugin</artifactId>
129124
<executions>
130-
<execution>
131-
<id>stash</id>
132-
<phase>package</phase>
133-
<goals>
134-
<goal>shade</goal>
135-
</goals>
136-
<configuration>
137-
<artifactSet>
138-
<includes>
139-
<include>com.google.guava:guava</include>
140-
</includes>
141-
</artifactSet>
142-
<shadedArtifactAttached>true</shadedArtifactAttached>
143-
<shadedClassifierName>stash</shadedClassifierName>
144-
<relocations>
145-
<relocation>
146-
<pattern>com.google.common</pattern>
147-
<shadedPattern>com.google.common.jenkins_client_jarjar</shadedPattern>
148-
</relocation>
149-
</relocations>
150-
</configuration>
151-
</execution>
152125
<execution>
153126
<id>httpclient</id>
154127
<phase>package</phase>
@@ -164,12 +137,6 @@
164137
</artifactSet>
165138
<shadedArtifactAttached>true</shadedArtifactAttached>
166139
<shadedClassifierName>apachehttp</shadedClassifierName>
167-
<relocations>
168-
<relocation>
169-
<pattern>com.google.common</pattern>
170-
<shadedPattern>com.google.common.jenkins_client_jarjar</shadedPattern>
171-
</relocation>
172-
</relocations>
173140
</configuration>
174141
</execution>
175142
</executions>

jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@
88

99
import java.io.IOException;
1010
import java.net.URI;
11+
import java.util.HashMap;
1112
import java.util.List;
1213
import java.util.Map;
1314

1415
import javax.xml.bind.JAXBException;
1516

17+
import com.offbytwo.jenkins.helper.FunctionalHelper;
18+
import com.offbytwo.jenkins.model.BaseModel;
1619
import org.apache.http.HttpStatus;
1720
import org.apache.http.client.HttpResponseException;
1821
import org.apache.http.entity.ContentType;
1922
import org.dom4j.DocumentException;
2023
import org.slf4j.Logger;
2124
import org.slf4j.LoggerFactory;
2225

23-
import com.google.common.base.Function;
24-
import com.google.common.base.Optional;
25-
import com.google.common.collect.ImmutableMap;
26-
import com.google.common.collect.Maps;
2726
import com.offbytwo.jenkins.client.JenkinsHttpClient;
2827
import com.offbytwo.jenkins.client.JenkinsHttpConnection;
2928
import com.offbytwo.jenkins.client.util.EncodingUtils;
@@ -45,6 +44,12 @@
4544
import com.offbytwo.jenkins.model.QueueReference;
4645
import com.offbytwo.jenkins.model.View;
4746
import java.io.Closeable;
47+
import java.util.Optional;
48+
import java.util.function.Function;
49+
import java.util.stream.Collectors;
50+
51+
import static com.offbytwo.jenkins.helper.FunctionalHelper.SET_CLIENT;
52+
import static java.util.stream.Collectors.toMap;
4853

4954
/**
5055
* The main starting point for interacting with a Jenkins server.
@@ -167,13 +172,10 @@ public Map<String, Job> getJobs(FolderJob folder, String view) throws IOExceptio
167172
viewClass = View.class;
168173
}
169174
List<Job> jobs = client.get(path, viewClass).getJobs();
170-
return Maps.uniqueIndex(jobs, new Function<Job, String>() {
171-
@Override
172-
public String apply(Job job) {
173-
job.setClient(client);
174-
return job.getName();
175-
}
176-
});
175+
176+
return jobs.stream()
177+
.map(SET_CLIENT(this.client))
178+
.collect(toMap(s -> s.getName(), s -> s));
177179
}
178180

179181
/**
@@ -198,22 +200,23 @@ public Map<String, View> getViews(FolderJob folder) throws IOException {
198200
// This is much better than using &depth=2
199201
// http://localhost:8080/api/json?pretty&tree=views[name,url,jobs[name,url]]
200202
List<View> views = client.get(UrlUtils.toBaseUrl(folder) + "?tree=views[name,url,jobs[name,url]]", MainView.class).getViews();
201-
return Maps.uniqueIndex(views, new Function<View, String>() {
202-
@Override
203-
public String apply(View view) {
204-
view.setClient(client);
205-
// TODO: Think about the following? Does there exists a
206-
// simpler/more elegant method?
203+
204+
//TODO: Think about this Lambda. It's too large? Make it smaller!
205+
return views.stream().map(view -> {
206+
SET_CLIENT(this.client);
207+
208+
// TODO: Think about the following? Does there exists a simpler/more
209+
// elegant method?
207210
for (Job job : view.getJobs()) {
208-
job.setClient(client);
211+
SET_CLIENT(this.client);
209212
}
210-
for (View item : view.getViews()) {
211-
item.setClient(client);
213+
for (View viewView : view.getViews()) {
214+
SET_CLIENT(this.client);
212215
}
213216

214-
return view.getName();
215-
}
216-
});
217+
return view;
218+
})
219+
.collect(Collectors.toMap(s -> s.getName(), v -> v));
217220
}
218221

219222
/**
@@ -317,10 +320,9 @@ public Optional<FolderJob> getFolderJob(Job job) throws IOException {
317320
try {
318321
FolderJob folder = client.get(job.getUrl(), FolderJob.class);
319322
if (!folder.isFolder()) {
320-
return Optional.absent();
323+
return Optional.empty();
321324
}
322325
folder.setClient(client);
323-
324326
return Optional.of(folder);
325327
} catch (HttpResponseException e) {
326328
LOGGER.debug("getForlderJob(job={}) status={}", job, e.getStatusCode());
@@ -487,8 +489,12 @@ public void createFolder(FolderJob folder, String jobName) throws IOException {
487489
public void createFolder(FolderJob folder, String jobName, Boolean crumbFlag) throws IOException {
488490
// https://gist.github.com/stuart-warren/7786892 was slightly helpful
489491
// here
490-
ImmutableMap<String, String> params = ImmutableMap.of("mode", "com.cloudbees.hudson.plugins.folder.Folder",
491-
"name", EncodingUtils.formParameter(jobName), "from", "", "Submit", "OK");
492+
//TODO: JDK9+: Map.of(...)
493+
Map<String, String> params = new HashMap<>();
494+
params.put("mode", "com.cloudbees.hudson.plugins.folder.Folder");
495+
params.put("name", jobName);
496+
params.put("from", "");
497+
params.put("Submit", "OK");
492498
client.post_form(UrlUtils.toBaseUrl(folder) + "createItem?", params, crumbFlag);
493499
}
494500

@@ -535,13 +541,9 @@ public LabelWithDetails getLabel(String labelName) throws IOException {
535541
*/
536542
public Map<String, Computer> getComputers() throws IOException {
537543
List<Computer> computers = client.get("computer/", Computer.class).getComputers();
538-
return Maps.uniqueIndex(computers, new Function<Computer, String>() {
539-
@Override
540-
public String apply(Computer computer) {
541-
computer.setClient(client);
542-
return computer.getDisplayName().toLowerCase();
543-
}
544-
});
544+
return computers.stream()
545+
.map(SET_CLIENT(this.client))
546+
.collect(Collectors.toMap(s -> s.getDisplayName().toLowerCase(), Function.identity()));
545547
}
546548

547549
/**

jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
package com.offbytwo.jenkins.client;
77

88
import com.fasterxml.jackson.databind.ObjectMapper;
9-
import com.google.common.collect.Lists;
10-
import com.google.common.io.ByteStreams;
119
import com.offbytwo.jenkins.client.util.EncodingUtils;
1210
import com.offbytwo.jenkins.client.util.RequestReleasingInputStream;
1311
import com.offbytwo.jenkins.client.validator.HttpResponseValidator;
@@ -47,6 +45,8 @@
4745
import java.io.IOException;
4846
import java.io.InputStream;
4947
import java.net.URI;
48+
import java.nio.ByteBuffer;
49+
import java.util.ArrayList;
5050
import java.util.List;
5151
import java.util.Map;
5252

@@ -291,7 +291,7 @@ public void post_form(String path, Map<String, String> data, boolean crumbFlag)
291291
if (data != null) {
292292
// https://gist.github.com/stuart-warren/7786892 was slightly
293293
// helpful here
294-
List<String> queryParams = Lists.newArrayList();
294+
List<String> queryParams = new ArrayList<>();
295295
for (String param : data.keySet()) {
296296
queryParams.add(param + "=" + EncodingUtils.formParameter(data.get(param)));
297297
}
@@ -493,7 +493,7 @@ protected void setLocalContext(final HttpContext localContext) {
493493

494494
private <T extends BaseModel> T objectFromResponse(Class<T> cls, HttpResponse response) throws IOException {
495495
InputStream content = response.getEntity().getContent();
496-
byte[] bytes = ByteStreams.toByteArray(content);
496+
byte[] bytes = IOUtils.toByteArray(content);
497497
T result = mapper.readValue(bytes, cls);
498498
// TODO: original:
499499
// T result = mapper.readValue(content, cls);
Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
1+
/*
2+
* Copyright (c) 2019 Karl Heinz Marbaise, and contributors.
3+
*
4+
* Distributed under the MIT license: http://opensource.org/licenses/MIT
5+
*/
16
package com.offbytwo.jenkins.client.util;
27

3-
import com.google.common.net.UrlEscapers;
48

9+
import java.io.UnsupportedEncodingException;
10+
import java.net.URLEncoder;
11+
import java.nio.charset.StandardCharsets;
12+
13+
/**
14+
* This class is a help class to centralize the
15+
* encoding parts which will call an appropriate library function.
16+
*
17+
* @author Karl Heinz Marbaise
18+
*/
519
public final class EncodingUtils {
620

721
private EncodingUtils() {
822
} // nope
923

1024
public static String encode(String pathPart) {
1125
// jenkins doesn't like the + for space, use %20 instead
12-
return UrlEscapers.urlPathSegmentEscaper().escape(pathPart);
26+
try {
27+
return URLEncoder.encode(pathPart, StandardCharsets.UTF_8.displayName());
28+
} catch (UnsupportedEncodingException e) {
29+
// Should never happen, because that would imply that
30+
// the parameter StandardCharsets.UTF_8 is wrong.
31+
throw new IllegalArgumentException(e);
32+
}
1333
}
1434

1535
public static String formParameter(String pathPart) {
1636
// jenkins doesn't like the + for space, use %20 instead
17-
return UrlEscapers.urlFormParameterEscaper().escape(pathPart);
37+
try {
38+
return URLEncoder.encode(pathPart, StandardCharsets.UTF_8.displayName());
39+
} catch (UnsupportedEncodingException e) {
40+
// Should never happen, because that would imply that
41+
// the parameter StandardCharsets.UTF_8 is wrong.
42+
throw new IllegalArgumentException(e);
43+
}
1844
}
1945

2046
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.offbytwo.jenkins.helper;
2+
3+
import com.offbytwo.jenkins.client.JenkinsHttpConnection;
4+
import com.offbytwo.jenkins.model.BaseModel;
5+
6+
import java.util.function.Function;
7+
8+
public final class FunctionalHelper {
9+
10+
private FunctionalHelper() {
11+
// intentionally empty.
12+
}
13+
14+
public static final <T extends BaseModel> Function<T, T> SET_CLIENT(JenkinsHttpConnection client) {
15+
return s -> {
16+
s.setClient(client);
17+
return s;
18+
};
19+
}
20+
21+
22+
}

0 commit comments

Comments
 (0)