[Major] refactored use of log4j to slf4j
This commit is contained in:
parent
eec0b67135
commit
38466685a0
22
pom.xml
22
pom.xml
|
@ -117,16 +117,28 @@
|
||||||
<version>4.10</version>
|
<version>4.10</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>log4j</groupId>
|
|
||||||
<artifactId>log4j</artifactId>
|
|
||||||
<version>1.2.17</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ch.eitchnet</groupId>
|
<groupId>ch.eitchnet</groupId>
|
||||||
<artifactId>ch.eitchnet.utils</artifactId>
|
<artifactId>ch.eitchnet.utils</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.eitchnet</groupId>
|
||||||
|
<artifactId>ch.eitchnet.log4j</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>1.7.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
<version>1.7.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -31,7 +31,8 @@ import java.util.Set;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
@ -50,7 +51,7 @@ public class XmlFilePersister {
|
||||||
//
|
//
|
||||||
private static final String XML_DEFAULT_ENCODING = "UTF-8";
|
private static final String XML_DEFAULT_ENCODING = "UTF-8";
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(XmlFilePersister.class);
|
private static final Logger logger = LoggerFactory.getLogger(XmlFilePersister.class);
|
||||||
|
|
||||||
private boolean verbose;
|
private boolean verbose;
|
||||||
private XmlPersistencePathBuilder xmlPathHelper;
|
private XmlPersistencePathBuilder xmlPathHelper;
|
||||||
|
@ -74,9 +75,9 @@ public class XmlFilePersister {
|
||||||
|
|
||||||
File pathF;
|
File pathF;
|
||||||
if (subType != null)
|
if (subType != null)
|
||||||
pathF = xmlPathHelper.getPathF(type, subType, id);
|
pathF = this.xmlPathHelper.getPathF(type, subType, id);
|
||||||
else
|
else
|
||||||
pathF = xmlPathHelper.getPathF(type, id);
|
pathF = this.xmlPathHelper.getPathF(type, id);
|
||||||
|
|
||||||
// if this is a new file, then check create parents, if the don't exist
|
// if this is a new file, then check create parents, if the don't exist
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
|
@ -87,15 +88,15 @@ public class XmlFilePersister {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Persisting " + type + " / " + subType + " / " + id + " to " + pathF.getAbsolutePath() + "...");
|
XmlFilePersister.logger.info("Persisting " + type + " / " + subType + " / " + id + " to " + pathF.getAbsolutePath() + "...");
|
||||||
|
|
||||||
BufferedOutputStream outStream = null;
|
BufferedOutputStream outStream = null;
|
||||||
try {
|
try {
|
||||||
|
|
||||||
outStream = new BufferedOutputStream(new FileOutputStream(pathF));
|
outStream = new BufferedOutputStream(new FileOutputStream(pathF));
|
||||||
|
|
||||||
OutputFormat outputFormat = new OutputFormat("XML", XML_DEFAULT_ENCODING, true);
|
OutputFormat outputFormat = new OutputFormat("XML", XmlFilePersister.XML_DEFAULT_ENCODING, true);
|
||||||
outputFormat.setIndent(1);
|
outputFormat.setIndent(1);
|
||||||
outputFormat.setIndenting(true);
|
outputFormat.setIndenting(true);
|
||||||
//of.setDoctype(null, null);
|
//of.setDoctype(null, null);
|
||||||
|
@ -113,13 +114,13 @@ public class XmlFilePersister {
|
||||||
try {
|
try {
|
||||||
outStream.close();
|
outStream.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error(e, e);
|
XmlFilePersister.logger.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Done.");
|
XmlFilePersister.logger.info("Done.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -131,24 +132,24 @@ public class XmlFilePersister {
|
||||||
|
|
||||||
File pathF;
|
File pathF;
|
||||||
if (subType != null)
|
if (subType != null)
|
||||||
pathF = xmlPathHelper.getPathF(type, subType, id);
|
pathF = this.xmlPathHelper.getPathF(type, subType, id);
|
||||||
else
|
else
|
||||||
pathF = xmlPathHelper.getPathF(type, id);
|
pathF = this.xmlPathHelper.getPathF(type, id);
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Remove persistence file for " + type + " / " + subType + " / " + id + " from "
|
XmlFilePersister.logger.info("Remove persistence file for " + type + " / " + subType + " / " + id + " from "
|
||||||
+ pathF.getAbsolutePath() + "...");
|
+ pathF.getAbsolutePath() + "...");
|
||||||
|
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
logger.error("Persistence file for " + type + " / " + subType + " / " + id + " does not exist at "
|
XmlFilePersister.logger.error("Persistence file for " + type + " / " + subType + " / " + id + " does not exist at "
|
||||||
+ pathF.getAbsolutePath());
|
+ pathF.getAbsolutePath());
|
||||||
} else if (!pathF.delete()) {
|
} else if (!pathF.delete()) {
|
||||||
throw new XmlPersistenceExecption("Could not delete persistence file for " + type + " / " + subType + " / "
|
throw new XmlPersistenceExecption("Could not delete persistence file for " + type + " / " + subType + " / "
|
||||||
+ id + " at " + pathF.getAbsolutePath());
|
+ id + " at " + pathF.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Done.");
|
XmlFilePersister.logger.info("Done.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -159,22 +160,22 @@ public class XmlFilePersister {
|
||||||
|
|
||||||
File pathF;
|
File pathF;
|
||||||
if (subType == null)
|
if (subType == null)
|
||||||
pathF = xmlPathHelper.getPathF(type);
|
pathF = this.xmlPathHelper.getPathF(type);
|
||||||
else
|
else
|
||||||
pathF = xmlPathHelper.getPathF(type, subType);
|
pathF = this.xmlPathHelper.getPathF(type, subType);
|
||||||
|
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
if (subType == null)
|
if (subType == null)
|
||||||
logger.error("Path for " + type + " at " + pathF.getAbsolutePath()
|
XmlFilePersister.logger.error("Path for " + type + " at " + pathF.getAbsolutePath()
|
||||||
+ " does not exist, so removing not possible!");
|
+ " does not exist, so removing not possible!");
|
||||||
else
|
else
|
||||||
logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
||||||
+ " does not exist, so removing not possible!");
|
+ " does not exist, so removing not possible!");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
File[] filesToRemove = pathF.listFiles();
|
File[] filesToRemove = pathF.listFiles();
|
||||||
boolean removed = FileHelper.deleteFiles(filesToRemove, verbose);
|
boolean removed = FileHelper.deleteFiles(filesToRemove, this.verbose);
|
||||||
|
|
||||||
if (!removed) {
|
if (!removed) {
|
||||||
if (subType == null)
|
if (subType == null)
|
||||||
|
@ -201,7 +202,7 @@ public class XmlFilePersister {
|
||||||
|
|
||||||
File pathF = this.xmlPathHelper.getPathF(type, subType);
|
File pathF = this.xmlPathHelper.getPathF(type, subType);
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
||||||
+ " does not exist, so no objects exist!");
|
+ " does not exist, so no objects exist!");
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
@ -212,8 +213,8 @@ public class XmlFilePersister {
|
||||||
keySet.add(name.substring(0, name.length() - XmlPersistencePathBuilder.FILE_EXT.length()));
|
keySet.add(name.substring(0, name.length() - XmlPersistencePathBuilder.FILE_EXT.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Found " + keySet.size() + " elements for " + type + " / " + subType);
|
XmlFilePersister.logger.info("Found " + keySet.size() + " elements for " + type + " / " + subType);
|
||||||
|
|
||||||
return keySet;
|
return keySet;
|
||||||
}
|
}
|
||||||
|
@ -221,7 +222,7 @@ public class XmlFilePersister {
|
||||||
// otherwise we need to iterate any existing subTypes and create a combined key set
|
// otherwise we need to iterate any existing subTypes and create a combined key set
|
||||||
File pathF = this.xmlPathHelper.getPathF(type);
|
File pathF = this.xmlPathHelper.getPathF(type);
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
||||||
+ " does not exist, so no objects exist!");
|
+ " does not exist, so no objects exist!");
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
@ -232,17 +233,17 @@ public class XmlFilePersister {
|
||||||
for (File subTypeFile : subTypeFiles) {
|
for (File subTypeFile : subTypeFiles) {
|
||||||
|
|
||||||
if (subTypeFile.isFile()) {
|
if (subTypeFile.isFile()) {
|
||||||
keySet.add(xmlPathHelper.getId(subTypeFile.getName()));
|
keySet.add(this.xmlPathHelper.getId(subTypeFile.getName()));
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
for (File f : subTypeFile.listFiles()) {
|
for (File f : subTypeFile.listFiles()) {
|
||||||
keySet.add(xmlPathHelper.getId(f.getName()));
|
keySet.add(this.xmlPathHelper.getId(f.getName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Found " + keySet.size() + " elements for " + type);
|
XmlFilePersister.logger.info("Found " + keySet.size() + " elements for " + type);
|
||||||
|
|
||||||
return keySet;
|
return keySet;
|
||||||
}
|
}
|
||||||
|
@ -261,15 +262,15 @@ public class XmlFilePersister {
|
||||||
|
|
||||||
File pathF = this.xmlPathHelper.getPathF(type, subType);
|
File pathF = this.xmlPathHelper.getPathF(type, subType);
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
||||||
+ " does not exist, so no objects exist!");
|
+ " does not exist, so no objects exist!");
|
||||||
return 0l;
|
return 0l;
|
||||||
}
|
}
|
||||||
|
|
||||||
int length = pathF.listFiles().length;
|
int length = pathF.listFiles().length;
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Found " + length + " elements for " + type + " / " + subType);
|
XmlFilePersister.logger.info("Found " + length + " elements for " + type + " / " + subType);
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
@ -279,7 +280,7 @@ public class XmlFilePersister {
|
||||||
|
|
||||||
File pathF = this.xmlPathHelper.getPathF(type);
|
File pathF = this.xmlPathHelper.getPathF(type);
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
||||||
+ " does not exist, so no objects exist!");
|
+ " does not exist, so no objects exist!");
|
||||||
return 0l;
|
return 0l;
|
||||||
}
|
}
|
||||||
|
@ -296,8 +297,8 @@ public class XmlFilePersister {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Found " + numberOfFiles + " elements for " + type);
|
XmlFilePersister.logger.info("Found " + numberOfFiles + " elements for " + type);
|
||||||
|
|
||||||
return numberOfFiles;
|
return numberOfFiles;
|
||||||
}
|
}
|
||||||
|
@ -361,7 +362,7 @@ public class XmlFilePersister {
|
||||||
|
|
||||||
File pathF = this.xmlPathHelper.getPathF(type, subType);
|
File pathF = this.xmlPathHelper.getPathF(type, subType);
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
||||||
+ " does not exist, so no objects exist!");
|
+ " does not exist, so no objects exist!");
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
@ -371,8 +372,8 @@ public class XmlFilePersister {
|
||||||
list.add(parseFile(subTypeF, docBuilder));
|
list.add(parseFile(subTypeF, docBuilder));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Loaded " + list.size() + " elements for " + type + " / " + subType);
|
XmlFilePersister.logger.info("Loaded " + list.size() + " elements for " + type + " / " + subType);
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -382,7 +383,7 @@ public class XmlFilePersister {
|
||||||
|
|
||||||
File pathF = this.xmlPathHelper.getPathF(type);
|
File pathF = this.xmlPathHelper.getPathF(type);
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " at " + pathF.getAbsolutePath()
|
||||||
+ " does not exist, so no objects exist!");
|
+ " does not exist, so no objects exist!");
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
@ -402,8 +403,8 @@ public class XmlFilePersister {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Loaded " + list.size() + " elements for " + type);
|
XmlFilePersister.logger.info("Loaded " + list.size() + " elements for " + type);
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -420,7 +421,7 @@ public class XmlFilePersister {
|
||||||
|
|
||||||
File pathF = this.xmlPathHelper.getPathF(type, subType, id);
|
File pathF = this.xmlPathHelper.getPathF(type, subType, id);
|
||||||
if (!pathF.exists()) {
|
if (!pathF.exists()) {
|
||||||
logger.error("Path for " + type + " / " + subType + " / " + id + " at " + pathF.getAbsolutePath()
|
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " / " + id + " at " + pathF.getAbsolutePath()
|
||||||
+ " does not exist, so object does not exist!");
|
+ " does not exist, so object does not exist!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,8 @@
|
||||||
*/
|
*/
|
||||||
package ch.eitchnet.xmlpers;
|
package ch.eitchnet.xmlpers;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import ch.eitchnet.utils.helper.SystemHelper;
|
import ch.eitchnet.utils.helper.SystemHelper;
|
||||||
import ch.eitchnet.utils.objectfilter.ITransactionObject;
|
import ch.eitchnet.utils.objectfilter.ITransactionObject;
|
||||||
|
@ -46,7 +47,7 @@ public class XmlPersistenceHandler {
|
||||||
*/
|
*/
|
||||||
public static final String CONFIG_DAO_FACTORY_CLASS = "ch.eitchnet.xmlpers.config.daoFactoryClass";
|
public static final String CONFIG_DAO_FACTORY_CLASS = "ch.eitchnet.xmlpers.config.daoFactoryClass";
|
||||||
|
|
||||||
protected static final Logger logger = Logger.getLogger(XmlPersistenceHandler.class);
|
protected static final Logger logger = LoggerFactory.getLogger(XmlPersistenceHandler.class);
|
||||||
|
|
||||||
protected boolean verbose;
|
protected boolean verbose;
|
||||||
protected ThreadLocal<XmlPersistenceTransaction> xmlPersistenceTxThreadLocal;
|
protected ThreadLocal<XmlPersistenceTransaction> xmlPersistenceTxThreadLocal;
|
||||||
|
@ -58,18 +59,18 @@ public class XmlPersistenceHandler {
|
||||||
*/
|
*/
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
|
|
||||||
String basePath = SystemHelper.getProperty(XmlPersistenceHandler.class.getSimpleName(), CONFIG_BASEPATH, null);
|
String basePath = SystemHelper.getProperty(XmlPersistenceHandler.class.getSimpleName(), XmlPersistenceHandler.CONFIG_BASEPATH, null);
|
||||||
verbose = SystemHelper.getPropertyBool(XmlPersistenceHandler.class.getSimpleName(), CONFIG_VERBOSE,
|
this.verbose = SystemHelper.getPropertyBool(XmlPersistenceHandler.class.getSimpleName(), XmlPersistenceHandler.CONFIG_VERBOSE,
|
||||||
Boolean.FALSE).booleanValue();
|
Boolean.FALSE).booleanValue();
|
||||||
|
|
||||||
// get class to use as transaction
|
// get class to use as transaction
|
||||||
String daoFactoryClassName = SystemHelper.getProperty(XmlPersistenceHandler.class.getSimpleName(),
|
String daoFactoryClassName = SystemHelper.getProperty(XmlPersistenceHandler.class.getSimpleName(),
|
||||||
CONFIG_DAO_FACTORY_CLASS, null);
|
XmlPersistenceHandler.CONFIG_DAO_FACTORY_CLASS, null);
|
||||||
try {
|
try {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Class<XmlDaoFactory> xmlDaoFactoryClass = (Class<XmlDaoFactory>) Class.forName(daoFactoryClassName);
|
Class<XmlDaoFactory> xmlDaoFactoryClass = (Class<XmlDaoFactory>) Class.forName(daoFactoryClassName);
|
||||||
|
|
||||||
xmlDaoFactory = xmlDaoFactoryClass.newInstance();
|
this.xmlDaoFactory = xmlDaoFactoryClass.newInstance();
|
||||||
|
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new XmlPersistenceExecption("XmlDaoFactory class does not exist " + daoFactoryClassName, e);
|
throw new XmlPersistenceExecption("XmlDaoFactory class does not exist " + daoFactoryClassName, e);
|
||||||
|
@ -78,10 +79,10 @@ public class XmlPersistenceHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
XmlPersistencePathBuilder pathBuilder = new XmlPersistencePathBuilder(basePath);
|
XmlPersistencePathBuilder pathBuilder = new XmlPersistencePathBuilder(basePath);
|
||||||
persister = new XmlFilePersister(pathBuilder, verbose);
|
this.persister = new XmlFilePersister(pathBuilder, this.verbose);
|
||||||
|
|
||||||
// initialize the Thread local object which is used per transaction
|
// initialize the Thread local object which is used per transaction
|
||||||
xmlPersistenceTxThreadLocal = new ThreadLocal<XmlPersistenceTransaction>();
|
this.xmlPersistenceTxThreadLocal = new ThreadLocal<XmlPersistenceTransaction>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,8 +90,8 @@ public class XmlPersistenceHandler {
|
||||||
*/
|
*/
|
||||||
public XmlPersistenceTransaction openTx() {
|
public XmlPersistenceTransaction openTx() {
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Opening new transaction...");
|
XmlPersistenceHandler.logger.info("Opening new transaction...");
|
||||||
|
|
||||||
// make sure no previous filter exists
|
// make sure no previous filter exists
|
||||||
XmlPersistenceTransaction xmlPersistenceTx = this.xmlPersistenceTxThreadLocal.get();
|
XmlPersistenceTransaction xmlPersistenceTx = this.xmlPersistenceTxThreadLocal.get();
|
||||||
|
@ -100,7 +101,7 @@ public class XmlPersistenceHandler {
|
||||||
// set a new persistence transaction object
|
// set a new persistence transaction object
|
||||||
ObjectFilter<ITransactionObject> objectFilter = new ObjectFilter<ITransactionObject>();
|
ObjectFilter<ITransactionObject> objectFilter = new ObjectFilter<ITransactionObject>();
|
||||||
xmlPersistenceTx = new XmlPersistenceTransaction();
|
xmlPersistenceTx = new XmlPersistenceTransaction();
|
||||||
xmlPersistenceTx.initialize(persister, xmlDaoFactory, objectFilter, verbose);
|
xmlPersistenceTx.initialize(this.persister, this.xmlDaoFactory, objectFilter, this.verbose);
|
||||||
|
|
||||||
this.xmlPersistenceTxThreadLocal.set(xmlPersistenceTx);
|
this.xmlPersistenceTxThreadLocal.set(xmlPersistenceTx);
|
||||||
|
|
||||||
|
@ -123,8 +124,8 @@ public class XmlPersistenceHandler {
|
||||||
*/
|
*/
|
||||||
public void commitTx() {
|
public void commitTx() {
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Committing transaction...");
|
XmlPersistenceHandler.logger.info("Committing transaction...");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
XmlPersistenceTransaction xmlPersistenceTx = this.xmlPersistenceTxThreadLocal.get();
|
XmlPersistenceTransaction xmlPersistenceTx = this.xmlPersistenceTxThreadLocal.get();
|
||||||
|
|
|
@ -22,14 +22,15 @@ package ch.eitchnet.xmlpers;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class XmlPersistencePathBuilder {
|
public class XmlPersistencePathBuilder {
|
||||||
private static final Logger logger = Logger.getLogger(XmlPersistencePathBuilder.class);
|
private static final Logger logger = LoggerFactory.getLogger(XmlPersistencePathBuilder.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -39,7 +40,7 @@ public class XmlPersistencePathBuilder {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static final int EXT_LENGTH = FILE_EXT.length();
|
public static final int EXT_LENGTH = XmlPersistencePathBuilder.FILE_EXT.length();
|
||||||
|
|
||||||
private String basePath;
|
private String basePath;
|
||||||
|
|
||||||
|
@ -61,7 +62,7 @@ public class XmlPersistencePathBuilder {
|
||||||
throw new XmlPersistenceExecption("Failed to build canonical path from " + basePath, e);
|
throw new XmlPersistenceExecption("Failed to build canonical path from " + basePath, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Using base path " + basePath);
|
XmlPersistencePathBuilder.logger.info("Using base path " + basePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,7 +70,7 @@ public class XmlPersistencePathBuilder {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String getFilename(String id) {
|
public String getFilename(String id) {
|
||||||
return id.concat(FILE_EXT);
|
return id.concat(XmlPersistencePathBuilder.FILE_EXT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,11 +78,11 @@ public class XmlPersistencePathBuilder {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String getId(String filename) {
|
public String getId(String filename) {
|
||||||
if (filename.charAt(filename.length() - EXT_LENGTH) != '.')
|
if (filename.charAt(filename.length() - XmlPersistencePathBuilder.EXT_LENGTH) != '.')
|
||||||
throw new XmlPersistenceExecption("The filename does not have a . at index "
|
throw new XmlPersistenceExecption("The filename does not have a . at index "
|
||||||
+ (filename.length() - EXT_LENGTH));
|
+ (filename.length() - XmlPersistencePathBuilder.EXT_LENGTH));
|
||||||
|
|
||||||
return filename.substring(0, filename.length() - EXT_LENGTH);
|
return filename.substring(0, filename.length() - XmlPersistencePathBuilder.EXT_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -91,7 +92,7 @@ public class XmlPersistencePathBuilder {
|
||||||
*/
|
*/
|
||||||
public String getPath(String type) {
|
public String getPath(String type) {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder(basePath);
|
StringBuilder sb = new StringBuilder(this.basePath);
|
||||||
sb.append("/");
|
sb.append("/");
|
||||||
sb.append(type);
|
sb.append(type);
|
||||||
|
|
||||||
|
@ -114,7 +115,7 @@ public class XmlPersistencePathBuilder {
|
||||||
*/
|
*/
|
||||||
public String getPath(String type, String subType) {
|
public String getPath(String type, String subType) {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder(basePath);
|
StringBuilder sb = new StringBuilder(this.basePath);
|
||||||
sb.append("/");
|
sb.append("/");
|
||||||
sb.append(type);
|
sb.append(type);
|
||||||
sb.append("/");
|
sb.append("/");
|
||||||
|
@ -140,7 +141,7 @@ public class XmlPersistencePathBuilder {
|
||||||
*/
|
*/
|
||||||
public String getPath(String type, String subType, String id) {
|
public String getPath(String type, String subType, String id) {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder(basePath);
|
StringBuilder sb = new StringBuilder(this.basePath);
|
||||||
sb.append("/");
|
sb.append("/");
|
||||||
sb.append(type);
|
sb.append(type);
|
||||||
sb.append("/");
|
sb.append("/");
|
||||||
|
|
|
@ -27,7 +27,8 @@ import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.w3c.dom.DOMImplementation;
|
import org.w3c.dom.DOMImplementation;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
@ -41,7 +42,7 @@ import ch.eitchnet.utils.objectfilter.ObjectFilter;
|
||||||
*/
|
*/
|
||||||
public class XmlPersistenceTransaction {
|
public class XmlPersistenceTransaction {
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(XmlPersistenceTransaction.class);
|
private static final Logger logger = LoggerFactory.getLogger(XmlPersistenceTransaction.class);
|
||||||
|
|
||||||
private boolean verbose;
|
private boolean verbose;
|
||||||
private XmlFilePersister persister;
|
private XmlFilePersister persister;
|
||||||
|
@ -64,23 +65,23 @@ public class XmlPersistenceTransaction {
|
||||||
}
|
}
|
||||||
|
|
||||||
private DocumentBuilder getDocBuilder() {
|
private DocumentBuilder getDocBuilder() {
|
||||||
if (docBuilder == null) {
|
if (this.docBuilder == null) {
|
||||||
try {
|
try {
|
||||||
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
this.docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
} catch (ParserConfigurationException e) {
|
} catch (ParserConfigurationException e) {
|
||||||
throw new XmlPersistenceExecption("Failed to load document builder: " + e.getLocalizedMessage(), e);
|
throw new XmlPersistenceExecption("Failed to load document builder: " + e.getLocalizedMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return docBuilder;
|
return this.docBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected DOMImplementation getDomImpl() {
|
protected DOMImplementation getDomImpl() {
|
||||||
if (domImplementation == null)
|
if (this.domImplementation == null)
|
||||||
domImplementation = getDocBuilder().getDOMImplementation();
|
this.domImplementation = getDocBuilder().getDOMImplementation();
|
||||||
return domImplementation;
|
return this.domImplementation;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -183,7 +184,7 @@ public class XmlPersistenceTransaction {
|
||||||
public <T> List<T> queryAll(String type, String subType) {
|
public <T> List<T> queryAll(String type, String subType) {
|
||||||
|
|
||||||
// XXX ok, this is very ugly, but for starters it will have to do
|
// XXX ok, this is very ugly, but for starters it will have to do
|
||||||
XmlDao<Object> dao = xmlDaoFactory.getDao(type);
|
XmlDao<Object> dao = this.xmlDaoFactory.getDao(type);
|
||||||
|
|
||||||
List<Element> elements = this.persister.queryAll(type, subType, getDocBuilder());
|
List<Element> elements = this.persister.queryAll(type, subType, getDocBuilder());
|
||||||
List<T> objects = new ArrayList<T>(elements.size());
|
List<T> objects = new ArrayList<T>(elements.size());
|
||||||
|
@ -214,7 +215,7 @@ public class XmlPersistenceTransaction {
|
||||||
*/
|
*/
|
||||||
public <T> T queryById(String type, String subType, String id) {
|
public <T> T queryById(String type, String subType, String id) {
|
||||||
|
|
||||||
XmlDao<Object> dao = xmlDaoFactory.getDao(type);
|
XmlDao<Object> dao = this.xmlDaoFactory.getDao(type);
|
||||||
|
|
||||||
Element element = this.persister.queryById(type, subType, id, getDocBuilder());
|
Element element = this.persister.queryById(type, subType, id, getDocBuilder());
|
||||||
if (element == null)
|
if (element == null)
|
||||||
|
@ -235,24 +236,24 @@ public class XmlPersistenceTransaction {
|
||||||
*/
|
*/
|
||||||
void commitTx() {
|
void commitTx() {
|
||||||
|
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("Committing...");
|
XmlPersistenceTransaction.logger.info("Committing...");
|
||||||
|
|
||||||
Set<String> keySet = objectFilter.keySet();
|
Set<String> keySet = this.objectFilter.keySet();
|
||||||
if (keySet.isEmpty())
|
if (keySet.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (String key : keySet) {
|
for (String key : keySet) {
|
||||||
|
|
||||||
XmlDao<Object> dao = xmlDaoFactory.getDao(key);
|
XmlDao<Object> dao = this.xmlDaoFactory.getDao(key);
|
||||||
|
|
||||||
List<ITransactionObject> removed = objectFilter.getRemoved(key);
|
List<ITransactionObject> removed = this.objectFilter.getRemoved(key);
|
||||||
if (removed.isEmpty()) {
|
if (removed.isEmpty()) {
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("No objects removed in this tx.");
|
XmlPersistenceTransaction.logger.info("No objects removed in this tx.");
|
||||||
} else {
|
} else {
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info(removed.size() + " objects removed in this tx.");
|
XmlPersistenceTransaction.logger.info(removed.size() + " objects removed in this tx.");
|
||||||
|
|
||||||
for (ITransactionObject object : removed) {
|
for (ITransactionObject object : removed) {
|
||||||
|
|
||||||
|
@ -260,17 +261,17 @@ public class XmlPersistenceTransaction {
|
||||||
String subType = dao.getSubType(object);
|
String subType = dao.getSubType(object);
|
||||||
String id = dao.getId(object);
|
String id = dao.getId(object);
|
||||||
|
|
||||||
persister.remove(type, subType, id);
|
this.persister.remove(type, subType, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ITransactionObject> updated = objectFilter.getUpdated(key);
|
List<ITransactionObject> updated = this.objectFilter.getUpdated(key);
|
||||||
if (updated.isEmpty()) {
|
if (updated.isEmpty()) {
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("No objects updated in this tx.");
|
XmlPersistenceTransaction.logger.info("No objects updated in this tx.");
|
||||||
} else {
|
} else {
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info(updated.size() + " objects updated in this tx.");
|
XmlPersistenceTransaction.logger.info(updated.size() + " objects updated in this tx.");
|
||||||
|
|
||||||
for (ITransactionObject object : updated) {
|
for (ITransactionObject object : updated) {
|
||||||
|
|
||||||
|
@ -279,17 +280,17 @@ public class XmlPersistenceTransaction {
|
||||||
String id = dao.getId(object);
|
String id = dao.getId(object);
|
||||||
|
|
||||||
Document asDom = dao.serializeToDom(object, getDomImpl());
|
Document asDom = dao.serializeToDom(object, getDomImpl());
|
||||||
persister.saveOrUpdate(type, subType, id, asDom);
|
this.persister.saveOrUpdate(type, subType, id, asDom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ITransactionObject> added = objectFilter.getAdded(key);
|
List<ITransactionObject> added = this.objectFilter.getAdded(key);
|
||||||
if (added.isEmpty()) {
|
if (added.isEmpty()) {
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info("No objects added in this tx.");
|
XmlPersistenceTransaction.logger.info("No objects added in this tx.");
|
||||||
} else {
|
} else {
|
||||||
if (verbose)
|
if (this.verbose)
|
||||||
logger.info(updated.size() + " objects added in this tx.");
|
XmlPersistenceTransaction.logger.info(updated.size() + " objects added in this tx.");
|
||||||
|
|
||||||
for (ITransactionObject object : added) {
|
for (ITransactionObject object : added) {
|
||||||
|
|
||||||
|
@ -298,12 +299,12 @@ public class XmlPersistenceTransaction {
|
||||||
String id = dao.getId(object);
|
String id = dao.getId(object);
|
||||||
|
|
||||||
Document asDom = dao.serializeToDom(object, getDomImpl());
|
Document asDom = dao.serializeToDom(object, getDomImpl());
|
||||||
persister.saveOrUpdate(type, subType, id, asDom);
|
this.persister.saveOrUpdate(type, subType, id, asDom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
objectFilter.clearCache();
|
this.objectFilter.clearCache();
|
||||||
logger.info("Completed TX");
|
XmlPersistenceTransaction.logger.info("Completed TX");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,8 @@ import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -42,7 +43,7 @@ import ch.eitchnet.xmlpers.test.impl.MyDaoFactory;
|
||||||
*/
|
*/
|
||||||
public class XmlPersistenceTest {
|
public class XmlPersistenceTest {
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(XmlPersistenceTest.class.getName());
|
private static final Logger logger = LoggerFactory.getLogger(XmlPersistenceTest.class.getName());
|
||||||
|
|
||||||
private static XmlPersistenceHandler persistenceHandler;
|
private static XmlPersistenceHandler persistenceHandler;
|
||||||
|
|
||||||
|
@ -67,13 +68,13 @@ public class XmlPersistenceTest {
|
||||||
System.setProperty(XmlPersistenceHandler.CONFIG_VERBOSE, "true");
|
System.setProperty(XmlPersistenceHandler.CONFIG_VERBOSE, "true");
|
||||||
System.setProperty(XmlPersistenceHandler.CONFIG_DAO_FACTORY_CLASS, MyDaoFactory.class.getName());
|
System.setProperty(XmlPersistenceHandler.CONFIG_DAO_FACTORY_CLASS, MyDaoFactory.class.getName());
|
||||||
|
|
||||||
persistenceHandler = new XmlPersistenceHandler();
|
XmlPersistenceTest.persistenceHandler = new XmlPersistenceHandler();
|
||||||
persistenceHandler.initialize();
|
XmlPersistenceTest.persistenceHandler.initialize();
|
||||||
|
|
||||||
logger.info("Initialized persistence handler.");
|
XmlPersistenceTest.logger.info("Initialized persistence handler.");
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error(e, e);
|
XmlPersistenceTest.logger.error(e.getMessage(), e);
|
||||||
|
|
||||||
throw new RuntimeException("Initialization failed: " + e.getLocalizedMessage(), e);
|
throw new RuntimeException("Initialization failed: " + e.getLocalizedMessage(), e);
|
||||||
}
|
}
|
||||||
|
@ -86,20 +87,20 @@ public class XmlPersistenceTest {
|
||||||
public void testCreate() {
|
public void testCreate() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.info("Trying to create...");
|
XmlPersistenceTest.logger.info("Trying to create...");
|
||||||
|
|
||||||
// new instance
|
// new instance
|
||||||
MyClass myClass = new MyClass("@id", "@name", "@subtype");
|
MyClass myClass = new MyClass("@id", "@name", "@subtype");
|
||||||
|
|
||||||
// persist instance
|
// persist instance
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
tx.add(myClass);
|
tx.add(myClass);
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
|
|
||||||
logger.info("Done creating.");
|
XmlPersistenceTest.logger.info("Done creating.");
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error(e, e);
|
XmlPersistenceTest.logger.error(e.getMessage(), e);
|
||||||
Assert.fail("Failed: " + e.getLocalizedMessage());
|
Assert.fail("Failed: " + e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,18 +112,18 @@ public class XmlPersistenceTest {
|
||||||
public void testRead() {
|
public void testRead() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.info("Trying to read...");
|
XmlPersistenceTest.logger.info("Trying to read...");
|
||||||
|
|
||||||
// query MyClass with id @id
|
// query MyClass with id @id
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
MyClass myClass = tx.queryById(MyClass.class.getName(), "@subtype", "@id");
|
MyClass myClass = tx.queryById(MyClass.class.getName(), "@subtype", "@id");
|
||||||
logger.info("Found MyClass: " + myClass);
|
XmlPersistenceTest.logger.info("Found MyClass: " + myClass);
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
|
|
||||||
logger.info("Done reading.");
|
XmlPersistenceTest.logger.info("Done reading.");
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error(e, e);
|
XmlPersistenceTest.logger.error(e.getMessage(), e);
|
||||||
Assert.fail("Failed: " + e.getLocalizedMessage());
|
Assert.fail("Failed: " + e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,24 +135,24 @@ public class XmlPersistenceTest {
|
||||||
public void testUpdate() {
|
public void testUpdate() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.info("Trying to update an object...");
|
XmlPersistenceTest.logger.info("Trying to update an object...");
|
||||||
|
|
||||||
// query the instance
|
// query the instance
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
MyClass myClass = tx.queryById(MyClass.class.getName(), "@subtype", "@id");
|
MyClass myClass = tx.queryById(MyClass.class.getName(), "@subtype", "@id");
|
||||||
logger.info("Found MyClass: " + myClass);
|
XmlPersistenceTest.logger.info("Found MyClass: " + myClass);
|
||||||
|
|
||||||
// modify the instance
|
// modify the instance
|
||||||
myClass.setName("@name_modified");
|
myClass.setName("@name_modified");
|
||||||
|
|
||||||
// update the instance
|
// update the instance
|
||||||
tx.update(myClass);
|
tx.update(myClass);
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
|
|
||||||
logger.info("Done updating.");
|
XmlPersistenceTest.logger.info("Done updating.");
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error(e, e);
|
XmlPersistenceTest.logger.error(e.getMessage(), e);
|
||||||
Assert.fail("Failed: " + e.getLocalizedMessage());
|
Assert.fail("Failed: " + e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -162,17 +163,17 @@ public class XmlPersistenceTest {
|
||||||
@Test
|
@Test
|
||||||
public void testRemove() {
|
public void testRemove() {
|
||||||
|
|
||||||
logger.info("Trying to remove...");
|
XmlPersistenceTest.logger.info("Trying to remove...");
|
||||||
|
|
||||||
// query the instance
|
// query the instance
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
MyClass myClass = tx.queryById(MyClass.class.getName(), "@subtype", "@id");
|
MyClass myClass = tx.queryById(MyClass.class.getName(), "@subtype", "@id");
|
||||||
logger.info("Found MyClass: " + myClass);
|
XmlPersistenceTest.logger.info("Found MyClass: " + myClass);
|
||||||
|
|
||||||
tx.remove(myClass);
|
tx.remove(myClass);
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
|
|
||||||
logger.info("Done removing.");
|
XmlPersistenceTest.logger.info("Done removing.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -182,13 +183,13 @@ public class XmlPersistenceTest {
|
||||||
public void testQueryFail() {
|
public void testQueryFail() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.info("Trying to query removed object...");
|
XmlPersistenceTest.logger.info("Trying to query removed object...");
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
MyClass myClass = tx.queryById(MyClass.class.getName(), "@subtype", "@id");
|
MyClass myClass = tx.queryById(MyClass.class.getName(), "@subtype", "@id");
|
||||||
logger.info("Found MyClass: " + myClass);
|
XmlPersistenceTest.logger.info("Found MyClass: " + myClass);
|
||||||
logger.info("Done querying removed object");
|
XmlPersistenceTest.logger.info("Done querying removed object");
|
||||||
} finally {
|
} finally {
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,20 +200,20 @@ public class XmlPersistenceTest {
|
||||||
public void testReCreate() {
|
public void testReCreate() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.info("Trying to recreate...");
|
XmlPersistenceTest.logger.info("Trying to recreate...");
|
||||||
|
|
||||||
// new instance
|
// new instance
|
||||||
MyClass myClass = new MyClass("@id", "@name", "@subtype");
|
MyClass myClass = new MyClass("@id", "@name", "@subtype");
|
||||||
|
|
||||||
// persist instance
|
// persist instance
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
tx.add(myClass);
|
tx.add(myClass);
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
|
|
||||||
logger.info("Done creating.");
|
XmlPersistenceTest.logger.info("Done creating.");
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error(e, e);
|
XmlPersistenceTest.logger.error(e.getMessage(), e);
|
||||||
Assert.fail("Failed: " + e.getLocalizedMessage());
|
Assert.fail("Failed: " + e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -233,10 +234,10 @@ public class XmlPersistenceTest {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
logger.info("Trying to query all...");
|
XmlPersistenceTest.logger.info("Trying to query all...");
|
||||||
|
|
||||||
// query all
|
// query all
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
List<Object> list = tx.queryAll(MyClass.class.getName());
|
List<Object> list = tx.queryAll(MyClass.class.getName());
|
||||||
Assert.assertTrue("Expected only one object, found " + list.size(), list.size() == 1);
|
Assert.assertTrue("Expected only one object, found " + list.size(), list.size() == 1);
|
||||||
|
|
||||||
|
@ -248,10 +249,10 @@ public class XmlPersistenceTest {
|
||||||
list = tx.queryAll(MyClass.class.getName(), "@inexistant");
|
list = tx.queryAll(MyClass.class.getName(), "@inexistant");
|
||||||
Assert.assertTrue("Expected no objects, found " + list.size(), list.size() == 0);
|
Assert.assertTrue("Expected no objects, found " + list.size(), list.size() == 0);
|
||||||
|
|
||||||
logger.info("Done querying.");
|
XmlPersistenceTest.logger.info("Done querying.");
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,7 +263,7 @@ public class XmlPersistenceTest {
|
||||||
public void testKeySet() {
|
public void testKeySet() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
|
|
||||||
Set<String> keySet = tx.queryKeySet(MyClass.class.getName());
|
Set<String> keySet = tx.queryKeySet(MyClass.class.getName());
|
||||||
Assert.assertTrue("Expected one key, found " + keySet.size(), keySet.size() == 1);
|
Assert.assertTrue("Expected one key, found " + keySet.size(), keySet.size() == 1);
|
||||||
|
@ -276,7 +277,7 @@ public class XmlPersistenceTest {
|
||||||
Assert.assertTrue("Expected no keys, found " + keySet, keySet.size() == 0);
|
Assert.assertTrue("Expected no keys, found " + keySet, keySet.size() == 0);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,13 +288,13 @@ public class XmlPersistenceTest {
|
||||||
public void testRemoveAll() {
|
public void testRemoveAll() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
|
|
||||||
List<ITransactionObject> objects = tx.queryAll(MyClass.class.getName(), "@subType");
|
List<ITransactionObject> objects = tx.queryAll(MyClass.class.getName(), "@subType");
|
||||||
tx.removeAll(objects);
|
tx.removeAll(objects);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,13 +305,13 @@ public class XmlPersistenceTest {
|
||||||
public void testSize() {
|
public void testSize() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
XmlPersistenceTransaction tx = persistenceHandler.openTx();
|
XmlPersistenceTransaction tx = XmlPersistenceTest.persistenceHandler.openTx();
|
||||||
|
|
||||||
long size = tx.querySize(MyClass.class.getName(), "@subType");
|
long size = tx.querySize(MyClass.class.getName(), "@subType");
|
||||||
Assert.assertTrue("Expected size = 0, found: " + size, size == 0);
|
Assert.assertTrue("Expected size = 0, found: " + size, size == 0);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
persistenceHandler.commitTx();
|
XmlPersistenceTest.persistenceHandler.commitTx();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue