directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r687549 - /directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/
Date Thu, 21 Aug 2008 04:19:14 GMT
Author: akarasulu
Date: Wed Aug 20 21:19:13 2008
New Revision: 687549

URL: http://svn.apache.org/viewvc?rev=687549&view=rev
Log:
added new Monitor interface for Cursor close handling along with default implementaiton and
added setter to Cursor interface with fixes to implementations

Added:
    directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ClosureMonitor.java
    directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/DefaultClosureMonitor.java
Modified:
    directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/AbstractCursor.java
    directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/Cursor.java
    directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/EmptyCursor.java
    directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/IteratorCursor.java
    directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ListCursor.java
    directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/SingletonCursor.java

Modified: directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/AbstractCursor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/AbstractCursor.java?rev=687549&r1=687548&r2=687549&view=diff
==============================================================================
--- directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/AbstractCursor.java
(original)
+++ directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/AbstractCursor.java
Wed Aug 20 21:19:13 2008
@@ -30,40 +30,41 @@
  */
 public abstract class AbstractCursor<E> implements Cursor<E>
 {
-    private boolean closed;
-    private Exception reason;
+    private ClosureMonitor monitor = new DefaultClosureMonitor();
 
-
-    protected void checkClosed( String operation ) throws Exception
+    
+    public final void setClosureMonitor( ClosureMonitor monitor )
     {
-        if ( isClosed() )
+        if ( monitor == null )
         {
-            if ( reason != null )
-            {
-                throw reason;
-            }
-            
-            throw new CursorClosedException( "Attempting " + operation + " operation on a
closed Cursor." );
+            throw new NullPointerException( "monitor" );
         }
+        
+        this.monitor = monitor;
+    }
+    
+
+    protected final void checkNotClosed( String operation ) throws Exception
+    {
+        monitor.checkNotClosed();
     }
 
 
-    public boolean isClosed()
+    public final boolean isClosed()
     {
-        return closed;
+        return monitor.isClosed();
     }
 
 
-    public void close( Exception reason ) throws Exception
+    public void close( Exception cause ) throws Exception
     {
-        this.reason = reason;
-        closed = true;
+        monitor.close( cause );
     }
 
 
     public void close() throws Exception
     {
-        closed = true;
+        monitor.close();
     }
 
 

Added: directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ClosureMonitor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ClosureMonitor.java?rev=687549&view=auto
==============================================================================
--- directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ClosureMonitor.java
(added)
+++ directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ClosureMonitor.java
Wed Aug 20 21:19:13 2008
@@ -0,0 +1,81 @@
+/*
+ *   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.directory.server.core.cursor;
+
+
+/**
+ * A monitor used by Cursors to detect conditions when they should stop 
+ * performing some work during advance operations such as next(), previous(),
+ * first() etc, and release resources.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public interface ClosureMonitor
+{
+    /**
+     * Sets monitor state to closed, and sets the cause to a 
+     * CursorClosedException without an error message string.
+     */
+    void close();
+
+    
+    /**
+     * Sets monitor state to closed, and sets the cause to a 
+     * CursorClosedException with a specific error message string.
+     * 
+     * @param cause error message string
+     */
+    void close( String cause );
+    
+    
+    /**
+     * Sets monitor state to closed, and sets the cause to a specific 
+     * Exception.
+     * 
+     * @param cause the exception to associate with the closure
+     */
+    void close( Exception cause );
+    
+    
+    /**
+     * Gets whether the state of this ClosureMonitor is set to closed.
+     *
+     * @return true if state is closed, false if open
+     */
+    boolean isClosed();
+    
+    
+    /**
+     * Checks if state of this ClosureMonitor is set to closed and if so, 
+     * throws the causing Exception.
+     *
+     * @throws Exception the cause of the closure
+     */
+    void checkNotClosed() throws Exception;
+    
+    
+    /**
+     * Gets the cause of the closure.
+     *
+     * @return the causing Exception
+     */
+    Exception getCause();
+}

Modified: directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/Cursor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/Cursor.java?rev=687549&r1=687548&r2=687549&view=diff
==============================================================================
--- directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/Cursor.java
(original)
+++ directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/Cursor.java
Wed Aug 20 21:19:13 2008
@@ -214,4 +214,12 @@
      * @throws Exception if for some reason this Cursor could not be closed
      */
     void close( Exception reason ) throws Exception;
+    
+    
+    /**
+     * Sets a non-null closure monitor to associate with this Cursor.
+     *
+     * @param monitor the monitor to use for detecting Cursor close events
+     */
+    void setClosureMonitor( ClosureMonitor monitor );
 }

