Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.persist.core

File DataSourceConfiguration.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
19% of files have more coverage

Code metrics

2
29
4
1
86
66
9
0.31
7.25
4
2.25

Classes

Class Line # Actions
DataSourceConfiguration 23 29 9
0.7428571674.3%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    package io.sunshower.persist.core;
2   
3    import static io.sunshower.persist.core.DataSourceConfigurations.useLocation;
4   
5    import io.sunshower.common.Identifier;
6    import io.sunshower.persistence.Dialect;
7    import io.sunshower.persistence.UnsupportedDatabaseException;
8    import java.sql.Connection;
9    import java.sql.SQLException;
10    import java.util.Properties;
11    import javax.inject.Singleton;
12    import javax.naming.InitialContext;
13    import javax.naming.NamingException;
14    import javax.sql.DataSource;
15    import org.cfg4j.provider.ConfigurationProvider;
16    import org.slf4j.Logger;
17    import org.slf4j.LoggerFactory;
18    import org.springframework.context.annotation.Bean;
19    import org.springframework.context.annotation.Configuration;
20    import org.springframework.jdbc.datasource.DriverManagerDataSource;
21   
22    @Configuration
 
23    public class DataSourceConfiguration {
24   
25    static final Logger log = LoggerFactory.getLogger(DataSourceConfiguration.class);
26   
 
27  2 toggle @Bean
28    @Singleton
29    public Dialect databaseDialect(DataSource dataSource) throws SQLException {
30  2 try (final Connection cnx = dataSource.getConnection()) {
31  2 final String name = cnx.getMetaData().getDatabaseProductName().toLowerCase();
32  2 switch (name) {
33  2 case "h2":
34  2 log.info("dialect is 'h2'");
35  2 return Dialect.H2;
36  0 case "postgresql":
37  0 log.info("dialect is 'postgres'");
38  0 return Dialect.Postgres;
39    }
40  0 throw new UnsupportedDatabaseException("Sunshower does not support the database: " + name);
41    }
42    }
43   
 
44  2 toggle @Bean
45    public DatabaseConfigurationSource databaseConfigurationSource(ConfigurationProvider provider) {
46  2 return provider.bind("jdbc", DatabaseConfigurationSource.class);
47    }
48   
 
49  2 toggle @Bean
50    public DataSource dataSource(DatabaseConfigurationSource cfg) throws NamingException {
51  2 if (useLocation(cfg)) {
52  2 log.info("Starting JDBC data-source...");
53   
54  2 final DriverManagerDataSource result = new DriverManagerDataSource();
55    // final DataSource result = new HikariDataSource(toNative(cfg));
56    // final HikariDataSource result = new HikariDataSource();
57  2 result.setDriverClassName("com.arjuna.ats.jdbc.TransactionalDriver");
58  2 final Properties properties = new Properties();
59  2 properties.setProperty("user", "sa");
60  2 properties.setProperty("password", "");
61  2 properties.setProperty("DYNAMIC_CLASS", "io.sunshower.persist.core.JtaDynamicClass");
62    //
63  2 result.setConnectionProperties(properties);
64    // result.setMaximumPoolSize(1);
65   
66  2 result.setUrl(process(cfg.url()));
67    //
68    // "jdbc:arjuna:h2:mem:frapper;MODE=PostgreSQL;LOCK_MODE=0;MV_STORE=false;DB_CLOSE_DELAY=-1;"));
69    // result.setUrl(
70    // process(
71    //
72    // "jdbc:arjuna:h2:mem:frapper;MODE=PostgreSQL;LOCK_MODE=0;MV_STORE=false;DB_CLOSE_DELAY=-1;"));
73  2 log.info("Successfully started data-source");
74  2 return result;
75    } else {
76  0 log.info("Starting JNDI data-source...");
77  0 final DataSource result = InitialContext.doLookup(cfg.jndiPath());
78  0 log.info("Successfully started data-source");
79  0 return result;
80    }
81    }
82   
 
83  3 toggle static String process(String s) {
84  3 return s.replaceAll("\\{\\{RANDOM}}", Identifier.random().toString());
85    }
86    }