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 * http://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.server.core.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 * @version $Rev$, $Date$
34 */
35 public class DefaultClosureMonitor implements ClosureMonitor
36 {
37 private boolean closed;
38 private Exception cause;
39
40
41 /* (non-Javadoc)
42 * @see org.apache.directory.server.core.cursor.ClosureMonitor#close()
43 */
44 public final void close()
45 {
46 // state check needed to "try" not to overwrite exception (lack of
47 // synchronization may still allow overwriting but who cares that much
48 if ( ! closed )
49 {
50 // not going to sync because who cares if it takes a little longer
51 // to stop but we need to set cause before toggling closed state
52 // or else check for closure can throw null cause
53 cause = new CursorClosedException();
54 closed = true;
55 }
56 }
57
58
59 /* (non-Javadoc)
60 * @see org.apache.directory.server.core.cursor.ClosureMonitor#close(java.lang.String)
61 */
62 public final void close( final String cause )
63 {
64 // state check needed to "try" not to overwrite exception (lack of
65 // synchronization may still allow overwriting but who cares that much
66 if ( ! closed )
67 {
68 // not going to sync because who cares if it takes a little longer
69 // to stop but we need to set cause before toggling closed state
70 // or else check for closure can throw null cause
71 this.cause = new CursorClosedException( cause );
72 closed = true;
73 }
74 }
75
76
77 /* (non-Javadoc)
78 * @see org.apache.directory.server.core.cursor.ClosureMonitor#close(java.lang.Exception)
79 */
80 public final void close( final Exception cause )
81 {
82 // state check needed to "try" not to overwrite exception (lack of
83 // synchronization may still allow overwriting but who cares that much
84 if ( ! closed )
85 {
86 // not going to sync because who cares if it takes a little longer
87 // to stop but we need to set cause before toggling closed state
88 // or else check for closure can throw null cause
89 this.cause = cause;
90 closed = true;
91 }
92 }
93
94
95 /* (non-Javadoc)
96 * @see org.apache.directory.server.core.cursor.ClosureMonitor#getCause()
97 */
98 public final Exception getCause()
99 {
100 return cause;
101 }
102
103
104 /* (non-Javadoc)
105 * @see org.apache.directory.server.core.cursor.ClosureMonitor#isClosed()
106 */
107 public final boolean isClosed()
108 {
109 return closed;
110 }
111
112
113 /*
114 * (non-Javadoc)
115 * @see org.apache.directory.server.core.cursor.ClosureMonitor#checkNotClosed()
116 */
117 public void checkNotClosed() throws Exception
118 {
119 // lack of synchronization may cause pass but eventually it will work
120 if ( closed )
121 {
122 throw cause;
123 }
124 }
125 }