Added: directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/DefaultClosureMonitor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/DefaultClosureMonitor.java?rev=687549&view=auto
==============================================================================
--- directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/DefaultClosureMonitor.java
(added)
+++ directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/DefaultClosureMonitor.java
Wed Aug 20 21:19:13 2008
@@ -0,0 +1,125 @@
+/*
+ *   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.directory.server.core.cursor;
+
+
+/**
+ * A basic ClosureMonitor that simply uses a boolean for state and a cause 
+ * exception.
+ * 
+ * Note that we consciously chose not to synchronize close() operations with
+ * checks to see if the monitor state is closed because it costs to 
+ * synchronize and it's OK for the Cursor not to stop immediately when close()
+ * is called.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class DefaultClosureMonitor implements ClosureMonitor
+{
+    private boolean closed;
+    private Exception cause;
+
+    
+    /* (non-Javadoc)
+     * @see org.apache.directory.server.core.cursor.ClosureMonitor#close()
+     */
+    public final void close()
+    {
+        // state check needed to "try" not to overwrite exception (lack of 
+        // synchronization may still allow overwriting but who cares that much
+        if ( ! closed )
+        {
+            // not going to sync because who cares if it takes a little longer 
+            // to stop but we need to set cause before toggling closed state 
+            // or else check for closure can throw null cause 
+            cause = new CursorClosedException();
+            closed = true;
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.directory.server.core.cursor.ClosureMonitor#close(java.lang.String)
+     */
+    public final void close( final String cause )
+    {
+        // state check needed to "try" not to overwrite exception (lack of 
+        // synchronization may still allow overwriting but who cares that much
+        if ( ! closed )
+        {
+            // not going to sync because who cares if it takes a little longer 
+            // to stop but we need to set cause before toggling closed state 
+            // or else check for closure can throw null cause 
+            this.cause = new CursorClosedException( cause );
+            closed = true;
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.directory.server.core.cursor.ClosureMonitor#close(java.lang.Exception)
+     */
+    public final void close( final Exception cause )
+    {
+        // state check needed to "try" not to overwrite exception (lack of 
+        // synchronization may still allow overwriting but who cares that much
+        if ( ! closed )
+        {
+            // not going to sync because who cares if it takes a little longer 
+            // to stop but we need to set cause before toggling closed state 
+            // or else check for closure can throw null cause 
+            this.cause = cause;
+            closed = true;
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.directory.server.core.cursor.ClosureMonitor#getCause()
+     */
+    public final Exception getCause()
+    {
+        return cause;
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.directory.server.core.cursor.ClosureMonitor#isClosed()
+     */
+    public final boolean isClosed()
+    {
+        return closed;
+    }
+
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.directory.server.core.cursor.ClosureMonitor#checkNotClosed()
+     */
+    public void checkNotClosed() throws Exception
+    {
+        // lack of synchronization may cause pass but eventually it will work
+        if ( closed )
+        {
+            throw cause;
+        }
+    }
+}

Modified: directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/EmptyCursor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/EmptyCursor.java?rev=687549&r1=687548&r2=687549&view=diff
==============================================================================
--- directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/EmptyCursor.java
(original)
+++ directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/EmptyCursor.java
Wed Aug 20 21:19:13 2008
@@ -34,59 +34,59 @@
 
     public void before( E element ) throws Exception
     {
-        checkClosed( "before()" );
+        checkNotClosed( "before()" );
     }
 
 
     public void after( E element ) throws Exception
     {
-        checkClosed( "after()" );
+        checkNotClosed( "after()" );
     }
 
 
     public void beforeFirst() throws Exception
     {
-        checkClosed( "beforeFirst()" );
+        checkNotClosed( "beforeFirst()" );
     }
 
 
     public void afterLast() throws Exception
     {
-        checkClosed( "afterLast()" );
+        checkNotClosed( "afterLast()" );
     }
 
 
     public boolean first() throws Exception
     {
-        checkClosed( "first()" );
+        checkNotClosed( "first()" );
         return false;
     }
 
 
     public boolean last() throws Exception
     {
-        checkClosed( "last()" );
+        checkNotClosed( "last()" );
         return false;
     }
 
 
     public boolean previous() throws Exception
     {
-        checkClosed( "previous()" );
+        checkNotClosed( "previous()" );
         return false;
     }
 
 
     public boolean next() throws Exception
     {
-        checkClosed( "next()" );
+        checkNotClosed( "next()" );
         return false;
     }
 
 
     public E get() throws Exception
     {
-        checkClosed( "get()" );
+        checkNotClosed( "get()" );
         throw new InvalidCursorPositionException( "This cursor is empty and cannot return
elements!" );
     }
 

Modified: directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/IteratorCursor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/IteratorCursor.java?rev=687549&r1=687548&r2=687549&view=diff
==============================================================================
--- directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/IteratorCursor.java
(original)
+++ directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/IteratorCursor.java
Wed Aug 20 21:19:13 2008
@@ -90,7 +90,7 @@
 
     public boolean next() throws Exception
     {
-        checkClosed( "next()" );
+        checkNotClosed( "next()" );
         if ( values.hasNext() )
         {
             current = values.next();
@@ -103,7 +103,7 @@
 
     public E get() throws Exception
     {
-        checkClosed( "get()" );
+        checkNotClosed( "get()" );
         return current;
     }
 

Modified: directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ListCursor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ListCursor.java?rev=687549&r1=687548&r2=687549&view=diff
==============================================================================
--- directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ListCursor.java
(original)
+++ directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/ListCursor.java
Wed Aug 20 21:19:13 2008
@@ -197,7 +197,7 @@
      */
     public void before( E element ) throws Exception
     {
-        checkClosed( "before()" );
+        checkNotClosed( "before()" );
 
         if ( comparator == null )
         {
@@ -228,7 +228,7 @@
 
     public void after( E element ) throws Exception
     {
-        checkClosed( "after()" );
+        checkNotClosed( "after()" );
 
         if ( comparator == null )
         {
@@ -259,21 +259,21 @@
 
     public void beforeFirst() throws Exception
     {
-        checkClosed( "beforeFirst()" );
+        checkNotClosed( "beforeFirst()" );
         this.index = -1;
     }
 
 
     public void afterLast() throws Exception
     {
-        checkClosed( "afterLast()" );
+        checkNotClosed( "afterLast()" );
         this.index = end;
     }
 
 
     public boolean first() throws Exception
     {
-        checkClosed( "first()" );
+        checkNotClosed( "first()" );
 
         if ( list.size() > 0 )
         {
@@ -287,7 +287,7 @@
 
     public boolean last() throws Exception
     {
-        checkClosed( "last()" );
+        checkNotClosed( "last()" );
 
         if ( list.size() > 0 )
         {
@@ -301,35 +301,35 @@
 
     public boolean isFirst() throws Exception
     {
-        checkClosed( "isFirst()" );
+        checkNotClosed( "isFirst()" );
         return list.size() > 0 && index == start;
     }
 
 
     public boolean isLast() throws Exception
     {
-        checkClosed( "isLast()" );
+        checkNotClosed( "isLast()" );
         return list.size() > 0 && index == end - 1;
     }
 
 
     public boolean isAfterLast() throws Exception
     {
-        checkClosed( "isAfterLast()" );
+        checkNotClosed( "isAfterLast()" );
         return index == end;
     }
 
 
     public boolean isBeforeFirst() throws Exception
     {
-        checkClosed( "isBeforeFirst()" );
+        checkNotClosed( "isBeforeFirst()" );
         return index == -1;
     }
 
 
     public boolean previous() throws Exception
     {
-        checkClosed( "previous()" );
+        checkNotClosed( "previous()" );
 
         // if parked at -1 we cannot go backwards
         if ( index == -1 )
@@ -362,7 +362,7 @@
 
     public boolean next() throws Exception
     {
-        checkClosed( "next()" );
+        checkNotClosed( "next()" );
 
         // if parked at -1 we advance to the start index and return true
         if ( list.size() > 0 && index == -1 )
@@ -396,7 +396,7 @@
 
     public E get() throws Exception
     {
-        checkClosed( "get()" );
+        checkNotClosed( "get()" );
         if ( index < start || index >= end )
         {
             throw new IOException( "Cursor not positioned at an element" );

Modified: directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/SingletonCursor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/SingletonCursor.java?rev=687549&r1=687548&r2=687549&view=diff
==============================================================================
--- directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/SingletonCursor.java
(original)
+++ directory/apacheds/trunk/core-cursor/src/main/java/org/apache/directory/server/core/cursor/SingletonCursor.java
Wed Aug 20 21:19:13 2008
@@ -58,7 +58,7 @@
 
     public void before( E element ) throws Exception
     {
-        checkClosed( "before()" );
+        checkNotClosed( "before()" );
 
         if ( comparator == null )
         {
@@ -81,7 +81,7 @@
 
     public void after( E element ) throws Exception
     {
-        checkClosed( "after()" );
+        checkNotClosed( "after()" );
 
         if ( comparator == null )
         {
@@ -104,7 +104,7 @@
 
     public void beforeFirst() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         beforeFirst = true;
         afterLast = false;
         onSingleton = false;
@@ -113,7 +113,7 @@
 
     public void afterLast() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         beforeFirst = false;
         afterLast = true;
         onSingleton = false;
@@ -122,7 +122,7 @@
 
     public boolean first() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         beforeFirst = false;
         onSingleton = true;
         afterLast = false;
@@ -132,7 +132,7 @@
 
     public boolean last() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         beforeFirst = false;
         onSingleton = true;
         afterLast = false;
@@ -142,35 +142,35 @@
 
     public boolean isFirst() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         return onSingleton;
     }
 
 
     public boolean isLast() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         return onSingleton;
     }
 
 
     public boolean isAfterLast() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         return afterLast;
     }
 
 
     public boolean isBeforeFirst() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         return beforeFirst;
     }
 
 
     public boolean previous() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         if ( beforeFirst )
         {
             return false;
@@ -194,7 +194,7 @@
 
     public boolean next() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         if ( beforeFirst )
         {
             beforeFirst = false;
@@ -218,7 +218,7 @@
 
     public E get() throws Exception
     {
-        checkClosed( "()" );
+        checkNotClosed( "()" );
         if ( onSingleton )
         {
             return singleton;



Mime
View raw message