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 */ 020 package org.sonar.batch.bootstrapper; 021 022 import com.google.common.collect.Lists; 023 import org.sonar.api.batch.bootstrap.ProjectReactor; 024 import org.sonar.batch.bootstrap.BootstrapModule; 025 import org.sonar.batch.bootstrap.Module; 026 027 import java.util.Arrays; 028 import java.util.List; 029 import java.util.Map; 030 031 /** 032 * Entry point for batch bootstrappers. 033 * 034 * @since 2.14 035 */ 036 public final class Batch { 037 038 private LoggingConfiguration logging; 039 private List components; 040 private ProjectReactor projectReactor; 041 042 private Batch(Builder builder) { 043 components = Lists.newArrayList(); 044 components.addAll(builder.components); 045 components.add(builder.environment); 046 projectReactor = builder.projectReactor; 047 if (builder.isEnableLoggingConfiguration()) { 048 logging = LoggingConfiguration.create().setProperties((Map) projectReactor.getRoot().getProperties()); 049 } 050 } 051 052 public LoggingConfiguration getLoggingConfiguration() { 053 return logging; 054 } 055 056 public Batch execute() { 057 configureLogging(); 058 startBatch(); 059 return this; 060 } 061 062 private void configureLogging() { 063 if (logging != null) { 064 logging.configure(); 065 } 066 } 067 068 private void startBatch() { 069 Module bootstrapModule = new BootstrapModule(projectReactor, components.toArray(new Object[components.size()])).init(); 070 try { 071 bootstrapModule.start(); 072 } finally { 073 try { 074 bootstrapModule.stop(); 075 } catch (Exception e) { 076 // never throw exceptions in a finally block 077 } 078 } 079 } 080 081 082 public static Builder builder() { 083 return new Builder(); 084 } 085 086 public static final class Builder { 087 private ProjectReactor projectReactor; 088 private EnvironmentInformation environment; 089 private List components = Lists.newArrayList(); 090 private boolean enableLoggingConfiguration = true; 091 092 private Builder() { 093 } 094 095 public Builder setProjectReactor(ProjectReactor projectReactor) { 096 this.projectReactor = projectReactor; 097 return this; 098 } 099 100 public Builder setEnvironment(EnvironmentInformation env) { 101 this.environment = env; 102 return this; 103 } 104 105 public Builder setComponents(List l) { 106 this.components = l; 107 return this; 108 } 109 110 public Builder addComponents(Object... components) { 111 this.components.addAll(Arrays.asList(components)); 112 return this; 113 } 114 115 public Builder addComponent(Object component) { 116 this.components.add(component); 117 return this; 118 } 119 120 public boolean isEnableLoggingConfiguration() { 121 return enableLoggingConfiguration; 122 } 123 124 /** 125 * Logback is configured by default. It can be disabled, but n this case the batch bootstrapper must provide its 126 * own implementation of SLF4J. 127 */ 128 public Builder setEnableLoggingConfiguration(boolean b) { 129 this.enableLoggingConfiguration = b; 130 return this; 131 } 132 133 public Batch build() { 134 if (projectReactor == null) { 135 throw new IllegalStateException("ProjectReactor is not set"); 136 } 137 if (environment == null) { 138 throw new IllegalStateException("EnvironmentInfo is not set"); 139 } 140 if (components == null) { 141 throw new IllegalStateException("Batch components are not set"); 142 } 143 return new Batch(this); 144 } 145 } 146 }