DefaultResourceTypes.class,
UserManagedMetrics.class,
ProjectFileSystemLogger.class,
- DatabaseSemaphoreImpl.class,
Periods.class,
// maven
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core;
-
-import org.sonar.api.utils.DatabaseSemaphore;
-import org.sonar.core.persistence.SemaphoreDao;
-
-/**
- * @since 3.4
- */
-public class DatabaseSemaphoreImpl implements DatabaseSemaphore {
-
- private SemaphoreDao dao;
-
- public DatabaseSemaphoreImpl(SemaphoreDao dao) {
- this.dao = dao;
- }
-
- public boolean acquire(String name, int maxDurationInSeconds) {
- return dao.acquire(name, maxDurationInSeconds).isAcquired();
- }
-
- public void release(String name) {
- dao.release(name);
- }
-}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core;
-
-import org.junit.Test;
-import org.sonar.core.persistence.Lock;
-import org.sonar.core.persistence.SemaphoreDao;
-
-import static org.mockito.Matchers.anyInt;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-public class DatabaseSemaphoreImplTest {
-
- @Test
- public void should_be_a_bridge_over_dao() {
- Lock lock = mock(Lock.class);
- SemaphoreDao dao = mock(SemaphoreDao.class);
- when(dao.acquire(anyString(), anyInt())).thenReturn(lock);
-
- DatabaseSemaphoreImpl impl = new DatabaseSemaphoreImpl(dao);
-
- impl.acquire("do-xxx", 50000);
- verify(dao).acquire("do-xxx", 50000);
-
- impl.release("do-xxx");
- verify(dao).release("do-xxx");
- }
-}
import org.sonar.core.metric.CacheMetricFinder;
import org.sonar.core.notification.DefaultNotificationManager;
import org.sonar.core.persistence.DaoUtils;
+import org.sonar.core.persistence.DatabaseSemaphoreImpl;
import org.sonar.core.persistence.DatabaseVersion;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.resource.DefaultResourcePermissions;
container.addSingleton(DefaultUserFinder.class);
container.addSingleton(ResourceTypes.class);
container.addSingleton(MetricProvider.class);
+ container.addSingleton(DatabaseSemaphoreImpl.class);
container.addSingleton(CheckSemaphore.class);
}
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
+import org.sonar.api.utils.DatabaseSemaphore;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.ProjectTree;
-import org.sonar.core.persistence.Lock;
-import org.sonar.core.persistence.SemaphoreDao;
+
+import static org.sonar.api.utils.DatabaseSemaphore.Lock;
public class CheckSemaphore {
private static final Logger LOG = LoggerFactory.getLogger(CheckSemaphore.class);
- private final SemaphoreDao semaphoreDao;
+ private final DatabaseSemaphore databaseSemaphore;
private final ProjectTree projectTree;
private final Settings settings;
- public CheckSemaphore(SemaphoreDao semaphoreDao, ProjectTree projectTree, Settings settings) {
- this.semaphoreDao = semaphoreDao;
+ public CheckSemaphore(DatabaseSemaphore databaseSemaphore, ProjectTree projectTree, Settings settings) {
+ this.databaseSemaphore = databaseSemaphore;
this.projectTree = projectTree;
this.settings = settings;
}
private Lock acquire() {
LOG.debug("Acquire semaphore on project : {}, with key {}", getProject(), getSemaphoreKey());
if (!isForceAnalyseActivated()) {
- return semaphoreDao.acquire(getSemaphoreKey());
+ return databaseSemaphore.acquire(getSemaphoreKey());
} else {
- return semaphoreDao.acquire(getSemaphoreKey(), 0);
+ // In force mode, we acquire the lock regardless there's a existing lock or not
+ return databaseSemaphore.acquire(getSemaphoreKey(), 0);
}
}
private void release() {
LOG.debug("Release semaphore on project : {}, with key {}", getProject(), getSemaphoreKey());
- semaphoreDao.release(getSemaphoreKey());
+ databaseSemaphore.release(getSemaphoreKey());
}
private String getSemaphoreKey() {
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
+import org.sonar.api.utils.DatabaseSemaphore;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.ProjectTree;
-import org.sonar.core.persistence.Lock;
-import org.sonar.core.persistence.SemaphoreDao;
import java.util.Date;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.sonar.api.utils.DatabaseSemaphore.Lock;
public class CheckSemaphoreTest {
private CheckSemaphore checkSemaphore;
- private SemaphoreDao semaphoreDao;
+ private DatabaseSemaphore databaseSemaphore;
private ProjectTree projectTree;
private Settings settings;
public void setUp() {
lock = mock(Lock.class);
- semaphoreDao = mock(SemaphoreDao.class);
- when(semaphoreDao.acquire(anyString())).thenReturn(lock);
- when(semaphoreDao.acquire(anyString(), anyInt())).thenReturn(lock);
+ databaseSemaphore = mock(DatabaseSemaphore.class);
+ when(databaseSemaphore.acquire(anyString())).thenReturn(lock);
+ when(databaseSemaphore.acquire(anyString(), anyInt())).thenReturn(lock);
projectTree = mock(ProjectTree.class);
settings = new Settings();
project = new Project("key", "branch", "name");
when(projectTree.getRootProject()).thenReturn(project);
- checkSemaphore = new CheckSemaphore(semaphoreDao, projectTree, settings);
+ checkSemaphore = new CheckSemaphore(databaseSemaphore, projectTree, settings);
}
@Test
when(lock.isAcquired()).thenReturn(true);
checkSemaphore.start();
- verify(semaphoreDao).acquire(anyString());
+ verify(databaseSemaphore).acquire(anyString());
}
@Test
when(lock.isAcquired()).thenReturn(true);
checkSemaphore.start();
- verify(semaphoreDao).acquire("batch-key");
+ verify(databaseSemaphore).acquire("batch-key");
}
@Test
when(lock.isAcquired()).thenReturn(true);
checkSemaphore.start();
- verify(semaphoreDao).acquire("batch-key:branch");
+ verify(databaseSemaphore).acquire("batch-key:branch");
}
@Test
setForceMode(true);
when(lock.isAcquired()).thenReturn(true);
checkSemaphore.start();
- verify(semaphoreDao).acquire(anyString(), anyInt());
+ verify(databaseSemaphore).acquire(anyString(), anyInt());
}
@Test(expected = SonarException.class)
when(lock.getLocketAt()).thenReturn(new Date());
when(lock.isAcquired()).thenReturn(false);
checkSemaphore.start();
- verify(semaphoreDao, never()).acquire(anyString());
+ verify(databaseSemaphore, never()).acquire(anyString());
}
@Test
setDryRunMode(true);
settings = new Settings().setProperty(CoreProperties.DRY_RUN, true);
checkSemaphore.start();
- verify(semaphoreDao, never()).acquire(anyString());
- verify(semaphoreDao, never()).acquire(anyString(), anyInt());
+ verify(databaseSemaphore, never()).acquire(anyString());
+ verify(databaseSemaphore, never()).acquire(anyString(), anyInt());
}
@Test
public void shouldReleaseSemaphore() {
checkSemaphore.stop();
- verify(semaphoreDao).release(anyString());
+ verify(databaseSemaphore).release(anyString());
}
@Test
public void shouldNotReleaseSemaphoreInDryRunMode() {
setDryRunMode(true);
checkSemaphore.stop();
- verify(semaphoreDao, never()).release(anyString());
+ verify(databaseSemaphore, never()).release(anyString());
}
private void setDryRunMode(boolean isInDryRunMode) {
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.core.persistence;
+
+import org.sonar.api.utils.DatabaseSemaphore;
+
+/**
+ * @since 3.4
+ */
+public class DatabaseSemaphoreImpl implements DatabaseSemaphore {
+
+ private SemaphoreDao dao;
+
+ public DatabaseSemaphoreImpl(SemaphoreDao dao) {
+ this.dao = dao;
+ }
+
+ public Lock acquire(String name, int maxDurationInSeconds) {
+ return dao.acquire(name, maxDurationInSeconds);
+ }
+
+ public Lock acquire(String name) {
+ return dao.acquire(name);
+ }
+
+ public void release(String name) {
+ dao.release(name);
+ }
+}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-
-package org.sonar.core.persistence;
-
-import java.util.Date;
-
-/**
- * @since 3.4
- */
-public class Lock {
-
- private String name;
- private boolean acquired;
- private Date locketAt;
- private Date createdAt;
- private Date updatedAt;
- private Long durationSinceLocked;
-
- public Lock(String name, boolean acquired, Date locketAt, Date createdAt, Date updatedAt) {
- this.name = name;
- this.acquired = acquired;
- this.locketAt = locketAt;
- this.createdAt = createdAt;
- this.updatedAt = updatedAt;
- }
-
- public String getName() {
- return name;
- }
-
- public Date getLocketAt() {
- return locketAt;
- }
-
- public Date getCreatedAt() {
- return createdAt;
- }
-
- public Date getUpdatedAt() {
- return updatedAt;
- }
-
- public boolean isAcquired() {
- return acquired;
- }
-
- public Long getDurationSinceLocked() {
- return durationSinceLocked;
- }
-
- public void setDurationSinceLocked(Long durationSinceLocked) {
- this.durationSinceLocked = durationSinceLocked;
- }
-}
import java.util.Date;
+import static org.sonar.api.utils.DatabaseSemaphore.Lock;
+
/**
* @since 3.4
*/
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.core.persistence;
+
+import org.junit.Test;
+
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.sonar.api.utils.DatabaseSemaphore.Lock;
+
+public class DatabaseSemaphoreImplTest {
+
+ @Test
+ public void should_be_a_bridge_over_dao() {
+ Lock lock = mock(Lock.class);
+ SemaphoreDao dao = mock(SemaphoreDao.class);
+ when(dao.acquire(anyString(), anyInt())).thenReturn(lock);
+
+ DatabaseSemaphoreImpl impl = new DatabaseSemaphoreImpl(dao);
+
+ impl.acquire("do-xxx", 50000);
+ verify(dao).acquire("do-xxx", 50000);
+
+ impl.acquire("do-xxx");
+ verify(dao).acquire("do-xxx");
+
+ impl.release("do-xxx");
+ verify(dao).release("do-xxx");
+ }
+}
import java.util.concurrent.atomic.AtomicInteger;
import static org.fest.assertions.Assertions.assertThat;
+import static org.sonar.api.utils.DatabaseSemaphore.Lock;
public class SemaphoreDaoTest extends AbstractDaoTestCase {
import org.sonar.api.BatchComponent;
import org.sonar.api.ServerComponent;
+import java.util.Date;
+
/**
* A semaphore shared among all the processes that can connect to the central database.
*
*/
public interface DatabaseSemaphore extends BatchComponent, ServerComponent {
- boolean acquire(String name, int maxDurationInSeconds);
+ /**
+ * Try to acquire a lock on a name, for a given duration.
+ * 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.
+ *
+ * @param name the key of the semaphore
+ * @param maxDurationInSeconds the max duration in seconds the semaphore will be acquired (a value of zero can be used to always acquire a lock)
+ * @return a lock containing information if the lock could be acquired or not, the duration since locked, etc.
+ */
+ Lock acquire(String name, int maxDurationInSeconds);
+
+ /**
+ * Try to acquire the lock on a name.
+ * The lock will be acquired only if there's no existing lock.
+ *
+ * @param name the key of the semaphore
+ * @return a lock containing information if the lock could be acquired or not, the duration since locked, etc.
+ */
+ Lock acquire(String name);
+ /**
+ * Release the lock on a semaphore by its name.
+ *
+ * @param name the key of the semaphore
+ */
void release(String name);
+ static class Lock {
+
+ private String name;
+ private boolean acquired;
+ private Date locketAt;
+ private Date createdAt;
+ private Date updatedAt;
+ private Long durationSinceLocked;
+
+ public Lock(String name, boolean acquired, Date locketAt, Date createdAt, Date updatedAt) {
+ this.name = name;
+ this.acquired = acquired;
+ this.locketAt = locketAt;
+ this.createdAt = createdAt;
+ this.updatedAt = updatedAt;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Date getLocketAt() {
+ return locketAt;
+ }
+
+ public Date getCreatedAt() {
+ return createdAt;
+ }
+
+ public Date getUpdatedAt() {
+ return updatedAt;
+ }
+
+ public boolean isAcquired() {
+ return acquired;
+ }
+
+ public Long getDurationSinceLocked() {
+ return durationSinceLocked;
+ }
+
+ public void setDurationSinceLocked(Long durationSinceLocked) {
+ this.durationSinceLocked = durationSinceLocked;
+ }
+
+ }
+
}