/*=============================================================================*
* Copyright 2004 The Apache Software Foundation
*
* Licensed 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.ws.util.lock;
import EDU.oswego.cs.dl.util.concurrent.ReentrantLock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.util.i18n.Keys;
import org.apache.ws.util.i18n.Messages;
import org.apache.ws.util.i18n.MessagesImpl;
/**
* LOG-DONE
* This class is a simple reentrant lock implementation.
*/
public class Lock
{
private static final Log LOG = LogFactory.getLog( Lock.class );
public static final Messages MSG = MessagesImpl.getInstance();
private int m_waiters;
private LockManager m_lockManager;
private Object m_key;
private ReentrantLock m_lock;
/**
* Creates a new {@link Lock} object.
*
* @param lockManager DOCUMENT_ME
* @param key DOCUMENT_ME
*/
public Lock( LockManager lockManager,
Object key )
{
m_lockManager = lockManager;
m_key = key;
m_lock = new ReentrantLock( );
}
/**
* DOCUMENT_ME
*
* @throws InterruptedException DOCUMENT_ME
*/
public synchronized void acquire( )
throws InterruptedException
{
LOG.trace(MSG.getMessage( Keys.ACQUIRE_LCK));
m_waiters++;
m_lock.acquire( );
m_waiters--;
}
/**
* DOCUMENT_ME
*/
public synchronized void release( )
{
LOG.trace(MSG.getMessage( Keys.RELEASE_LCK));
m_lock.release( );
if ( ( m_waiters == 0 ) && ( m_lockManager != null ) )
{
m_lockManager.removeLock( m_key );
}
}
}