# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1352573688 -14400 # Node ID ccb029c396aa3956a127d51819889ccaa865ef9d # Parent eca51f09ed0a02f95c07f1c12a79981a9ce616c8 Add a DatabaseInitializer diff -r eca51f09ed0a -r ccb029c396aa pom.xml --- a/pom.xml Fri Nov 09 03:22:29 2012 +0400 +++ b/pom.xml Sat Nov 10 22:54:48 2012 +0400 @@ -103,13 +103,6 @@ commons-dbcp commons-dbcp ${commons-dbcp.version} - test - - - org.apache.derby - derby - ${derby.version} - test org.codehaus.jackson diff -r eca51f09ed0a -r ccb029c396aa src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy --- a/src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy Fri Nov 09 03:22:29 2012 +0400 +++ b/src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy Sat Nov 10 22:54:48 2012 +0400 @@ -27,7 +27,7 @@ */ public class JPADataAccessObject { protected static String persistenceUnitName = null; - protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), PropertySelector.select()); + protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), JPAPropertySelector.select()); protected EntityManager em = emf.createEntityManager(); final static Logger log = LoggerFactory.getLogger(JPADataAccessObject.class.getSimpleName()); diff -r eca51f09ed0a -r ccb029c396aa src/main/java/ru/indvdum/jpa/dao/JPAPropertySelector.groovy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ru/indvdum/jpa/dao/JPAPropertySelector.groovy Sat Nov 10 22:54:48 2012 +0400 @@ -0,0 +1,55 @@ +package ru.indvdum.jpa.dao + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com) + * @since 03.11.2011 13:53:32 + * + */ +class JPAPropertySelector { + public static final String SHOWSQL = "custom.showsql" + public static final String SYNCHRONIZEDB = "custom.synchronize" + public static final String SCHEMANAME = "custom.schemaname" + public static final String DBDICTIONARY = "custom.dbdictionary" + public static final String RUNTIMEENHANCEMENT = "custom.runtimeenhancement" + + private static Properties properties = new Properties() + + private static boolean isProperty(String propertyName) { + return Boolean.valueOf(properties.getProperty(propertyName)).booleanValue() + } + + private static String getSystemProperty(String propertyName) { + properties.getProperty(propertyName) + } + + static void setSystemProperty(String propertyName, String propertyValue) { + properties.setProperty(propertyName, propertyValue) + } + + static Map select() { + Map emfProperties = [:] + + if(isProperty(SHOWSQL)) { + emfProperties["openjpa.Log"] = "DefaultLevel=INFO, Tool=INFO, SQL=TRACE" + emfProperties["openjpa.ConnectionFactoryProperties"] = "PrintParameters=true, PrettyPrint=true" + } + + if(isProperty(SYNCHRONIZEDB)) { + emfProperties["openjpa.jdbc.SynchronizeMappings"] = "buildSchema(ForeignKeys=true)" + } + + if(getSystemProperty(SCHEMANAME) != null) { + emfProperties["openjpa.jdbc.Schema"] = getSystemProperty(SCHEMANAME) + } + + if(getSystemProperty(DBDICTIONARY)) { + emfProperties["openjpa.jdbc.DBDictionary"] = getSystemProperty(DBDICTIONARY) + } + + if(getSystemProperty(RUNTIMEENHANCEMENT)) { + emfProperties["openjpa.RuntimeUnenhancedClasses"] = getSystemProperty(RUNTIMEENHANCEMENT) + } + + return emfProperties + } +} diff -r eca51f09ed0a -r ccb029c396aa src/main/java/ru/indvdum/jpa/dao/PropertySelector.groovy --- a/src/main/java/ru/indvdum/jpa/dao/PropertySelector.groovy Fri Nov 09 03:22:29 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -package ru.indvdum.jpa.dao - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com) - * @since 03.11.2011 13:53:32 - * - */ -class PropertySelector { - public static final String SHOWSQL = "custom.showsql" - public static final String SYNCHRONIZEDB = "custom.synchronize" - public static final String SCHEMANAME = "custom.schemaname" - public static final String DBDICTIONARY = "custom.dbdictionary" - public static final String RUNTIMEENHANCEMENT = "custom.runtimeenhancement" - - private static Properties properties = new Properties() - - private static boolean isProperty(String propertyName) { - return Boolean.valueOf(properties.getProperty(propertyName)).booleanValue() - } - - private static String getSystemProperty(String propertyName) { - properties.getProperty(propertyName) - } - - static void setSystemProperty(String propertyName, String propertyValue) { - properties.setProperty(propertyName, propertyValue) - } - - static Map select() { - Map emfProperties = [:] - - if(isProperty(SHOWSQL)) { - emfProperties["openjpa.Log"] = "DefaultLevel=INFO, Tool=INFO, SQL=TRACE" - emfProperties["openjpa.ConnectionFactoryProperties"] = "PrintParameters=true, PrettyPrint=true" - } - - if(isProperty(SYNCHRONIZEDB)) { - emfProperties["openjpa.jdbc.SynchronizeMappings"] = "buildSchema(ForeignKeys=true)" - } - - if(getSystemProperty(SCHEMANAME) != null) { - emfProperties["openjpa.jdbc.Schema"] = getSystemProperty(SCHEMANAME) - } - - if(getSystemProperty(DBDICTIONARY)) { - emfProperties["openjpa.jdbc.DBDictionary"] = getSystemProperty(DBDICTIONARY) - } - - if(getSystemProperty(RUNTIMEENHANCEMENT)) { - emfProperties["openjpa.RuntimeUnenhancedClasses"] = getSystemProperty(RUNTIMEENHANCEMENT) - } - - return emfProperties - } -} diff -r eca51f09ed0a -r ccb029c396aa src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java Sat Nov 10 22:54:48 2012 +0400 @@ -0,0 +1,78 @@ +package ru.indvdum.jpa.init; + +import java.util.ResourceBundle; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.commons.dbcp.BasicDataSource; + +import ru.indvdum.jpa.dao.JPAPropertySelector; +import ru.indvdum.jpa.props.Props; + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com) + * @since 10.11.2012 21:40:11 + * + */ +public class DatabaseInitializer { + + protected static String dataSource = null; + + protected static String getDataSourceName() { + if (dataSource != null) + return dataSource; + + if (dataSource == null) + dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY); + if (dataSource == null) + dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY); + if (dataSource == null) + dataSource = "jdbc/database"; + return dataSource; + } + + public static void init() throws NamingException { + String url = System.getProperty(Props.DATABASE_URL_PROPERTY); + String driver = System.getProperty(Props.DATABASE_DRIVER_PROPERTY); + String username = System.getProperty(Props.DATABASE_USERNAME_PROPERTY); + String password = System.getProperty(Props.DATABASE_PASSWORD_PROPERTY); + String maxActive = System.getProperty(Props.DATABASE_MAX_ACTIVE_PROPERTY); + if (url != null) { + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setUrl(url); + dataSource.setDriverClassName(driver); + dataSource.setUsername(username); + dataSource.setPassword(password); + if (maxActive != null && maxActive.matches("\\d+")) + dataSource.setMaxActive(Integer.parseInt(maxActive)); + + init(dataSource); + } else + initDerby(); + } + + public static void init(BasicDataSource dataSource) throws NamingException { + InitialContext context = new InitialContext(); + BasicDataSource existedDataSource = (BasicDataSource) context.lookup(getDataSourceName()); + if (existedDataSource == null) + context.bind(getDataSourceName(), dataSource); + } + + public static void initDerby() throws NamingException { + System.setProperty("derby.stream.error.field", "java.lang.System.err"); + + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setUrl("jdbc:derby:memory:myDb;create=true"); + dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver"); + dataSource.setUsername(""); + dataSource.setPassword(""); + dataSource.setMaxActive(2); + + init(dataSource); + + JPAPropertySelector.setSystemProperty(JPAPropertySelector.RUNTIMEENHANCEMENT, "supported"); + JPAPropertySelector.setSystemProperty(JPAPropertySelector.SHOWSQL, "true"); + JPAPropertySelector.setSystemProperty(JPAPropertySelector.SYNCHRONIZEDB, "true"); + } +} diff -r eca51f09ed0a -r ccb029c396aa src/main/java/ru/indvdum/jpa/props/Props.java --- a/src/main/java/ru/indvdum/jpa/props/Props.java Fri Nov 09 03:22:29 2012 +0400 +++ b/src/main/java/ru/indvdum/jpa/props/Props.java Sat Nov 10 22:54:48 2012 +0400 @@ -3,12 +3,19 @@ /** * @author indvdum (gotoindvdum[at]gmail[dot]com) * @since 09.11.2012 1:14:24 - * + * */ public class Props { - + public static final String PERSISTANCE_UNIT_NAME_PROPERTY = "jpadao.persistenceUnitName"; public static final String DATA_SOURCE_PROPERTY = "jpadao.datasource"; + + public static final String DATABASE_URL_PROPERTY = "jpadao.db.url"; + public static final String DATABASE_DRIVER_PROPERTY = "jpadao.db.driver"; + public static final String DATABASE_USERNAME_PROPERTY = "jpadao.db.username"; + public static final String DATABASE_PASSWORD_PROPERTY = "jpadao.db.password"; + public static final String DATABASE_MAX_ACTIVE_PROPERTY = "jpadao.db.max_active"; + public static final String JPADAO_PROPERTY_FILE = "jpadao"; - + } diff -r eca51f09ed0a -r ccb029c396aa src/test/java/ru/indvdum/jpa/tests/AbstractJPATest.java --- a/src/test/java/ru/indvdum/jpa/tests/AbstractJPATest.java Fri Nov 09 03:22:29 2012 +0400 +++ b/src/test/java/ru/indvdum/jpa/tests/AbstractJPATest.java Sat Nov 10 22:54:48 2012 +0400 @@ -1,15 +1,8 @@ package ru.indvdum.jpa.tests; -import java.util.ResourceBundle; - -import javax.naming.InitialContext; -import javax.naming.NamingException; - -import org.apache.commons.dbcp.BasicDataSource; import org.junit.BeforeClass; -import ru.indvdum.jpa.dao.PropertySelector; -import ru.indvdum.jpa.props.Props; +import ru.indvdum.jpa.init.DatabaseInitializer; /** * @author indvdum (gotoindvdum[at]gmail[dot]com) @@ -20,52 +13,7 @@ @BeforeClass public static void setUpClass() throws Exception { - if ("true".equalsIgnoreCase(System.getProperty("JPA.realDatabaseConnection"))) { - initRealDatabase(); - } else { - initDerby(); - } - } - - protected static String dataSource = null; - - protected static String getDataSourceName() { - if (dataSource != null) - return dataSource; - - if (dataSource == null) - dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY); - if (dataSource == null) - dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY); - if (dataSource == null) - dataSource = "jdbc/database"; - return dataSource; - } - - private static void initDerby() throws NamingException { - System.setProperty("derby.stream.error.field", "java.lang.System.err"); - - InitialContext context = new InitialContext(); - BasicDataSource datasource = (BasicDataSource) context.lookup(getDataSourceName()); - - if (datasource == null) { - datasource = new BasicDataSource(); - datasource.setUrl("jdbc:derby:memory:myDb;create=true"); - datasource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver"); - datasource.setUsername(""); - datasource.setPassword(""); - datasource.setMaxActive(2); - - context.bind(getDataSourceName(), datasource); - } - - PropertySelector.setSystemProperty(PropertySelector.RUNTIMEENHANCEMENT, "supported"); - PropertySelector.setSystemProperty(PropertySelector.SHOWSQL, "true"); - PropertySelector.setSystemProperty(PropertySelector.SYNCHRONIZEDB, "true"); - } - - private static void initRealDatabase() throws NamingException { - // TODO: need realize + DatabaseInitializer.init(); } }