[New] Default ignore if persistence unit exists
Can be enforced by setting li.strolch.xmlpers.allowOverwriteOnCreate property
This commit is contained in:
parent
c73fe1e313
commit
380b58fd07
|
@ -30,14 +30,17 @@ public class FileDao {
|
||||||
|
|
||||||
private final PersistenceTransaction tx;
|
private final PersistenceTransaction tx;
|
||||||
private final boolean verbose;
|
private final boolean verbose;
|
||||||
|
private final boolean allowOverwriteOnCreate;
|
||||||
private final PathBuilder pathBuilder;
|
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("TX must not be null!", tx);
|
||||||
DBC.PRE.assertNotNull("pathBuilder must not be null!", pathBuilder);
|
DBC.PRE.assertNotNull("pathBuilder must not be null!", pathBuilder);
|
||||||
this.tx = tx;
|
this.tx = tx;
|
||||||
this.pathBuilder = pathBuilder;
|
this.pathBuilder = pathBuilder;
|
||||||
this.verbose = verbose;
|
this.verbose = verbose;
|
||||||
|
this.allowOverwriteOnCreate = allowOverwriteOnCreate;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertIsIdRef(IoOperation ioOperation, ObjectRef objectRef) {
|
private void assertIsIdRef(IoOperation ioOperation, ObjectRef objectRef) {
|
||||||
|
@ -118,8 +121,8 @@ public class FileDao {
|
||||||
File directoryPath = objectRef.getPath(this.pathBuilder);
|
File directoryPath = objectRef.getPath(this.pathBuilder);
|
||||||
if (!directoryPath.getAbsolutePath().startsWith(this.pathBuilder.getRootPath().getAbsolutePath())) {
|
if (!directoryPath.getAbsolutePath().startsWith(this.pathBuilder.getRootPath().getAbsolutePath())) {
|
||||||
String msg = "The path for {0} is invalid as not child of {1}"; //$NON-NLS-1$
|
String msg = "The path for {0} is invalid as not child of {1}"; //$NON-NLS-1$
|
||||||
msg = MessageFormat
|
msg = MessageFormat.format(msg, directoryPath.getAbsolutePath(),
|
||||||
.format(msg, directoryPath.getAbsolutePath(), this.pathBuilder.getRootPath().getAbsolutePath());
|
this.pathBuilder.getRootPath().getAbsolutePath());
|
||||||
throw new IllegalArgumentException(msg);
|
throw new IllegalArgumentException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,7 +212,11 @@ public class FileDao {
|
||||||
if (path.exists()) {
|
if (path.exists()) {
|
||||||
String msg = "Persistence unit already exists for {0} at {1}"; //$NON-NLS-1$
|
String msg = "Persistence unit already exists for {0} at {1}"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, objectRef.getName(), path.getAbsolutePath());
|
msg = MessageFormat.format(msg, objectRef.getName(), path.getAbsolutePath());
|
||||||
|
if (this.allowOverwriteOnCreate) {
|
||||||
|
logger.error(msg);
|
||||||
|
} else {
|
||||||
throw new XmlPersistenceException(msg);
|
throw new XmlPersistenceException(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ public class PersistenceConstants {
|
||||||
|
|
||||||
private static final String PROP_PREFIX = "li.strolch.xmlpers.";
|
private static final String PROP_PREFIX = "li.strolch.xmlpers.";
|
||||||
public static final String PROP_VERBOSE = PROP_PREFIX + "verbose";
|
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_BASEPATH = PROP_PREFIX + "basePath";
|
||||||
public static final String PROP_DAO_FACTORY_CLASS = PROP_PREFIX + "daoFactoryClass";
|
public static final String PROP_DAO_FACTORY_CLASS = PROP_PREFIX + "daoFactoryClass";
|
||||||
public static final String PROP_XML_IO_MOD = PROP_PREFIX + "ioMode";
|
public static final String PROP_XML_IO_MOD = PROP_PREFIX + "ioMode";
|
||||||
|
|
|
@ -19,12 +19,9 @@ import static li.strolch.utils.helper.PropertiesHelper.*;
|
||||||
import static li.strolch.xmlpers.api.PersistenceConstants.*;
|
import static li.strolch.xmlpers.api.PersistenceConstants.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.Properties;
|
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.api.*;
|
||||||
import li.strolch.xmlpers.objref.LockableObject;
|
import li.strolch.xmlpers.objref.LockableObject;
|
||||||
import li.strolch.xmlpers.objref.ObjectReferenceCache;
|
import li.strolch.xmlpers.objref.ObjectReferenceCache;
|
||||||
|
@ -40,6 +37,7 @@ public class DefaultPersistenceManager implements PersistenceManager {
|
||||||
|
|
||||||
protected boolean initialized;
|
protected boolean initialized;
|
||||||
protected boolean verbose;
|
protected boolean verbose;
|
||||||
|
protected boolean allowOverwriteOnCreate;
|
||||||
protected IoMode ioMode;
|
protected IoMode ioMode;
|
||||||
private PersistenceContextFactoryDelegator ctxFactory;
|
private PersistenceContextFactoryDelegator ctxFactory;
|
||||||
private ObjectReferenceCache objectRefCache;
|
private ObjectReferenceCache objectRefCache;
|
||||||
|
@ -53,6 +51,8 @@ public class DefaultPersistenceManager implements PersistenceManager {
|
||||||
|
|
||||||
// get properties
|
// get properties
|
||||||
boolean verbose = getPropertyBool(properties, context, PROP_VERBOSE, Boolean.FALSE);
|
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()));
|
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());
|
long lockTime = getPropertyLong(properties, context, PROP_LOCK_TIME_MILLIS, LockableObject.getLockTime());
|
||||||
String basePath = getProperty(properties, context, PROP_BASEPATH, null);
|
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$
|
logger.info(MessageFormat.format("Using base path {0}", basePathF)); //$NON-NLS-1$
|
||||||
|
|
||||||
this.verbose = verbose;
|
this.verbose = verbose;
|
||||||
|
this.allowOverwriteOnCreate = allowOverwriteOnCreate;
|
||||||
this.ioMode = ioMode;
|
this.ioMode = ioMode;
|
||||||
this.ctxFactory = new PersistenceContextFactoryDelegator();
|
this.ctxFactory = new PersistenceContextFactoryDelegator();
|
||||||
|
|
||||||
|
@ -103,6 +104,6 @@ public class DefaultPersistenceManager implements PersistenceManager {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized PersistenceTransaction openTx() {
|
public synchronized PersistenceTransaction openTx() {
|
||||||
return new DefaultPersistenceTransaction(this, this.ioMode, this.verbose);
|
return new DefaultPersistenceTransaction(this, this.ioMode, this.verbose, this.allowOverwriteOnCreate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,13 +51,14 @@ public class DefaultPersistenceTransaction implements PersistenceTransaction {
|
||||||
|
|
||||||
private Set<LockableObject> lockedObjects;
|
private Set<LockableObject> lockedObjects;
|
||||||
|
|
||||||
public DefaultPersistenceTransaction(PersistenceManager manager, IoMode ioMode, boolean verbose) {
|
public DefaultPersistenceTransaction(PersistenceManager manager, IoMode ioMode, boolean verbose,
|
||||||
|
boolean allowOverwriteOnCreate) {
|
||||||
this.startTime = System.nanoTime();
|
this.startTime = System.nanoTime();
|
||||||
this.startTimeDate = new Date();
|
this.startTimeDate = new Date();
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.verbose = verbose;
|
this.verbose = verbose;
|
||||||
this.objectFilter = new ObjectFilter();
|
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.objectDao = new ObjectDao(this, this.fileDao, this.objectFilter);
|
||||||
this.metadataDao = new MetadataDao(manager.getPathBuilder(), this, verbose);
|
this.metadataDao = new MetadataDao(manager.getPathBuilder(), this, verbose);
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ public class FileDaoTest extends AbstractPersistenceTest {
|
||||||
|
|
||||||
private static final String TEST_PATH = "target/db/FileDaoTest/";
|
private static final String TEST_PATH = "target/db/FileDaoTest/";
|
||||||
private static final boolean VERBOSE = true;
|
private static final boolean VERBOSE = true;
|
||||||
|
private static final boolean ALLOW_OVERWRITE_ON_CREATE = false;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() {
|
public static void beforeClass() {
|
||||||
|
@ -51,7 +52,8 @@ public class FileDaoTest extends AbstractPersistenceTest {
|
||||||
public void testCrudSax() {
|
public void testCrudSax() {
|
||||||
setup(IoMode.SAX);
|
setup(IoMode.SAX);
|
||||||
try (PersistenceTransaction tx = this.persistenceManager.openTx()) {
|
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);
|
testCrud(tx, fileDao);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,7 +62,8 @@ public class FileDaoTest extends AbstractPersistenceTest {
|
||||||
public void testCrudDom() {
|
public void testCrudDom() {
|
||||||
setup(IoMode.DOM);
|
setup(IoMode.DOM);
|
||||||
try (PersistenceTransaction tx = this.persistenceManager.openTx()) {
|
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);
|
testCrud(tx, fileDao);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue