Skip to content

Commit 9300c6d

Browse files
committed
Merge pull request #6683 from hengyunab
* gh-6683: Test that LaunchedURLClassLoader works when thread is interrupted Ensure that LaunchedURLClassLoader works when thread is interrupted
2 parents ffc0dc4 + 85c0b44 commit 9300c6d

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/data/RandomAccessDataFile.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -244,17 +244,10 @@ private class FilePool {
244244
}
245245

246246
public RandomAccessFile acquire() throws IOException {
247-
try {
248-
this.available.acquire();
249-
RandomAccessFile file = this.files.poll();
250-
return (file == null
251-
? new RandomAccessFile(RandomAccessDataFile.this.file, "r")
252-
: file);
253-
}
254-
catch (InterruptedException ex) {
255-
Thread.currentThread().interrupt();
256-
throw new IOException(ex);
257-
}
247+
this.available.acquireUninterruptibly();
248+
RandomAccessFile file = this.files.poll();
249+
return (file == null
250+
? new RandomAccessFile(RandomAccessDataFile.this.file, "r") : file);
258251
}
259252

260253
public void release(RandomAccessFile file) {
@@ -263,22 +256,16 @@ public void release(RandomAccessFile file) {
263256
}
264257

265258
public void close() throws IOException {
259+
this.available.acquireUninterruptibly(this.size);
266260
try {
267-
this.available.acquire(this.size);
268-
try {
269-
RandomAccessFile file = this.files.poll();
270-
while (file != null) {
271-
file.close();
272-
file = this.files.poll();
273-
}
274-
}
275-
finally {
276-
this.available.release(this.size);
261+
RandomAccessFile file = this.files.poll();
262+
while (file != null) {
263+
file.close();
264+
file = this.files.poll();
277265
}
278266
}
279-
catch (InterruptedException ex) {
280-
Thread.currentThread().interrupt();
281-
throw new IOException(ex);
267+
finally {
268+
this.available.release(this.size);
282269
}
283270
}
284271

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/LaunchedURLClassLoaderTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
*
3737
* @author Dave Syer
3838
* @author Phillip Webb
39+
* @author Andy Wilkinson
3940
*/
4041
@SuppressWarnings("resource")
4142
public class LaunchedURLClassLoaderTests {
@@ -101,4 +102,23 @@ public void resolveFromNested() throws Exception {
101102
assertThat(resource.openConnection().getInputStream().read(), equalTo(3));
102103
}
103104

105+
@Test
106+
public void resolveFromNestedWhileThreadIsInterrupted() throws Exception {
107+
File file = this.temporaryFolder.newFile();
108+
TestJarCreator.createTestJar(file);
109+
JarFile jarFile = new JarFile(file);
110+
URL url = jarFile.getUrl();
111+
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { url },
112+
null);
113+
try {
114+
Thread.currentThread().interrupt();
115+
URL resource = loader.getResource("nested.jar!/3.dat");
116+
assertThat(resource.toString(), equalTo(url + "nested.jar!/3.dat"));
117+
assertThat(resource.openConnection().getInputStream().read(), equalTo(3));
118+
}
119+
finally {
120+
Thread.interrupted();
121+
}
122+
}
123+
104124
}

0 commit comments

Comments
 (0)