001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.utils.concurrent;
020
021import java.util.concurrent.locks.ReentrantLock;
022
023import org.slf4j.*;
024
025/**
026 * This class is for logging and debugging purposes. It allows distinguishing the lock on the JPPF queue (driver and client) from any other lock in the same JVM.
027 * @author Laurent Cohen
028 */
029public class JPPFQueueLock extends ReentrantLock {
030  /**
031   * Logger for this class.
032   */
033  private static final Logger log = LoggerFactory.getLogger(JPPFQueueLock.class);
034  /**
035   * Determines whether the debug level is enabled in the log configuration, without the cost of a method call.
036   */
037  private static final boolean debugEnabled = log.isDebugEnabled();
038  /**
039   * Name given to this lock.
040   */
041  private final String name;
042
043  /**
044   * Creates an instance of {@code JPPFReentrantLock}.
045   * @param name the name.
046   */
047  public JPPFQueueLock(final String name) {
048    final String s = getClass().getSimpleName();
049    this.name = (name == null) ? s : s + " [" + name + "]";
050  }
051
052  @Override
053  public String toString() {
054    return name;
055  }
056
057  @Override
058  public void lock() {
059    if (debugEnabled) log.debug(">> locking {}", this);
060    super.lock();
061    if (debugEnabled) log.debug(">> locked {}", this);
062  }
063
064  @Override
065  public void unlock() {
066    if (debugEnabled) log.debug("<< unlocking {}", this);
067    super.unlock();
068    if (debugEnabled) log.debug("<< unlocked {}", this);
069  }
070}