diff --git a/.gitignore b/.gitignore index c836fce..97c9eaa 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* /target/ + +.settings/ diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 29abf99..0000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,6 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/main/java=UTF-8 -encoding//src/main/resources=UTF-8 -encoding//src/test/java=UTF-8 -encoding//src/test/resources=UTF-8 -encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 9ae306b..0000000 --- a/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,8 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=13 -org.eclipse.jdt.core.compiler.compliance=13 -org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore -org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=13 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f..0000000 --- a/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/pom.xml b/pom.xml index dd1f49f..7a4d7fd 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ 4.0.0 rdb-utils - 0.1.7 + 0.1.8 RdbUtils jar diff --git a/src/main/java/info/unterrainer/commons/rdbutils/RdbUtils.java b/src/main/java/info/unterrainer/commons/rdbutils/RdbUtils.java index f7a7a11..23441cd 100644 --- a/src/main/java/info/unterrainer/commons/rdbutils/RdbUtils.java +++ b/src/main/java/info/unterrainer/commons/rdbutils/RdbUtils.java @@ -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. + *

+ * Runs Liquibase-update to apply any changes.
+ * 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. @@ -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. + *

+ * Runs liquibase-update to apply any changes.
+ * 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 properties = getProperties(prefix); - liquibaseUpdate(classLoaderSource, properties); + liquibaseUpdate(classLoaderSource, properties, masterFileName); EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName, properties); ShutdownHook.register(() -> { if (factory != null && factory.isOpen()) @@ -90,8 +139,8 @@ private static Map getProperties(final String prefix) { return result; } - private static void liquibaseUpdate(final Class classLoaderSource, final Map properties) - throws RdbUtilException { + public static void liquibaseUpdate(final Class classLoaderSource, final Map properties, + final String masterFileName) throws RdbUtilException { Connection connection; try { log.info("getting connection from DriverManager"); @@ -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 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)