001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.util;
029
030import org.opencms.main.CmsLog;
031
032import org.apache.commons.logging.Log;
033
034/**
035 * Low-level utility class used for waiting for an action performed by another thread.<p>
036 *
037 * This is really a thin wrapper around the wait() and notifyAll() methods.
038 */
039public class CmsWaitHandle {
040
041    /** Logger instance for this class. */
042    private static final Log LOG = CmsLog.getLog(CmsWaitHandle.class);
043
044    /** Flag which indicates whether release has already been called. */
045    private boolean m_released;
046
047    /** Flag indicating whether the wait handle is single-use. */
048    private boolean m_singleUse;
049
050    /**
051     * Creates a reusable wait handle.<p>
052     */
053    public CmsWaitHandle() {
054
055        m_singleUse = false;
056    }
057
058    /**
059     * Creates a wait handle.<p>
060     *
061     * The argument controls whether the wait handle will be single-use or reusable. The difference is that, for a single-use
062     * wait handle, all calls to enter() will return immediately after the first call to release(), while calling enter() on
063     * a reusable wait handle will always wait for the <em>next</em> release call.
064     *
065     * @param singleUse true if a single-use wait handle should be created
066     */
067    public CmsWaitHandle(boolean singleUse) {
068
069        m_singleUse = singleUse;
070    }
071
072    /**
073     * Waits for a maximum of waitTime, but returns if another thread calls release().<p>
074     *
075     * @param waitTime the maximum wait time
076     */
077    public synchronized void enter(long waitTime) {
078
079        if (m_singleUse && m_released) {
080            return;
081        }
082
083        try {
084            wait(waitTime);
085        } catch (InterruptedException e) {
086            // should never happen, but log it just in case...
087            LOG.error(e.getLocalizedMessage(), e);
088        }
089    }
090
091    /**
092     * Releases all currently waiting threads.<p>
093     */
094    public synchronized void release() {
095
096        notifyAll();
097        m_released = true;
098    }
099
100}