src/main/java/ru/indvdum/jpa/init/DatabaseInitializer.java
changeset 5 ccb029c396aa
child 6 180decac2433
equal deleted inserted replaced
4:eca51f09ed0a 5:ccb029c396aa
       
     1 package ru.indvdum.jpa.init;
       
     2 
       
     3 import java.util.ResourceBundle;
       
     4 
       
     5 import javax.naming.InitialContext;
       
     6 import javax.naming.NamingException;
       
     7 
       
     8 import org.apache.commons.dbcp.BasicDataSource;
       
     9 
       
    10 import ru.indvdum.jpa.dao.JPAPropertySelector;
       
    11 import ru.indvdum.jpa.props.Props;
       
    12 
       
    13 /**
       
    14  * @author indvdum (gotoindvdum[at]gmail[dot]com)
       
    15  * @since 10.11.2012 21:40:11
       
    16  * 
       
    17  */
       
    18 public class DatabaseInitializer {
       
    19 
       
    20 	protected static String dataSource = null;
       
    21 
       
    22 	protected static String getDataSourceName() {
       
    23 		if (dataSource != null)
       
    24 			return dataSource;
       
    25 
       
    26 		if (dataSource == null)
       
    27 			dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY);
       
    28 		if (dataSource == null)
       
    29 			dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY);
       
    30 		if (dataSource == null)
       
    31 			dataSource = "jdbc/database";
       
    32 		return dataSource;
       
    33 	}
       
    34 
       
    35 	public static void init() throws NamingException {
       
    36 		String url = System.getProperty(Props.DATABASE_URL_PROPERTY);
       
    37 		String driver = System.getProperty(Props.DATABASE_DRIVER_PROPERTY);
       
    38 		String username = System.getProperty(Props.DATABASE_USERNAME_PROPERTY);
       
    39 		String password = System.getProperty(Props.DATABASE_PASSWORD_PROPERTY);
       
    40 		String maxActive = System.getProperty(Props.DATABASE_MAX_ACTIVE_PROPERTY);
       
    41 		if (url != null) {
       
    42 			BasicDataSource dataSource = new BasicDataSource();
       
    43 			dataSource.setUrl(url);
       
    44 			dataSource.setDriverClassName(driver);
       
    45 			dataSource.setUsername(username);
       
    46 			dataSource.setPassword(password);
       
    47 			if (maxActive != null && maxActive.matches("\\d+"))
       
    48 				dataSource.setMaxActive(Integer.parseInt(maxActive));
       
    49 
       
    50 			init(dataSource);
       
    51 		} else
       
    52 			initDerby();
       
    53 	}
       
    54 
       
    55 	public static void init(BasicDataSource dataSource) throws NamingException {
       
    56 		InitialContext context = new InitialContext();
       
    57 		BasicDataSource existedDataSource = (BasicDataSource) context.lookup(getDataSourceName());
       
    58 		if (existedDataSource == null)
       
    59 			context.bind(getDataSourceName(), dataSource);
       
    60 	}
       
    61 
       
    62 	public static void initDerby() throws NamingException {
       
    63 		System.setProperty("derby.stream.error.field", "java.lang.System.err");
       
    64 
       
    65 		BasicDataSource dataSource = new BasicDataSource();
       
    66 		dataSource.setUrl("jdbc:derby:memory:myDb;create=true");
       
    67 		dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
       
    68 		dataSource.setUsername("");
       
    69 		dataSource.setPassword("");
       
    70 		dataSource.setMaxActive(2);
       
    71 
       
    72 		init(dataSource);
       
    73 
       
    74 		JPAPropertySelector.setSystemProperty(JPAPropertySelector.RUNTIMEENHANCEMENT, "supported");
       
    75 		JPAPropertySelector.setSystemProperty(JPAPropertySelector.SHOWSQL, "true");
       
    76 		JPAPropertySelector.setSystemProperty(JPAPropertySelector.SYNCHRONIZEDB, "true");
       
    77 	}
       
    78 }