Refactor the repository store implementation in order to store repositories in specific paths.

This commit is contained in:
Mohamed Karray
2018-11-15 10:04:16 +01:00
parent 2cd2cfbde2
commit f8ae7cedf7
51 changed files with 628 additions and 594 deletions

View File

@@ -0,0 +1,90 @@
package sonia.scm.repository.xml;
import org.apache.commons.lang.StringUtils;
import sonia.scm.ModelObject;
import sonia.scm.repository.Repository;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement(name = "repository-link")
@XmlAccessorType(XmlAccessType.FIELD)
public class RepositoryPath implements ModelObject {
private String path;
private String id;
private Long lastModified;
private Long creationDate;
@XmlTransient
private Repository repository;
/**
* Needed from JAXB
*/
public RepositoryPath() {
}
public RepositoryPath(String path, String id, Repository repository) {
this.path = path;
this.id = id;
this.repository = repository;
}
public Repository getRepository() {
return repository;
}
public void setRepository(Repository repository) {
this.repository = repository;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public String getId() {
return id;
}
@Override
public void setLastModified(Long lastModified) {
this.lastModified = lastModified;
}
@Override
public Long getCreationDate() {
return creationDate;
}
@Override
public void setCreationDate(Long creationDate) {
this.creationDate = creationDate;
}
public void setId(String id) {
this.id = id;
}
@Override
public Long getLastModified() {
return lastModified;
}
@Override
public String getType() {
return getRepository()!= null? getRepository().getType():"";
}
@Override
public boolean isValid() {
return StringUtils.isNotEmpty(path);
}
}

View File

@@ -36,80 +36,124 @@ package sonia.scm.repository.xml;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import sonia.scm.repository.InitialRepositoryLocationResolver;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.PathBasedRepositoryDAO;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryDAO;
import sonia.scm.repository.RepositoryPathNotFoundException;
import sonia.scm.store.ConfigurationStoreFactory;
import sonia.scm.xml.AbstractXmlDAO;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Optional;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class XmlRepositoryDAO
extends AbstractXmlDAO<Repository, XmlRepositoryDatabase>
implements RepositoryDAO
{
extends AbstractXmlDAO<Repository, XmlRepositoryDatabase>
implements PathBasedRepositoryDAO {
/** Field description */
/**
* Field description
*/
public static final String STORE_NAME = "repositories";
private InitialRepositoryLocationResolver initialRepositoryLocationResolver;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param storeFactory
*/
@Inject
public XmlRepositoryDAO(ConfigurationStoreFactory storeFactory)
{
public XmlRepositoryDAO(ConfigurationStoreFactory storeFactory, InitialRepositoryLocationResolver initialRepositoryLocationResolver) {
super(storeFactory.getStore(XmlRepositoryDatabase.class, STORE_NAME));
this.initialRepositoryLocationResolver = initialRepositoryLocationResolver;
}
//~--- methods --------------------------------------------------------------
@Override
public boolean contains(NamespaceAndName namespaceAndName)
{
public boolean contains(NamespaceAndName namespaceAndName) {
return db.contains(namespaceAndName);
}
//~--- get methods ----------------------------------------------------------
@Override
public Repository get(NamespaceAndName namespaceAndName)
{
public Repository get(NamespaceAndName namespaceAndName) {
return db.get(namespaceAndName);
}
//~--- methods --------------------------------------------------------------
@Override
public void modify(Repository repository) {
db.remove(repository.getId());
add(repository);
}
@Override
public void add(Repository repository) {
String path = initialRepositoryLocationResolver.getDirectory(repository).getAbsolutePath();
RepositoryPath repositoryPath = new RepositoryPath(path, repository.getId(), repository.clone());
synchronized (store) {
db.add(repositoryPath);
storeDB();
}
}
@Override
public Repository get(String id) {
RepositoryPath repositoryPath = db.get(id);
if (repositoryPath != null) {
return repositoryPath.getRepository();
}
return null;
}
@Override
public Collection<Repository> getAll() {
return db.getRepositories();
}
/**
* Method description
*
*
* @param repository
*
* @return
*/
@Override
protected Repository clone(Repository repository)
{
protected Repository clone(Repository repository) {
return repository.clone();
}
/**
* Method description
*
*
* @return
*/
@Override
protected XmlRepositoryDatabase createNewDatabase()
{
protected XmlRepositoryDatabase createNewDatabase() {
return new XmlRepositoryDatabase();
}
@Override
public Path getPath(Repository repository) throws RepositoryPathNotFoundException {
Optional<RepositoryPath> repositoryPath = db.getPaths().stream()
.filter(repoPath -> repoPath.getId().equals(repository.getId()))
.findFirst();
if (!repositoryPath.isPresent()) {
throw new RepositoryPathNotFoundException();
} else {
return Paths.get(repositoryPath.get().getPath());
}
}
}

View File

@@ -44,21 +44,29 @@ import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
//~--- JDK imports ------------------------------------------------------------
@XmlRootElement(name = "repository-db")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlRepositoryDatabase implements XmlDatabase<Repository>
{
public class XmlRepositoryDatabase implements XmlDatabase<RepositoryPath> {
public XmlRepositoryDatabase()
{
private Long creationTime;
private Long lastModified;
@XmlJavaTypeAdapter(XmlRepositoryMapAdapter.class)
@XmlElement(name = "repositories")
private Map<String, RepositoryPath> repositoryPathMap = new LinkedHashMap<>();
public XmlRepositoryDatabase() {
long c = System.currentTimeMillis();
creationTime = c;
lastModified = c;
}
@@ -74,14 +82,14 @@ public class XmlRepositoryDatabase implements XmlDatabase<Repository>
}
@Override
public void add(Repository repository)
public void add(RepositoryPath repositoryPath)
{
repositoryMap.put(createKey(repository), repository);
repositoryPathMap.put(createKey(repositoryPath.getRepository()), repositoryPath);
}
public boolean contains(NamespaceAndName namespaceAndName)
{
return repositoryMap.containsKey(createKey(namespaceAndName));
return repositoryPathMap.containsKey(createKey(namespaceAndName));
}
@Override
@@ -92,61 +100,55 @@ public class XmlRepositoryDatabase implements XmlDatabase<Repository>
public boolean contains(Repository repository)
{
return repositoryMap.containsKey(createKey(repository));
return repositoryPathMap.containsKey(createKey(repository));
}
public void remove(Repository repository)
{
repositoryMap.remove(createKey(repository));
repositoryPathMap.remove(createKey(repository));
}
@Override
public Repository remove(String id)
public RepositoryPath remove(String id)
{
Repository r = get(id);
remove(r);
return r;
return repositoryPathMap.remove(createKey(get(id).getRepository()));
}
@Override
public Collection<Repository> values()
{
return repositoryMap.values();
}
//~--- get methods ----------------------------------------------------------
public Repository get(NamespaceAndName namespaceAndName)
{
return repositoryMap.get(createKey(namespaceAndName));
}
/**
* Method description
*
*
* @param id
*
* @return
*/
@Override
public Repository get(String id)
{
Repository repository = null;
for (Repository r : values())
{
if (r.getId().equals(id))
{
repository = r;
break;
}
public Collection<Repository> getRepositories() {
List<Repository> repositories = new ArrayList<>();
for (RepositoryPath repositoryPath : repositoryPathMap.values()) {
Repository repository = repositoryPath.getRepository();
repositories.add(repository);
}
return repositories;
}
return repository;
@Override
public Collection<RepositoryPath> values()
{
return repositoryPathMap.values();
}
public Collection<RepositoryPath> getPaths() {
return repositoryPathMap.values();
}
public Repository get(NamespaceAndName namespaceAndName) {
RepositoryPath repositoryPath = repositoryPathMap.get(createKey(namespaceAndName));
if (repositoryPath != null) {
return repositoryPath.getRepository();
}
return null;
}
@Override
public RepositoryPath get(String id) {
return values().stream()
.filter(repoPath -> repoPath.getId().equals(id))
.findFirst()
.orElse(null);
}
/**
@@ -198,17 +200,4 @@ public class XmlRepositoryDatabase implements XmlDatabase<Repository>
{
this.lastModified = lastModified;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Long creationTime;
/** Field description */
private Long lastModified;
/** Field description */
@XmlJavaTypeAdapter(XmlRepositoryMapAdapter.class)
@XmlElement(name = "repositories")
private Map<String, Repository> repositoryMap = new LinkedHashMap<>();
}

View File

@@ -54,7 +54,7 @@ import javax.xml.bind.annotation.XmlRootElement;
*/
@XmlRootElement(name = "repositories")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlRepositoryList implements Iterable<Repository>
public class XmlRepositoryList implements Iterable<RepositoryPath>
{
/**
@@ -70,9 +70,9 @@ public class XmlRepositoryList implements Iterable<Repository>
*
* @param repositoryMap
*/
public XmlRepositoryList(Map<String, Repository> repositoryMap)
public XmlRepositoryList(Map<String, RepositoryPath> repositoryMap)
{
this.repositories = new LinkedList<Repository>(repositoryMap.values());
this.repositories = new LinkedList<>(repositoryMap.values());
}
//~--- methods --------------------------------------------------------------
@@ -84,7 +84,7 @@ public class XmlRepositoryList implements Iterable<Repository>
* @return
*/
@Override
public Iterator<Repository> iterator()
public Iterator<RepositoryPath> iterator()
{
return repositories.iterator();
}
@@ -97,7 +97,7 @@ public class XmlRepositoryList implements Iterable<Repository>
*
* @return
*/
public LinkedList<Repository> getRepositories()
public LinkedList<RepositoryPath> getRepositoryPaths()
{
return repositories;
}
@@ -110,7 +110,7 @@ public class XmlRepositoryList implements Iterable<Repository>
*
* @param repositories
*/
public void setRepositories(LinkedList<Repository> repositories)
public void setRepositories(LinkedList<RepositoryPath> repositories)
{
this.repositories = repositories;
}
@@ -118,6 +118,6 @@ public class XmlRepositoryList implements Iterable<Repository>
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlElement(name = "repository")
private LinkedList<Repository> repositories;
@XmlElement(name = "repository-path")
private LinkedList<RepositoryPath> repositories;
}

View File

@@ -1,19 +1,19 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* <p>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* <p>
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -24,45 +24,76 @@
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* <p>
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.repository.Repository;
import sonia.scm.store.StoreConstants;
import sonia.scm.store.StoreException;
import sonia.scm.util.IOUtil;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
//~--- JDK imports ------------------------------------------------------------
/**
*
* @author Sebastian Sdorra
*/
public class XmlRepositoryMapAdapter extends XmlAdapter<XmlRepositoryList, Map<String, Repository>> {
public class XmlRepositoryMapAdapter extends XmlAdapter<XmlRepositoryList, Map<String, RepositoryPath>> {
@Override
public XmlRepositoryList marshal(Map<String, Repository> repositoryMap) {
return new XmlRepositoryList(repositoryMap);
}
public XmlRepositoryList marshal(Map<String, RepositoryPath> repositoryMap) {
XmlRepositoryList repositoryPaths = new XmlRepositoryList(repositoryMap);
try {
JAXBContext context = JAXBContext.newInstance(Repository.class);
Marshaller marshaller = context.createMarshaller();
@Override
public Map<String, Repository> unmarshal(XmlRepositoryList repositories) {
Map<String, Repository> repositoryMap = new LinkedHashMap<>();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
for (Repository repository : repositories) {
repositoryMap.put(XmlRepositoryDatabase.createKey(repository),
repository);
// marshall the repo_path/metadata.xml files
for (RepositoryPath repositoryPath : repositoryPaths.getRepositoryPaths()) {
File dir = new File(repositoryPath.getPath());
if (!dir.exists()){
IOUtil.mkdirs(dir);
}
marshaller.marshal(repositoryPath.getRepository(), getRepositoryMetadataFile(dir));
}
} catch (JAXBException ex) {
throw new StoreException("failed to marshall repository database", ex);
}
return repositoryMap;
return repositoryPaths;
}
private File getRepositoryMetadataFile(File dir) {
return new File(dir, StoreConstants.REPOSITORY_METADATA.concat(StoreConstants.FILE_EXTENSION));
}
@Override
public Map<String, RepositoryPath> unmarshal(XmlRepositoryList repositories) {
Map<String, RepositoryPath> repositoryPathMap = new LinkedHashMap<>();
try {
JAXBContext context = JAXBContext.newInstance(Repository.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
for (RepositoryPath repositoryPath : repositories) {
Repository repository = (Repository) unmarshaller.unmarshal(getRepositoryMetadataFile(new File(repositoryPath.getPath())));
repositoryPath.setRepository(repository);
repositoryPathMap.put(XmlRepositoryDatabase.createKey(repository), repositoryPath);
}
} catch (JAXBException ex) {
throw new StoreException("failed to unmarshall object", ex);
}
return repositoryPathMap;
}
}

View File

@@ -79,7 +79,7 @@ public class JAXBConfigurationEntryStoreFactory
{
this.keyGenerator = keyGenerator;
directory = new File(context.getBaseDirectory(),
StoreConstants.CONFIGDIRECTORY_NAME);
StoreConstants.CONFIG_DIRECTORY_NAME);
IOUtil.mkdirs(directory);
}

View File

@@ -42,7 +42,7 @@ import sonia.scm.util.IOUtil;
import java.io.File;
/**
* JAXB implementation of {@link JAXBConfigurationStoreFactory}.
* JAXB implementation of {@link ConfigurationStoreFactory}.
*
* @author Sebastian Sdorra
*/
@@ -63,7 +63,7 @@ public class JAXBConfigurationStoreFactory implements ConfigurationStoreFactory
*/
@Inject
public JAXBConfigurationStoreFactory(SCMContextProvider context) {
configDirectory = new File(context.getBaseDirectory(), StoreConstants.CONFIGDIRECTORY_NAME);
configDirectory = new File(context.getBaseDirectory(), StoreConstants.CONFIG_DIRECTORY_NAME);
IOUtil.mkdirs(configDirectory);
}

View File

@@ -37,12 +37,14 @@ package sonia.scm.store;
*
* @author Sebastian Sdorra
*/
public interface StoreConstants
public class StoreConstants
{
/** Field description */
public static final String CONFIGDIRECTORY_NAME = "config";
private StoreConstants() { }
public static final String CONFIG_DIRECTORY_NAME = "config";
public static final String REPOSITORY_METADATA = "metadata";
/** Field description */
public static final String FILE_EXTENSION = ".xml";
}

View File

@@ -1,19 +1,19 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* <p>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* <p>
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -24,9 +24,8 @@
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* <p>
* http://bitbucket.org/sdorra/scm-manager
*
*/
@@ -35,18 +34,14 @@ package sonia.scm.xml;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.GenericDAO;
import sonia.scm.ModelObject;
import sonia.scm.group.xml.XmlGroupDAO;
import sonia.scm.store.ConfigurationStore;
import sonia.scm.util.AssertUtil;
import java.util.Collection;
import java.util.stream.Collectors;
//~--- JDK imports ------------------------------------------------------------
@@ -58,7 +53,7 @@ import java.util.stream.Collectors;
* @param <T>
*/
public abstract class AbstractXmlDAO<I extends ModelObject,
T extends XmlDatabase<I>> implements GenericDAO<I>
T extends XmlDatabase> implements GenericDAO<I>
{
/** Field description */
@@ -192,6 +187,7 @@ public abstract class AbstractXmlDAO<I extends ModelObject,
* @param item
*/
@Override
@SuppressWarnings("unchecked")
public void modify(I item)
{
if (logger.isTraceEnabled())
@@ -219,9 +215,10 @@ public abstract class AbstractXmlDAO<I extends ModelObject,
* @return
*/
@Override
@SuppressWarnings("unchecked")
public I get(String id)
{
return db.get(id);
return (I) db.get(id);
}
/**
@@ -293,7 +290,7 @@ public abstract class AbstractXmlDAO<I extends ModelObject,
//~--- fields ---------------------------------------------------------------
/** Field description */
private final ConfigurationStore<T> store;
protected final ConfigurationStore<T> store;
/** Field description */
protected T db;