src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java
changeset 7 4c2e4f602872
parent 6 180decac2433
     1.1 --- a/src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java	Sun Nov 11 01:24:41 2012 +0400
     1.2 +++ b/src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java	Sun Nov 11 02:45:26 2012 +0400
     1.3 @@ -1,13 +1,17 @@
     1.4  package ru.indvdum.jpa.init;
     1.5  
     1.6 +import java.util.MissingResourceException;
     1.7  import java.util.ResourceBundle;
     1.8  
     1.9  import javax.naming.InitialContext;
    1.10  import javax.naming.NamingException;
    1.11  
    1.12 +import org.apache.commons.configuration.ConfigurationException;
    1.13 +import org.apache.commons.configuration.XMLConfiguration;
    1.14  import org.apache.commons.dbcp.BasicDataSource;
    1.15 +import org.slf4j.Logger;
    1.16 +import org.slf4j.LoggerFactory;
    1.17  
    1.18 -import ru.indvdum.jpa.dao.JPAPropertySelector;
    1.19  import ru.indvdum.jpa.props.Props;
    1.20  
    1.21  /**
    1.22 @@ -17,22 +21,33 @@
    1.23   */
    1.24  public class DatabaseInitializer {
    1.25  
    1.26 +	protected static Logger log = LoggerFactory.getLogger(DatabaseInitializer.class.getSimpleName());
    1.27 +
    1.28  	protected static String dataSource = null;
    1.29  
    1.30 -	protected static String getDataSourceName() {
    1.31 +	protected static String getDataSourceName() throws ConfigurationException {
    1.32  		if (dataSource != null)
    1.33  			return dataSource;
    1.34  
    1.35  		if (dataSource == null)
    1.36  			dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY);
    1.37 -		if (dataSource == null)
    1.38 -			dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY);
    1.39 +		if (dataSource == null) {
    1.40 +			try {
    1.41 +				dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY);
    1.42 +			} catch (MissingResourceException e) {
    1.43 +				log.info("Configuration file " + Props.JPADAO_PROPERTY_FILE + ".properties not found");
    1.44 +			}
    1.45 +		}
    1.46 +		if (dataSource == null) {
    1.47 +			XMLConfiguration conf = new XMLConfiguration("META-INF/persistence.xml");
    1.48 +			dataSource = conf.getString("persistence-unit.non-jta-data-source");
    1.49 +		}
    1.50  		if (dataSource == null)
    1.51  			dataSource = "jdbc/database";
    1.52  		return dataSource;
    1.53  	}
    1.54  
    1.55 -	public static void init() throws NamingException {
    1.56 +	public static void init() throws NamingException, ConfigurationException {
    1.57  		String url = System.getProperty(Props.DATABASE_URL_PROPERTY);
    1.58  		String driver = System.getProperty(Props.DATABASE_DRIVER_PROPERTY);
    1.59  		String username = System.getProperty(Props.DATABASE_USERNAME_PROPERTY);
    1.60 @@ -52,14 +67,14 @@
    1.61  			initDerby();
    1.62  	}
    1.63  
    1.64 -	public static void init(BasicDataSource dataSource) throws NamingException {
    1.65 +	public static void init(BasicDataSource dataSource) throws NamingException, ConfigurationException {
    1.66  		InitialContext context = new InitialContext();
    1.67  		BasicDataSource existedDataSource = (BasicDataSource) context.lookup(getDataSourceName());
    1.68  		if (existedDataSource == null)
    1.69  			context.bind(getDataSourceName(), dataSource);
    1.70  	}
    1.71  
    1.72 -	public static void initDerby() throws NamingException {
    1.73 +	public static void initDerby() throws NamingException, ConfigurationException {
    1.74  		System.setProperty("derby.stream.error.field", "java.lang.System.err");
    1.75  
    1.76  		BasicDataSource dataSource = new BasicDataSource();