aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src/main
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-12-14 18:27:29 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-12-14 18:27:29 +0100
commitaf07212b764549c16cd252a9311bb68c642e8179 (patch)
tree2276df7ea25c510e83da3c70e90d93f75e07e4b2 /sonar-core/src/main
parent189a12a09eb4fd34b632fb2582eb479e15a08125 (diff)
downloadsonarqube-af07212b764549c16cd252a9311bb68c642e8179.tar.gz
sonarqube-af07212b764549c16cd252a9311bb68c642e8179.zip
SONAR-3887 refactor semaphore API
Diffstat (limited to 'sonar-core/src/main')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java34
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/SemaphoresImpl.java (renamed from sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java)10
2 files changed, 24 insertions, 20 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java
index 471315c2d40..3e36545012b 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java
@@ -23,11 +23,10 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.apache.commons.lang.time.DateUtils;
import org.apache.ibatis.session.SqlSession;
+import org.sonar.api.utils.Semaphores;
import java.util.Date;
-import static org.sonar.api.utils.DatabaseSemaphore.Lock;
-
/**
* @since 3.4
*/
@@ -39,7 +38,7 @@ public class SemaphoreDao {
this.mybatis = mybatis;
}
- public Lock acquire(String name, int maxDurationInSeconds) {
+ public Semaphores.Semaphore acquire(String name, int maxDurationInSeconds) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Semaphore name must not be empty");
Preconditions.checkArgument(maxDurationInSeconds >= 0, "Semaphore max duration must be positive: " + maxDurationInSeconds);
@@ -47,7 +46,7 @@ public class SemaphoreDao {
try {
SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
Date lockedAt = org.sonar.api.utils.DateUtils.parseDate("2001-01-01");
- createSemaphore(name, lockedAt, session);
+ createDto(name, lockedAt, session);
boolean isAcquired = doAcquire(name, maxDurationInSeconds, session, mapper);
SemaphoreDto semaphore = selectSemaphore(name, session);
return createLock(semaphore, session, isAcquired);
@@ -56,7 +55,7 @@ public class SemaphoreDao {
}
}
- public Lock acquire(String name) {
+ public Semaphores.Semaphore acquire(String name) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Semaphore name must not be empty");
SqlSession session = mybatis.openSession();
@@ -67,7 +66,7 @@ public class SemaphoreDao {
if (semaphore != null) {
return createLock(semaphore, session, false);
} else {
- semaphore = createSemaphore(name, now, session);
+ semaphore = createDto(name, now, session);
return createLock(semaphore, session, true);
}
} finally {
@@ -93,12 +92,12 @@ public class SemaphoreDao {
return ok;
}
- private SemaphoreDto createSemaphore(String name, Date lockedAt, SqlSession session) {
+ private SemaphoreDto createDto(String name, Date lockedAt, SqlSession session) {
try {
SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
SemaphoreDto semaphore = new SemaphoreDto()
- .setName(name)
- .setLockedAt(lockedAt);
+ .setName(name)
+ .setLockedAt(lockedAt);
mapper.initialize(semaphore);
session.commit();
return semaphore;
@@ -109,12 +108,17 @@ public class SemaphoreDao {
}
}
- private Lock createLock(SemaphoreDto semaphore, SqlSession session, boolean acquired) {
- Lock lock = new Lock(semaphore.getName(), acquired, semaphore.getLockedAt(), semaphore.getCreatedAt(), semaphore.getUpdatedAt());
+ private Semaphores.Semaphore createLock(SemaphoreDto dto, SqlSession session, boolean acquired) {
+ Semaphores.Semaphore semaphore = new Semaphores.Semaphore()
+ .setName(dto.getName())
+ .setLocked(acquired)
+ .setLocketAt(dto.getLockedAt())
+ .setCreatedAt(dto.getCreatedAt())
+ .setUpdatedAt(dto.getUpdatedAt());
if (!acquired) {
- lock.setDurationSinceLocked(getDurationSinceLocked(semaphore, session));
+ semaphore.setDurationSinceLocked(getDurationSinceLocked(dto, session));
}
- return lock;
+ return semaphore;
}
private long getDurationSinceLocked(SemaphoreDto semaphore, SqlSession session) {
@@ -124,12 +128,12 @@ public class SemaphoreDao {
return now - locketAt;
}
- protected SemaphoreDto selectSemaphore(String name, SqlSession session){
+ protected SemaphoreDto selectSemaphore(String name, SqlSession session) {
SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
return mapper.selectSemaphore(name);
}
- protected Date now(SqlSession session){
+ protected Date now(SqlSession session) {
SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
return mapper.now();
}
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoresImpl.java
index 75a5bb7e2ed..b69cff90448 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseSemaphoreImpl.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoresImpl.java
@@ -19,24 +19,24 @@
*/
package org.sonar.core.persistence;
-import org.sonar.api.utils.DatabaseSemaphore;
+import org.sonar.api.utils.Semaphores;
/**
* @since 3.4
*/
-public class DatabaseSemaphoreImpl implements DatabaseSemaphore {
+public class SemaphoresImpl implements Semaphores {
private SemaphoreDao dao;
- public DatabaseSemaphoreImpl(SemaphoreDao dao) {
+ public SemaphoresImpl(SemaphoreDao dao) {
this.dao = dao;
}
- public Lock acquire(String name, int maxDurationInSeconds) {
+ public Semaphore acquire(String name, int maxDurationInSeconds) {
return dao.acquire(name, maxDurationInSeconds);
}
- public Lock acquire(String name) {
+ public Semaphore acquire(String name) {
return dao.acquire(name);
}