View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     https://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.model.cursor;
21  
22  
23  /**
24   * A basic ClosureMonitor that simply uses a boolean for state and a cause
25   * exception.
26   *
27   * Note that we consciously chose not to synchronize close() operations with
28   * checks to see if the monitor state is closed because it costs to
29   * synchronize and it's OK for the Cursor not to stop immediately when close()
30   * is called.
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class DefaultClosureMonitor implements ClosureMonitor
35  {
36      /** Tells if the monitor is closed or not */
37      private boolean closed;
38  
39      /** If we get an exception, the cause is stored in this variable */
40      private Exception cause;
41  
42  
43      /**
44       * {@inheritDoc}
45       */
46      @Override
47      public final void close()
48      {
49          // state check needed to "try" not to overwrite exception (lack of
50          // synchronization may still allow overwriting but who cares that much
51          if ( !closed )
52          {
53              // not going to sync because who cares if it takes a little longer
54              // to stop but we need to set cause before toggling closed state
55              // or else check for closure can throw null cause
56              cause = CursorClosedException.INSTANCE;
57              closed = true;
58          }
59      }
60  
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public final void close( final String cause )
67      {
68          // state check needed to "try" not to overwrite exception (lack of
69          // synchronization may still allow overwriting but who cares that much
70          if ( !closed )
71          {
72              // not going to sync because who cares if it takes a little longer
73              // to stop but we need to set cause before toggling closed state
74              // or else check for closure can throw null cause
75              this.cause = new CursorClosedException( cause );
76              closed = true;
77          }
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public final void close( final Exception cause )
86      {
87          // state check needed to "try" not to overwrite exception (lack of
88          // synchronization may still allow overwriting but who cares that much
89          if ( !closed )
90          {
91              // not going to sync because who cares if it takes a little longer
92              // to stop but we need to set cause before toggling closed state
93              // or else check for closure can throw null cause
94              this.cause = cause;
95              closed = true;
96          }
97      }
98  
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public final Exception getCause()
105     {
106         return cause;
107     }
108 
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public final boolean isClosed()
115     {
116         return closed;
117     }
118 
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public void checkNotClosed() throws CursorClosedException
125     {
126         // lack of synchronization may cause pass but eventually it will work
127         if ( closed )
128         {
129             throw new CursorClosedException( cause.getMessage() );
130         }
131     }
132 }