You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/10/14 08:49:57 UTC

svn commit: r825030 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/io/FileInstance.java java/org/apache/commons/runtime/io/FileLock.java java/org/apache/commons/runtime/io/FileLockImpl.java native/os/unix/dir.c

Author: mturk
Date: Wed Oct 14 06:49:56 2009
New Revision: 825030

URL: http://svn.apache.org/viewvc?rev=825030&view=rev
Log:
Split FileLock to abstract class and private implementation

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLockImpl.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLock.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/dir.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java?rev=825030&r1=825029&r2=825030&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java Wed Oct 14 06:49:56 2009
@@ -53,7 +53,7 @@
     /*
      * List of all lock regions to this file.
      */
-    private Vector<FileLock> locks = new Vector<FileLock>(2);
+    private Vector<FileLockImpl> locks = new Vector<FileLockImpl>(2);
 
     /**
      * Return the {@link Descriptor} accosicated with this file.
@@ -371,9 +371,9 @@
 
     private boolean regionOverlaps(long offset, long length)
     {
-        for (Enumeration<FileLock> e = locks.elements(); e.hasMoreElements();) {
+        for (Enumeration<FileLockImpl> e = locks.elements(); e.hasMoreElements();) {
             try {
-                FileLock lck = e.nextElement();
+                FileLockImpl lck = e.nextElement();
                 /* Check if we already have the valid lock
                  * for the requested region.
                  */
@@ -440,7 +440,7 @@
         }
 
         FileWrapper.lock(fd, type);
-        FileLock lock = new FileLock(fd, type);
+        FileLockImpl lock = new FileLockImpl(fd, type);
         /* Add the FileLock to the list of locks
          */
         locks.add(lock);
@@ -493,7 +493,7 @@
              */
             throw new OverlappingFileLockException();
         }
-        FileLock lock = new FileLock(fd, type, offset, length);
+        FileLockImpl lock = new FileLockImpl(fd, type, offset, length);
         /* Add the FileLock to the list of locks
          */
         locks.add(lock);

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLock.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLock.java?rev=825030&r1=825029&r2=825030&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLock.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLock.java Wed Oct 14 06:49:56 2009
@@ -73,61 +73,23 @@
  * Further care should be exercised when locking files maintained on network
  * file systems, since they often have further limitations.
  */
