1.1 --- a/pom.xml Fri Nov 09 03:22:29 2012 +0400
1.2 +++ b/pom.xml Sat Nov 10 22:54:48 2012 +0400
1.3 @@ -103,13 +103,6 @@
1.4 <groupId>commons-dbcp</groupId>
1.5 <artifactId>commons-dbcp</artifactId>
1.6 <version>${commons-dbcp.version}</version>
1.7 - <scope>test</scope>
1.8 - </dependency>
1.9 - <dependency>
1.10 - <groupId>org.apache.derby</groupId>
1.11 - <artifactId>derby</artifactId>
1.12 - <version>${derby.version}</version>
1.13 - <scope>test</scope>
1.14 </dependency>
1.15 <dependency>
1.16 <groupId>org.codehaus.jackson</groupId>
2.1 --- a/src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy Fri Nov 09 03:22:29 2012 +0400
2.2 +++ b/src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy Sat Nov 10 22:54:48 2012 +0400
2.3 @@ -27,7 +27,7 @@
2.4 */
2.5 public class JPADataAccessObject {
2.6
2.7 protected static String persistenceUnitName = null;
2.8 - protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), PropertySelector.select());
2.9 + protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), JPAPropertySelector.select());
2.10 protected EntityManager em = emf.createEntityManager();
2.11 final static Logger log = LoggerFactory.getLogger(JPADataAccessObject.class.getSimpleName());
2.12
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/src/main/java/ru/indvdum/jpa/dao/JPAPropertySelector.groovy Sat Nov 10 22:54:48 2012 +0400
3.3 @@ -0,0 +1,55 @@
3.4 +package ru.indvdum.jpa.dao
3.5 +
3.6 +/**
3.7 + * @author indvdum (gotoindvdum[at]gmail[dot]com)
3.8 + * @since 03.11.2011 13:53:32
3.9 + *
3.10 + */
3.11 +class JPAPropertySelector {
3.12 + public static final String SHOWSQL = "custom.showsql"
3.13 + public static final String SYNCHRONIZEDB = "custom.synchronize"
3.14 + public static final String SCHEMANAME = "custom.schemaname"
3.15 + public static final String DBDICTIONARY = "custom.dbdictionary"
3.16 + public static final String RUNTIMEENHANCEMENT = "custom.runtimeenhancement"
3.17 +
3.18 + private static Properties properties = new Properties()
3.19 +
3.20 + private static boolean isProperty(String propertyName) {
3.21 + return Boolean.valueOf(properties.getProperty(propertyName)).booleanValue()
3.22 + }
3.23 +
3.24 + private static String getSystemProperty(String propertyName) {
3.25 + properties.getProperty(propertyName)
3.26 + }
3.27 +
3.28 + static void setSystemProperty(String propertyName, String propertyValue) {
3.29 + properties.setProperty(propertyName, propertyValue)
3.30 + }
3.31 +
3.32 + static Map select() {
3.33 + Map<String, String> emfProperties = [:]
3.34 +
3.35 + if(isProperty(SHOWSQL)) {
3.36 + emfProperties["openjpa.Log"] = "DefaultLevel=INFO, Tool=INFO, SQL=TRACE"
3.37 + emfProperties["openjpa.ConnectionFactoryProperties"] = "PrintParameters=true, PrettyPrint=true"
3.38 + }
3.39 +
3.40 + if(isProperty(SYNCHRONIZEDB)) {
3.41 + emfProperties["openjpa.jdbc.SynchronizeMappings"] = "buildSchema(ForeignKeys=true)"
3.42 + }
3.43 +
3.44 + if(getSystemProperty(SCHEMANAME) != null) {
3.45 + emfProperties["openjpa.jdbc.Schema"] = getSystemProperty(SCHEMANAME)
3.46 + }
3.47 +
3.48 + if(getSystemProperty(DBDICTIONARY)) {
3.49 + emfProperties["openjpa.jdbc.DBDictionary"] = getSystemProperty(DBDICTIONARY)
3.50 + }
3.51 +
3.52 + if(getSystemProperty(RUNTIMEENHANCEMENT)) {
3.53 + emfProperties["openjpa.RuntimeUnenhancedClasses"] = getSystemProperty(RUNTIMEENHANCEMENT)
3.54 + }
3.55 +
3.56 + return emfProperties
3.57 + }
3.58 +}
4.1 --- a/src/main/java/ru/indvdum/jpa/dao/PropertySelector.groovy Fri Nov 09 03:22:29 2012 +0400
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,55 +0,0 @@
4.4 -package ru.indvdum.jpa.dao
4.5 -
4.6 -/**
4.7 - * @author indvdum (gotoindvdum[at]gmail[dot]com)
4.8 - * @since 03.11.2011 13:53:32
4.9 - *
4.10 - */
4.11 -class PropertySelector {
4.12 - public static final String SHOWSQL = "custom.showsql"
4.13 - public static final String SYNCHRONIZEDB = "custom.synchronize"
4.14 - public static final String SCHEMANAME = "custom.schemaname"
4.15 - public static final String DBDICTIONARY = "custom.dbdictionary"
4.16 - public static final String RUNTIMEENHANCEMENT = "custom.runtimeenhancement"
4.17 -
4.18 - private static Properties properties = new Properties()
4.19 -
4.20 - private static boolean isProperty(String propertyName) {
4.21 - return Boolean.valueOf(properties.getProperty(propertyName)).booleanValue()
4.22 - }
4.23 -
4.24 - private static String getSystemProperty(String propertyName) {
4.25 - properties.getProperty(propertyName)
4.26 - }
4.27 -
4.28 - static void setSystemProperty(String propertyName, String propertyValue) {
4.29 - properties.setProperty(propertyName, propertyValue)
4.30 - }
4.31 -
4.32 - static Map select() {
4.33 - Map<String, String> emfProperties = [:]
4.34 -
4.35 - if(isProperty(SHOWSQL)) {
4.36 - emfProperties["openjpa.Log"] = "DefaultLevel=INFO, Tool=INFO, SQL=TRACE"
4.37 - emfProperties["openjpa.ConnectionFactoryProperties"] = "PrintParameters=true, PrettyPrint=true"
4.38 - }
4.39 -
4.40 - if(isProperty(SYNCHRONIZEDB)) {
4.41 - emfProperties["openjpa.jdbc.SynchronizeMappings"] = "buildSchema(ForeignKeys=true)"
4.42 - }
4.43 -
4.44 - if(getSystemProperty(SCHEMANAME) != null) {
4.45 - emfProperties["openjpa.jdbc.Schema"] = getSystemProperty(SCHEMANAME)
4.46 - }
4.47 -
4.48 - if(getSystemProperty(DBDICTIONARY)) {
4.49 - emfProperties["openjpa.jdbc.DBDictionary"] = getSystemProperty(DBDICTIONARY)
4.50 - }
4.51 -
4.52 - if(getSystemProperty(RUNTIMEENHANCEMENT)) {
4.53 - emfProperties["openjpa.RuntimeUnenhancedClasses"] = getSystemProperty(RUNTIMEENHANCEMENT)
4.54 - }
4.55 -
4.56 - return emfProperties
4.57 - }
4.58 -}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java Sat Nov 10 22:54:48 2012 +0400
5.3 @@ -0,0 +1,78 @@
5.4 +package ru.indvdum.jpa.init;
5.5 +
5.6 +import java.util.ResourceBundle;
5.7 +
5.8 +import javax.naming.InitialContext;
5.9 +import javax.naming.NamingException;
5.10 +
5.11 +import org.apache.commons.dbcp.BasicDataSource;
5.12 +
5.13 +import ru.indvdum.jpa.dao.JPAPropertySelector;
5.14 +import ru.indvdum.jpa.props.Props;
5.15 +
5.16 +/**
5.17 + * @author indvdum (gotoindvdum[at]gmail[dot]com)
5.18 + * @since 10.11.2012 21:40:11
5.19 + *
5.20 + */
5.21 +public class DatabaseInitializer {
5.22 +
5.23 + protected static String dataSource = null;
5.24 +
5.25 + protected static String getDataSourceName() {
5.26 + if (dataSource != null)
5.27 + return dataSource;
5.28 +
5.29 + if (dataSource == null)
5.30 + dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY);
5.31 + if (dataSource == null)
5.32 + dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY);
5.33 + if (dataSource == null)
5.34 + dataSource = "jdbc/database";
5.35 + return dataSource;
5.36 + }
5.37 +
5.38 + public static void init() throws NamingException {
5.39 + String url = System.getProperty(Props.DATABASE_URL_PROPERTY);
5.40 + String driver = System.getProperty(Props.DATABASE_DRIVER_PROPERTY);
5.41 + String username = System.getProperty(Props.DATABASE_USERNAME_PROPERTY);
5.42 + String password = System.getProperty(Props.DATABASE_PASSWORD_PROPERTY);
5.43 + String maxActive = System.getProperty(Props.DATABASE_MAX_ACTIVE_PROPERTY);
5.44 + if (url != null) {
5.45 + BasicDataSource dataSource = new BasicDataSource();
5.46 + dataSource.setUrl(url);
5.47 + dataSource.setDriverClassName(driver);
5.48 + dataSource.setUsername(username);
5.49 + dataSource.setPassword(password);
5.50 + if (maxActive != null && maxActive.matches("\\d+"))
5.51 + dataSource.setMaxActive(Integer.parseInt(maxActive));
5.52 +
5.53 + init(dataSource);
5.54 + } else
5.55 + initDerby();
5.56 + }
5.57 +
5.58 + public static void init(BasicDataSource dataSource) throws NamingException {
5.59 + InitialContext context = new InitialContext();
5.60 + BasicDataSource existedDataSource = (BasicDataSource) context.lookup(getDataSourceName());
5.61 + if (existedDataSource == null)
5.62 + context.bind(getDataSourceName(), dataSource);
5.63 + }
5.64 +
5.65 + public static void initDerby() throws NamingException {
5.66 + System.setProperty("derby.stream.error.field", "java.lang.System.err");
5.67 +
5.68 + BasicDataSource dataSource = new BasicDataSource();
5.69 + dataSource.setUrl("jdbc:derby:memory:myDb;create=true");
5.70 + dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
5.71 + dataSource.setUsername("");
5.72 + dataSource.setPassword("");
5.73 + dataSource.setMaxActive(2);
5.74 +
5.75 + init(dataSource);
5.76 +
5.77 + JPAPropertySelector.setSystemProperty(JPAPropertySelector.RUNTIMEENHANCEMENT, "supported");
5.78 + JPAPropertySelector.setSystemProperty(JPAPropertySelector.SHOWSQL, "true");
5.79 + JPAPropertySelector.setSystemProperty(JPAPropertySelector.SYNCHRONIZEDB, "true");
5.80 + }
5.81 +}
6.1 --- a/src/main/java/ru/indvdum/jpa/props/Props.java Fri Nov 09 03:22:29 2012 +0400
6.2 +++ b/src/main/java/ru/indvdum/jpa/props/Props.java Sat Nov 10 22:54:48 2012 +0400
6.3 @@ -3,12 +3,19 @@
6.4 /**
6.5 * @author indvdum (gotoindvdum[at]gmail[dot]com)
6.6 * @since 09.11.2012 1:14:24
6.7 - *
6.8 + *
6.9 */
6.10 public class Props {
6.11 -
6.12 +
6.13 public static final String PERSISTANCE_UNIT_NAME_PROPERTY = "jpadao.persistenceUnitName";
6.14 public static final String DATA_SOURCE_PROPERTY = "jpadao.datasource";
6.15 +
6.16 + public static final String DATABASE_URL_PROPERTY = "jpadao.db.url";
6.17 + public static final String DATABASE_DRIVER_PROPERTY = "jpadao.db.driver";
6.18 + public static final String DATABASE_USERNAME_PROPERTY = "jpadao.db.username";
6.19 + public static final String DATABASE_PASSWORD_PROPERTY = "jpadao.db.password";
6.20 + public static final String DATABASE_MAX_ACTIVE_PROPERTY = "jpadao.db.max_active";
6.21 +
6.22 public static final String JPADAO_PROPERTY_FILE = "jpadao";
6.23 -
6.24 +
6.25 }
7.1 --- a/src/test/java/ru/indvdum/jpa/tests/AbstractJPATest.java Fri Nov 09 03:22:29 2012 +0400
7.2 +++ b/src/test/java/ru/indvdum/jpa/tests/AbstractJPATest.java Sat Nov 10 22:54:48 2012 +0400
7.3 @@ -1,15 +1,8 @@
7.4 package ru.indvdum.jpa.tests;
7.5
7.6 -import java.util.ResourceBundle;
7.7 -
7.8 -import javax.naming.InitialContext;
7.9 -import javax.naming.NamingException;
7.10 -
7.11 -import org.apache.commons.dbcp.BasicDataSource;
7.12 import org.junit.BeforeClass;
7.13
7.14 -import ru.indvdum.jpa.dao.PropertySelector;
7.15 -import ru.indvdum.jpa.props.Props;
7.16 +import ru.indvdum.jpa.init.DatabaseInitializer;
7.17
7.18 /**
7.19 * @author indvdum (gotoindvdum[at]gmail[dot]com)
7.20 @@ -20,52 +13,7 @@
7.21
7.22 @BeforeClass
7.23 public static void setUpClass() throws Exception {
7.24 - if ("true".equalsIgnoreCase(System.getProperty("JPA.realDatabaseConnection"))) {
7.25 - initRealDatabase();
7.26 - } else {
7.27 - initDerby();
7.28 - }
7.29 - }
7.30 -
7.31 - protected static String dataSource = null;
7.32 -
7.33 - protected static String getDataSourceName() {
7.34 - if (dataSource != null)
7.35 - return dataSource;
7.36 -
7.37 - if (dataSource == null)
7.38 - dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY);
7.39 - if (dataSource == null)
7.40 - dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY);
7.41 - if (dataSource == null)
7.42 - dataSource = "jdbc/database";
7.43 - return dataSource;
7.44 - }
7.45 -
7.46 - private static void initDerby() throws NamingException {
7.47 - System.setProperty("derby.stream.error.field", "java.lang.System.err");
7.48 -
7.49 - InitialContext context = new InitialContext();
7.50 - BasicDataSource datasource = (BasicDataSource) context.lookup(getDataSourceName());
7.51 -
7.52 - if (datasource == null) {
7.53 - datasource = new BasicDataSource();
7.54 - datasource.setUrl("jdbc:derby:memory:myDb;create=true");
7.55 - datasource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
7.56 - datasource.setUsername("");
7.57 - datasource.setPassword("");
7.58 - datasource.setMaxActive(2);
7.59 -
7.60 - context.bind(getDataSourceName(), datasource);
7.61 - }
7.62 -
7.63 - PropertySelector.setSystemProperty(PropertySelector.RUNTIMEENHANCEMENT, "supported");
7.64 - PropertySelector.setSystemProperty(PropertySelector.SHOWSQL, "true");
7.65 - PropertySelector.setSystemProperty(PropertySelector.SYNCHRONIZEDB, "true");
7.66 - }
7.67 -
7.68 - private static void initRealDatabase() throws NamingException {
7.69 - // TODO: need realize
7.70 + DatabaseInitializer.init();
7.71 }
7.72
7.73 }