]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2603 Generate the server key on the fly.
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 12 Aug 2011 14:54:28 +0000 (16:54 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 12 Aug 2011 14:54:28 +0000 (16:54 +0200)
The server must not be restarted in order to compute its key.

sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java
sonar-server/src/main/java/org/sonar/server/platform/ServerKeyGenerator.java
sonar-server/src/main/java/org/sonar/server/startup/ServerMetadataPersister.java
sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java
sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties-result.xml
sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testDeleteProperties.xml
sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testSaveProperties-result.xml
sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties-result.xml
sonar-server/src/test/resources/org/sonar/server/startup/ServerMetadataPersisterTest/testUpdateExistingProperties.xml

index d156e567b5c28ebc9bead51af4e29dc6e350257a..e68874e9b605b740b882a62bced2f7dfbd79f9b1 100644 (file)
@@ -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) {
index c3df9e063d63b175665c2989f9d01106645411cd..67e73e569a7e60a3632517f4f10af8f51091f530 100644 (file)
@@ -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;
index 2cb351d5d99c5a2d958e30759a2978a294a9974d..cf6727d176901aa894368c66a5101f07e14b44bd 100644 (file)
@@ -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();
   }
index 4d5f0049b596afdeb1a4b8f6f50a51b44ab993cd..53eab63819adef261582515ec79c335fc9fa72dc 100644 (file)
@@ -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();
 
index 0ade6f49fc96b0d4b247fd2707ef55c933b3960c..f7ada567379677b34e5bbf114b32fa1080dc8b6e 100644 (file)
@@ -3,5 +3,5 @@
   <properties id="1" prop_key="other" resource_id="[null]" text_value="some text" user_id="[null]"/>
 
   <!-- not null property -->
-  <properties id="5" prop_key="sonar.core.startTime" resource_id="[null]" text_value="2010-05-18T17:59:00+0000" user_id="[null]"/>
+  <properties id="4" prop_key="sonar.core.startTime" resource_id="[null]" text_value="2010-05-18T17:59:00+0000" user_id="[null]"/>
 </dataset>
\ No newline at end of file
index 8a3add2a585381e336b19f2f4f7e194ac6d69b46..ec292a1388df8f2663de0e8a4649805abaa458e6 100644 (file)
@@ -4,7 +4,6 @@
 
   <properties id="2" prop_key="sonar.core.id" resource_id="[null]" text_value="123" user_id="[null]"/>
   <properties id="3" prop_key="sonar.core.version" resource_id="[null]" text_value="2.2" user_id="[null]"/>
-  <properties id="4" prop_key="sonar.serverKey.secured" resource_id="[null]" text_value="1abcdef" user_id="[null]"/>
-  <properties id="5" prop_key="sonar.core.startTime" resource_id="[null]" text_value="2010-05-18T17:59:00+0000" user_id="[null]"/>
+  <properties id="4" prop_key="sonar.core.startTime" resource_id="[null]" text_value="2010-05-18T17:59:00+0000" user_id="[null]"/>
 
 </dataset>
\ No newline at end of file
index 8a3add2a585381e336b19f2f4f7e194ac6d69b46..ec292a1388df8f2663de0e8a4649805abaa458e6 100644 (file)
@@ -4,7 +4,6 @@
 
   <properties id="2" prop_key="sonar.core.id" resource_id="[null]" text_value="123" user_id="[null]"/>
   <properties id="3" prop_key="sonar.core.version" resource_id="[null]" text_value="2.2" user_id="[null]"/>
-  <properties id="4" prop_key="sonar.serverKey.secured" resource_id="[null]" text_value="1abcdef" user_id="[null]"/>
-  <properties id="5" prop_key="sonar.core.startTime" resource_id="[null]" text_value="2010-05-18T17:59:00+0000" user_id="[null]"/>
+  <properties id="4" prop_key="sonar.core.startTime" resource_id="[null]" text_value="2010-05-18T17:59:00+0000" user_id="[null]"/>
 
 </dataset>
\ No newline at end of file
index 5d4648e011aefb26c2f281a21b2998de7f374215..365acc0ccd3b465afdb25c816331881a55e089f1 100644 (file)
@@ -5,5 +5,5 @@
   <properties id="2" prop_key="sonar.core.id" resource_id="[null]" text_value="123" user_id="[null]"/>
   <properties id="3" prop_key="sonar.core.version" resource_id="[null]" text_value="2.2" user_id="[null]"/>
   <properties id="4" prop_key="sonar.core.startTime" resource_id="[null]" text_value="2010-05-18T17:59:00+0000" user_id="[null]"/>
-  <properties id="5" prop_key="sonar.serverKey.secured" resource_id="[null]" text_value="1abcdef" user_id="[null]"/>
+
 </dataset>
\ No newline at end of file
index bf4537c513d656fa72b43d98ba9c5162fd08d753..824615a0cc51b456783d84432898d2e2f5c34a54 100644 (file)
@@ -5,6 +5,5 @@
   <properties id="2" prop_key="sonar.core.id" resource_id="[null]" text_value="65" user_id="[null]"/>
   <properties id="3" prop_key="sonar.core.version" resource_id="[null]" text_value="1.9" user_id="[null]"/>
   <properties id="4" prop_key="sonar.core.startTime" resource_id="[null]" text_value="2008-04-18T17:59:00+0000" user_id="[null]"/>
-  <properties id="5" prop_key="sonar.serverKey.secured" resource_id="[null]" text_value="other" user_id="[null]"/>
 
 </dataset>
\ No newline at end of file