Skip to content

Commit c265fb1

Browse files
author
Gerald Unterrainer
committed
Merge branch 'develop'
2 parents 0fd0bea + 991dcbd commit c265fb1

File tree

6 files changed

+56
-23
lines changed

6 files changed

+56
-23
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@
2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
2424
/target/
25+
26+
.settings/

.settings/org.eclipse.core.resources.prefs

-6
This file was deleted.

.settings/org.eclipse.jdt.core.prefs

-8
This file was deleted.

.settings/org.eclipse.m2e.core.prefs

-4
This file was deleted.

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>rdb-utils</artifactId>
13-
<version>0.1.7</version>
13+
<version>0.1.8</version>
1414
<name>RdbUtils</name>
1515
<packaging>jar</packaging>
1616

src/main/java/info/unterrainer/commons/rdbutils/RdbUtils.java

+53-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ public static EntityManagerFactory createAutoclosingEntityManagerFactory(final C
5252
return createAutoclosingEntityManagerFactory(classLoaderSource, persistenceUnitName, null);
5353
}
5454

55+
/**
56+
* Creates a new {@link EntityManagerFactory} with default-parameters or
57+
* parameters given via environment variables.
58+
* <p>
59+
* Runs Liquibase-update to apply any changes.<br>
60+
* Installs a shutdown-hook that ensures that the connection to the database is
61+
* properly closed.
62+
*
63+
* @param classLoaderSource the source of the class-loader to use
64+
* @param persistenceUnitName the name of the persistence-unit to use (from your
65+
* persistence.xml)
66+
* @param masterFileName the master-file (should not end with -master since
67+
* this would auto-load it using the non-specific
68+
* method)
69+
* @return an {@link EntityManagerFactory}
70+
* @throws RdbUtilException if the database could not have been opened by
71+
* Liquibase.
72+
*/
73+
public static EntityManagerFactory createSpecificAutoclosingEntityManagerFactory(final Class<?> classLoaderSource,
74+
final String persistenceUnitName, final String masterFileName) throws RdbUtilException {
75+
return createSpecificAutoclosingEntityManagerFactory(classLoaderSource, persistenceUnitName, null,
76+
masterFileName);
77+
}
78+
5579
/**
5680
* Creates a new {@link EntityManagerFactory} with default-parameters or
5781
* parameters given via environment variables.
@@ -70,8 +94,33 @@ public static EntityManagerFactory createAutoclosingEntityManagerFactory(final C
7094
*/
7195
public static EntityManagerFactory createAutoclosingEntityManagerFactory(final Class<?> classLoaderSource,
7296
final String persistenceUnitName, final String prefix) throws RdbUtilException {
97+
return createSpecificAutoclosingEntityManagerFactory(classLoaderSource, persistenceUnitName, prefix, "-master");
98+
}
99+
100+
/**
101+
* Creates a new {@link EntityManagerFactory} with default-parameters or
102+
* parameters given via environment variables.
103+
* <p>
104+
* Runs liquibase-update to apply any changes.<br>
105+
* Installs a shutdown-hook that ensures that the connection to the database is
106+
* properly closed.
107+
*
108+
* @param classLoaderSource the source of the class-loader to use
109+
* @param persistenceUnitName the name of the persistence-unit to use (from your
110+
* persistence.xml)
111+
* @param prefix the prefix
112+
* @param masterFileName the master-file (should not end with -master since
113+
* this would auto-load it using the non-specific
114+
* method)
115+
* @return an {@link EntityManagerFactory}
116+
* @throws RdbUtilException if the database could not have been opened by
117+
* liquibase.
118+
*/
119+
public static EntityManagerFactory createSpecificAutoclosingEntityManagerFactory(final Class<?> classLoaderSource,
120+
final String persistenceUnitName, final String prefix, final String masterFileName)
121+
throws RdbUtilException {
73122
Map<String, String> properties = getProperties(prefix);
74-
liquibaseUpdate(classLoaderSource, properties);
123+
liquibaseUpdate(classLoaderSource, properties, masterFileName);
75124
EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
76125
ShutdownHook.register(() -> {
77126
if (factory != null && factory.isOpen())
@@ -90,8 +139,8 @@ private static Map<String, String> getProperties(final String prefix) {
90139
return result;
91140
}
92141

93-
private static void liquibaseUpdate(final Class<?> classLoaderSource, final Map<String, String> properties)
94-
throws RdbUtilException {
142+
public static void liquibaseUpdate(final Class<?> classLoaderSource, final Map<String, String> properties,
143+
final String masterFileName) throws RdbUtilException {
95144
Connection connection;
96145
try {
97146
log.info("getting connection from DriverManager");
@@ -103,7 +152,7 @@ private static void liquibaseUpdate(final Class<?> classLoaderSource, final Map<
103152
.findCorrectDatabaseImplementation(new JdbcConnection(connection));
104153
log.info("scanning file-system for master-changelog files");
105154
List<Path> masterLogFiles = Resources.walk(classLoaderSource,
106-
path -> path.toString().endsWith("-master.xml"));
155+
path -> path.toString().endsWith(masterFileName + ".xml"));
107156
for (Path p : masterLogFiles)
108157
log.info("found file [{}]", p.toString());
109158
if (masterLogFiles.size() == 0)

0 commit comments

Comments
 (0)