aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java
blob: b580455032238d43e8ea4a21b8a813b5168d135e (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2012 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar 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.
 *
 * Sonar 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 Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.batch.bootstrapper;

import com.google.common.collect.Lists;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.batch.bootstrap.BootstrapModule;
import org.sonar.batch.bootstrap.Module;
import org.sonar.core.PicoUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Entry point for batch bootstrappers.
 *
 * @since 2.14
 */
public final class Batch {

  private LoggingConfiguration logging;
  private List<Object> components;
  private ProjectReactor projectReactor;

  private Batch(Builder builder) {
    components = Lists.newArrayList();
    components.addAll(builder.components);
    components.add(builder.environment);
    projectReactor = builder.projectReactor;
    if (builder.isEnableLoggingConfiguration()) {
      logging = LoggingConfiguration.create().setProperties((Map) projectReactor.getRoot().getProperties());
    }
  }

  public LoggingConfiguration getLoggingConfiguration() {
    return logging;
  }

  public Batch execute() {
    configureLogging();
    startBatch();
    return this;
  }

  private void configureLogging() {
    if (logging != null) {
      logging.configure();
    }
  }

  private void startBatch() {
    Module bootstrapModule = new BootstrapModule(projectReactor, components.toArray(new Object[components.size()])).init();
    try {
      bootstrapModule.start();
    } catch (RuntimeException e) {
      PicoUtils.propagateStartupException(e);
    } finally {
      try {
        bootstrapModule.stop();
      } catch (Exception e) {
        // never throw exceptions in a finally block
      }
    }
  }


  public static Builder builder() {
    return new Builder();
  }

  public static final class Builder {
    private ProjectReactor projectReactor;
    private EnvironmentInformation environment;
    private List components = Lists.newArrayList();
    private boolean enableLoggingConfiguration = true;

    private Builder() {
    }

    public Builder setProjectReactor(ProjectReactor projectReactor) {
      this.projectReactor = projectReactor;
      return this;
    }

    public Builder setEnvironment(EnvironmentInformation env) {
      this.environment = env;
      return this;
    }

    public Builder setComponents(List l) {
      this.components = l;
      return this;
    }

    public Builder addComponents(Object... components) {
      this.components.addAll(Arrays.asList(components));
      return this;
    }

    public Builder addComponent(Object component) {
      this.components.add(component);
      return this;
    }

    public boolean isEnableLoggingConfiguration() {
      return enableLoggingConfiguration;
    }

    /**
     * Logback is configured by default. It can be disabled, but n this case the batch bootstrapper must provide its
     * own implementation of SLF4J.
     */
    public Builder setEnableLoggingConfiguration(boolean b) {
      this.enableLoggingConfiguration = b;
      return this;
    }

    public Batch build() {
      if (projectReactor == null) {
        throw new IllegalStateException("ProjectReactor is not set");
      }
      if (environment == null) {
        throw new IllegalStateException("EnvironmentInfo is not set");
      }
      if (components == null) {
        throw new IllegalStateException("Batch components are not set");
      }
      return new Batch(this);
    }
  }
}