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