# HG changeset patch
# User indvdum (gotoindvdum[at]gmail[dot]com)
# Date 1352587526 -14400
# Node ID 4c2e4f6028726419a37d1d753bf326b3b3343a14
# Parent 180decac243312f7b0dc1a061b514f6820249f09
Reading parameters directed from persistence.xml
diff -r 180decac2433 -r 4c2e4f602872 pom.xml
--- a/pom.xml Sun Nov 11 01:24:41 2012 +0400
+++ b/pom.xml Sun Nov 11 02:45:26 2012 +0400
@@ -109,5 +109,10 @@
jackson-jaxrs
${jackson-jaxrs.version}
+
+ commons-configuration
+ commons-configuration
+ ${commons-configuration.version}
+
diff -r 180decac2433 -r 4c2e4f602872 src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy
--- a/src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy Sun Nov 11 01:24:41 2012 +0400
+++ b/src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy Sun Nov 11 02:45:26 2012 +0400
@@ -1,39 +1,18 @@
-package ru.indvdum.jpa.dao
import java.sql.Connection
-import java.sql.SQLException
-import java.util.ResourceBundle;
import java.util.Map.Entry
-
-import javax.persistence.EntityManager
-import javax.persistence.EntityManagerFactory
-import javax.persistence.EntityTransaction
-import javax.persistence.NoResultException
-import javax.persistence.Persistence
-import javax.persistence.Query
-import javax.persistence.criteria.CriteriaBuilder
-import javax.persistence.criteria.CriteriaQuery
-import javax.persistence.criteria.Predicate
-import javax.persistence.criteria.Root
-import javax.sql.DataSource
-
-import org.apache.openjpa.conf.OpenJPAConfiguration
-import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI
-import org.apache.openjpa.persistence.OpenJPAPersistence
-import org.slf4j.Logger
-import org.slf4j.LoggerFactory
-
import ru.indvdum.jpa.props.Props;
+package ru.indvdum.jpa.dao
import java.sql.Connection
import java.sql.SQLException
import java.util.Map.Entry
import javax.persistence.EntityManager
import javax.persistence.EntityManagerFactory
import javax.persistence.EntityTransaction
import javax.persistence.NoResultException
import javax.persistence.Persistence
import javax.persistence.Query
import javax.persistence.criteria.CriteriaBuilder
import javax.persistence.criteria.CriteriaQuery
import javax.persistence.criteria.Predicate
import javax.persistence.criteria.Root
import javax.sql.DataSource
import org.apache.commons.configuration.XMLConfiguration
import org.apache.openjpa.conf.OpenJPAConfiguration
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI
import org.apache.openjpa.persistence.OpenJPAPersistence
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import ru.indvdum.jpa.props.Props
/**
* @author indvdum (gotoindvdum[at]gmail[dot]com)
* @since 08.11.2012 23:35:04
*
*/
public class JPADataAccessObject {
-
protected static String persistenceUnitName = null;
+
protected static Logger log = LoggerFactory.getLogger(JPADataAccessObject.class.getSimpleName());
+ protected static String persistenceUnitName = null;
protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), JPAPropertySelector.select());
protected EntityManager em = emf.createEntityManager();
- final static Logger log = LoggerFactory.getLogger(JPADataAccessObject.class.getSimpleName());
JPADataAccessObject() {
em.isOpen();
- }
protected static String getPersistenceUnitName() {
if (persistenceUnitName != null)
return persistenceUnitName;
if (persistenceUnitName == null)
persistenceUnitName = System.getProperty(Props.PERSISTANCE_UNIT_NAME_PROPERTY);
if (persistenceUnitName == null)
persistenceUnitName = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.PERSISTANCE_UNIT_NAME_PROPERTY);
if (persistenceUnitName == null)
persistenceUnitName = "database";
return persistenceUnitName;
}
+ }
protected static String getPersistenceUnitName() {
if (persistenceUnitName != null)
return persistenceUnitName;
if (persistenceUnitName == null)
persistenceUnitName = System.getProperty(Props.PERSISTANCE_UNIT_NAME_PROPERTY);
if (persistenceUnitName == null) {
try {
persistenceUnitName = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.PERSISTANCE_UNIT_NAME_PROPERTY);
} catch (MissingResourceException e) {
log.info("Configuration file " + Props.JPADAO_PROPERTY_FILE + ".properties not found");
}
}
if (persistenceUnitName == null) {
XMLConfiguration conf = new XMLConfiguration("META-INF/persistence.xml");
persistenceUnitName = conf.getString("persistence-unit[@name]");
}
if (persistenceUnitName == null)
persistenceUnitName = "database";
return persistenceUnitName;
}
public static Connection getSQLConnection() throws SQLException {
OpenJPAEntityManagerFactorySPI openjpaemf = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(emf);
diff -r 180decac2433 -r 4c2e4f602872 src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java
--- a/src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java Sun Nov 11 01:24:41 2012 +0400
+++ b/src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java Sun Nov 11 02:45:26 2012 +0400
@@ -1,13 +1,17 @@
package ru.indvdum.jpa.init;
+import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.naming.InitialContext;
import javax.naming.NamingException;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.dbcp.BasicDataSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-import ru.indvdum.jpa.dao.JPAPropertySelector;
import ru.indvdum.jpa.props.Props;
/**
@@ -17,22 +21,33 @@
*/
public class DatabaseInitializer {
+ protected static Logger log = LoggerFactory.getLogger(DatabaseInitializer.class.getSimpleName());
+
protected static String dataSource = null;
- protected static String getDataSourceName() {
+ protected static String getDataSourceName() throws ConfigurationException {
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) {
+ try {
+ dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY);
+ } catch (MissingResourceException e) {
+ log.info("Configuration file " + Props.JPADAO_PROPERTY_FILE + ".properties not found");
+ }
+ }
+ if (dataSource == null) {
+ XMLConfiguration conf = new XMLConfiguration("META-INF/persistence.xml");
+ dataSource = conf.getString("persistence-unit.non-jta-data-source");
+ }
if (dataSource == null)
dataSource = "jdbc/database";
return dataSource;
}
- public static void init() throws NamingException {
+ public static void init() throws NamingException, ConfigurationException {
String url = System.getProperty(Props.DATABASE_URL_PROPERTY);
String driver = System.getProperty(Props.DATABASE_DRIVER_PROPERTY);
String username = System.getProperty(Props.DATABASE_USERNAME_PROPERTY);
@@ -52,14 +67,14 @@
initDerby();
}
- public static void init(BasicDataSource dataSource) throws NamingException {
+ public static void init(BasicDataSource dataSource) throws NamingException, ConfigurationException {
InitialContext context = new InitialContext();
BasicDataSource existedDataSource = (BasicDataSource) context.lookup(getDataSourceName());
if (existedDataSource == null)
context.bind(getDataSourceName(), dataSource);
}
- public static void initDerby() throws NamingException {
+ public static void initDerby() throws NamingException, ConfigurationException {
System.setProperty("derby.stream.error.field", "java.lang.System.err");
BasicDataSource dataSource = new BasicDataSource();