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;
39 import static org.sonar.server.edition.EditionManagementState.PendingStatus.UNINSTALL_IN_PROGRESS;
41 public class StandaloneEditionManagementStateImpl implements MutableEditionManagementState, Startable {
42 private static final String CURRENT_EDITION_KEY = "currentEditionKey";
43 private static final String PENDING_INSTALLATION_STATUS = "pendingInstallStatus";
44 private static final String PENDING_EDITION_KEY = "pendingEditionKey";
45 private static final String PENDING_LICENSE = "pendingLicense";
47 private final DbClient dbClient;
48 private String currentEditionKey;
49 private PendingStatus pendingInstallationStatus;
50 private String pendingEditionKey;
51 private String pendingLicense;
53 public StandaloneEditionManagementStateImpl(DbClient dbClient) {
54 this.dbClient = dbClient;
59 try (DbSession dbSession = dbClient.openSession(false)) {
60 // load current state value
61 Map<String, Optional<String>> internalPropertyValues = dbClient.internalPropertiesDao().selectByKeys(dbSession,
62 ImmutableSet.of(CURRENT_EDITION_KEY, PENDING_INSTALLATION_STATUS, PENDING_EDITION_KEY, PENDING_LICENSE));
63 this.currentEditionKey = internalPropertyValues.getOrDefault(CURRENT_EDITION_KEY, empty())
64 .map(StandaloneEditionManagementStateImpl::emptyToNull)
66 this.pendingInstallationStatus = internalPropertyValues.getOrDefault(PENDING_INSTALLATION_STATUS, empty())
67 .map(StandaloneEditionManagementStateImpl::emptyToNull)
68 .map(PendingStatus::valueOf)
70 this.pendingEditionKey = internalPropertyValues.getOrDefault(PENDING_EDITION_KEY, empty())
71 .map(StandaloneEditionManagementStateImpl::emptyToNull)
73 this.pendingLicense = internalPropertyValues.getOrDefault(PENDING_LICENSE, empty())
74 .map(StandaloneEditionManagementStateImpl::emptyToNull)
85 public Optional<String> getCurrentEditionKey() {
87 return Optional.ofNullable(currentEditionKey);
91 public PendingStatus getPendingInstallationStatus() {
93 return pendingInstallationStatus;
97 public Optional<String> getPendingEditionKey() {
99 return Optional.ofNullable(pendingEditionKey);
103 public Optional<String> getPendingLicense() {
105 return Optional.ofNullable(pendingLicense);
109 public synchronized PendingStatus startAutomaticInstall(License license) {
111 checkLicense(license);
112 changeStatusToFrom(AUTOMATIC_IN_PROGRESS, NONE);
113 this.pendingLicense = license.getContent();
114 this.pendingEditionKey = license.getEditionKey();
116 return this.pendingInstallationStatus;
120 public synchronized PendingStatus startManualInstall(License license) {
122 checkLicense(license);
123 changeStatusToFrom(MANUAL_IN_PROGRESS, NONE);
124 this.pendingLicense = license.getContent();
125 this.pendingEditionKey = license.getEditionKey();
126 this.pendingInstallationStatus = MANUAL_IN_PROGRESS;
128 return this.pendingInstallationStatus;
132 public synchronized PendingStatus newEditionWithoutInstall(String newEditionKey) {
134 requireNonNull(newEditionKey, "newEditionKey can't be null");
135 checkArgument(!newEditionKey.isEmpty(), "newEditionKey can't be empty");
136 changeStatusToFrom(NONE, NONE);
137 this.currentEditionKey = newEditionKey;
139 return this.pendingInstallationStatus;
143 public synchronized PendingStatus automaticInstallReady() {
145 changeStatusToFrom(AUTOMATIC_READY, AUTOMATIC_IN_PROGRESS);
147 return this.pendingInstallationStatus;
151 public synchronized PendingStatus finalizeInstallation() {
153 changeStatusToFrom(NONE, AUTOMATIC_READY, MANUAL_IN_PROGRESS, UNINSTALL_IN_PROGRESS);
155 this.pendingInstallationStatus = NONE;
156 this.currentEditionKey = this.pendingEditionKey;
157 this.pendingEditionKey = null;
158 this.pendingLicense = null;
160 return this.pendingInstallationStatus;
164 public synchronized PendingStatus uninstall() {
166 changeStatusToFrom(UNINSTALL_IN_PROGRESS, NONE);
167 checkState(currentEditionKey != null, "There is no edition currently installed");
169 this.pendingInstallationStatus = UNINSTALL_IN_PROGRESS;
170 this.pendingEditionKey = null;
171 this.pendingLicense = null;
172 this.currentEditionKey = null;
174 return this.pendingInstallationStatus;
177 private void ensureStarted() {
178 checkState(pendingInstallationStatus != null, "%s is not started", getClass().getSimpleName());
181 private void changeStatusToFrom(PendingStatus newStatus, PendingStatus... validPendingStatuses) {
182 checkState(Arrays.stream(validPendingStatuses).anyMatch(s -> s == pendingInstallationStatus),
183 "Can't move to %s when status is %s (should be any of %s)",
184 newStatus, pendingInstallationStatus, Arrays.toString(validPendingStatuses));
185 this.pendingInstallationStatus = newStatus;
188 private void persistProperties() {
189 try (DbSession dbSession = dbClient.openSession(false)) {
190 InternalPropertiesDao internalPropertiesDao = dbClient.internalPropertiesDao();
191 if (pendingInstallationStatus == NONE || pendingInstallationStatus == UNINSTALL_IN_PROGRESS) {
192 internalPropertiesDao.saveAsEmpty(dbSession, PENDING_EDITION_KEY);
193 internalPropertiesDao.saveAsEmpty(dbSession, PENDING_LICENSE);
195 internalPropertiesDao.save(dbSession, PENDING_EDITION_KEY, pendingEditionKey);
196 internalPropertiesDao.save(dbSession, PENDING_LICENSE, pendingLicense);
198 if (currentEditionKey == null) {
199 internalPropertiesDao.saveAsEmpty(dbSession, CURRENT_EDITION_KEY);
201 internalPropertiesDao.save(dbSession, CURRENT_EDITION_KEY, currentEditionKey);
203 internalPropertiesDao.save(dbSession, PENDING_INSTALLATION_STATUS, pendingInstallationStatus.name());
208 private static void checkLicense(License license) {
209 requireNonNull(license, "license can't be null");
212 private static String emptyToNull(String s) {
213 return s.isEmpty() ? null : s;