The server must not be restarted in order to compute its key.
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;
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.
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");
}
}
- 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() {
return null;
}
- public String getKey() {
- return key;
- }
-
@Override
public boolean equals(Object o) {
if (this == o) {
}
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);
key = toKey(organization, address);
}
}
- log(previousKey, key);
return key;
}
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;
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();
}
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();
<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
<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
<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
<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
<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