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.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 public final void close() 47 { 48 // state check needed to "try" not to overwrite exception (lack of 49 // synchronization may still allow overwriting but who cares that much 50 if ( !closed ) 51 { 52 // not going to sync because who cares if it takes a little longer 53 // to stop but we need to set cause before toggling closed state 54 // or else check for closure can throw null cause 55 cause = CursorClosedException.INSTANCE; 56 closed = true; 57 } 58 } 59 60 61 /** 62 * {@inheritDoc} 63 */ 64 public final void close( final String cause ) 65 { 66 // state check needed to "try" not to overwrite exception (lack of 67 // synchronization may still allow overwriting but who cares that much 68 if ( !closed ) 69 { 70 // not going to sync because who cares if it takes a little longer 71 // to stop but we need to set cause before toggling closed state 72 // or else check for closure can throw null cause 73 this.cause = new CursorClosedException( cause ); 74 closed = true; 75 } 76 } 77 78 79 /** 80 * {@inheritDoc} 81 */ 82 public final void close( final Exception cause ) 83 { 84 // state check needed to "try" not to overwrite exception (lack of 85 // synchronization may still allow overwriting but who cares that much 86 if ( !closed ) 87 { 88 // not going to sync because who cares if it takes a little longer 89 // to stop but we need to set cause before toggling closed state 90 // or else check for closure can throw null cause 91 this.cause = cause; 92 closed = true; 93 } 94 } 95 96 97 /** 98 * {@inheritDoc} 99 */ 100 public final Exception getCause() 101 { 102 return cause; 103 } 104 105 106 /** 107 * {@inheritDoc} 108 */ 109 public final boolean isClosed() 110 { 111 return closed; 112 } 113 114 115 /** 116 * {@inheritDoc} 117 */ 118 public void checkNotClosed() throws CursorClosedException 119 { 120 // lack of synchronization may cause pass but eventually it will work 121 if ( closed ) 122 { 123 throw new CursorClosedException( cause.getMessage() ); 124 } 125 } 126 }