diff --git a/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java b/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java index fd11d9c8..a3910db6 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java @@ -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"; diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/DefaultConfig.java b/ethereumj-core/src/main/java/org/ethereum/facade/DefaultConfig.java new file mode 100644 index 00000000..e80df9f9 --- /dev/null +++ b/ethereumj-core/src/main/java/org/ethereum/facade/DefaultConfig.java @@ -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; + } + +} diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumFactory.java b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumFactory.java index fd63e401..7f3e4647 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumFactory.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumFactory.java @@ -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); } + } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java index b8a16174..9ddac091 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java @@ -58,6 +58,7 @@ public class EthereumImpl implements Ethereum { ApplicationContext ctx; public EthereumImpl() { + System.out.println(); } @PostConstruct