001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.api.utils;
021
022import org.sonar.api.BatchComponent;
023import org.sonar.api.ServerComponent;
024
025import java.util.Date;
026
027/**
028 * A semaphore shared among all the processes that can connect to the central database.
029 *
030 * @since 3.4
031 */
032public interface DatabaseSemaphore extends BatchComponent, ServerComponent {
033
034  /**
035   * Try to acquire a lock on a name, for a given duration.
036   * The lock will be acquired if there's no existing lock on this name or if a lock exists but the max duration is reached.
037   *
038   * @param name the key of the semaphore
039   * @param maxDurationInSeconds the max duration in seconds the semaphore will be acquired (a value of zero can be used to always acquire a lock)
040   * @return a lock containing information if the lock could be acquired or not, the duration since locked, etc.
041   */
042  Lock acquire(String name, int maxDurationInSeconds);
043
044  /**
045   * Try to acquire the lock on a name.
046   * The lock will be acquired only if there's no existing lock.
047   *
048   * @param name the key of the semaphore
049   * @return a lock containing information if the lock could be acquired or not, the duration since locked, etc.
050   */
051  Lock acquire(String name);
052
053  /**
054   * Release the lock on a semaphore by its name.
055   *
056   * @param name the key of the semaphore
057   */
058  void release(String name);
059
060  class Lock {
061
062    private String name;
063    private boolean acquired;
064    private Date locketAt;
065    private Date createdAt;
066    private Date updatedAt;
067    private Long durationSinceLocked;
068
069    public Lock(String name, boolean acquired, Date locketAt, Date createdAt, Date updatedAt) {
070      this.name = name;
071      this.acquired = acquired;
072      this.locketAt = locketAt;
073      this.createdAt = createdAt;
074      this.updatedAt = updatedAt;
075    }
076
077    public String getName() {
078      return name;
079    }
080
081    public Date getLocketAt() {
082      return locketAt;
083    }
084
085    public Date getCreatedAt() {
086      return createdAt;
087    }
088
089    public Date getUpdatedAt() {
090      return updatedAt;
091    }
092
093    public boolean isAcquired() {
094      return acquired;
095    }
096
097    public Long getDurationSinceLocked() {
098      return durationSinceLocked;
099    }
100
101    public void setDurationSinceLocked(Long durationSinceLocked) {
102      this.durationSinceLocked = durationSinceLocked;
103    }
104
105  }
106
107}