diff options
5 files changed, 181 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java index 3ee901fc9c2..84b4f3fda6f 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java @@ -85,6 +85,16 @@ public class PropertiesDao implements BatchComponent, ServerComponent { } } + public PropertyDto selectGlobalProperty(String propertyKey) { + SqlSession session = mybatis.openSession(); + PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + try { + return mapper.selectByKey(new PropertyDto().setKey(propertyKey)); + } finally { + MyBatis.closeQuietly(session); + } + } + public List<PropertyDto> selectProjectProperties(String resourceKey) { SqlSession session = mybatis.openSession(); PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java index 242938e0e84..64df0517675 100644 --- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java @@ -95,6 +95,19 @@ public class PropertiesDaoTest extends AbstractDaoTestCase { } @Test + public void selectGlobalProperty() { + setupData("selectGlobalProperties"); + + PropertyDto prop = dao.selectGlobalProperty("global.one"); + assertThat(prop).isNotNull(); + assertThat(prop.getValue(), is("one")); + + assertThat(dao.selectGlobalProperty("project.one")).isNull(); + assertThat(dao.selectGlobalProperty("user.one")).isNull(); + assertThat(dao.selectGlobalProperty("unexisting")).isNull(); + } + + @Test public void selectProjectProperties() { setupData("selectProjectProperties"); List<PropertyDto> properties = dao.selectProjectProperties("org.struts:struts"); diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index ae490bf7224..6d1a852c9a6 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -91,6 +91,7 @@ import org.sonar.server.startup.GenerateBootstrapIndex; import org.sonar.server.startup.GeneratePluginIndex; import org.sonar.server.startup.GwtPublisher; import org.sonar.server.startup.JdbcDriverDeployer; +import org.sonar.server.startup.LogServerId; import org.sonar.server.startup.RegisterMetrics; import org.sonar.server.startup.RegisterNewDashboards; import org.sonar.server.startup.RegisterNewMeasureFilters; @@ -288,6 +289,7 @@ public final class Platform { startupContainer.addSingleton(RegisterNewMeasureFilters.class); startupContainer.addSingleton(RegisterNewDashboards.class); startupContainer.addSingleton(RenameDeprecatedPropertyKeys.class); + startupContainer.addSingleton(LogServerId.class); startupContainer.startComponents(); startupContainer.getComponentByType(ServerLifecycleNotifier.class).notifyStart(); diff --git a/sonar-server/src/main/java/org/sonar/server/startup/LogServerId.java b/sonar-server/src/main/java/org/sonar/server/startup/LogServerId.java new file mode 100644 index 00000000000..631e92458a7 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/startup/LogServerId.java @@ -0,0 +1,70 @@ +/* + * 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.server.startup; + +import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.CoreProperties; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; + +/** + * @since 3.5 + */ +public final class LogServerId { + + private final PropertiesDao propertiesDao; + + public LogServerId(PropertiesDao propertiesDao) { + this.propertiesDao = propertiesDao; + } + + public void start() { + logServerId(LoggerFactory.getLogger(LogServerId.class)); + } + + @VisibleForTesting + protected void logServerId(Logger logger) { + PropertyDto serverIdProp = propertiesDao.selectGlobalProperty(CoreProperties.PERMANENT_SERVER_ID); + if (serverIdProp != null) { + // a server ID has been generated, let's print out the other useful informations that can help debugging license issues + PropertyDto organisationProp = propertiesDao.selectGlobalProperty(CoreProperties.ORGANISATION); + PropertyDto ipAddressProp = propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID_IP_ADDRESS); + + StringBuilder message = new StringBuilder("Server information:\n"); + message.append(" - ID : "); + addQuotedValue(serverIdProp, message); + message.append(" - Organisation : "); + addQuotedValue(organisationProp, message); + message.append(" - Registered IP : "); + addQuotedValue(ipAddressProp, message); + + logger.info(message.toString()); + } + } + + private void addQuotedValue(PropertyDto property, StringBuilder message) { + message.append("\""); + message.append(property.getValue()); + message.append("\"\n"); + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/startup/LogServerIdTest.java b/sonar-server/src/test/java/org/sonar/server/startup/LogServerIdTest.java new file mode 100644 index 00000000000..d926fefaf9f --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/startup/LogServerIdTest.java @@ -0,0 +1,86 @@ +/* + * 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.server.startup; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.slf4j.Logger; +import org.sonar.api.CoreProperties; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; + +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class LogServerIdTest { + + @Mock + private PropertiesDao dao; + + @Mock + private Logger logger; + + private LogServerId logServerId; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + + when(dao.selectGlobalProperty(CoreProperties.PERMANENT_SERVER_ID)).thenReturn(new PropertyDto().setValue("123456789")); + when(dao.selectGlobalProperty(CoreProperties.ORGANISATION)).thenReturn(new PropertyDto().setValue("SonarSource")); + when(dao.selectGlobalProperty(CoreProperties.SERVER_ID_IP_ADDRESS)).thenReturn(new PropertyDto().setValue("1.2.3.4")); + + logServerId = new LogServerId(dao); + } + + @Test + public void shouldLogMessage() { + logServerId.logServerId(logger); + + String log = + "Server information:\n" + + " - ID : \"123456789\"\n" + + " - Organisation : \"SonarSource\"\n" + + " - Registered IP : \"1.2.3.4\"\n"; + + verify(logger, times(1)).info(log); + } + + @Test + public void shouldNotLogMessage() { + when(dao.selectGlobalProperty(CoreProperties.PERMANENT_SERVER_ID)).thenReturn(null); + + logServerId.logServerId(logger); + + verify(logger, never()).info(anyString()); + } + + @Test + public void testStartMethod() { + // just to have 100% coverage ;-) + logServerId.start(); + } + +} |