Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LICENSE-96 implement support for staging and new server id format
- Loading branch information
Showing
17 changed files
with
1,307 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
server/sonar-server/src/main/java/org/sonar/server/platform/serverid/ServerIdChecksum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2018 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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 this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.platform.serverid; | |||
|
|||
import org.apache.commons.codec.digest.DigestUtils; | |||
import org.sonar.api.config.Configuration; | |||
|
|||
import static org.sonar.process.ProcessProperties.Property.JDBC_URL; | |||
|
|||
public class ServerIdChecksum { | |||
|
|||
private final Configuration config; | |||
private final JdbcUrlSanitizer jdbcUrlSanitizer; | |||
|
|||
public ServerIdChecksum(Configuration config, JdbcUrlSanitizer jdbcUrlSanitizer) { | |||
this.config = config; | |||
this.jdbcUrlSanitizer = jdbcUrlSanitizer; | |||
} | |||
|
|||
public String computeFor(String serverId) { | |||
String jdbcUrl = config.get(JDBC_URL.getKey()).orElseThrow(() -> new IllegalStateException("Missing JDBC URL")); | |||
return DigestUtils.sha256Hex(serverId + "|" + jdbcUrlSanitizer.sanitize(jdbcUrl)); | |||
} | |||
|
|||
} |
34 changes: 34 additions & 0 deletions
34
server/sonar-server/src/main/java/org/sonar/server/platform/serverid/ServerIdFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,34 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2018 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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 this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.platform.serverid; | |||
|
|||
import org.sonar.core.platform.ServerId; | |||
|
|||
public interface ServerIdFactory { | |||
/** | |||
* Create a new ServerId from scratch. | |||
*/ | |||
ServerId create(); | |||
|
|||
/** | |||
* Create a new ServerId from the current serverId. | |||
*/ | |||
ServerId create(ServerId currentServerId); | |||
} |
69 changes: 69 additions & 0 deletions
69
...er/sonar-server/src/main/java/org/sonar/server/platform/serverid/ServerIdFactoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,69 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2018 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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 this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.platform.serverid; | |||
|
|||
import com.google.common.annotations.VisibleForTesting; | |||
import java.nio.charset.StandardCharsets; | |||
import java.util.Locale; | |||
import java.util.zip.CRC32; | |||
import org.apache.commons.lang.StringUtils; | |||
import org.sonar.api.config.Configuration; | |||
import org.sonar.core.platform.ServerId; | |||
import org.sonar.core.util.UuidFactory; | |||
|
|||
import static org.sonar.process.ProcessProperties.Property.JDBC_URL; | |||
|
|||
public class ServerIdFactoryImpl implements ServerIdFactory { | |||
|
|||
private final Configuration config; | |||
private final UuidFactory uuidFactory; | |||
private final JdbcUrlSanitizer jdbcUrlSanitizer; | |||
|
|||
public ServerIdFactoryImpl(Configuration config, UuidFactory uuidFactory, JdbcUrlSanitizer jdbcUrlSanitizer) { | |||
this.config = config; | |||
this.uuidFactory = uuidFactory; | |||
this.jdbcUrlSanitizer = jdbcUrlSanitizer; | |||
} | |||
|
|||
@Override | |||
public ServerId create() { | |||
return ServerId.of(computeDatabaseId(), uuidFactory.create()); | |||
} | |||
|
|||
@Override | |||
public ServerId create(ServerId currentServerId) { | |||
return ServerId.of(computeDatabaseId(), currentServerId.getDatasetId()); | |||
} | |||
|
|||
private String computeDatabaseId() { | |||
String jdbcUrl = config.get(JDBC_URL.getKey()).orElseThrow(() -> new IllegalStateException("Missing JDBC URL")); | |||
return crc32Hex(jdbcUrlSanitizer.sanitize(jdbcUrl)); | |||
} | |||
|
|||
@VisibleForTesting | |||
static String crc32Hex(String str) { | |||
CRC32 crc32 = new CRC32(); | |||
crc32.update(str.getBytes(StandardCharsets.UTF_8)); | |||
long hash = crc32.getValue(); | |||
String s = Long.toHexString(hash).toUpperCase(Locale.ENGLISH); | |||
return StringUtils.leftPad(s, ServerId.DATABASE_ID_LENGTH, "0"); | |||
} | |||
|
|||
} |
Oops, something went wrong.