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