| author | indvdum (gotoindvdum[at]gmail[dot]com) |
| Sun, 11 Nov 2012 02:45:26 +0400 | |
| changeset 7 | 4c2e4f602872 |
| parent 6 | 180decac2433 |
| permissions | -rw-r--r-- |
| indvdum@5 | 1 |
package ru.indvdum.jpa.init; |
| indvdum@5 | 2 |
|
| indvdum@7 | 3 |
import java.util.MissingResourceException; |
| indvdum@5 | 4 |
import java.util.ResourceBundle; |
| indvdum@5 | 5 |
|
| indvdum@5 | 6 |
import javax.naming.InitialContext; |
| indvdum@5 | 7 |
import javax.naming.NamingException; |
| indvdum@5 | 8 |
|
| indvdum@7 | 9 |
import org.apache.commons.configuration.ConfigurationException; |
| indvdum@7 | 10 |
import org.apache.commons.configuration.XMLConfiguration; |
| indvdum@5 | 11 |
import org.apache.commons.dbcp.BasicDataSource; |
| indvdum@7 | 12 |
import org.slf4j.Logger; |
| indvdum@7 | 13 |
import org.slf4j.LoggerFactory; |
| indvdum@5 | 14 |
|
| indvdum@5 | 15 |
import ru.indvdum.jpa.props.Props; |
| indvdum@5 | 16 |
|
| indvdum@5 | 17 |
/** |
| indvdum@5 | 18 |
* @author indvdum (gotoindvdum[at]gmail[dot]com) |
| indvdum@5 | 19 |
* @since 10.11.2012 21:40:11 |
| indvdum@5 | 20 |
* |
| indvdum@5 | 21 |
*/ |
| indvdum@5 | 22 |
public class DatabaseInitializer {
|
| indvdum@5 | 23 |
|
| indvdum@7 | 24 |
protected static Logger log = LoggerFactory.getLogger(DatabaseInitializer.class.getSimpleName()); |
| indvdum@7 | 25 |
|
| indvdum@5 | 26 |
protected static String dataSource = null; |
| indvdum@5 | 27 |
|
| indvdum@7 | 28 |
protected static String getDataSourceName() throws ConfigurationException {
|
| indvdum@5 | 29 |
if (dataSource != null) |
| indvdum@5 | 30 |
return dataSource; |
| indvdum@5 | 31 |
|
| indvdum@5 | 32 |
if (dataSource == null) |
| indvdum@5 | 33 |
dataSource = System.getProperty(Props.DATA_SOURCE_PROPERTY); |
| indvdum@7 | 34 |
if (dataSource == null) {
|
| indvdum@7 | 35 |
try {
|
| indvdum@7 | 36 |
dataSource = ResourceBundle.getBundle(Props.JPADAO_PROPERTY_FILE).getString(Props.DATA_SOURCE_PROPERTY); |
| indvdum@7 | 37 |
} catch (MissingResourceException e) {
|
| indvdum@7 | 38 |
log.info("Configuration file " + Props.JPADAO_PROPERTY_FILE + ".properties not found");
|
| indvdum@7 | 39 |
} |
| indvdum@7 | 40 |
} |
| indvdum@7 | 41 |
if (dataSource == null) {
|
| indvdum@7 | 42 |
XMLConfiguration conf = new XMLConfiguration("META-INF/persistence.xml");
|
| indvdum@7 | 43 |
dataSource = conf.getString("persistence-unit.non-jta-data-source");
|
| indvdum@7 | 44 |
} |
| indvdum@5 | 45 |
if (dataSource == null) |
| indvdum@5 | 46 |
dataSource = "jdbc/database"; |
| indvdum@5 | 47 |
return dataSource; |
| indvdum@5 | 48 |
} |
| indvdum@5 | 49 |
|
| indvdum@7 | 50 |
public static void init() throws NamingException, ConfigurationException {
|
| indvdum@5 | 51 |
String url = System.getProperty(Props.DATABASE_URL_PROPERTY); |
| indvdum@5 | 52 |
String driver = System.getProperty(Props.DATABASE_DRIVER_PROPERTY); |
| indvdum@5 | 53 |
String username = System.getProperty(Props.DATABASE_USERNAME_PROPERTY); |
| indvdum@5 | 54 |
String password = System.getProperty(Props.DATABASE_PASSWORD_PROPERTY); |
| indvdum@5 | 55 |
String maxActive = System.getProperty(Props.DATABASE_MAX_ACTIVE_PROPERTY); |
| indvdum@5 | 56 |
if (url != null) {
|
| indvdum@5 | 57 |
BasicDataSource dataSource = new BasicDataSource(); |
| indvdum@5 | 58 |
dataSource.setUrl(url); |
| indvdum@5 | 59 |
dataSource.setDriverClassName(driver); |
| indvdum@5 | 60 |
dataSource.setUsername(username); |
| indvdum@5 | 61 |
dataSource.setPassword(password); |
| indvdum@5 | 62 |
if (maxActive != null && maxActive.matches("\\d+"))
|
| indvdum@5 | 63 |
dataSource.setMaxActive(Integer.parseInt(maxActive)); |
| indvdum@5 | 64 |
|
| indvdum@5 | 65 |
init(dataSource); |
| indvdum@5 | 66 |
} else |
| indvdum@5 | 67 |
initDerby(); |
| indvdum@5 | 68 |
} |
| indvdum@5 | 69 |
|
| indvdum@7 | 70 |
public static void init(BasicDataSource dataSource) throws NamingException, ConfigurationException {
|
| indvdum@5 | 71 |
InitialContext context = new InitialContext(); |
| indvdum@5 | 72 |
BasicDataSource existedDataSource = (BasicDataSource) context.lookup(getDataSourceName()); |
| indvdum@5 | 73 |
if (existedDataSource == null) |
| indvdum@5 | 74 |
context.bind(getDataSourceName(), dataSource); |
| indvdum@5 | 75 |
} |
| indvdum@5 | 76 |
|
| indvdum@7 | 77 |
public static void initDerby() throws NamingException, ConfigurationException {
|
| indvdum@5 | 78 |
System.setProperty("derby.stream.error.field", "java.lang.System.err");
|
| indvdum@5 | 79 |
|
| indvdum@5 | 80 |
BasicDataSource dataSource = new BasicDataSource(); |
| indvdum@5 | 81 |
dataSource.setUrl("jdbc:derby:memory:myDb;create=true");
|
| indvdum@5 | 82 |
dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
|
| indvdum@5 | 83 |
dataSource.setUsername("");
|
| indvdum@5 | 84 |
dataSource.setPassword("");
|
| indvdum@5 | 85 |
dataSource.setMaxActive(2); |
| indvdum@5 | 86 |
|
| indvdum@5 | 87 |
init(dataSource); |
| indvdum@5 | 88 |
} |
| indvdum@5 | 89 |
} |