src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java
changeset 5 ccb029c396aa
child 6 180decac2433
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java	Sat Nov 10 22:54:48 2012 +0400
     1.3 @@ -0,0 +1,78 @@
     1.4 +package ru.indvdum.jpa.init;
     1.5 +
     1.6 +import java.util.ResourceBundle;
     1.7 +
     1.8 +import javax.naming.InitialContext;
     1.9 +import javax.naming.NamingException;
    1.10 +
    1.11 +import org.apache.commons.dbcp.BasicDataSource;
    1.12 +
    1.13 +import ru.indvdum.jpa.dao.JPAPropertySelector;
    1.14 +import ru.indvdum.jpa.props.Props;
    1.15 +
    1.16 +/**
    1.17 + * @author indvdum (gotoindvdum[at]gmail[dot]com)
    1.18 + * @since 10.11.2012 21:40:11
    1.19 + * 
    1.20 + */
    1.21 +public class DatabaseInitializer {
    1.22 +
    1.23 +	protected static String dataSource = null;
    1.24 +
    1.25 +	protected static String getDataSourceName() {
    1.26 +		if (dataSource != null)
    1.27 +			return dataSource;
    1.28 +
    1.29 +		if (dataSource == null)
    1.30 +			dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY);
    1.31 +		if (dataSource == null)
    1.32 +			dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY);
    1.33 +		if (dataSource == null)
    1.34 +			dataSource = "jdbc/database";
    1.35 +		return dataSource;
    1.36 +	}
    1.37 +
    1.38 +	public static void init() throws NamingException {
    1.39 +		String url = System.getProperty(Props.DATABASE_URL_PROPERTY);
    1.40 +		String driver = System.getProperty(Props.DATABASE_DRIVER_PROPERTY);
    1.41 +		String username = System.getProperty(Props.DATABASE_USERNAME_PROPERTY);
    1.42 +		String password = System.getProperty(Props.DATABASE_PASSWORD_PROPERTY);
    1.43 +		String maxActive = System.getProperty(Props.DATABASE_MAX_ACTIVE_PROPERTY);
    1.44 +		if (url != null) {
    1.45 +			BasicDataSource dataSource = new BasicDataSource();
    1.46 +			dataSource.setUrl(url);
    1.47 +			dataSource.setDriverClassName(driver);
    1.48 +			dataSource.setUsername(username);
    1.49 +			dataSource.setPassword(password);
    1.50 +			if (maxActive != null && maxActive.matches("\\d+"))
    1.51 +				dataSource.setMaxActive(Integer.parseInt(maxActive));
    1.52 +
    1.53 +			init(dataSource);
    1.54 +		} else
    1.55 +			initDerby();
    1.56 +	}
    1.57 +
    1.58 +	public static void init(BasicDataSource dataSource) throws NamingException {
    1.59 +		InitialContext context = new InitialContext();
    1.60 +		BasicDataSource existedDataSource = (BasicDataSource) context.lookup(getDataSourceName());
    1.61 +		if (existedDataSource == null)
    1.62 +			context.bind(getDataSourceName(), dataSource);
    1.63 +	}
    1.64 +
    1.65 +	public static void initDerby() throws NamingException {
    1.66 +		System.setProperty("derby.stream.error.field", "java.lang.System.err");
    1.67 +
    1.68 +		BasicDataSource dataSource = new BasicDataSource();
    1.69 +		dataSource.setUrl("jdbc:derby:memory:myDb;create=true");
    1.70 +		dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
    1.71 +		dataSource.setUsername("");
    1.72 +		dataSource.setPassword("");
    1.73 +		dataSource.setMaxActive(2);
    1.74 +
    1.75 +		init(dataSource);
    1.76 +
    1.77 +		JPAPropertySelector.setSystemProperty(JPAPropertySelector.RUNTIMEENHANCEMENT, "supported");
    1.78 +		JPAPropertySelector.setSystemProperty(JPAPropertySelector.SHOWSQL, "true");
    1.79 +		JPAPropertySelector.setSystemProperty(JPAPropertySelector.SYNCHRONIZEDB, "true");
    1.80 +	}
    1.81 +}