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