Skip to content

Commit d8dd56d

Browse files
EmmanuelRouxartembilan
authored andcommitted
GH-3076: NioFileLocker: add file existence check
Fixes #3076 * Return `null` instead of throwing an exception * Fix license header * Add missing `@since` * Avoid over-engineered test logic * Fix code style * Test non-existent file does not get created * Fix: remove unused import
1 parent 80ed25e commit d8dd56d

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/locking/FileChannelCache.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
import java.util.concurrent.ConcurrentHashMap;
2626
import java.util.concurrent.ConcurrentMap;
2727

28+
import org.springframework.lang.Nullable;
29+
2830
/**
2931
* Static cache of FileLocks that can be used to ensure that only a single lock is used inside this ClassLoader.
3032
*
3133
* @author Iwein Fuld
3234
* @author Gary Russell
35+
* @author Emmanuel Roux
3336
* @since 2.0
3437
*/
3538
final class FileChannelCache {
@@ -49,9 +52,10 @@ private FileChannelCache() {
4952
* <p>
5053
* Thread safe.
5154
*/
55+
@Nullable
5256
public static FileLock tryLockFor(File fileToLock) throws IOException {
5357
FileChannel channel = channelCache.get(fileToLock);
54-
if (channel == null) {
58+
if (channel == null && fileToLock.exists()) {
5559
@SuppressWarnings("resource")
5660
FileChannel newChannel = new RandomAccessFile(fileToLock, "rw").getChannel();
5761
FileChannel original = channelCache.putIfAbsent(fileToLock, newChannel);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.file.locking;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
24+
import org.junit.Rule;
25+
import org.junit.Test;
26+
import org.junit.rules.TemporaryFolder;
27+
28+
/**
29+
* @author Emmanuel Roux
30+
* @since 4.3.22
31+
*/
32+
public class FileChannelCacheTests {
33+
34+
@Rule
35+
public TemporaryFolder temp = new TemporaryFolder();
36+
37+
@Test
38+
public void throwsExceptionWhenFileNotExists() throws IOException {
39+
File testFile = new File(temp.getRoot(), "test0");
40+
assertThat(testFile.exists()).isFalse();
41+
assertThat(FileChannelCache.tryLockFor(testFile)).isNull();
42+
assertThat(testFile.exists()).isFalse();
43+
}
44+
45+
@Test
46+
public void fileLocked() throws IOException {
47+
File testFile = temp.newFile("test1");
48+
testFile.createNewFile();
49+
assertThat(testFile.exists()).isTrue();
50+
assertThat(FileChannelCache.tryLockFor(testFile)).isNotNull();
51+
FileChannelCache.closeChannelFor(testFile);
52+
}
53+
54+
}

spring-integration-file/src/test/java/org/springframework/integration/file/locking/NioFileLockerTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
/**
3737
* @author Iwein Fuld
3838
* @author Gary Russell
39+
* @author Emmanuel Roux
3940
*/
4041
public class NioFileLockerTests {
4142

@@ -78,4 +79,21 @@ public void fileNotListedWhenLockedByOtherFilter() throws IOException {
7879
filter1.unlock(testFile);
7980
}
8081

82+
@Test
83+
public void fileLockedWhenNotAlreadyLockedAndExists() throws IOException {
84+
NioFileLocker locker = new NioFileLocker();
85+
File testFile = new File(workdir, "test2");
86+
testFile.createNewFile();
87+
assertThat(locker.lock(testFile)).isTrue();
88+
locker.unlock(testFile);
89+
}
90+
91+
@Test
92+
public void fileNotLockedWhenNotExists() throws IOException {
93+
NioFileLocker locker = new NioFileLocker();
94+
File testFile = new File(workdir, "test3");
95+
assertThat(locker.lock(testFile)).isFalse();
96+
assertThat(testFile.exists()).isFalse();
97+
}
98+
8199
}

0 commit comments

Comments
 (0)