3 * Copyright (C) 2009-2019 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.ce.task.projectanalysis.analysis;
22 import com.google.common.collect.ImmutableMap;
24 import java.util.Optional;
25 import javax.annotation.CheckForNull;
26 import javax.annotation.Nullable;
27 import org.sonar.ce.task.util.InitializedProperty;
28 import org.sonar.db.component.BranchType;
29 import org.sonar.server.project.Project;
30 import org.sonar.server.qualityprofile.QualityProfile;
32 import static com.google.common.base.Preconditions.checkState;
33 import static java.util.Objects.requireNonNull;
34 import static org.apache.commons.lang.StringUtils.defaultIfBlank;
36 public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder {
37 private static final String BRANCH_NOT_SET = "Branch has not been set";
38 private final InitializedProperty<Boolean> organizationsEnabled = new InitializedProperty<>();
39 private final InitializedProperty<Organization> organization = new InitializedProperty<>();
40 private final InitializedProperty<String> uuid = new InitializedProperty<>();
41 private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
42 private final InitializedProperty<Analysis> baseProjectSnapshot = new InitializedProperty<>();
43 private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
44 private final InitializedProperty<Branch> branch = new InitializedProperty<>();
45 private final InitializedProperty<String> pullRequestKey = new InitializedProperty<>();
46 private final InitializedProperty<Project> project = new InitializedProperty<>();
47 private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
48 private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
49 private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
50 private final InitializedProperty<String> scmRevision = new InitializedProperty<>();
53 public MutableAnalysisMetadataHolder setOrganizationsEnabled(boolean isOrganizationsEnabled) {
54 checkState(!this.organizationsEnabled.isInitialized(), "Organization enabled flag has already been set");
55 this.organizationsEnabled.setProperty(isOrganizationsEnabled);
60 public boolean isOrganizationsEnabled() {
61 checkState(organizationsEnabled.isInitialized(), "Organizations enabled flag has not been set");
62 return organizationsEnabled.getProperty();
66 public MutableAnalysisMetadataHolder setOrganization(Organization organization) {
67 checkState(!this.organization.isInitialized(), "Organization has already been set");
68 requireNonNull(organization, "Organization can't be null");
69 this.organization.setProperty(organization);
74 public Organization getOrganization() {
75 checkState(organization.isInitialized(), "Organization has not been set");
76 return organization.getProperty();
80 public MutableAnalysisMetadataHolder setUuid(String s) {
81 checkState(!uuid.isInitialized(), "Analysis uuid has already been set");
82 requireNonNull(s, "Analysis uuid can't be null");
83 this.uuid.setProperty(s);
88 public String getUuid() {
89 checkState(uuid.isInitialized(), "Analysis uuid has not been set");
90 return this.uuid.getProperty();
94 public MutableAnalysisMetadataHolder setAnalysisDate(long date) {
95 checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
96 this.analysisDate.setProperty(date);
101 public long getAnalysisDate() {
102 checkState(analysisDate.isInitialized(), "Analysis date has not been set");
103 return this.analysisDate.getProperty();
107 public boolean hasAnalysisDateBeenSet() {
108 return analysisDate.isInitialized();
112 public boolean isFirstAnalysis() {
113 return getBaseAnalysis() == null;
117 public MutableAnalysisMetadataHolder setBaseAnalysis(@Nullable Analysis baseAnalysis) {
118 checkState(!this.baseProjectSnapshot.isInitialized(), "Base project snapshot has already been set");
119 this.baseProjectSnapshot.setProperty(baseAnalysis);
125 public Analysis getBaseAnalysis() {
126 checkState(baseProjectSnapshot.isInitialized(), "Base project snapshot has not been set");
127 return baseProjectSnapshot.getProperty();
131 public MutableAnalysisMetadataHolder setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
132 checkState(!this.crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has already been set");
133 this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
138 public boolean isCrossProjectDuplicationEnabled() {
139 checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
140 return crossProjectDuplicationEnabled.getProperty();
144 public MutableAnalysisMetadataHolder setBranch(Branch branch) {
145 checkState(!this.branch.isInitialized(), "Branch has already been set");
146 this.branch.setProperty(branch);
151 public Branch getBranch() {
152 checkState(branch.isInitialized(), BRANCH_NOT_SET);
153 return branch.getProperty();
157 public MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey) {
158 checkState(!this.pullRequestKey.isInitialized(), "Pull request key has already been set");
159 this.pullRequestKey.setProperty(pullRequestKey);
164 public String getPullRequestKey() {
165 checkState(pullRequestKey.isInitialized(), "Pull request key has not been set");
166 return pullRequestKey.getProperty();
170 public MutableAnalysisMetadataHolder setProject(Project project) {
171 checkState(!this.project.isInitialized(), "Project has already been set");
172 this.project.setProperty(project);
177 public Project getProject() {
178 checkState(project.isInitialized(), "Project has not been set");
179 return project.getProperty();
183 public MutableAnalysisMetadataHolder setRootComponentRef(int rootComponentRef) {
185 checkState(!this.rootComponentRef.isInitialized(), "Root component ref has already been set");
186 this.rootComponentRef.setProperty(rootComponentRef);
191 public int getRootComponentRef() {
192 checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
193 return rootComponentRef.getProperty();
197 public MutableAnalysisMetadataHolder setQProfilesByLanguage(Map<String, QualityProfile> qprofilesByLanguage) {
198 checkState(!this.qProfilesPerLanguage.isInitialized(), "QProfiles by language has already been set");
199 this.qProfilesPerLanguage.setProperty(ImmutableMap.copyOf(qprofilesByLanguage));
204 public Map<String, QualityProfile> getQProfilesByLanguage() {
205 checkState(qProfilesPerLanguage.isInitialized(), "QProfiles by language has not been set");
206 return qProfilesPerLanguage.getProperty();
210 public MutableAnalysisMetadataHolder setScannerPluginsByKey(Map<String, ScannerPlugin> pluginsByKey) {
211 checkState(!this.pluginsByKey.isInitialized(), "Plugins by key has already been set");
212 this.pluginsByKey.setProperty(ImmutableMap.copyOf(pluginsByKey));
217 public Map<String, ScannerPlugin> getScannerPluginsByKey() {
218 checkState(pluginsByKey.isInitialized(), "Plugins by key has not been set");
219 return pluginsByKey.getProperty();
223 public MutableAnalysisMetadataHolder setScmRevision(@Nullable String s) {
224 checkState(!this.scmRevision.isInitialized(), "ScmRevision has already been set");
225 this.scmRevision.setProperty(defaultIfBlank(s, null));
230 public Optional<String> getScmRevision() {
231 if (!scmRevision.isInitialized()) {
232 return Optional.empty();
234 return Optional.ofNullable(scmRevision.getProperty());
238 public boolean isShortLivingBranch() {
239 checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
240 Branch prop = branch.getProperty();
241 return prop != null && prop.getType() == BranchType.SHORT;
245 public boolean isLongLivingBranch() {
246 checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
247 Branch prop = branch.getProperty();
248 return prop != null && prop.getType() == BranchType.LONG;
252 public boolean isPullRequest() {
253 checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
254 Branch prop = branch.getProperty();
255 return prop != null && prop.getType() == BranchType.PULL_REQUEST;