applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / NativeLock.java
index ccaa407..4255858 100644 (file)
-package uk.ac.vamsas.client.simpleclient;
-
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileLock;
-import java.nio.channels.ReadableByteChannel;
-
-/**
- * @author JimP
- *
- */
-public class NativeLock extends Lock {
-
-  protected FileLock lock = null;
-
-  /**
-   * @param lockfile
-   * @param block true means thread will block until a lock is obtained.
-   */
-  public NativeLock(File lockfile, boolean block) {
-    super(lockfile);
-    // try and get a lock.
-    lock = null;
-    
-    try {
-      /*if (!lockfile.createNewFile()) {
-          log.warn("Failed to create locked file "+lockfile);
-          return;
-        }
-      */
-      rafile=new RandomAccessFile(lockfile,"rw");
-      if (block)
-        lock = rafile.getChannel().lock();
-      else
-        lock = rafile.getChannel().tryLock();
-      if (lock==null || !lock.isValid()) {
-        // failed to get lock. Close the file channel
-        log.debug("failed to get lock for "+lockfile);
-        rafile.getChannel().close();
-        lock=null;
-      }
-    } catch (FileNotFoundException e) {
-      //
-      log.debug("Lock failed - normal behaviour for windows locking.");
-      //log.error("Error! Couldn't create a lockfile at "
-      //  + lockfile.getAbsolutePath(), e);
-    } catch (IOException e) {
-      log.error("Error! Problems with IO when creating a lock on "
-          + lockfile.getAbsolutePath(),e);
-    } // catch (Exception )
-  }
-
-  public boolean isLocked() {
-    if (lock != null && lock.isValid()) {
-      return true;
-    }
-    return false;
-  }
-
-  public void release() {
-    release(true);
-  }
-
-  public void release(boolean closeChannel) {
-    if (lock==null)
-      return;
-    try {
-      // channel.close should be called before release() for rigourous locking.
-      if (lock.isValid() && rafile!=null && rafile.getFD().valid() && rafile.getChannel()!=null) {
-        if (closeChannel && rafile.getChannel().isOpen()) {
-            rafile.close();
-            rafile=null; 
-        } else {
-          // just release the lock without doing anything to the channe;l
-          if (lock!=null && lock.isValid())
-            lock.release();
-        }
-      }
-    } catch (IOException e) {
-      log.warn("Whilst releasing lock",e);
-    }
-    lock=null;
-  }
-
-  /**
-   * gets Locked Stream for reading from
-   * @param atStart true to start reading at beginning of file.
-   * @return null if file not locked
-   * @throws IOException
-   */
-  public FileInputStream getFileInputStream(boolean atStart) throws IOException {
-    if (!isLocked())
-      return null;
-    if (atStart)
-      rafile.seek(0);
-    return new FileInputStream(rafile.getFD());
-  }
-
-  /**
-   * gets Locked stream to write to
-   * FileInput always starts at the *end* of the file (after any truncation)
-   * @param clear true means file will be cleared to zero length
-   * @return null if file is not locked
-   * @throws IOException
-   */
-  public FileOutputStream getFileOutputStream(boolean clear) throws IOException {
-    if (!isLocked())
-      return null;
-    if (clear) {
-      rafile.seek(0);
-      rafile.setLength(0);
-    } else
-      rafile.seek(rafile.length());
-    return new LockedFileOutputStream(rafile.getFD());
-  }
-
-  /**
-   * return buffered output stream to locked file.
-   * @param clear - true means file is truncated to 0 length before writing 
-   * @return
-   */
-  public BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException {
-    OutputStream fos = getFileOutputStream(clear);
-    if (fos!=null)
-      return new BufferedOutputStream(fos);
-    return null;
-  }
-  /**
-   * @see uk.ac.vamsas.client.simpleclient.Lock#finalize()
-   */
-  protected void finalize() throws Throwable {
-    release(true); // we explicitly lose the lock here.
-    // log.debug("lock closing through garbage collection ?");
-    super.finalize();
-  }
-
-  /* (non-Javadoc)
-   * @see uk.ac.vamsas.client.simpleclient.Lock#getLength()
-   */
-  public long length() {
-    if (isLocked()){
-      try {
-        return rafile.length();
-      } catch (Exception e) {
-        log.debug("getLength exception:",e);
-      }
-    }
-    return -1;
-  }
-
-  public RandomAccessFile getRaFile() throws IOException {
-    if (isLocked())
-      return rafile;
-    else
-      log.debug("Failed to getRaFile on "+target);
-    return null;
-  }  
-  
-  public FileChannel getRaChannel() throws IOException {
-    if (isLocked())
-      return rafile.getChannel();
-    else
-      log.debug("Failed to getRaChannel on "+target);
-    return null;
-  }
-
-  public boolean isTargetLockFile(File afile) {
-    if (isLocked() && target.equals(afile))
-      return true;
-    return false;
-  }
-
-}
+/*\r
+ * This file is part of the Vamsas Client version 0.1. \r
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ *  Andrew Waterhouse and Dominik Lindner.\r
+ * \r
+ * Earlier versions have also been incorporated into Jalview version 2.4 \r
+ * since 2008, and TOPALi version 2 since 2007.\r
+ * \r
+ * The Vamsas Client is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *  \r
+ * The Vamsas Client is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public License\r
+ * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package uk.ac.vamsas.client.simpleclient;\r
+\r
+import java.io.BufferedInputStream;\r
+import java.io.BufferedOutputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.io.RandomAccessFile;\r
+import java.nio.channels.FileChannel;\r
+import java.nio.channels.FileLock;\r
+import java.nio.channels.ReadableByteChannel;\r
+\r
+/**\r
+ * @author JimP\r
+ * \r
+ */\r
+public class NativeLock extends Lock {\r
+\r
+  protected FileLock lock = null;\r
+\r
+  /**\r
+   * @param lockfile\r
+   * @param block\r
+   *          true means thread will block until a lock is obtained.\r
+   */\r
+  public NativeLock(File lockfile, boolean block) {\r
+    super(lockfile);\r
+    // try and get a lock.\r
+    lock = null;\r
+\r
+    try {\r
+      /*\r
+       * if (!lockfile.createNewFile()) {\r
+       * log.warn("Failed to create locked file "+lockfile); return; }\r
+       */\r
+      rafile = new RandomAccessFile(lockfile, "rw");\r
+      if (block)\r
+        lock = rafile.getChannel().lock();\r
+      else\r
+        lock = rafile.getChannel().tryLock();\r
+      if (lock == null || !lock.isValid()) {\r
+        // failed to get lock. Close the file channel\r
+        log.debug("failed to get lock for " + lockfile);\r
+        rafile.getChannel().close();\r
+        lock = null;\r
+      }\r
+    } catch (FileNotFoundException e) {\r
+      //\r
+      log.debug("Lock failed - normal behaviour for windows locking.");\r
+      // log.error("Error! Couldn't create a lockfile at "\r
+      // + lockfile.getAbsolutePath(), e);\r
+    } catch (IOException e) {\r
+      log.error("Error! Problems with IO when creating a lock on "\r
+          + lockfile.getAbsolutePath(), e);\r
+    } // catch (Exception )\r
+  }\r
+\r
+  public boolean isLocked() {\r
+    if (lock != null && lock.isValid()) {\r
+      return true;\r
+    }\r
+    return false;\r
+  }\r
+\r
+  public void release() {\r
+    release(true);\r
+  }\r
+\r
+  public void release(boolean closeChannel) {\r
+    if (lock == null)\r
+      return;\r
+    try {\r
+      // channel.close should be called before release() for rigourous locking.\r
+      if (lock.isValid() && rafile != null && rafile.getFD().valid()\r
+          && rafile.getChannel() != null) {\r
+        if (closeChannel && rafile.getChannel().isOpen()) {\r
+          rafile.close();\r
+          rafile = null;\r
+        } else {\r
+          // just release the lock without doing anything to the channe;l\r
+          if (lock != null && lock.isValid())\r
+            lock.release();\r
+        }\r
+      }\r
+    } catch (IOException e) {\r
+      log.warn("Whilst releasing lock", e);\r
+    }\r
+    lock = null;\r
+  }\r
+\r
+  /**\r
+   * gets Locked Stream for reading from\r
+   * \r
+   * @param atStart\r
+   *          true to start reading at beginning of file.\r
+   * @return null if file not locked\r
+   * @throws IOException\r
+   */\r
+  public FileInputStream getFileInputStream(boolean atStart) throws IOException {\r
+    if (!isLocked())\r
+      return null;\r
+    if (atStart)\r
+      rafile.seek(0);\r
+    return new FileInputStream(rafile.getFD());\r
+  }\r
+\r
+  /**\r
+   * gets Locked stream to write to FileInput always starts at the *end* of the\r
+   * file (after any truncation)\r
+   * \r
+   * @param clear\r
+   *          true means file will be cleared to zero length\r
+   * @return null if file is not locked\r
+   * @throws IOException\r
+   */\r
+  public FileOutputStream getFileOutputStream(boolean clear) throws IOException {\r
+    if (!isLocked())\r
+      return null;\r
+    if (clear) {\r
+      rafile.seek(0);\r
+      rafile.setLength(0);\r
+    } else\r
+      rafile.seek(rafile.length());\r
+    return new LockedFileOutputStream(rafile.getFD());\r
+  }\r
+\r
+  /**\r
+   * return buffered output stream to locked file.\r
+   * \r
+   * @param clear\r
+   *          - true means file is truncated to 0 length before writing\r
+   * @return\r
+   */\r
+  public BufferedOutputStream getBufferedOutputStream(boolean clear)\r
+      throws IOException {\r
+    OutputStream fos = getFileOutputStream(clear);\r
+    if (fos != null)\r
+      return new BufferedOutputStream(fos);\r
+    return null;\r
+  }\r
+\r
+  /**\r
+   * @see uk.ac.vamsas.client.simpleclient.Lock#finalize()\r
+   */\r
+  protected void finalize() throws Throwable {\r
+    release(true); // we explicitly lose the lock here.\r
+    // log.debug("lock closing through garbage collection ?");\r
+    super.finalize();\r
+  }\r
+\r
+  /*\r
+   * (non-Javadoc)\r
+   * \r
+   * @see uk.ac.vamsas.client.simpleclient.Lock#getLength()\r
+   */\r
+  public long length() {\r
+    if (isLocked()) {\r
+      try {\r
+        return rafile.length();\r
+      } catch (Exception e) {\r
+        log.debug("getLength exception:", e);\r
+      }\r
+    }\r
+    return -1;\r
+  }\r
+\r
+  public RandomAccessFile getRaFile() throws IOException {\r
+    if (isLocked())\r
+      return rafile;\r
+    else\r
+      log.debug("Failed to getRaFile on " + target);\r
+    return null;\r
+  }\r
+\r
+  public FileChannel getRaChannel() throws IOException {\r
+    if (isLocked())\r
+      return rafile.getChannel();\r
+    else\r
+      log.debug("Failed to getRaChannel on " + target);\r
+    return null;\r
+  }\r
+\r
+  public boolean isTargetLockFile(File afile) {\r
+    if (isLocked() && target.equals(afile))\r
+      return true;\r
+    return false;\r
+  }\r
+\r
+}\r