001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.batch;
021
022import org.apache.commons.configuration.Configuration;
023import org.apache.commons.lang.StringUtils;
024import org.sonar.api.batch.bootstrap.ProjectReactor;
025import org.sonar.batch.bootstrap.BootstrapModule;
026import org.sonar.batch.bootstrap.Module;
027import org.sonar.batch.bootstrapper.Reactor;
028
029import java.util.Iterator;
030import java.util.Properties;
031
032/**
033 * @deprecated Replaced by {@link org.sonar.batch.bootstrapper.Batch} since version 2.14.
034 */
035@Deprecated
036public final class Batch {
037
038  private Module bootstrapModule;
039
040  public Batch(ProjectReactor reactor, Object... bootstrapperComponents) {
041    this.bootstrapModule = new BootstrapModule(reactor, bootstrapperComponents).init();
042  }
043
044  /**
045   * @deprecated since 2.9 because commons-configuration is replaced by ProjectDefinition#properties. Used by Ant Task 1.1
046   */
047  @Deprecated
048  public Batch(Configuration configuration, Object... bootstrapperComponents) {//NOSONAR configuration is not needed
049    // because it's already included in ProjectDefinition.
050    this.bootstrapModule = new BootstrapModule(extractProjectReactor(bootstrapperComponents), bootstrapperComponents).init();
051  }
052
053  static ProjectReactor extractProjectReactor(Object[] components) {
054    Reactor deprecatedReactor = null;
055    for (Object component : components) {
056      if (component instanceof ProjectReactor) {
057        return (ProjectReactor) component;
058      }
059      if (component instanceof Reactor) {
060        deprecatedReactor = (Reactor) component;
061      }
062    }
063
064    if (deprecatedReactor == null) {
065      throw new IllegalArgumentException("Project reactor is not defined");
066    }
067    return deprecatedReactor.toProjectReactor();
068  }
069
070  /**
071   * Used by Gradle 1.0
072   *
073   * @deprecated in version 2.12
074   */
075  @Deprecated
076  public static Batch create(ProjectReactor projectReactor, Configuration configuration, Object... bootstrapperComponents) {
077    if (configuration != null) {
078      projectReactor.getRoot().setProperties(convertToProperties(configuration));
079    }
080    return new Batch(projectReactor, bootstrapperComponents);
081  }
082
083  static Properties convertToProperties(Configuration configuration) {
084    Properties props = new Properties();
085    Iterator keys = configuration.getKeys();
086    while (keys.hasNext()) {
087      String key = (String) keys.next();
088      // Configuration#getString() automatically splits strings by comma separator.
089      String value = StringUtils.join(configuration.getStringArray(key), ",");
090      props.setProperty(key, value);
091    }
092    return props;
093  }
094
095  /**
096   * for unit tests
097   */
098  Batch(Module bootstrapModule) {
099    this.bootstrapModule = bootstrapModule;
100  }
101
102  public void execute() {
103    try {
104      bootstrapModule.start();
105    } finally {
106      bootstrapModule.stop();
107    }
108  }
109}