-public final class FileLock
+public abstract class FileLock
 {
 
     /*
-     * The lock starting position.
-     */
-    private final long  offset;
-    /*
-     * The lock length in bytes.
-     */
-    private final long  length;
-    /*
-     * File Descriptor for this lock.
-     */
-    private Descriptor  fd;
-    /*
-     * FileLockType flags.
+     * FileLockType mode.
      */
     private EnumSet<FileLockType> mode;
 
     /**
-     * Create new {@code FileLock} that locks the entire file.
+     * Create new {@code FileLock} instance.
      *
-     * @param fd
-     *          the file {@code Descriptor} this lock is attached to.
      * @param mode
      *          file lock mode.
      */
-    protected FileLock(Descriptor fd, EnumSet<FileLockType> mode)
+    public FileLock(EnumSet<FileLockType> mode)
     {
-        this.fd   = fd;
         this.mode = mode;
-        offset    =  0;
-        length    = -1;
-    }
-
-    /**
-     * Create new {@code FileLock} that locks the region of file.
-     *
-     * @param fd
-     *          the file {@code Descriptor} this lock is attached to.
-     * @param mode
-     *          file lock mode.
-     * @param start
-     *          the starting position for the lock.
-     * @param len
-     *          the length of the lock.
-     */
-    protected FileLock(Descriptor fd, EnumSet<FileLockType> mode,
-                       long start, long len)
-    {
-        this.fd   = fd;
-        this.mode = mode;
-        offset    = start;
-        length    = len;
     }
 
     /**
@@ -140,21 +102,7 @@
      *          the length of the comparative lock.
      * @return {@code true} if there is an overlap, {@code false} otherwise.
      */
-    public final boolean overlaps(long start, long len)
-    {
-        /*
-         * Negative length means the entire file
-         * so it will always overlap with another lock
-         */
-        if (length < 0 || len < 0)
-            return true;
-        final long end = offset + length - 1;
-        final long siz = start  + len    - 1;
-        if (end < start || offset > siz) {
-            return false;
-        }
-        return true;
-    }
+    public abstract boolean overlaps(long start, long len);
 
     /**
      * Indicates whether this lock is a valid file lock. The lock is
@@ -163,33 +111,21 @@
      *
      * @return {@code true} if the lock is valid, {@code false} otherwise.
      */
-    public boolean isValid()
-    {
-        if (fd == null)
-            return false;
-        else
-            return fd.valid();
-    }
+    public abstract boolean isValid();
 
     /**
      * Returns the lock's starting position in the file.
      *
      * @return the lock position.
      */
-    public final long position()
-    {
-        return offset;
-    }
+    public abstract long position();
 
     /**
      * Returns the length of the file lock in bytes.
      *
      * @return the size of the file lock in bytes.
      */
-    public final long size()
-    {
-        return length;
-    }
+    public abstract long size();
 
     /**
      * Indicates if the file lock is shared with other processes
@@ -198,7 +134,7 @@
      * @return {@code true} if the lock is a shared lock,
      *          {@code false} if it is exclusive.
      */
-    public boolean isShared()
+    public final boolean isShared()
     {
         return mode.contains(FileLockType.SHARED);
     }
@@ -209,7 +145,7 @@
      * @return {@code true} if the lock is blocking lock,
      *          {@code false} if it is non-blocking.
      */
-    public boolean isBlocking()
+    public final boolean isBlocking()
     {
         return !mode.contains(FileLockType.NONBLOCK);
     }
@@ -229,18 +165,7 @@
      * @throws IOException
      *          If some other I/O error occurs.
      */
-    public void release()
-        throws IOException
-    {
-        if (fd == null) {
-            // Already released
-            return;
-        }
-        if (length < 0)
-            FileWrapper.unlock(fd);
-        else
-            FileWrapper.unlock(fd, offset, length);
-        fd = null;
-    }
+    public abstract void release()
+        throws IOException;
 
 }

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLockImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLockImpl.java?rev=825030&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLockImpl.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLockImpl.java Wed Oct 14 06:49:56 2009
@@ -0,0 +1,150 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.io;
+
+import org.apache.commons.runtime.Descriptor;
+import org.apache.commons.runtime.exception.ClosedDescriptorException;
+import org.apache.commons.runtime.exception.AsyncClosedDescriptorException;
+import org.apache.commons.runtime.exception.InvalidDescriptorException;
+import org.apache.commons.runtime.exception.TimeoutException;
+import java.io.IOException;
+import java.util.EnumSet;
+
+/**
+ * A {@code FileLock} implementatiom.
+ */
+class FileLockImpl extends FileLock
+{
+
+    /*
+     * The lock starting position.
+     */
+    private final long  offset;
+    /*
+     * The lock length in bytes.
+     */
+    private final long  length;
+    /*
+     * File Descriptor for this lock.
+     */
+    private Descriptor  fd;
+
+    /**
+     * Create new {@code FileLock} that locks the entire file.
+     *
+     */
+    public FileLockImpl(Descriptor fd, EnumSet<FileLockType> mode)
+    {
+        super(mode);
+        this.fd   = fd;
+        offset    =  0;
+        length    = -1;
+    }
+
+    /**
+     * Create new {@code FileLock} that locks the region of file.
+     *
+     */
+    public FileLockImpl(Descriptor fd, EnumSet<FileLockType> mode,
+                        long start, long len)
+    {
+        super(mode);
+        this.fd   = fd;
+        offset    = start;
+        length    = len;
+    }
+
+    /**
+     * Indicates if the receiver's lock region overlaps the region described
+     * in the parameter list.
+     *
+     */
+    @Override
+    public final boolean overlaps(long start, long len)
+    {
+        /*
+         * Negative length means the entire file
+         * so it will always overlap with another lock
+         */
+        if (length < 0 || len < 0)
+            return true;
+        final long end = offset + length - 1;
+        final long siz = start  + len    - 1;
+        if (end < start || offset > siz) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Indicates whether this lock is a valid file lock. The lock is
+     * valid unless the underlying Descriptor has been closed or it has been
+     * explicitly released.
+     *
+     */
+    @Override
+    public boolean isValid()
+    {
+        if (fd == null)
+            return false;
+        else
+            return fd.valid();
+    }
+
+    /**
+     * Returns the lock's starting position in the file.
+     *
+     */
+    @Override
+    public final long position()
+    {
+        return offset;
+    }
+
+    /**
+     * Returns the length of the file lock in bytes.
+     *
+     */
+    @Override
+    public final long size()
+    {
+        return length;
+    }
+
+
+    /**
+     * Releases this particular lock on the file.
+     * If the lock is invalid then this method has no effect.
+     * Once released, the lock becomes invalid.
+     *
+     */
+    @Override
+    public void release()
+        throws IOException
+    {
+        if (fd == null) {
+            // Already released
+            return;
+        }
+        if (length < 0)
+            FileWrapper.unlock(fd);
+        else
+            FileWrapper.unlock(fd, offset, length);
+        fd = null;
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileLockImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/dir.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/dir.c?rev=825030&r1=825029&r2=825030&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/dir.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/dir.c Wed Oct 14 06:49:56 2009
@@ -151,7 +151,7 @@
                             type = ACR_FT_UNKFILE;
                         break;
                     }
-                    rv = ACR_NewFileInfoObject(_E, dp->d_name, type);
+                    rv = ACR_NewFileInfoObjectA(_E, dp->d_name, type);
                 }
                 else
                     break;