You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnalysisMetadataHolderImpl.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.ce.task.projectanalysis.analysis;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.util.Map;
  23. import javax.annotation.CheckForNull;
  24. import javax.annotation.Nullable;
  25. import org.sonar.ce.task.util.InitializedProperty;
  26. import org.sonar.db.component.BranchType;
  27. import org.sonar.server.project.Project;
  28. import org.sonar.server.qualityprofile.QualityProfile;
  29. import static com.google.common.base.Preconditions.checkState;
  30. import static java.util.Objects.requireNonNull;
  31. public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder {
  32. private static final String BRANCH_NOT_SET = "Branch has not been set";
  33. private final InitializedProperty<Boolean> organizationsEnabled = new InitializedProperty<>();
  34. private final InitializedProperty<Organization> organization = new InitializedProperty<>();
  35. private final InitializedProperty<String> uuid = new InitializedProperty<>();
  36. private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
  37. private final InitializedProperty<Analysis> baseProjectSnapshot = new InitializedProperty<>();
  38. private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
  39. private final InitializedProperty<Branch> branch = new InitializedProperty<>();
  40. private final InitializedProperty<String> pullRequestKey = new InitializedProperty<>();
  41. private final InitializedProperty<Project> project = new InitializedProperty<>();
  42. private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
  43. private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
  44. private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
  45. @Override
  46. public MutableAnalysisMetadataHolder setOrganizationsEnabled(boolean isOrganizationsEnabled) {
  47. checkState(!this.organizationsEnabled.isInitialized(), "Organization enabled flag has already been set");
  48. this.organizationsEnabled.setProperty(isOrganizationsEnabled);
  49. return this;
  50. }
  51. @Override
  52. public boolean isOrganizationsEnabled() {
  53. checkState(organizationsEnabled.isInitialized(), "Organizations enabled flag has not been set");
  54. return organizationsEnabled.getProperty();
  55. }
  56. @Override
  57. public MutableAnalysisMetadataHolder setOrganization(Organization organization) {
  58. checkState(!this.organization.isInitialized(), "Organization has already been set");
  59. requireNonNull(organization, "Organization can't be null");
  60. this.organization.setProperty(organization);
  61. return this;
  62. }
  63. @Override
  64. public Organization getOrganization() {
  65. checkState(organization.isInitialized(), "Organization has not been set");
  66. return organization.getProperty();
  67. }
  68. @Override
  69. public MutableAnalysisMetadataHolder setUuid(String s) {
  70. checkState(!uuid.isInitialized(), "Analysis uuid has already been set");
  71. requireNonNull(s, "Analysis uuid can't be null");
  72. this.uuid.setProperty(s);
  73. return this;
  74. }
  75. @Override
  76. public String getUuid() {
  77. checkState(uuid.isInitialized(), "Analysis uuid has not been set");
  78. return this.uuid.getProperty();
  79. }
  80. @Override
  81. public MutableAnalysisMetadataHolder setAnalysisDate(long date) {
  82. checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
  83. this.analysisDate.setProperty(date);
  84. return this;
  85. }
  86. @Override
  87. public long getAnalysisDate() {
  88. checkState(analysisDate.isInitialized(), "Analysis date has not been set");
  89. return this.analysisDate.getProperty();
  90. }
  91. @Override
  92. public boolean hasAnalysisDateBeenSet() {
  93. return analysisDate.isInitialized();
  94. }
  95. @Override
  96. public boolean isFirstAnalysis() {
  97. return getBaseAnalysis() == null;
  98. }
  99. @Override
  100. public MutableAnalysisMetadataHolder setBaseAnalysis(@Nullable Analysis baseAnalysis) {
  101. checkState(!this.baseProjectSnapshot.isInitialized(), "Base project snapshot has already been set");
  102. this.baseProjectSnapshot.setProperty(baseAnalysis);
  103. return this;
  104. }
  105. @Override
  106. @CheckForNull
  107. public Analysis getBaseAnalysis() {
  108. checkState(baseProjectSnapshot.isInitialized(), "Base project snapshot has not been set");
  109. return baseProjectSnapshot.getProperty();
  110. }
  111. @Override
  112. public MutableAnalysisMetadataHolder setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
  113. checkState(!this.crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has already been set");
  114. this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
  115. return this;
  116. }
  117. @Override
  118. public boolean isCrossProjectDuplicationEnabled() {
  119. checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
  120. return crossProjectDuplicationEnabled.getProperty();
  121. }
  122. @Override
  123. public MutableAnalysisMetadataHolder setBranch(Branch branch) {
  124. checkState(!this.branch.isInitialized(), "Branch has already been set");
  125. this.branch.setProperty(branch);
  126. return this;
  127. }
  128. @Override
  129. public Branch getBranch() {
  130. checkState(branch.isInitialized(), BRANCH_NOT_SET);
  131. return branch.getProperty();
  132. }
  133. @Override
  134. public MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey) {
  135. checkState(!this.pullRequestKey.isInitialized(), "Pull request key has already been set");
  136. this.pullRequestKey.setProperty(pullRequestKey);
  137. return this;
  138. }
  139. @Override
  140. public String getPullRequestKey() {
  141. checkState(pullRequestKey.isInitialized(), "Pull request key has not been set");
  142. return pullRequestKey.getProperty();
  143. }
  144. @Override
  145. public MutableAnalysisMetadataHolder setProject(Project project) {
  146. checkState(!this.project.isInitialized(), "Project has already been set");
  147. this.project.setProperty(project);
  148. return this;
  149. }
  150. @Override
  151. public Project getProject() {
  152. checkState(project.isInitialized(), "Project has not been set");
  153. return project.getProperty();
  154. }
  155. @Override
  156. public MutableAnalysisMetadataHolder setRootComponentRef(int rootComponentRef) {
  157. checkState(!this.rootComponentRef.isInitialized(), "Root component ref has already been set");
  158. this.rootComponentRef.setProperty(rootComponentRef);
  159. return this;
  160. }
  161. @Override
  162. public int getRootComponentRef() {
  163. checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
  164. return rootComponentRef.getProperty();
  165. }
  166. @Override
  167. public MutableAnalysisMetadataHolder setQProfilesByLanguage(Map<String, QualityProfile> qprofilesByLanguage) {
  168. checkState(!this.qProfilesPerLanguage.isInitialized(), "QProfiles by language has already been set");
  169. this.qProfilesPerLanguage.setProperty(ImmutableMap.copyOf(qprofilesByLanguage));
  170. return this;
  171. }
  172. @Override
  173. public Map<String, QualityProfile> getQProfilesByLanguage() {
  174. checkState(qProfilesPerLanguage.isInitialized(), "QProfiles by language has not been set");
  175. return qProfilesPerLanguage.getProperty();
  176. }
  177. @Override
  178. public MutableAnalysisMetadataHolder setScannerPluginsByKey(Map<String, ScannerPlugin> pluginsByKey) {
  179. checkState(!this.pluginsByKey.isInitialized(), "Plugins by key has already been set");
  180. this.pluginsByKey.setProperty(ImmutableMap.copyOf(pluginsByKey));
  181. return this;
  182. }
  183. @Override
  184. public Map<String, ScannerPlugin> getScannerPluginsByKey() {
  185. checkState(pluginsByKey.isInitialized(), "Plugins by key has not been set");
  186. return pluginsByKey.getProperty();
  187. }
  188. @Override
  189. public boolean isShortLivingBranch() {
  190. checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
  191. Branch prop = branch.getProperty();
  192. return prop != null && prop.getType() == BranchType.SHORT;
  193. }
  194. @Override
  195. public boolean isLongLivingBranch() {
  196. checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
  197. Branch prop = branch.getProperty();
  198. return prop != null && prop.getType() == BranchType.LONG;
  199. }
  200. @Override
  201. public boolean isPullRequest() {
  202. checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
  203. Branch prop = branch.getProperty();
  204. return prop != null && prop.getType() == BranchType.PULL_REQUEST;
  205. }
  206. }