001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.bootstrapper;
021    
022    import com.google.common.collect.Lists;
023    import com.google.common.collect.Maps;
024    import org.sonar.batch.bootstrap.BootstrapContainer;
025    
026    import java.util.Collections;
027    import java.util.List;
028    import java.util.Map;
029    
030    /**
031     * Entry point for sonar-runner 2.1.
032     *
033     * @since 2.14
034     */
035    public final class Batch {
036    
037      private boolean started = false;
038      private LoggingConfiguration logging;
039      private List<Object> components;
040      private Map<String, String> bootstrapProperties = Maps.newHashMap();
041      private BootstrapContainer bootstrapContainer;
042    
043      private Batch(Builder builder) {
044        components = Lists.newArrayList();
045        components.addAll(builder.components);
046        if (builder.environment != null) {
047          components.add(builder.environment);
048        }
049        if (builder.bootstrapProperties != null) {
050          bootstrapProperties.putAll(builder.bootstrapProperties);
051        }
052        if (builder.isEnableLoggingConfiguration()) {
053          logging = LoggingConfiguration.create(builder.environment).setProperties(bootstrapProperties);
054        }
055      }
056    
057      public LoggingConfiguration getLoggingConfiguration() {
058        return logging;
059      }
060    
061      /**
062       * @deprecated since 4.4 use {@link #start()}, {@link #executeTask(Map)} and then {@link #stop()}
063       */
064      @Deprecated
065      public synchronized Batch execute() {
066        configureLogging();
067        start().executeTask(bootstrapProperties).stop();
068        return this;
069      }
070    
071      /**
072       * @since 4.4
073       */
074      public synchronized Batch start() {
075        if (started) {
076          throw new IllegalStateException("Batch is already started");
077        }
078    
079        configureLogging();
080        bootstrapContainer = BootstrapContainer.create(bootstrapProperties, components);
081        bootstrapContainer.startComponents();
082        this.started = true;
083    
084        return this;
085      }
086    
087      /**
088       * @since 4.4
089       */
090      public Batch executeTask(Map<String, String> taskProperties, Object... components) {
091        if (!started) {
092          throw new IllegalStateException("Batch is not started. Unable to execute task.");
093        }
094    
095        bootstrapContainer.executeTask(taskProperties, components);
096        return this;
097      }
098    
099      /**
100       * @since 4.4
101       */
102      public synchronized void stop() {
103        if (!started) {
104          throw new IllegalStateException("Batch is not started.");
105        }
106    
107        bootstrapContainer.stopComponents();
108    
109        this.started = false;
110      }
111    
112      private void configureLogging() {
113        if (logging != null) {
114          logging.configure();
115        }
116      }
117    
118      public static Builder builder() {
119        return new Builder();
120      }
121    
122      public static final class Builder {
123        private Map<String, String> bootstrapProperties;
124        private EnvironmentInformation environment;
125        private List<Object> components = Lists.newArrayList();
126        private boolean enableLoggingConfiguration = true;
127    
128        private Builder() {
129        }
130    
131        public Builder setEnvironment(EnvironmentInformation env) {
132          this.environment = env;
133          return this;
134        }
135    
136        public Builder setComponents(List<Object> l) {
137          this.components = l;
138          return this;
139        }
140    
141        /**
142         * @deprecated since 3.7 use {@link #setBootstrapProperties(Map)}
143         */
144        @Deprecated
145        public Builder setGlobalProperties(Map<String, String> globalProperties) {
146          this.bootstrapProperties = globalProperties;
147          return this;
148        }
149    
150        public Builder setBootstrapProperties(Map<String, String> bootstrapProperties) {
151          this.bootstrapProperties = bootstrapProperties;
152          return this;
153        }
154    
155        public Builder addComponents(Object... components) {
156          Collections.addAll(this.components, components);
157          return this;
158        }
159    
160        public Builder addComponent(Object component) {
161          this.components.add(component);
162          return this;
163        }
164    
165        public boolean isEnableLoggingConfiguration() {
166          return enableLoggingConfiguration;
167        }
168    
169        /**
170         * Logback is configured by default. It can be disabled, but n this case the batch bootstrapper must provide its
171         * own implementation of SLF4J.
172         */
173        public Builder setEnableLoggingConfiguration(boolean b) {
174          this.enableLoggingConfiguration = b;
175          return this;
176        }
177    
178        public Batch build() {
179          if (components == null) {
180            throw new IllegalStateException("Batch components are not set");
181          }
182          return new Batch(this);
183        }
184      }
185    }