Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Unterrainer committed Jun 11, 2021
2 parents 0fd0bea + 991dcbd commit c265fb1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/

.settings/
6 changes: 0 additions & 6 deletions .settings/org.eclipse.core.resources.prefs

This file was deleted.

8 changes: 0 additions & 8 deletions .settings/org.eclipse.jdt.core.prefs

This file was deleted.

4 changes: 0 additions & 4 deletions .settings/org.eclipse.m2e.core.prefs

This file was deleted.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<modelVersion>4.0.0</modelVersion>
<artifactId>rdb-utils</artifactId>
<version>0.1.7</version>
<version>0.1.8</version>
<name>RdbUtils</name>
<packaging>jar</packaging>

Expand Down
57 changes: 53 additions & 4 deletions src/main/java/info/unterrainer/commons/rdbutils/RdbUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ public static EntityManagerFactory createAutoclosingEntityManagerFactory(final C
return createAutoclosingEntityManagerFactory(classLoaderSource, persistenceUnitName, null);
}

/**
* Creates a new {@link EntityManagerFactory} with default-parameters or
* parameters given via environment variables.
* <p>
* Runs Liquibase-update to apply any changes.<br>
* Installs a shutdown-hook that ensures that the connection to the database is
* properly closed.
*
* @param classLoaderSource the source of the class-loader to use
* @param persistenceUnitName the name of the persistence-unit to use (from your
* persistence.xml)
* @param masterFileName the master-file (should not end with -master since
* this would auto-load it using the non-specific
* method)
* @return an {@link EntityManagerFactory}
* @throws RdbUtilException if the database could not have been opened by
* Liquibase.
*/
public static EntityManagerFactory createSpecificAutoclosingEntityManagerFactory(final Class<?> classLoaderSource,
final String persistenceUnitName, final String masterFileName) throws RdbUtilException {
return createSpecificAutoclosingEntityManagerFactory(classLoaderSource, persistenceUnitName, null,
masterFileName);
}

/**
* Creates a new {@link EntityManagerFactory} with default-parameters or
* parameters given via environment variables.
Expand All @@ -70,8 +94,33 @@ public static EntityManagerFactory createAutoclosingEntityManagerFactory(final C
*/
public static EntityManagerFactory createAutoclosingEntityManagerFactory(final Class<?> classLoaderSource,
final String persistenceUnitName, final String prefix) throws RdbUtilException {
return createSpecificAutoclosingEntityManagerFactory(classLoaderSource, persistenceUnitName, prefix, "-master");
}

/**
* Creates a new {@link EntityManagerFactory} with default-parameters or
* parameters given via environment variables.
* <p>
* Runs liquibase-update to apply any changes.<br>
* Installs a shutdown-hook that ensures that the connection to the database is
* properly closed.
*
* @param classLoaderSource the source of the class-loader to use
* @param persistenceUnitName the name of the persistence-unit to use (from your
* persistence.xml)
* @param prefix the prefix
* @param masterFileName the master-file (should not end with -master since
* this would auto-load it using the non-specific
* method)
* @return an {@link EntityManagerFactory}
* @throws RdbUtilException if the database could not have been opened by
* liquibase.
*/
public static EntityManagerFactory createSpecificAutoclosingEntityManagerFactory(final Class<?> classLoaderSource,
final String persistenceUnitName, final String prefix, final String masterFileName)
throws RdbUtilException {
Map<String, String> properties = getProperties(prefix);
liquibaseUpdate(classLoaderSource, properties);
liquibaseUpdate(classLoaderSource, properties, masterFileName);
EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
ShutdownHook.register(() -> {
if (factory != null && factory.isOpen())
Expand All @@ -90,8 +139,8 @@ private static Map<String, String> getProperties(final String prefix) {
return result;
}

private static void liquibaseUpdate(final Class<?> classLoaderSource, final Map<String, String> properties)
throws RdbUtilException {
public static void liquibaseUpdate(final Class<?> classLoaderSource, final Map<String, String> properties,
final String masterFileName) throws RdbUtilException {
Connection connection;
try {
log.info("getting connection from DriverManager");
Expand All @@ -103,7 +152,7 @@ private static void liquibaseUpdate(final Class<?> classLoaderSource, final Map<
.findCorrectDatabaseImplementation(new JdbcConnection(connection));
log.info("scanning file-system for master-changelog files");
List<Path> masterLogFiles = Resources.walk(classLoaderSource,
path -> path.toString().endsWith("-master.xml"));
path -> path.toString().endsWith(masterFileName + ".xml"));
for (Path p : masterLogFiles)
log.info("found file [{}]", p.toString());
if (masterLogFiles.size() == 0)
Expand Down

0 comments on commit c265fb1

Please sign in to comment.