/*
*
* 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.
*
*/
using System;
using Apache.Qpid.Messaging;
namespace Apache.Qpid.Client
{
/// Closeable provides monitoring of the state of a closeable resource; whether it is open or closed. It also provides a lock on which
/// attempts to close the resource from multiple threads can be coordinated.
///
///
CRC Card
///
Responsibilities
Collaborations
///
Close (and clean-up) a resource.
///
Monitor the state of a closeable resource.
///
Synchronous attempts to close resource from concurrent threads.
///
///
///
/// Poor encapsulation of the close lock. Better to completely hide the implementation, such that there is a method, e.g., DoSingleClose,
/// that sub-classes implement. Guaranteed to only be called by one thread at once, and iff the object is not already closed. That is, multiple
/// simultaneous closes will result in a single call to the real close method. Put the wait and condition checking loop in this base class.
///
public abstract class Closeable : ICloseable
{
/// Constant representing the closed state.
protected const int CLOSED = 1;
/// Constant representing the open state.
protected const int NOT_CLOSED = 2;
/// Used to ensure orderly closing of the object.
protected readonly object _closingLock = new object();
/// Indicates the state of this resource; open or closed.
protected int _closed = NOT_CLOSED;
///
/// Checks the not closed.
///
///
/// Don't like check methods that throw exceptions. a) it can come as a surprise without checked exceptions, b) it limits the
/// callers choice, if the caller would prefer a boolean, c) it is not side-effect free programming, where such could be used. Get rid
/// of this and replace with boolean.
protected void CheckNotClosed()
{
if (_closed == CLOSED)
{
throw new InvalidOperationException("Object " + ToString() + " has been closed");
}
}
/// Indicates whether this resource is closed.
/// true if closed; otherwise, false.
public bool Closed
{
get
{
return _closed == CLOSED;
}
}
/// Close this resource.
public abstract void Close();
}
}