3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.edition;
22 import com.google.common.collect.ImmutableSet;
23 import java.util.Arrays;
25 import java.util.Optional;
26 import org.picocontainer.Startable;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.property.InternalPropertiesDao;
31 import static com.google.common.base.Preconditions.checkArgument;
32 import static com.google.common.base.Preconditions.checkState;
33 import static java.util.Objects.requireNonNull;
34 import static java.util.Optional.empty;
35 import static org.sonar.server.edition.EditionManagementState.PendingStatus.AUTOMATIC_IN_PROGRESS;
36 import static org.sonar.server.edition.EditionManagementState.PendingStatus.AUTOMATIC_READY;
37 import static org.sonar.server.edition.EditionManagementState.PendingStatus.MANUAL_IN_PROGRESS;
38 import static org.sonar.server.edition.EditionManagementState.PendingStatus.NONE;
40 public class StandaloneEditionManagementStateImpl implements MutableEditionManagementState, Startable {
41 private static final String CURRENT_EDITION_KEY = "currentEditionKey";
42 private static final String PENDING_INSTALLATION_STATUS = "pendingInstallStatus";
43 private static final String PENDING_EDITION_KEY = "pendingEditionKey";
44 private static final String PENDING_LICENSE = "pendingLicense";
46 private final DbClient dbClient;
47 private String currentEditionKey;
48 private PendingStatus pendingInstallationStatus;
49 private String pendingEditionKey;
50 private String pendingLicense;
52 public StandaloneEditionManagementStateImpl(DbClient dbClient) {
53 this.dbClient = dbClient;
58 try (DbSession dbSession = dbClient.openSession(false)) {
59 // load current state value
60 Map<String, Optional<String>> internalPropertyValues = dbClient.internalPropertiesDao().selectByKeys(dbSession,
61 ImmutableSet.of(CURRENT_EDITION_KEY, PENDING_INSTALLATION_STATUS, PENDING_EDITION_KEY, PENDING_LICENSE));
62 this.currentEditionKey = internalPropertyValues.getOrDefault(CURRENT_EDITION_KEY, empty())
63 .map(StandaloneEditionManagementStateImpl::emptyToNull)
65 this.pendingInstallationStatus = internalPropertyValues.getOrDefault(PENDING_INSTALLATION_STATUS, empty())
66 .map(StandaloneEditionManagementStateImpl::emptyToNull)
67 .map(PendingStatus::valueOf)
69 this.pendingEditionKey = internalPropertyValues.getOrDefault(PENDING_EDITION_KEY, empty())
70 .map(StandaloneEditionManagementStateImpl::emptyToNull)
72 this.pendingLicense = internalPropertyValues.getOrDefault(PENDING_LICENSE, empty())
73 .map(StandaloneEditionManagementStateImpl::emptyToNull)
84 public Optional<String> getCurrentEditionKey() {
86 return Optional.ofNullable(currentEditionKey);
90 public PendingStatus getPendingInstallationStatus() {
92 return pendingInstallationStatus;
96 public Optional<String> getPendingEditionKey() {
98 return Optional.ofNullable(pendingEditionKey);
102 public Optional<String> getPendingLicense() {
104 return Optional.ofNullable(pendingLicense);
108 public synchronized PendingStatus startAutomaticInstall(License license) {
110 checkLicense(license);
111 changeStatusToFrom(AUTOMATIC_IN_PROGRESS, NONE);
112 this.pendingLicense = license.getContent();
113 this.pendingEditionKey = license.getEditionKey();
115 return this.pendingInstallationStatus;
119 public synchronized PendingStatus startManualInstall(License license) {
121 checkLicense(license);
122 changeStatusToFrom(MANUAL_IN_PROGRESS, NONE);
123 this.pendingLicense = license.getContent();
124 this.pendingEditionKey = license.getEditionKey();
125 this.pendingInstallationStatus = MANUAL_IN_PROGRESS;
127 return this.pendingInstallationStatus;
131 public synchronized PendingStatus newEditionWithoutInstall(String newEditionKey) {
133 requireNonNull(newEditionKey, "newEditionKey can't be null");
134 checkArgument(!newEditionKey.isEmpty(), "newEditionKey can't be empty");
135 changeStatusToFrom(NONE, NONE);
136 this.currentEditionKey = newEditionKey;
138 return this.pendingInstallationStatus;
142 public synchronized PendingStatus automaticInstallReady() {
144 changeStatusToFrom(AUTOMATIC_READY, AUTOMATIC_IN_PROGRESS);
146 return this.pendingInstallationStatus;
150 public synchronized PendingStatus finalizeInstallation() {
152 changeStatusToFrom(NONE, AUTOMATIC_READY, MANUAL_IN_PROGRESS);
154 this.pendingInstallationStatus = NONE;
155 this.currentEditionKey = this.pendingEditionKey;
156 this.pendingEditionKey = null;
157 this.pendingLicense = null;
159 return this.pendingInstallationStatus;
162 private void ensureStarted() {
163 checkState(pendingInstallationStatus != null, "%s is not started", getClass().getSimpleName());
166 private void changeStatusToFrom(PendingStatus newStatus, PendingStatus... validPendingStatuses) {
167 checkState(Arrays.stream(validPendingStatuses).anyMatch(s -> s == pendingInstallationStatus),
168 "Can't move to %s when status is %s (should be any of %s)",
169 newStatus, pendingInstallationStatus, Arrays.toString(validPendingStatuses));
170 this.pendingInstallationStatus = newStatus;
173 private void persistProperties() {
174 try (DbSession dbSession = dbClient.openSession(false)) {
175 InternalPropertiesDao internalPropertiesDao = dbClient.internalPropertiesDao();
176 if (pendingInstallationStatus == NONE) {
177 internalPropertiesDao.saveAsEmpty(dbSession, PENDING_EDITION_KEY);
178 internalPropertiesDao.saveAsEmpty(dbSession, PENDING_LICENSE);
180 internalPropertiesDao.save(dbSession, PENDING_EDITION_KEY, pendingEditionKey);
181 internalPropertiesDao.save(dbSession, PENDING_LICENSE, pendingLicense);
183 if (currentEditionKey == null) {
184 internalPropertiesDao.saveAsEmpty(dbSession, CURRENT_EDITION_KEY);
186 internalPropertiesDao.save(dbSession, CURRENT_EDITION_KEY, currentEditionKey);
188 internalPropertiesDao.save(dbSession, PENDING_INSTALLATION_STATUS, pendingInstallationStatus.name());
193 private static void checkLicense(License license) {
194 requireNonNull(license, "license can't be null");
197 private static String emptyToNull(String s) {
198 return s.isEmpty() ? null : s;