package org.sonar.core.persistence;
import ch.qos.logback.classic.Level;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.StringUtils;
+import com.google.common.io.Closeables;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.mapping.Environment;
-import org.apache.ibatis.session.*;
+import org.apache.ibatis.session.Configuration;
+import org.apache.ibatis.session.ExecutorType;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.type.JdbcType;
import org.slf4j.LoggerFactory;
import org.sonar.api.database.model.MeasureMapper;
import org.sonar.api.database.model.MeasureModel;
import org.sonar.core.config.Logback;
-import org.sonar.core.dashboard.*;
+import org.sonar.core.dashboard.ActiveDashboardDto;
+import org.sonar.core.dashboard.ActiveDashboardMapper;
+import org.sonar.core.dashboard.DashboardDto;
+import org.sonar.core.dashboard.DashboardMapper;
+import org.sonar.core.dashboard.WidgetDto;
+import org.sonar.core.dashboard.WidgetMapper;
+import org.sonar.core.dashboard.WidgetPropertyDto;
+import org.sonar.core.dashboard.WidgetPropertyMapper;
import org.sonar.core.dependency.DependencyDto;
import org.sonar.core.dependency.DependencyMapper;
import org.sonar.core.dependency.ResourceSnapshotDto;
import org.sonar.core.dependency.ResourceSnapshotMapper;
import org.sonar.core.duplication.DuplicationMapper;
import org.sonar.core.duplication.DuplicationUnitDto;
-import org.sonar.core.filter.*;
+import org.sonar.core.filter.CriterionDto;
+import org.sonar.core.filter.CriterionMapper;
+import org.sonar.core.filter.FilterColumnDto;
+import org.sonar.core.filter.FilterColumnMapper;
+import org.sonar.core.filter.FilterDto;
+import org.sonar.core.filter.FilterMapper;
import org.sonar.core.properties.PropertiesMapper;
import org.sonar.core.properties.PropertyDto;
import org.sonar.core.purge.PurgeMapper;
import org.sonar.core.purge.PurgeVendorMapper;
import org.sonar.core.purge.PurgeableSnapshotDto;
-import org.sonar.core.resource.*;
+import org.sonar.core.resource.ResourceDto;
+import org.sonar.core.resource.ResourceIndexDto;
+import org.sonar.core.resource.ResourceIndexerMapper;
+import org.sonar.core.resource.ResourceKeyUpdaterMapper;
+import org.sonar.core.resource.ResourceMapper;
+import org.sonar.core.resource.SnapshotDto;
import org.sonar.core.review.ReviewCommentDto;
import org.sonar.core.review.ReviewCommentMapper;
import org.sonar.core.review.ReviewDto;
import org.sonar.core.rule.RuleMapper;
import org.sonar.core.template.LoadedTemplateDto;
import org.sonar.core.template.LoadedTemplateMapper;
-import org.sonar.core.user.*;
+import org.sonar.core.user.AuthorDto;
+import org.sonar.core.user.AuthorMapper;
+import org.sonar.core.user.GroupDto;
+import org.sonar.core.user.GroupRoleDto;
+import org.sonar.core.user.RoleMapper;
+import org.sonar.core.user.UserDto;
+import org.sonar.core.user.UserMapper;
+import org.sonar.core.user.UserRoleDto;
import java.io.InputStream;
public MyBatis start() {
LogFactory.useSlf4jLogging();
+
Configuration conf = new Configuration();
conf.setEnvironment(new Environment("production", createTransactionFactory(), database.getDataSource()));
+ conf.setDatabaseId(database.getDialect().getId());
conf.setUseGeneratedKeys(true);
conf.setLazyLoadingEnabled(false);
conf.setJdbcTypeForNull(JdbcType.NULL);
conf.getVariables().setProperty("_true", database.getDialect().getTrueSqlValue());
conf.getVariables().setProperty("_false", database.getDialect().getFalseSqlValue());
- conf.setDatabaseId(database.getDialect().getId());
loadAlias(conf, "ActiveDashboard", ActiveDashboardDto.class);
loadAlias(conf, "Author", AuthorDto.class);
loadAlias(conf, "WidgetProperty", WidgetPropertyDto.class);
loadAlias(conf, "MeasureModel", MeasureModel.class);
- Class[] mappers = {ActiveDashboardMapper.class, AuthorMapper.class, FilterMapper.class, CriterionMapper.class, FilterColumnMapper.class, DashboardMapper.class,
+ Class<?>[] mappers = {ActiveDashboardMapper.class, AuthorMapper.class, FilterMapper.class, CriterionMapper.class, FilterColumnMapper.class, DashboardMapper.class,
DependencyMapper.class, DuplicationMapper.class, LoadedTemplateMapper.class, PropertiesMapper.class, PurgeMapper.class, PurgeVendorMapper.class,
ResourceKeyUpdaterMapper.class, ResourceIndexerMapper.class, ResourceMapper.class, ResourceSnapshotMapper.class, ReviewCommentMapper.class,
ReviewMapper.class, RoleMapper.class, RuleMapper.class, SchemaMigrationMapper.class, UserMapper.class, WidgetMapper.class, WidgetPropertyMapper.class,
MeasureMapper.class};
loadMappers(conf, mappers);
configureLogback(mappers);
+
sessionFactory = new SqlSessionFactoryBuilder().build(conf);
return this;
}
}
}
- private void loadMappers(Configuration mybatisConf, Class... mapperClasses) {
+ private void loadMappers(Configuration mybatisConf, Class<?>... mapperClasses) {
for (Class mapperClass : mapperClasses) {
loadMapper(mybatisConf, mapperClass);
}
/**
* See http://www.mybatis.org/core/logging.html :
*/
- private void configureLogback(Class[] mapperClasses) {
+ private void configureLogback(Class<?>... mapperClasses) {
if (settings.getBoolean("sonar.showSql")) {
Level level = (settings.getBoolean("sonar.showSqlResults") ? Level.TRACE : Level.DEBUG);
for (Class mapperClass : mapperClasses) {
}
}
- private void loadMapper(Configuration mybatisConf, Class mapperClass) {
- // trick to use database-specific XML files for a single Mapper Java interface
- InputStream input = getPathToMapper(mapperClass);
- try {
- XMLMapperBuilder mapperParser = new XMLMapperBuilder(input, mybatisConf, mapperClass.getName(), mybatisConf.getSqlFragments());
- mapperParser.parse();
- mybatisConf.addLoadedResource(mapperClass.getName());
+ private void loadMapper(Configuration configuration, Class mapperClass) {
+ String mapperName = mapperClass.getName();
+ InputStream input = null;
+ try {
+ input = getClass().getResourceAsStream("/" + mapperName.replace('.', '/') + ".xml");
+ new XMLMapperBuilder(input, configuration, mapperName, configuration.getSqlFragments()).parse();
+ configuration.addLoadedResource(mapperName);
} finally {
- IOUtils.closeQuietly(input);
+ Closeables.closeQuietly(input);
}
}
- private InputStream getPathToMapper(Class mapperClass) {
- InputStream input = getClass().getResourceAsStream(
- "/" + StringUtils.replace(mapperClass.getName(), ".", "/") + "-" + database.getDialect().getId() + ".xml");
- if (input == null) {
- input = getClass().getResourceAsStream("/" + StringUtils.replace(mapperClass.getName(), ".", "/") + ".xml");
- }
- return input;
- }
-
private void loadAlias(Configuration conf, String alias, Class dtoClass) {
conf.getTypeAliasRegistry().registerAlias(alias, dtoClass);
}
private static JdbcTransactionFactory createTransactionFactory() {
return new JdbcTransactionFactory();
}
-
}