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