From: Simon Brandhof Date: Fri, 12 Aug 2011 14:54:28 +0000 (+0200) Subject: SONAR-2603 Generate the server key on the fly. X-Git-Tag: 2.11^2~161 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d1390ffe5692ca3a4d45e430288b34f41bd89354;p=sonarqube.git SONAR-2603 Generate the server key on the fly. The server must not be restarted in order to compute its key. --- diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java index d156e567b5c..e68874e9b60 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java @@ -22,8 +22,8 @@ package org.sonar.server.platform; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; -import org.sonar.api.database.configuration.Property; import org.sonar.api.database.DatabaseSession; +import org.sonar.api.database.configuration.Property; import org.sonar.api.platform.Server; import org.sonar.jpa.session.DatabaseSessionFactory; @@ -38,7 +38,6 @@ public final class ServerImpl extends Server { private String id; private String version; private final Date startedAt; - private String key; /** * This component can't use Configuration because of startup sequence. It must be started before plugins. @@ -59,7 +58,6 @@ public final class ServerImpl extends Server { public void start() { try { id = new SimpleDateFormat("yyyyMMddHHmmss").format(startedAt); - key = initKey(); version = loadVersionFromManifest("/META-INF/maven/org.codehaus.sonar/sonar-plugin-api/pom.properties"); if (StringUtils.isBlank(version)) { throw new ServerStartException("Unknown Sonar version"); @@ -70,15 +68,13 @@ public final class ServerImpl extends Server { } } - private String initKey() { + public String getKey() { DatabaseSession session = dbSessionFactory.getSession(); Property organization = session.getSingleResult(Property.class, "key", CoreProperties.ORGANIZATION); Property baseUrl = session.getSingleResult(Property.class, "key", CoreProperties.SERVER_BASE_URL); - Property previousKey = session.getSingleResult(Property.class, "key", CoreProperties.SERVER_KEY); return keyGenerator.generate( - organization!=null ? organization.getValue() : null, - baseUrl != null ? baseUrl.getValue() : null, - previousKey!=null ? previousKey.getValue() : null); + organization != null ? organization.getValue() : null, + baseUrl != null ? baseUrl.getValue() : null); } public String getId() { @@ -117,10 +113,6 @@ public final class ServerImpl extends Server { return null; } - public String getKey() { - return key; - } - @Override public boolean equals(Object o) { if (this == o) { diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerKeyGenerator.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerKeyGenerator.java index c3df9e063d6..67e73e569a7 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerKeyGenerator.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerKeyGenerator.java @@ -51,10 +51,6 @@ public class ServerKeyGenerator { } public String generate(String organization, String baseUrl) { - return generate(organization, baseUrl, null); - } - - public String generate(String organization, String baseUrl, String previousKey) { String key = null; if (StringUtils.isNotBlank(organization) && StringUtils.isNotBlank(baseUrl)) { InetAddress address = extractAddressFromUrl(baseUrl); @@ -62,7 +58,6 @@ public class ServerKeyGenerator { key = toKey(organization, address); } } - log(previousKey, key); return key; } @@ -92,20 +87,6 @@ public class ServerKeyGenerator { return acceptPrivateAddress || (!address.isLoopbackAddress() && !address.isLinkLocalAddress()); } - void log(String previousKey, String newKey) { - if (StringUtils.isNotBlank(newKey) && StringUtils.isNotBlank(previousKey) && !previousKey.equals(newKey)) { - LoggerFactory.getLogger(getClass()).warn("Server key has changed. Licensed plugins may be disabled. " - + "Please check the organization name (Settings page) and the server IP addresses."); - } - if (StringUtils.isNotBlank(newKey)) { - Logs.INFO.info("Server key: " + newKey); - - } else if (StringUtils.isNotBlank(previousKey)) { - LoggerFactory.getLogger(getClass()).warn("Server key has been removed. Licensed plugins may be disabled. " - + "Please check the organization name (Settings page) and the server IP addresses."); - } - } - InetAddress extractAddressFromUrl(String baseUrl) { if (StringUtils.isBlank(baseUrl)) { return null; diff --git a/sonar-server/src/main/java/org/sonar/server/startup/ServerMetadataPersister.java b/sonar-server/src/main/java/org/sonar/server/startup/ServerMetadataPersister.java index 2cb351d5d99..cf6727d1769 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/ServerMetadataPersister.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/ServerMetadataPersister.java @@ -39,7 +39,6 @@ public class ServerMetadataPersister { public void start() { setProperty(CoreProperties.SERVER_ID, server.getId()); setProperty(CoreProperties.SERVER_VERSION, server.getVersion()); - setProperty(CoreProperties.SERVER_KEY, server.getKey()); setProperty(CoreProperties.SERVER_STARTTIME, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(server.getStartedAt())); session.commit(); } diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java index 4d5f0049b59..53eab63819a 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java @@ -72,7 +72,7 @@ public class ServerImplTest extends AbstractDbUnitTestCase { setupData("shouldGenerateKey"); ServerKeyGenerator keyGenerator = mock(ServerKeyGenerator.class); - when(keyGenerator.generate("World Company", "http://192.168.0.1", null)).thenReturn("abcde"); + when(keyGenerator.generate("World Company", "http://192.168.0.1")).thenReturn("abcde"); ServerImpl server = new ServerImpl(getSessionFactory(), keyGenerator, new Date()); server.start(); diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties-result.xml b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties-result.xml index 0ade6f49fc9..f7ada567379 100644 --- a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties-result.xml +++ b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties-result.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties.xml b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties.xml index 8a3add2a585..ec292a1388d 100644 --- a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties.xml +++ b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties.xml @@ -4,7 +4,6 @@ - - + \ No newline at end of file diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testSaveProperties-result.xml b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testSaveProperties-result.xml index 8a3add2a585..ec292a1388d 100644 --- a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testSaveProperties-result.xml +++ b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testSaveProperties-result.xml @@ -4,7 +4,6 @@ - - + \ No newline at end of file diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties-result.xml b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties-result.xml index 5d4648e011a..365acc0ccd3 100644 --- a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties-result.xml +++ b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties-result.xml @@ -5,5 +5,5 @@ - + \ No newline at end of file diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties.xml b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties.xml index bf4537c513d..824615a0cc5 100644 --- a/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties.xml +++ b/sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties.xml @@ -5,6 +5,5 @@ - \ No newline at end of file