8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.scene.web; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.io.RandomAccessFile; 31 import static java.lang.String.format; 32 import java.nio.channels.FileLock; 33 import java.nio.channels.OverlappingFileLockException; 34 import java.util.HashMap; 35 import java.util.Map; 36 import java.util.logging.Level; 37 import java.util.logging.Logger; 38 39 final class DirectoryLock { 40 41 private static final Logger logger = 42 Logger.getLogger(DirectoryLock.class.getName()); 43 private static final Map<File,Descriptor> descriptors = new HashMap<>(); 44 45 46 private Descriptor descriptor; 47 48 49 DirectoryLock(File directory) throws IOException, 50 DirectoryAlreadyInUseException 51 { 52 directory = canonicalize(directory); 53 descriptor = descriptors.get(directory); 54 if (descriptor == null) { 55 File lockFile = lockFile(directory); 56 RandomAccessFile lockRaf = new RandomAccessFile(lockFile, "rw"); 57 try { 58 FileLock lock = lockRaf.getChannel().tryLock(); 59 if (lock == null) { 60 throw new DirectoryAlreadyInUseException( 61 directory.toString(), null); 62 } 63 descriptor = new Descriptor(directory, lockRaf, lock); 64 descriptors.put(directory, descriptor); 65 } catch (OverlappingFileLockException ex) { 66 throw new DirectoryAlreadyInUseException( 67 directory.toString(), ex); 68 } finally { 69 if (descriptor == null) { // tryLock failed 70 try { 71 lockRaf.close(); 72 } catch (IOException ex) { 73 logger.log(Level.WARNING, format("Error closing [%s]", 74 lockFile), ex); 75 } 76 } 77 } 78 } 79 descriptor.referenceCount++; 80 } 81 82 83 void close() { 84 if (descriptor == null) { 85 return; 86 } 87 descriptor.referenceCount--; 88 if (descriptor.referenceCount == 0) { 89 try { 90 descriptor.lock.release(); 91 } catch (IOException ex) { 92 logger.log(Level.WARNING, format("Error releasing " 93 + "lock on [%s]", lockFile(descriptor.directory)), ex); 94 } 95 try { 96 descriptor.lockRaf.close(); 97 } catch (IOException ex) { 98 logger.log(Level.WARNING, format("Error closing [%s]", 99 lockFile(descriptor.directory)), ex); 100 } 101 descriptors.remove(descriptor.directory); 102 } 103 descriptor = null; 104 } 105 106 107 static int referenceCount(File directory) throws IOException { 108 Descriptor d = descriptors.get(canonicalize(directory)); 109 return d == null ? 0 : d.referenceCount; 110 } 111 112 static File canonicalize(File directory) throws IOException { 113 String path = directory.getCanonicalPath(); 114 if (path.length() > 0 115 && path.charAt(path.length() - 1) != File.separatorChar) 116 { 117 path += File.separatorChar; 118 } | 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.scene.web; 27 28 import com.sun.javafx.logging.PlatformLogger; 29 import java.io.File; 30 import java.io.IOException; 31 import java.io.RandomAccessFile; 32 import static java.lang.String.format; 33 import java.nio.channels.FileLock; 34 import java.nio.channels.OverlappingFileLockException; 35 import java.util.HashMap; 36 import java.util.Map; 37 38 final class DirectoryLock { 39 40 private static final PlatformLogger logger = 41 PlatformLogger.getLogger(DirectoryLock.class.getName()); 42 private static final Map<File,Descriptor> descriptors = new HashMap<>(); 43 44 45 private Descriptor descriptor; 46 47 48 DirectoryLock(File directory) throws IOException, 49 DirectoryAlreadyInUseException 50 { 51 directory = canonicalize(directory); 52 descriptor = descriptors.get(directory); 53 if (descriptor == null) { 54 File lockFile = lockFile(directory); 55 RandomAccessFile lockRaf = new RandomAccessFile(lockFile, "rw"); 56 try { 57 FileLock lock = lockRaf.getChannel().tryLock(); 58 if (lock == null) { 59 throw new DirectoryAlreadyInUseException( 60 directory.toString(), null); 61 } 62 descriptor = new Descriptor(directory, lockRaf, lock); 63 descriptors.put(directory, descriptor); 64 } catch (OverlappingFileLockException ex) { 65 throw new DirectoryAlreadyInUseException( 66 directory.toString(), ex); 67 } finally { 68 if (descriptor == null) { // tryLock failed 69 try { 70 lockRaf.close(); 71 } catch (IOException ex) { 72 logger.warning(format("Error closing [%s]", 73 lockFile), ex); 74 } 75 } 76 } 77 } 78 descriptor.referenceCount++; 79 } 80 81 82 void close() { 83 if (descriptor == null) { 84 return; 85 } 86 descriptor.referenceCount--; 87 if (descriptor.referenceCount == 0) { 88 try { 89 descriptor.lock.release(); 90 } catch (IOException ex) { 91 logger.warning(format("Error releasing lock on [%s]", 92 lockFile(descriptor.directory)), ex); 93 } 94 try { 95 descriptor.lockRaf.close(); 96 } catch (IOException ex) { 97 logger.warning(format("Error closing [%s]", 98 lockFile(descriptor.directory)), ex); 99 } 100 descriptors.remove(descriptor.directory); 101 } 102 descriptor = null; 103 } 104 105 106 static int referenceCount(File directory) throws IOException { 107 Descriptor d = descriptors.get(canonicalize(directory)); 108 return d == null ? 0 : d.referenceCount; 109 } 110 111 static File canonicalize(File directory) throws IOException { 112 String path = directory.getCanonicalPath(); 113 if (path.length() > 0 114 && path.charAt(path.length() - 1) != File.separatorChar) 115 { 116 path += File.separatorChar; 117 } |