1 /*
   2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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         }
 118         return new File(path);
 119     }
 120 
 121     private static File lockFile(File directory) {
 122         return new File(directory, ".lock");
 123     }
 124 
 125 
 126     private static class Descriptor {
 127         private final File directory;
 128         private final RandomAccessFile lockRaf;
 129         private final FileLock lock;
 130         private int referenceCount;
 131 
 132         private Descriptor(File directory,
 133                            RandomAccessFile lockRaf,
 134                            FileLock lock)
 135         {
 136             this.directory = directory;
 137             this.lockRaf = lockRaf;
 138             this.lock = lock;
 139         }
 140     }
 141 
 142     final class DirectoryAlreadyInUseException extends Exception {
 143         DirectoryAlreadyInUseException(String message, Throwable cause) {
 144             super(message, cause);
 145         }
 146     }
 147 }