aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectSyncContainer.java
blob: 816091786b32f79607c1a422f33f94ba45653d79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact 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.batch.cache;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import org.sonar.api.CoreProperties;
import org.sonar.batch.analysis.AnalysisProperties;
import org.sonar.batch.analysis.DefaultAnalysisMode;
import org.sonar.batch.bootstrap.GlobalProperties;
import org.sonar.batch.cache.WSLoader.LoadStrategy;
import org.sonar.batch.repository.DefaultProjectRepositoriesLoader;
import org.sonar.batch.repository.DefaultQualityProfileLoader;
import org.sonar.batch.repository.DefaultServerIssuesLoader;
import org.sonar.batch.repository.ProjectRepositoriesLoader;
import org.sonar.batch.repository.QualityProfileLoader;
import org.sonar.batch.repository.ServerIssuesLoader;
import org.sonar.batch.repository.user.UserRepositoryLoader;
import org.sonar.batch.rule.ActiveRulesLoader;
import org.sonar.batch.rule.DefaultActiveRulesLoader;
import org.sonar.batch.rule.DefaultRulesLoader;
import org.sonar.batch.rule.RulesLoader;
import org.sonar.core.platform.ComponentContainer;

public class ProjectSyncContainer extends ComponentContainer {
  private final boolean force;
  private final String projectKey;

  public ProjectSyncContainer(ComponentContainer globalContainer, @Nullable String projectKey, boolean force) {
    super(globalContainer);
    this.projectKey = projectKey;
    this.force = force;
  }

  @Override
  public void doBeforeStart() {
    addComponents();
  }

  @Override
  public void doAfterStart() {
    if (projectKey != null) {
      getComponentByType(ProjectCacheSynchronizer.class).load(projectKey, force);
    } else {
      getComponentByType(NonAssociatedCacheSynchronizer.class).execute(force);
    }
  }

  private static DefaultAnalysisMode createIssuesAnalysisMode(@Nullable String projectKey) {
    Map<String, String> props = new HashMap<>();
    props.put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES);
    if (projectKey != null) {
      props.put(CoreProperties.PROJECT_KEY_PROPERTY, projectKey);
    }
    GlobalProperties globalProps = new GlobalProperties(props);
    AnalysisProperties analysisProps = new AnalysisProperties(props);
    return new DefaultAnalysisMode(globalProps, analysisProps);
  }

  private void addComponents() {
    add(new StrategyWSLoaderProvider(LoadStrategy.SERVER_ONLY),
      new ProjectKeySupplier(projectKey),
      projectKey != null ? ProjectCacheSynchronizer.class : NonAssociatedCacheSynchronizer.class,
      UserRepositoryLoader.class,
      new ProjectPersistentCacheProvider(),
      createIssuesAnalysisMode(projectKey));

    addIfMissing(DefaultProjectCacheStatus.class, ProjectCacheStatus.class);
    addIfMissing(DefaultProjectRepositoriesLoader.class, ProjectRepositoriesLoader.class);
    addIfMissing(DefaultServerIssuesLoader.class, ServerIssuesLoader.class);
    addIfMissing(DefaultQualityProfileLoader.class, QualityProfileLoader.class);
    addIfMissing(DefaultRulesLoader.class, RulesLoader.class);
    addIfMissing(DefaultActiveRulesLoader.class, ActiveRulesLoader.class);
  }
}