Redesign config init

Extracting DefaultConfig class and open the door
to implement plug-able custom config classes
This commit is contained in:
Roman Mandeleil 2015-01-27 01:22:05 +02:00
parent f3e067d28b
commit 86e7643f63
4 changed files with 108 additions and 91 deletions

View File

@ -39,7 +39,6 @@ import static org.ethereum.util.ByteUtil.wrap;
* @author Roman Mandeleil
* @since 17.11.2014
*/
@Component
public class RepositoryImpl implements Repository {
final static String DETAILS_DB = "details";

View File

@ -0,0 +1,98 @@
package org.ethereum.facade;
import org.ethereum.config.SystemProperties;
import org.ethereum.db.RepositoryImpl;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.sql.SQLException;
import java.util.Properties;
/**
*
* @author: Roman Mandeleil
* Created on: 27/01/2015 01:05
*/
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "org.ethereum")
public class DefaultConfig {
private static final Logger logger = LoggerFactory.getLogger("general");
@Autowired
Ethereum eth;
@Bean
Repository repository(){
return new RepositoryImpl();
}
@Bean
public SessionFactory sessionFactory() throws SQLException {
LocalSessionFactoryBuilder builder =
new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("org.ethereum.db")
.addProperties(getHibernateProperties());
return builder.buildSessionFactory();
}
private Properties getHibernateProperties() {
Properties prop = new Properties();
if (SystemProperties.CONFIG.databaseReset())
prop.put("hibernate.hbm2ddl.auto", "create");
prop.put("hibernate.format_sql", "true");
// todo: useful but annoying consider define by system.properties
// prop.put("hibernate.show_sql", "true");
prop.put("hibernate.dialect",
"org.hibernate.dialect.HSQLDialect");
return prop;
}
@Bean
public DataSourceTransactionManager transactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
logger.info("Connecting to the block store");
System.setProperty("hsqldb.reconfig_logging", "false");
String url =
String.format("jdbc:hsqldb:file:./%s/blockchain/blockchain.db;" +
"create=%s;hsqldb.default_table_type=cached",
SystemProperties.CONFIG.databaseDir(),
SystemProperties.CONFIG.databaseReset());
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl(url);
ds.setUsername("sa");
return ds;
}
}

View File

@ -1,116 +1,35 @@
package org.ethereum.facade;
import org.ethereum.config.SystemProperties;
import org.ethereum.net.eth.EthHandler;
import org.ethereum.net.shh.ShhHandler;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.sql.SQLException;
import java.util.Properties;
import org.springframework.stereotype.Component;
/**
* @author Roman Mandeleil
* @since 13.11.2014
*/
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "org.ethereum")
@Component
public class EthereumFactory {
private static final Logger logger = LoggerFactory.getLogger("general");
@Bean
public SessionFactory sessionFactory() throws SQLException {
LocalSessionFactoryBuilder builder =
new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("org.ethereum.db")
.addProperties(getHibernateProperties());
return builder.buildSessionFactory();
}
private Properties getHibernateProperties() {
Properties prop = new Properties();
if (SystemProperties.CONFIG.databaseReset())
prop.put("hibernate.hbm2ddl.auto", "create");
prop.put("hibernate.format_sql", "true");
// todo: useful but annoying consider define by system.properties
// prop.put("hibernate.show_sql", "true");
prop.put("hibernate.dialect",
"org.hibernate.dialect.HSQLDialect");
return prop;
}
@Bean
public DataSourceTransactionManager transactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
logger.info("Connecting to the block store");
System.setProperty("hsqldb.reconfig_logging", "false");
String url =
String.format("jdbc:hsqldb:file:./%s/blockchain/blockchain.db;" +
"create=%s;hsqldb.default_table_type=cached",
SystemProperties.CONFIG.databaseDir(),
SystemProperties.CONFIG.databaseReset());
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl(url);
ds.setUsername("sa");
return ds;
}
@Autowired
Ethereum eth;
private static ApplicationContext context;
private static EthereumFactory factory;
public static Ethereum createEthereum() {
return createEthereum(DefaultConfig.class);
}
public static Ethereum createEthereum(Class clazz) {
logger.info("capability eth version: [{}]", EthHandler.VERSION);
logger.info("capability shh version: [{}]", ShhHandler.VERSION);
if (context == null) {
context = new AnnotationConfigApplicationContext(EthereumFactory.class);
factory = context.getBean(EthereumFactory.class);
}
return factory.eth;
ApplicationContext context = new AnnotationConfigApplicationContext(clazz);
return context.getBean(Ethereum.class);
}
}

View File

@ -58,6 +58,7 @@ public class EthereumImpl implements Ethereum {
ApplicationContext ctx;
public EthereumImpl() {
System.out.println();
}
@PostConstruct