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