mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 02:31:14 +01:00
re implement XmlRepositoryDAO
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package sonia.scm.repository.xml;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Repository;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import java.nio.file.Path;
|
||||
|
||||
class MetadataStore {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MetadataStore.class);
|
||||
|
||||
private final JAXBContext jaxbContext;
|
||||
|
||||
MetadataStore() {
|
||||
try {
|
||||
jaxbContext = JAXBContext.newInstance(Repository.class);
|
||||
} catch (JAXBException ex) {
|
||||
throw new IllegalStateException("failed to create jaxb context for repository", ex);
|
||||
}
|
||||
}
|
||||
|
||||
Repository read(Path path) {
|
||||
LOG.trace("read repository metadata from {}", path);
|
||||
try {
|
||||
return (Repository) jaxbContext.createUnmarshaller().unmarshal(path.toFile());
|
||||
} catch (JAXBException ex) {
|
||||
throw new InternalRepositoryException(
|
||||
ContextEntry.ContextBuilder.entity(Path.class, path.toString()).build(), "failed read repository metadata", ex
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void write(Path path, Repository repository) {
|
||||
LOG.trace("write repository metadata of {} to {}", repository.getNamespaceAndName(), path);
|
||||
try {
|
||||
Marshaller marshaller = jaxbContext.createMarshaller();
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
marshaller.marshal(repository, path.toFile());
|
||||
} catch (JAXBException ex) {
|
||||
throw new InternalRepositoryException(repository, "failed write repository metadata", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package sonia.scm.repository.xml;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.xml.IndentXMLStreamWriter;
|
||||
import sonia.scm.xml.XmlStreams;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
|
||||
class PathDatabase {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PathDatabase.class);
|
||||
|
||||
private static final String ENCODING = "UTF-8";
|
||||
private static final String VERSION = "1.0";
|
||||
|
||||
private static final String ELEMENT_REPOSITORIES = "repositories";
|
||||
private static final String ATTRIBUTE_CREATION_TIME = "creation-time";
|
||||
private static final String ATTRIBUTE_LAST_MODIFIED = "last-modified";
|
||||
|
||||
private static final String ELEMENT_REPOSITORY = "repository";
|
||||
private static final String ATTRIBUTE_ID = "id";
|
||||
|
||||
private final Path storePath;
|
||||
|
||||
PathDatabase(Path storePath){
|
||||
this.storePath = storePath;
|
||||
}
|
||||
|
||||
void write(Long creationTime, Long lastModified, Map<String, Path> pathDatabase) {
|
||||
ensureParentDirectoryExists();
|
||||
LOG.trace("write repository path database to {}", storePath);
|
||||
|
||||
try (IndentXMLStreamWriter writer = XmlStreams.createWriter(storePath)) {
|
||||
writer.writeStartDocument(ENCODING, VERSION);
|
||||
|
||||
writeRepositoriesStart(writer, creationTime, lastModified);
|
||||
for (Map.Entry<String, Path> e : pathDatabase.entrySet()) {
|
||||
writeRepository(writer, e.getKey(), e.getValue());
|
||||
}
|
||||
writer.writeEndElement();
|
||||
|
||||
writer.writeEndDocument();
|
||||
} catch (XMLStreamException | IOException ex) {
|
||||
throw new InternalRepositoryException(
|
||||
ContextEntry.ContextBuilder.entity(Path.class, storePath.toString()).build(),
|
||||
"failed to write repository path database",
|
||||
ex
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureParentDirectoryExists() {
|
||||
Path parent = storePath.getParent();
|
||||
if (!Files.exists(parent)) {
|
||||
try {
|
||||
Files.createDirectories(parent);
|
||||
} catch (IOException ex) {
|
||||
throw new InternalRepositoryException(
|
||||
ContextEntry.ContextBuilder.entity(Path.class, parent.toString()).build(),
|
||||
"failed to create parent directory",
|
||||
ex
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void writeRepositoriesStart(XMLStreamWriter writer, Long creationTime, Long lastModified) throws XMLStreamException {
|
||||
writer.writeStartElement(ELEMENT_REPOSITORIES);
|
||||
writer.writeAttribute(ATTRIBUTE_CREATION_TIME, String.valueOf(creationTime));
|
||||
writer.writeAttribute(ATTRIBUTE_LAST_MODIFIED, String.valueOf(lastModified));
|
||||
}
|
||||
|
||||
private void writeRepository(XMLStreamWriter writer, String id, Path value) throws XMLStreamException {
|
||||
writer.writeStartElement(ELEMENT_REPOSITORY);
|
||||
writer.writeAttribute(ATTRIBUTE_ID, id);
|
||||
writer.writeCharacters(value.toString());
|
||||
writer.writeEndElement();
|
||||
}
|
||||
|
||||
void read(OnRepositories onRepositories, OnRepository onRepository) {
|
||||
LOG.trace("read repository path database from {}", storePath);
|
||||
XMLStreamReader reader = null;
|
||||
try {
|
||||
reader = XmlStreams.createReader(storePath);
|
||||
|
||||
while (reader.hasNext()) {
|
||||
int eventType = reader.next();
|
||||
|
||||
if (eventType == XMLStreamReader.START_ELEMENT) {
|
||||
String element = reader.getLocalName();
|
||||
if (ELEMENT_REPOSITORIES.equals(element)) {
|
||||
readRepositories(reader, onRepositories);
|
||||
} else if (ELEMENT_REPOSITORY.equals(element)) {
|
||||
readRepository(reader, onRepository);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (XMLStreamException | IOException ex) {
|
||||
throw new InternalRepositoryException(
|
||||
ContextEntry.ContextBuilder.entity(Path.class, storePath.toString()).build(),
|
||||
"failed to read repository path database",
|
||||
ex
|
||||
);
|
||||
} finally {
|
||||
XmlStreams.close(reader);
|
||||
}
|
||||
}
|
||||
|
||||
private void readRepository(XMLStreamReader reader, OnRepository onRepository) throws XMLStreamException {
|
||||
String id = reader.getAttributeValue(null, ATTRIBUTE_ID);
|
||||
Path path = Paths.get(reader.getElementText());
|
||||
onRepository.handle(id, path);
|
||||
}
|
||||
|
||||
private void readRepositories(XMLStreamReader reader, OnRepositories onRepositories) {
|
||||
String creationTime = reader.getAttributeValue(null, ATTRIBUTE_CREATION_TIME);
|
||||
String lastModified = reader.getAttributeValue(null, ATTRIBUTE_LAST_MODIFIED);
|
||||
onRepositories.handle(Long.parseLong(creationTime), Long.parseLong(lastModified));
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface OnRepositories {
|
||||
|
||||
void handle(Long creationTime, Long lastModified);
|
||||
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface OnRepository {
|
||||
|
||||
void handle(String id, Path path);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,156 +33,229 @@ package sonia.scm.repository.xml;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.inject.Singleton;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.io.FileSystem;
|
||||
import sonia.scm.repository.InitialRepositoryLocationResolver;
|
||||
import sonia.scm.repository.InitialRepositoryLocationResolver.InitialRepositoryLocation;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.PathBasedRepositoryDAO;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
import sonia.scm.xml.AbstractXmlDAO;
|
||||
import sonia.scm.store.StoreConstants;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Clock;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class XmlRepositoryDAO
|
||||
extends AbstractXmlDAO<Repository, XmlRepositoryDatabase>
|
||||
implements PathBasedRepositoryDAO {
|
||||
public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
|
||||
|
||||
public static final String STORE_NAME = "repositories";
|
||||
private static final String STORE_NAME = "repositories";
|
||||
|
||||
private final PathDatabase pathDatabase;
|
||||
private final MetadataStore metadataStore = new MetadataStore();
|
||||
|
||||
private InitialRepositoryLocationResolver initialRepositoryLocationResolver;
|
||||
private final FileSystem fileSystem;
|
||||
private final SCMContextProvider context;
|
||||
private final InitialRepositoryLocationResolver locationResolver;
|
||||
private final FileSystem fileSystem;
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
@VisibleForTesting
|
||||
Clock clock = Clock.systemUTC();
|
||||
|
||||
private Long creationTime;
|
||||
private Long lastModified;
|
||||
|
||||
private Map<String, Path> pathById;
|
||||
private Map<String, Repository> byId;
|
||||
private Map<NamespaceAndName, Repository> byNamespaceAndName;
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
* @param storeFactory
|
||||
* @param fileSystem
|
||||
* @param context
|
||||
*/
|
||||
@Inject
|
||||
public XmlRepositoryDAO(ConfigurationStoreFactory storeFactory, InitialRepositoryLocationResolver initialRepositoryLocationResolver, FileSystem fileSystem, SCMContextProvider context) {
|
||||
super(storeFactory.getStore(XmlRepositoryDatabase.class, STORE_NAME));
|
||||
this.initialRepositoryLocationResolver = initialRepositoryLocationResolver;
|
||||
this.fileSystem = fileSystem;
|
||||
public XmlRepositoryDAO(SCMContextProvider context, InitialRepositoryLocationResolver locationResolver, FileSystem fileSystem) {
|
||||
this.context = context;
|
||||
this.locationResolver = locationResolver;
|
||||
this.fileSystem = fileSystem;
|
||||
|
||||
this.creationTime = clock.millis();
|
||||
|
||||
this.pathById = new LinkedHashMap<>();
|
||||
this.byId = new LinkedHashMap<>();
|
||||
this.byNamespaceAndName = new LinkedHashMap<>();
|
||||
|
||||
pathDatabase = new PathDatabase(createStorePath());
|
||||
read();
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
private void read() {
|
||||
Path storePath = createStorePath();
|
||||
|
||||
@Override
|
||||
public boolean contains(NamespaceAndName namespaceAndName) {
|
||||
return db.contains(namespaceAndName);
|
||||
if (!Files.exists(storePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
pathDatabase.read(this::loadDates, this::loadRepository);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Repository get(NamespaceAndName namespaceAndName) {
|
||||
return db.get(namespaceAndName);
|
||||
private void loadDates(Long creationTime, Long lastModified) {
|
||||
this.creationTime = creationTime;
|
||||
this.lastModified = lastModified;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
private void loadRepository(String id, Path repositoryPath) {
|
||||
Path metadataPath = createMetadataPath(context.resolve(repositoryPath));
|
||||
|
||||
Repository repository = metadataStore.read(metadataPath);
|
||||
|
||||
byId.put(id, repository);
|
||||
byNamespaceAndName.put(repository.getNamespaceAndName(), repository);
|
||||
pathById.put(id, repositoryPath);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
Path createStorePath() {
|
||||
return context.getBaseDirectory()
|
||||
.toPath()
|
||||
.resolve(StoreConstants.CONFIG_DIRECTORY_NAME)
|
||||
.resolve(STORE_NAME.concat(StoreConstants.FILE_EXTENSION));
|
||||
}
|
||||
|
||||
|
||||
@VisibleForTesting
|
||||
Path createMetadataPath(Path repositoryPath) {
|
||||
return repositoryPath.resolve(StoreConstants.REPOSITORY_METADATA.concat(StoreConstants.FILE_EXTENSION));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modify(Repository repository) {
|
||||
RepositoryPath repositoryPath = findExistingRepositoryPath(repository.getId()).orElseThrow(() -> new InternalRepositoryException(repository, "path object for repository not found"));
|
||||
repositoryPath.setRepository(repository);
|
||||
repositoryPath.setToBeSynchronized(true);
|
||||
storeDB();
|
||||
public String getType() {
|
||||
return "xml";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getCreationTime() {
|
||||
return creationTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLastModified() {
|
||||
return lastModified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Repository repository) {
|
||||
InitialRepositoryLocation initialLocation = initialRepositoryLocationResolver.getRelativeRepositoryPath(repository.getId());
|
||||
Repository clone = repository.clone();
|
||||
|
||||
Path repositoryPath = locationResolver.getPath(repository.getId());
|
||||
Path resolvedPath = context.resolve(repositoryPath);
|
||||
|
||||
try {
|
||||
fileSystem.create(initialLocation.getAbsolutePath());
|
||||
fileSystem.create(resolvedPath.toFile());
|
||||
|
||||
Path metadataPath = createMetadataPath(resolvedPath);
|
||||
metadataStore.write(metadataPath, repository);
|
||||
|
||||
synchronized (this) {
|
||||
pathById.put(repository.getId(), repositoryPath);
|
||||
|
||||
byId.put(repository.getId(), clone);
|
||||
byNamespaceAndName.put(repository.getNamespaceAndName(), clone);
|
||||
|
||||
writePathDatabase();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new InternalRepositoryException(repository, "could not create directory for repository data: " + initialLocation.getAbsolutePath(), e);
|
||||
}
|
||||
RepositoryPath repositoryPath = new RepositoryPath(initialLocation.getRelativePath(), repository.getId(), repository.clone());
|
||||
repositoryPath.setToBeSynchronized(true);
|
||||
synchronized (store) {
|
||||
db.add(repositoryPath);
|
||||
storeDB();
|
||||
throw new InternalRepositoryException(repository, "failed to create filesystem", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void writePathDatabase() {
|
||||
lastModified = clock.millis();
|
||||
pathDatabase.write(creationTime, lastModified, pathById);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Repository repository) {
|
||||
return byId.containsKey(repository.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(NamespaceAndName namespaceAndName) {
|
||||
return byNamespaceAndName.containsKey(namespaceAndName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String id) {
|
||||
return byId.containsKey(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository get(NamespaceAndName namespaceAndName) {
|
||||
return byNamespaceAndName.get(namespaceAndName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository get(String id) {
|
||||
RepositoryPath repositoryPath = db.get(id);
|
||||
if (repositoryPath != null) {
|
||||
return repositoryPath.getRepository();
|
||||
}
|
||||
return null;
|
||||
return byId.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Repository> getAll() {
|
||||
return db.getRepositories();
|
||||
return ImmutableList.copyOf(byNamespaceAndName.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* @param repository
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected Repository clone(Repository repository) {
|
||||
return repository.clone();
|
||||
public void modify(Repository repository) {
|
||||
Repository clone = repository.clone();
|
||||
|
||||
synchronized (this) {
|
||||
// remove old namespaceAndName from map, in case of rename
|
||||
Repository prev = byId.put(clone.getId(), clone);
|
||||
if (prev != null) {
|
||||
byNamespaceAndName.remove(prev.getNamespaceAndName());
|
||||
}
|
||||
byNamespaceAndName.put(clone.getNamespaceAndName(), clone);
|
||||
|
||||
writePathDatabase();
|
||||
}
|
||||
|
||||
Path repositoryPath = context.resolve(getPath(repository.getId()));
|
||||
Path metadataPath = createMetadataPath(repositoryPath);
|
||||
metadataStore.write(metadataPath, clone);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Repository repository) {
|
||||
Path directory = getPath(repository.getId());
|
||||
super.delete(repository);
|
||||
try {
|
||||
fileSystem.destroy(directory.toFile());
|
||||
} catch (IOException e) {
|
||||
throw new InternalRepositoryException(repository, "could not delete repository directory", e);
|
||||
}
|
||||
}
|
||||
Path path;
|
||||
synchronized (this) {
|
||||
Repository prev = byId.remove(repository.getId());
|
||||
if (prev != null) {
|
||||
byNamespaceAndName.remove(prev.getNamespaceAndName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected XmlRepositoryDatabase createNewDatabase() {
|
||||
return new XmlRepositoryDatabase();
|
||||
path = pathById.remove(repository.getId());
|
||||
|
||||
writePathDatabase();
|
||||
}
|
||||
|
||||
path = context.resolve(path);
|
||||
|
||||
try {
|
||||
fileSystem.destroy(path.toFile());
|
||||
} catch (IOException e) {
|
||||
throw new InternalRepositoryException(repository, "failed to destroy filesystem", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path getPath(String repositoryId) {
|
||||
return context
|
||||
.getBaseDirectory()
|
||||
.toPath()
|
||||
.resolve(
|
||||
findExistingRepositoryPath(repositoryId)
|
||||
.map(RepositoryPath::getPath)
|
||||
.orElseThrow(() -> new InternalRepositoryException(ContextEntry.ContextBuilder.entity("repository", repositoryId), "could not find base directory for repository")));
|
||||
}
|
||||
|
||||
private Optional<RepositoryPath> findExistingRepositoryPath(String repositoryId) {
|
||||
return db.values().stream()
|
||||
.filter(repoPath -> repoPath.getId().equals(repositoryId))
|
||||
.findAny();
|
||||
return pathById.get(repositoryId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,32 +35,14 @@ package sonia.scm.store;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Collections2;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.security.KeyGenerator;
|
||||
import sonia.scm.xml.IndentXMLStreamWriter;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import sonia.scm.xml.XmlStreams;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
@@ -68,11 +50,14 @@ import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -255,74 +240,6 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param writer
|
||||
*/
|
||||
private void close(XMLStreamWriter writer)
|
||||
{
|
||||
if (writer != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
writer.close();
|
||||
}
|
||||
catch (XMLStreamException ex)
|
||||
{
|
||||
logger.error("could not close writer", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param reader
|
||||
*/
|
||||
private void close(XMLStreamReader reader)
|
||||
{
|
||||
if (reader != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
reader.close();
|
||||
}
|
||||
catch (XMLStreamException ex)
|
||||
{
|
||||
logger.error("could not close reader", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
private Reader createReader() throws FileNotFoundException
|
||||
{
|
||||
return new InputStreamReader(new FileInputStream(file), Charsets.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
private Writer createWriter() throws FileNotFoundException
|
||||
{
|
||||
return new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -333,15 +250,13 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
|
||||
{
|
||||
logger.debug("load configuration from {}", file);
|
||||
|
||||
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
|
||||
|
||||
XMLStreamReader reader = null;
|
||||
|
||||
try
|
||||
{
|
||||
Unmarshaller u = context.createUnmarshaller();
|
||||
|
||||
reader = xmlInputFactory.createXMLStreamReader(createReader());
|
||||
reader = XmlStreams.createReader(file);
|
||||
|
||||
// configuration
|
||||
reader.nextTag();
|
||||
@@ -390,7 +305,7 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
|
||||
}
|
||||
finally
|
||||
{
|
||||
close(reader);
|
||||
XmlStreams.close(reader);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -402,17 +317,8 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
|
||||
{
|
||||
logger.debug("store configuration to {}", file);
|
||||
|
||||
IndentXMLStreamWriter writer = null;
|
||||
|
||||
try
|
||||
try (IndentXMLStreamWriter writer = XmlStreams.createWriter(file))
|
||||
{
|
||||
//J-
|
||||
writer = new IndentXMLStreamWriter(
|
||||
XMLOutputFactory.newInstance().createXMLStreamWriter(
|
||||
createWriter()
|
||||
)
|
||||
);
|
||||
//J+
|
||||
writer.writeStartDocument();
|
||||
|
||||
// configuration start
|
||||
@@ -453,10 +359,6 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
|
||||
{
|
||||
throw new StoreException("could not store configuration", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
close(writer);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
71
scm-dao-xml/src/main/java/sonia/scm/xml/XmlStreams.java
Normal file
71
scm-dao-xml/src/main/java/sonia/scm/xml/XmlStreams.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package sonia.scm.xml;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public final class XmlStreams {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(XmlStreams.class);
|
||||
|
||||
private XmlStreams() {
|
||||
}
|
||||
|
||||
public static void close(XMLStreamWriter writer) {
|
||||
if (writer != null) {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (XMLStreamException ex) {
|
||||
LOG.error("could not close writer", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void close(XMLStreamReader reader) {
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (XMLStreamException ex) {
|
||||
LOG.error("could not close reader", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static XMLStreamReader createReader(Path path) throws IOException, XMLStreamException {
|
||||
return createReader(Files.newBufferedReader(path, Charsets.UTF_8));
|
||||
}
|
||||
|
||||
public static XMLStreamReader createReader(File file) throws IOException, XMLStreamException {
|
||||
return createReader(file.toPath());
|
||||
}
|
||||
|
||||
private static XMLStreamReader createReader(Reader reader) throws XMLStreamException {
|
||||
return XMLInputFactory.newInstance().createXMLStreamReader(reader);
|
||||
}
|
||||
|
||||
|
||||
public static IndentXMLStreamWriter createWriter(Path path) throws IOException, XMLStreamException {
|
||||
return createWriter(Files.newBufferedWriter(path, Charsets.UTF_8));
|
||||
}
|
||||
|
||||
public static IndentXMLStreamWriter createWriter(File file) throws IOException, XMLStreamException {
|
||||
return createWriter(file.toPath());
|
||||
}
|
||||
|
||||
private static IndentXMLStreamWriter createWriter(Writer writer) throws XMLStreamException {
|
||||
return new IndentXMLStreamWriter(XMLOutputFactory.newFactory().createXMLStreamWriter(writer));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user