From 380b58fd07c8490fe6df874484ac31b178803d72 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Tue, 3 May 2022 06:48:51 +0200 Subject: [PATCH] [New] Default ignore if persistence unit exists Can be enforced by setting li.strolch.xmlpers.allowOverwriteOnCreate property --- .../main/java/li/strolch/xmlpers/api/FileDao.java | 15 +++++++++++---- .../strolch/xmlpers/api/PersistenceConstants.java | 1 + .../xmlpers/impl/DefaultPersistenceManager.java | 9 +++++---- .../impl/DefaultPersistenceTransaction.java | 5 +++-- .../java/li/strolch/xmlpers/test/FileDaoTest.java | 7 +++++-- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileDao.java b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileDao.java index b0f350edc..8ee40fd34 100644 --- a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileDao.java +++ b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileDao.java @@ -30,14 +30,17 @@ public class FileDao { private final PersistenceTransaction tx; private final boolean verbose; + private final boolean allowOverwriteOnCreate; private final PathBuilder pathBuilder; - public FileDao(PersistenceTransaction tx, PathBuilder pathBuilder, boolean verbose) { + public FileDao(PersistenceTransaction tx, PathBuilder pathBuilder, boolean verbose, + boolean allowOverwriteOnCreate) { DBC.PRE.assertNotNull("TX must not be null!", tx); DBC.PRE.assertNotNull("pathBuilder must not be null!", pathBuilder); this.tx = tx; this.pathBuilder = pathBuilder; this.verbose = verbose; + this.allowOverwriteOnCreate = allowOverwriteOnCreate; } private void assertIsIdRef(IoOperation ioOperation, ObjectRef objectRef) { @@ -118,8 +121,8 @@ public class FileDao { File directoryPath = objectRef.getPath(this.pathBuilder); if (!directoryPath.getAbsolutePath().startsWith(this.pathBuilder.getRootPath().getAbsolutePath())) { String msg = "The path for {0} is invalid as not child of {1}"; //$NON-NLS-1$ - msg = MessageFormat - .format(msg, directoryPath.getAbsolutePath(), this.pathBuilder.getRootPath().getAbsolutePath()); + msg = MessageFormat.format(msg, directoryPath.getAbsolutePath(), + this.pathBuilder.getRootPath().getAbsolutePath()); throw new IllegalArgumentException(msg); } @@ -209,7 +212,11 @@ public class FileDao { if (path.exists()) { String msg = "Persistence unit already exists for {0} at {1}"; //$NON-NLS-1$ msg = MessageFormat.format(msg, objectRef.getName(), path.getAbsolutePath()); - throw new XmlPersistenceException(msg); + if (this.allowOverwriteOnCreate) { + logger.error(msg); + } else { + throw new XmlPersistenceException(msg); + } } } } \ No newline at end of file diff --git a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/PersistenceConstants.java b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/PersistenceConstants.java index 8ffd123f5..0ad1ebc54 100644 --- a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/PersistenceConstants.java +++ b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/PersistenceConstants.java @@ -23,6 +23,7 @@ public class PersistenceConstants { private static final String PROP_PREFIX = "li.strolch.xmlpers."; public static final String PROP_VERBOSE = PROP_PREFIX + "verbose"; + public static final String PROP_ALLOW_OVERWRITE_ON_CREATE = PROP_PREFIX + "allowOverwriteOnCreate"; public static final String PROP_BASEPATH = PROP_PREFIX + "basePath"; public static final String PROP_DAO_FACTORY_CLASS = PROP_PREFIX + "daoFactoryClass"; public static final String PROP_XML_IO_MOD = PROP_PREFIX + "ioMode"; diff --git a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/impl/DefaultPersistenceManager.java b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/impl/DefaultPersistenceManager.java index ac5bbd4d3..8afa34670 100644 --- a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/impl/DefaultPersistenceManager.java +++ b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/impl/DefaultPersistenceManager.java @@ -19,12 +19,9 @@ import static li.strolch.utils.helper.PropertiesHelper.*; import static li.strolch.xmlpers.api.PersistenceConstants.*; import java.io.File; -import java.lang.reflect.Field; import java.text.MessageFormat; import java.util.Properties; -import li.strolch.utils.helper.PropertiesHelper; -import li.strolch.utils.helper.StringHelper; import li.strolch.xmlpers.api.*; import li.strolch.xmlpers.objref.LockableObject; import li.strolch.xmlpers.objref.ObjectReferenceCache; @@ -40,6 +37,7 @@ public class DefaultPersistenceManager implements PersistenceManager { protected boolean initialized; protected boolean verbose; + protected boolean allowOverwriteOnCreate; protected IoMode ioMode; private PersistenceContextFactoryDelegator ctxFactory; private ObjectReferenceCache objectRefCache; @@ -53,6 +51,8 @@ public class DefaultPersistenceManager implements PersistenceManager { // get properties boolean verbose = getPropertyBool(properties, context, PROP_VERBOSE, Boolean.FALSE); + boolean allowOverwriteOnCreate = getPropertyBool(properties, context, PROP_ALLOW_OVERWRITE_ON_CREATE, + Boolean.TRUE); IoMode ioMode = IoMode.valueOf(getProperty(properties, context, PROP_XML_IO_MOD, IoMode.DOM.name())); long lockTime = getPropertyLong(properties, context, PROP_LOCK_TIME_MILLIS, LockableObject.getLockTime()); String basePath = getProperty(properties, context, PROP_BASEPATH, null); @@ -74,6 +74,7 @@ public class DefaultPersistenceManager implements PersistenceManager { logger.info(MessageFormat.format("Using base path {0}", basePathF)); //$NON-NLS-1$ this.verbose = verbose; + this.allowOverwriteOnCreate = allowOverwriteOnCreate; this.ioMode = ioMode; this.ctxFactory = new PersistenceContextFactoryDelegator(); @@ -103,6 +104,6 @@ public class DefaultPersistenceManager implements PersistenceManager { @Override public synchronized PersistenceTransaction openTx() { - return new DefaultPersistenceTransaction(this, this.ioMode, this.verbose); + return new DefaultPersistenceTransaction(this, this.ioMode, this.verbose, this.allowOverwriteOnCreate); } } diff --git a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/impl/DefaultPersistenceTransaction.java b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/impl/DefaultPersistenceTransaction.java index b20977c2b..3e61593d9 100644 --- a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/impl/DefaultPersistenceTransaction.java +++ b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/impl/DefaultPersistenceTransaction.java @@ -51,13 +51,14 @@ public class DefaultPersistenceTransaction implements PersistenceTransaction { private Set lockedObjects; - public DefaultPersistenceTransaction(PersistenceManager manager, IoMode ioMode, boolean verbose) { + public DefaultPersistenceTransaction(PersistenceManager manager, IoMode ioMode, boolean verbose, + boolean allowOverwriteOnCreate) { this.startTime = System.nanoTime(); this.startTimeDate = new Date(); this.manager = manager; this.verbose = verbose; this.objectFilter = new ObjectFilter(); - this.fileDao = new FileDao(this, manager.getPathBuilder(), verbose); + this.fileDao = new FileDao(this, manager.getPathBuilder(), verbose, allowOverwriteOnCreate); this.objectDao = new ObjectDao(this, this.fileDao, this.objectFilter); this.metadataDao = new MetadataDao(manager.getPathBuilder(), this, verbose); diff --git a/li.strolch.xmlpers/src/test/java/li/strolch/xmlpers/test/FileDaoTest.java b/li.strolch.xmlpers/src/test/java/li/strolch/xmlpers/test/FileDaoTest.java index 12345a210..49c04bfa5 100644 --- a/li.strolch.xmlpers/src/test/java/li/strolch/xmlpers/test/FileDaoTest.java +++ b/li.strolch.xmlpers/src/test/java/li/strolch/xmlpers/test/FileDaoTest.java @@ -34,6 +34,7 @@ public class FileDaoTest extends AbstractPersistenceTest { private static final String TEST_PATH = "target/db/FileDaoTest/"; private static final boolean VERBOSE = true; + private static final boolean ALLOW_OVERWRITE_ON_CREATE = false; @BeforeClass public static void beforeClass() { @@ -51,7 +52,8 @@ public class FileDaoTest extends AbstractPersistenceTest { public void testCrudSax() { setup(IoMode.SAX); try (PersistenceTransaction tx = this.persistenceManager.openTx()) { - FileDao fileDao = new FileDao(tx, this.persistenceManager.getPathBuilder(), VERBOSE); + FileDao fileDao = new FileDao(tx, this.persistenceManager.getPathBuilder(), VERBOSE, + ALLOW_OVERWRITE_ON_CREATE); testCrud(tx, fileDao); } } @@ -60,7 +62,8 @@ public class FileDaoTest extends AbstractPersistenceTest { public void testCrudDom() { setup(IoMode.DOM); try (PersistenceTransaction tx = this.persistenceManager.openTx()) { - FileDao fileDao = new FileDao(tx, this.persistenceManager.getPathBuilder(), VERBOSE); + FileDao fileDao = new FileDao(tx, this.persistenceManager.getPathBuilder(), VERBOSE, + ALLOW_OVERWRITE_ON_CREATE); testCrud(tx, fileDao); } }