|
1 | 1 | /* |
2 | | - * Copyright 2002-2013 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | 7 | * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
19 | 19 | import java.io.File; |
20 | 20 | import java.io.IOException; |
21 | 21 | import java.io.RandomAccessFile; |
| 22 | +import java.nio.channels.FileChannel; |
22 | 23 | import java.nio.channels.FileLock; |
23 | 24 | import java.nio.channels.OverlappingFileLockException; |
24 | | -import java.nio.channels.FileChannel; |
25 | 25 | import java.util.concurrent.ConcurrentHashMap; |
26 | 26 | import java.util.concurrent.ConcurrentMap; |
27 | 27 |
|
28 | 28 | /** |
29 | 29 | * Static cache of FileLocks that can be used to ensure that only a single lock is used inside this ClassLoader. |
30 | 30 | * |
31 | 31 | * @author Iwein Fuld |
| 32 | + * @author Gary Russell |
32 | 33 | * @since 2.0 |
33 | 34 | */ |
34 | 35 | final class FileChannelCache { |
35 | 36 |
|
36 | | - private static ConcurrentMap<File, FileChannel> channelCache = new ConcurrentHashMap<File, FileChannel>(); |
| 37 | + private static ConcurrentMap<File, FileChannel> channelCache = new ConcurrentHashMap<File, FileChannel>(); |
37 | 38 |
|
38 | 39 |
|
39 | | - /** |
40 | | - * Try to get a lock for this file while guaranteeing that the same channel will be used for all file locks in this |
41 | | - * VM. If the lock could not be acquired this method will return <code>null</code>. |
42 | | - * <p> |
43 | | - * Locks acquired through this method should be passed back to #closeChannelFor to prevent memory leaks. |
44 | | - * <p> |
45 | | - * Thread safe. |
46 | | - */ |
47 | | - public static FileLock tryLockFor(File fileToLock) throws IOException { |
48 | | - FileChannel channel = channelCache.get(fileToLock); |
49 | | - if (channel == null) { |
50 | | - FileChannel newChannel = new RandomAccessFile(fileToLock, "rw").getChannel(); |
51 | | - FileChannel original = channelCache.putIfAbsent(fileToLock, newChannel); |
52 | | - channel = (original != null) ? original : newChannel; |
53 | | - } |
54 | | - FileLock lock = null; |
55 | | - if (channel != null) { |
56 | | - try { |
57 | | - lock = channel.tryLock(); |
58 | | - } |
59 | | - catch (OverlappingFileLockException e) { |
60 | | - // File is already locked in this thread or virtual machine |
61 | | - } |
62 | | - } |
63 | | - return lock; |
64 | | - } |
| 40 | + /** |
| 41 | + * Try to get a lock for this file while guaranteeing that the same channel will be used for all file locks in this |
| 42 | + * VM. If the lock could not be acquired this method will return <code>null</code>. |
| 43 | + * <p> |
| 44 | + * Locks acquired through this method should be passed back to #closeChannelFor to prevent memory leaks. |
| 45 | + * <p> |
| 46 | + * Thread safe. |
| 47 | + */ |
| 48 | + public static FileLock tryLockFor(File fileToLock) throws IOException { |
| 49 | + FileChannel channel = channelCache.get(fileToLock); |
| 50 | + if (channel == null) { |
| 51 | + @SuppressWarnings("resource") |
| 52 | + FileChannel newChannel = new RandomAccessFile(fileToLock, "rw").getChannel(); |
| 53 | + FileChannel original = channelCache.putIfAbsent(fileToLock, newChannel); |
| 54 | + if (original != null) { |
| 55 | + channel = original; |
| 56 | + try { |
| 57 | + newChannel.close(); |
| 58 | + } |
| 59 | + catch (IOException e) { |
| 60 | + // ignore |
| 61 | + } |
| 62 | + } |
| 63 | + else { |
| 64 | + channel = newChannel; |
| 65 | + } |
| 66 | + } |
| 67 | + FileLock lock = null; |
| 68 | + if (channel != null) { |
| 69 | + try { |
| 70 | + lock = channel.tryLock(); |
| 71 | + } |
| 72 | + catch (OverlappingFileLockException e) { |
| 73 | + // File is already locked in this thread or virtual machine |
| 74 | + } |
| 75 | + } |
| 76 | + return lock; |
| 77 | + } |
65 | 78 |
|
66 | | - /** |
67 | | - * Close the channel for the file passed in. |
68 | | - * <p> |
69 | | - * Thread safe. |
70 | | - */ |
71 | | - public static void closeChannelFor(File fileToUnlock) { |
72 | | - FileChannel fileChannel = channelCache.remove(fileToUnlock); |
73 | | - if (fileChannel != null) { |
74 | | - try { |
| 79 | + /** |
| 80 | + * Close the channel for the file passed in. |
| 81 | + * <p> |
| 82 | + * Thread safe. |
| 83 | + */ |
| 84 | + public static void closeChannelFor(File fileToUnlock) { |
| 85 | + FileChannel fileChannel = channelCache.remove(fileToUnlock); |
| 86 | + if (fileChannel != null) { |
| 87 | + try { |
75 | 88 | fileChannel.close(); |
76 | 89 | } |
77 | | - catch (IOException e) { |
| 90 | + catch (IOException e) { |
78 | 91 | // ignore |
79 | 92 | } |
80 | | - } |
81 | | - } |
| 93 | + } |
| 94 | + } |
82 | 95 |
|
83 | | - public static boolean isLocked(File file) { |
84 | | - return channelCache.containsKey(file); |
85 | | - } |
| 96 | + public static boolean isLocked(File file) { |
| 97 | + return channelCache.containsKey(file); |
| 98 | + } |
86 | 99 |
|
87 | 100 | } |
0 commit comments