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.bootstrap;
021
022 import org.apache.commons.configuration.PropertiesConfiguration;
023 import org.sonar.api.batch.bootstrap.ProjectReactor;
024 import org.sonar.api.utils.HttpDownloader;
025 import org.sonar.batch.FakeMavenPluginExecutor;
026 import org.sonar.batch.MavenPluginExecutor;
027 import org.sonar.batch.ServerMetadata;
028 import org.sonar.batch.config.BatchSettings;
029 import org.sonar.batch.config.BatchSettingsEnhancer;
030 import org.sonar.jpa.session.DatabaseSessionProvider;
031 import org.sonar.jpa.session.DefaultDatabaseConnector;
032 import org.sonar.jpa.session.ThreadLocalDatabaseSessionFactory;
033 import org.sonar.core.persistence.DaoUtils;
034 import org.sonar.core.persistence.MyBatis;
035
036 import java.net.URLClassLoader;
037
038 /**
039 * Level 1 components
040 */
041 public class BootstrapModule extends Module {
042
043 private Object[] boostrapperComponents;
044 private ProjectReactor reactor;
045
046 public BootstrapModule(ProjectReactor reactor, Object... boostrapperComponents) {
047 this.reactor = reactor;
048 this.boostrapperComponents = boostrapperComponents;
049 }
050
051 @Override
052 protected void configure() {
053 addCoreSingleton(reactor);
054 addCoreSingleton(new PropertiesConfiguration());
055 addCoreSingleton(BatchSettings.class);
056 addCoreSingleton(DryRun.class);
057 addCoreSingleton(ServerMetadata.class);// registered here because used by BootstrapClassLoader
058 addCoreSingleton(TempDirectories.class);// registered here because used by BootstrapClassLoader
059 addCoreSingleton(HttpDownloader.class);// registered here because used by BootstrapClassLoader
060 addCoreSingleton(ArtifactDownloader.class);// registered here because used by BootstrapClassLoader
061 addCoreSingleton(JdbcDriverHolder.class);
062
063 URLClassLoader bootstrapClassLoader = getComponentByType(JdbcDriverHolder.class).getClassLoader();
064 // set as the current context classloader for hibernate, else it does not find the JDBC driver.
065 Thread.currentThread().setContextClassLoader(bootstrapClassLoader);
066
067 addCoreSingleton(BatchDatabase.class);
068 addCoreSingleton(MyBatis.class);
069 addCoreSingleton(DefaultDatabaseConnector.class);
070 addCoreSingleton(ThreadLocalDatabaseSessionFactory.class);
071 for (Class daoClass : DaoUtils.getDaoClasses()) {
072 addCoreSingleton(daoClass);
073 }
074 addAdapter(new DatabaseSessionProvider());
075 for (Object component : boostrapperComponents) {
076 addCoreSingleton(component);
077 }
078 if (!isMavenPluginExecutorRegistered()) {
079 addCoreSingleton(FakeMavenPluginExecutor.class);
080 }
081
082 addCoreSingleton(BatchPluginRepository.class);
083 addCoreSingleton(BatchExtensionInstaller.class);
084 addCoreSingleton(BatchSettingsEnhancer.class);
085 }
086
087 boolean isMavenPluginExecutorRegistered() {
088 if (boostrapperComponents != null) {
089 for (Object component : boostrapperComponents) {
090 if (component instanceof Class && MavenPluginExecutor.class.isAssignableFrom((Class<?>) component)) {
091 return true;
092 }
093 }
094 }
095 return false;
096 }
097
098 @Override
099 protected void doStart() {
100 boolean dryRun = getComponentByType(DryRun.class).isEnabled();
101 Module batchComponents = installChild(new BatchModule(dryRun));
102 batchComponents.start();
103 }
104 }