mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 02:31:14 +01:00
merge + refactor getStoreDirectory
This commit is contained in:
@@ -21,6 +21,9 @@ public class RepositoryPath implements ModelObject {
|
||||
@XmlTransient
|
||||
private Repository repository;
|
||||
|
||||
@XmlTransient
|
||||
private boolean toBeSynchronized;
|
||||
|
||||
/**
|
||||
* Needed from JAXB
|
||||
*/
|
||||
@@ -87,4 +90,12 @@ public class RepositoryPath implements ModelObject {
|
||||
public boolean isValid() {
|
||||
return StringUtils.isNotEmpty(path);
|
||||
}
|
||||
|
||||
public boolean toBeSynchronized() {
|
||||
return toBeSynchronized;
|
||||
}
|
||||
|
||||
public void setToBeSynchronized(boolean toBeSynchronized) {
|
||||
this.toBeSynchronized = toBeSynchronized;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,19 +37,21 @@ package sonia.scm.repository.xml;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
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.repository.RepositoryPathNotFoundException;
|
||||
import sonia.scm.store.JAXBConfigurationStore;
|
||||
import sonia.scm.store.Store;
|
||||
import sonia.scm.store.StoreConstants;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.xml.AbstractXmlDAO;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -61,19 +63,21 @@ public class XmlRepositoryDAO
|
||||
extends AbstractXmlDAO<Repository, XmlRepositoryDatabase>
|
||||
implements PathBasedRepositoryDAO {
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
public static final String STORE_NAME = "repositories";
|
||||
|
||||
private InitialRepositoryLocationResolver initialRepositoryLocationResolver;
|
||||
private final FileSystem fileSystem;
|
||||
private final SCMContextProvider context;
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
@Inject
|
||||
public XmlRepositoryDAO(InitialRepositoryLocationResolver initialRepositoryLocationResolver, SCMContextProvider context) {
|
||||
super(new JAXBConfigurationStore<>(XmlRepositoryDatabase.class, new File(context.getBaseDirectory(), StoreConstants.CONFIG_DIRECTORY_NAME + File.separator + STORE_NAME + StoreConstants.FILE_EXTENSION)));
|
||||
IOUtil.mkdirs(new File(context.getBaseDirectory(), StoreConstants.CONFIG_DIRECTORY_NAME + File.separator + STORE_NAME + StoreConstants.FILE_EXTENSION));
|
||||
public XmlRepositoryDAO(InitialRepositoryLocationResolver initialRepositoryLocationResolver, FileSystem fileSystem, SCMContextProvider context) {
|
||||
super(new JAXBConfigurationStore<>(XmlRepositoryDatabase.class,
|
||||
new File(context.getBaseDirectory(), Store.CONFIG.getGlobalStoreDirectory()+File.separator+ STORE_NAME + StoreConstants.FILE_EXTENSION)));
|
||||
this.initialRepositoryLocationResolver = initialRepositoryLocationResolver;
|
||||
this.fileSystem = fileSystem;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -95,14 +99,22 @@ public class XmlRepositoryDAO
|
||||
|
||||
@Override
|
||||
public void modify(Repository repository) {
|
||||
db.remove(repository.getId());
|
||||
add(repository);
|
||||
RepositoryPath repositoryPath = findExistingRepositoryPath(repository).orElseThrow(() -> new InternalRepositoryException(repository, "path object for repository not found"));
|
||||
repositoryPath.setRepository(repository);
|
||||
repositoryPath.setToBeSynchronized(true);
|
||||
storeDB();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Repository repository) {
|
||||
String path = initialRepositoryLocationResolver.getDirectory(repository).getAbsolutePath();
|
||||
RepositoryPath repositoryPath = new RepositoryPath(path, repository.getId(), repository.clone());
|
||||
InitialRepositoryLocation initialLocation = initialRepositoryLocationResolver.getRelativeRepositoryPath(repository);
|
||||
try {
|
||||
fileSystem.create(initialLocation.getAbsolutePath());
|
||||
} 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();
|
||||
@@ -134,6 +146,17 @@ public class XmlRepositoryDAO
|
||||
return repository.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Repository repository) {
|
||||
Path directory = getPath(repository);
|
||||
super.delete(repository);
|
||||
try {
|
||||
fileSystem.destroy(directory.toFile());
|
||||
} catch (IOException e) {
|
||||
throw new InternalRepositoryException(repository, "could not delete repository directory", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -145,15 +168,19 @@ public class XmlRepositoryDAO
|
||||
}
|
||||
|
||||
@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 {
|
||||
public Path getPath(Repository repository) {
|
||||
return context
|
||||
.getBaseDirectory()
|
||||
.toPath()
|
||||
.resolve(
|
||||
findExistingRepositoryPath(repository)
|
||||
.map(RepositoryPath::getPath)
|
||||
.orElseThrow(() -> new InternalRepositoryException(repository, "could not find base directory for repository")));
|
||||
}
|
||||
|
||||
return Paths.get(repositoryPath.get().getPath());
|
||||
}
|
||||
private Optional<RepositoryPath> findExistingRepositoryPath(Repository repository) {
|
||||
return db.values().stream()
|
||||
.filter(repoPath -> repoPath.getId().equals(repository.getId()))
|
||||
.findAny();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,16 +98,6 @@ public class XmlRepositoryDatabase implements XmlDatabase<RepositoryPath> {
|
||||
return get(id) != null;
|
||||
}
|
||||
|
||||
public boolean contains(Repository repository)
|
||||
{
|
||||
return repositoryPathMap.containsKey(createKey(repository));
|
||||
}
|
||||
|
||||
public void remove(Repository repository)
|
||||
{
|
||||
repositoryPathMap.remove(createKey(repository));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RepositoryPath remove(String id)
|
||||
{
|
||||
@@ -129,11 +119,6 @@ public class XmlRepositoryDatabase implements XmlDatabase<RepositoryPath> {
|
||||
return repositoryPathMap.values();
|
||||
}
|
||||
|
||||
public Collection<RepositoryPath> getPaths() {
|
||||
return repositoryPathMap.values();
|
||||
}
|
||||
|
||||
|
||||
public Repository get(NamespaceAndName namespaceAndName) {
|
||||
RepositoryPath repositoryPath = repositoryPathMap.get(createKey(namespaceAndName));
|
||||
if (repositoryPath != null) {
|
||||
|
||||
@@ -31,10 +31,12 @@
|
||||
|
||||
package sonia.scm.repository.xml;
|
||||
|
||||
import sonia.scm.SCMContext;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
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;
|
||||
@@ -42,6 +44,8 @@ import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -62,14 +66,20 @@ public class XmlRepositoryMapAdapter extends XmlAdapter<XmlRepositoryList, Map<S
|
||||
|
||||
// marshall the repo_path/metadata.xml files
|
||||
for (RepositoryPath repositoryPath : repositoryPaths.getRepositoryPaths()) {
|
||||
File dir = new File(repositoryPath.getPath());
|
||||
if (!dir.exists()){
|
||||
IOUtil.mkdirs(dir);
|
||||
if (repositoryPath.toBeSynchronized()) {
|
||||
|
||||
File baseDirectory = SCMContext.getContext().getBaseDirectory();
|
||||
Path dir = baseDirectory.toPath().resolve(repositoryPath.getPath());
|
||||
|
||||
if (!Files.isDirectory(dir)) {
|
||||
throw new InternalRepositoryException(repositoryPath.getRepository(), "repository path not found");
|
||||
}
|
||||
marshaller.marshal(repositoryPath.getRepository(), getRepositoryMetadataFile(dir.toFile()));
|
||||
repositoryPath.setToBeSynchronized(false);
|
||||
}
|
||||
marshaller.marshal(repositoryPath.getRepository(), getRepositoryMetadataFile(dir));
|
||||
}
|
||||
} catch (JAXBException ex) {
|
||||
throw new StoreException("failed to marshall repository database", ex);
|
||||
throw new StoreException("failed to marshal repository database", ex);
|
||||
}
|
||||
|
||||
return repositoryPaths;
|
||||
@@ -81,18 +91,21 @@ public class XmlRepositoryMapAdapter extends XmlAdapter<XmlRepositoryList, Map<S
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, RepositoryPath> unmarshal(XmlRepositoryList repositories) {
|
||||
public Map<String, RepositoryPath> unmarshal(XmlRepositoryList repositoryPaths) {
|
||||
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())));
|
||||
for (RepositoryPath repositoryPath : repositoryPaths) {
|
||||
SCMContextProvider contextProvider = SCMContext.getContext();
|
||||
File baseDirectory = contextProvider.getBaseDirectory();
|
||||
Repository repository = (Repository) unmarshaller.unmarshal(getRepositoryMetadataFile(baseDirectory.toPath().resolve(repositoryPath.getPath()).toFile()));
|
||||
|
||||
repositoryPath.setRepository(repository);
|
||||
repositoryPathMap.put(XmlRepositoryDatabase.createKey(repository), repositoryPath);
|
||||
}
|
||||
} catch (JAXBException ex) {
|
||||
throw new StoreException("failed to unmarshall object", ex);
|
||||
throw new StoreException("failed to unmarshal object", ex);
|
||||
}
|
||||
return repositoryPathMap;
|
||||
}
|
||||
|
||||
@@ -34,14 +34,12 @@ package sonia.scm.store;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryLocationResolver;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.MessageFormat;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -56,55 +54,54 @@ public abstract class FileBasedStoreFactory {
|
||||
* the logger for FileBasedStoreFactory
|
||||
*/
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FileBasedStoreFactory.class);
|
||||
private SCMContextProvider contextProvider;
|
||||
private RepositoryLocationResolver repositoryLocationResolver;
|
||||
private Store store;
|
||||
|
||||
private final String dataDirectoryName;
|
||||
private File storeDirectory;
|
||||
|
||||
private File dataDirectory;
|
||||
|
||||
protected FileBasedStoreFactory(RepositoryLocationResolver repositoryLocationResolver, String dataDirectoryName) {
|
||||
protected FileBasedStoreFactory(SCMContextProvider contextProvider , RepositoryLocationResolver repositoryLocationResolver, Store store) {
|
||||
this.contextProvider = contextProvider;
|
||||
this.repositoryLocationResolver = repositoryLocationResolver;
|
||||
this.dataDirectoryName = dataDirectoryName;
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
/**
|
||||
* Returns data directory for given name.
|
||||
*
|
||||
* @param name name of data directory
|
||||
*
|
||||
* @return data directory
|
||||
*/
|
||||
protected File getDirectory(String name) {
|
||||
if (dataDirectory == null) {
|
||||
dataDirectory = new File(repositoryLocationResolver.getGlobalStoreDirectory(), dataDirectoryName);
|
||||
LOG.debug("get data directory {}", dataDirectory);
|
||||
}
|
||||
|
||||
File storeDirectory = new File(dataDirectory, name);
|
||||
IOUtil.mkdirs(storeDirectory);
|
||||
return storeDirectory;
|
||||
protected File getStoreLocation(StoreParameters storeParameters) {
|
||||
return getStoreLocation(storeParameters.getName(), storeParameters.getType(), storeParameters.getRepository());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns data directory for given name.
|
||||
*
|
||||
* @param name name of data directory
|
||||
*
|
||||
* @return data directory
|
||||
*/
|
||||
protected File getDirectory(String name, Repository repository) {
|
||||
if (dataDirectory == null) {
|
||||
try {
|
||||
dataDirectory = new File(repositoryLocationResolver.getStoresDirectory(repository), dataDirectoryName);
|
||||
} catch (IOException e) {
|
||||
throw new InternalRepositoryException(repository, MessageFormat.format("Error on getting the store directory {0} of the repository {1}", dataDirectory.getAbsolutePath(), repository.getNamespaceAndName()), e);
|
||||
protected File getStoreLocation(String name, Class type, Repository repository) {
|
||||
if (storeDirectory == null) {
|
||||
if (repository != null) {
|
||||
LOG.debug("create store with type :{}, name:{} and repository {}", type, name, repository.getNamespaceAndName());
|
||||
storeDirectory = this.getStoreDirectory(store, repository);
|
||||
} else {
|
||||
LOG.debug("create store with type :{} and name:{} ", type, name);
|
||||
storeDirectory = this.getStoreDirectory(store);
|
||||
}
|
||||
LOG.debug("create data directory {}", dataDirectory);
|
||||
IOUtil.mkdirs(storeDirectory);
|
||||
}
|
||||
File storeDirectory = new File(dataDirectory, name);
|
||||
IOUtil.mkdirs(storeDirectory);
|
||||
return storeDirectory;
|
||||
return new File(this.storeDirectory, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the store directory of a specific repository
|
||||
* @param store the type of the store
|
||||
* @param repository the repo
|
||||
* @return the store directory of a specific repository
|
||||
*/
|
||||
private File getStoreDirectory(Store store, Repository repository) {
|
||||
return new File (repositoryLocationResolver.getRepositoryDirectory(repository), store.getRepositoryStoreDirectory());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the global store directory
|
||||
* @param store the type of the store
|
||||
* @return the global store directory
|
||||
*/
|
||||
private File getStoreDirectory(Store store) {
|
||||
return new File(contextProvider.getBaseDirectory(), store.getGlobalStoreDirectory());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,14 +31,17 @@
|
||||
package sonia.scm.store;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.repository.RepositoryLocationResolver;
|
||||
import sonia.scm.security.KeyGenerator;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* File based store factory.
|
||||
@@ -48,8 +51,6 @@ import sonia.scm.security.KeyGenerator;
|
||||
@Singleton
|
||||
public class FileBlobStoreFactory extends FileBasedStoreFactory implements BlobStoreFactory {
|
||||
|
||||
private static final String DIRECTORY_NAME = "blob";
|
||||
|
||||
/**
|
||||
* the logger for FileBlobStoreFactory
|
||||
*/
|
||||
@@ -60,21 +61,22 @@ public class FileBlobStoreFactory extends FileBasedStoreFactory implements BlobS
|
||||
/**
|
||||
* Constructs a new instance.
|
||||
*
|
||||
* @param repositoryLocationResolver
|
||||
* @param repositoryLocationResolver location resolver
|
||||
* @param keyGenerator key generator
|
||||
*/
|
||||
@Inject
|
||||
public FileBlobStoreFactory(RepositoryLocationResolver repositoryLocationResolver, KeyGenerator keyGenerator) {
|
||||
super(repositoryLocationResolver, DIRECTORY_NAME);
|
||||
public FileBlobStoreFactory(SCMContextProvider contextProvider ,RepositoryLocationResolver repositoryLocationResolver, KeyGenerator keyGenerator) {
|
||||
super(contextProvider, repositoryLocationResolver, Store.BLOB);
|
||||
this.keyGenerator = keyGenerator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public BlobStore getStore(StoreParameters storeParameters) {
|
||||
if (storeParameters.getRepository() != null) {
|
||||
return new FileBlobStore(keyGenerator, getDirectory(storeParameters.getName(), storeParameters.getRepository()));
|
||||
}
|
||||
return new FileBlobStore(keyGenerator, getDirectory(storeParameters.getName()));
|
||||
File storeLocation = getStoreLocation(storeParameters);
|
||||
IOUtil.mkdirs(storeLocation);
|
||||
return new FileBlobStore(keyGenerator, storeLocation);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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,109 +24,43 @@
|
||||
* 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.store;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryLocationResolver;
|
||||
import sonia.scm.security.KeyGenerator;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class JAXBConfigurationEntryStoreFactory
|
||||
implements ConfigurationEntryStoreFactory
|
||||
{
|
||||
public class JAXBConfigurationEntryStoreFactory extends FileBasedStoreFactory
|
||||
implements ConfigurationEntryStoreFactory {
|
||||
|
||||
/**
|
||||
* the logger for JAXBConfigurationEntryStoreFactory
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(JAXBConfigurationEntryStoreFactory.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param keyGenerator
|
||||
* @param context
|
||||
*/
|
||||
@Inject
|
||||
public JAXBConfigurationEntryStoreFactory(KeyGenerator keyGenerator,
|
||||
SCMContextProvider context)
|
||||
{
|
||||
this.keyGenerator = keyGenerator;
|
||||
directory = new File(context.getBaseDirectory(),
|
||||
StoreConstants.CONFIG_DIRECTORY_NAME);
|
||||
IOUtil.mkdirs(directory);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private ConfigurationEntryStore getStore(Class type, String name)
|
||||
{
|
||||
logger.debug("create new configuration store for type {} with name {}",
|
||||
type, name);
|
||||
|
||||
//J-
|
||||
return new JAXBConfigurationEntryStore(
|
||||
new File(directory,name.concat(StoreConstants.FILE_EXTENSION)),
|
||||
keyGenerator,
|
||||
type
|
||||
);
|
||||
//J+
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private File directory;
|
||||
|
||||
/** Field description */
|
||||
private KeyGenerator keyGenerator;
|
||||
|
||||
@Override
|
||||
public ConfigurationEntryStore getStore(StoreParameters storeParameters) {
|
||||
if (storeParameters.getRepository() != null){
|
||||
return getStore(storeParameters.getType(),storeParameters.getName(),storeParameters.getRepository());
|
||||
}
|
||||
return getStore(storeParameters.getType(),storeParameters.getName());
|
||||
@Inject
|
||||
public JAXBConfigurationEntryStoreFactory(SCMContextProvider contextProvider , RepositoryLocationResolver repositoryLocationResolver, KeyGenerator keyGenerator) {
|
||||
super(contextProvider, repositoryLocationResolver, Store.CONFIG);
|
||||
this.keyGenerator = keyGenerator;
|
||||
}
|
||||
|
||||
private ConfigurationEntryStore getStore(Class type, String name, Repository repository) {
|
||||
return null;
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public ConfigurationEntryStore getStore(StoreParameters storeParameters) {
|
||||
return new JAXBConfigurationEntryStore(getStoreLocation(storeParameters.getName().concat(StoreConstants.FILE_EXTENSION), storeParameters.getType(), storeParameters.getRepository()), keyGenerator, storeParameters.getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,15 +32,8 @@ package sonia.scm.store;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.repository.RepositoryLocationResolver;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* JAXB implementation of {@link StoreFactory}.
|
||||
@@ -48,14 +41,7 @@ import java.io.IOException;
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class JAXBConfigurationStoreFactory implements ConfigurationStoreFactory {
|
||||
|
||||
/**
|
||||
* the logger for JAXBConfigurationStoreFactory
|
||||
*/
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JAXBConfigurationStoreFactory.class);
|
||||
|
||||
private RepositoryLocationResolver repositoryLocationResolver;
|
||||
public class JAXBConfigurationStoreFactory extends FileBasedStoreFactory implements ConfigurationStoreFactory {
|
||||
|
||||
/**
|
||||
* Constructs a new instance.
|
||||
@@ -63,57 +49,13 @@ public class JAXBConfigurationStoreFactory implements ConfigurationStoreFactory
|
||||
* @param repositoryLocationResolver Resolver to get the repository Directory
|
||||
*/
|
||||
@Inject
|
||||
public JAXBConfigurationStoreFactory(RepositoryLocationResolver repositoryLocationResolver) {
|
||||
this.repositoryLocationResolver = repositoryLocationResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create the global config directory.
|
||||
*
|
||||
* @return the global config directory.
|
||||
*/
|
||||
private File getGlobalConfigDirectory() {
|
||||
File baseDirectory = repositoryLocationResolver.getInitialBaseDirectory();
|
||||
File configDirectory = new File(baseDirectory, StoreConstants.CONFIG_DIRECTORY_NAME);
|
||||
return getOrCreateFile(configDirectory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create the repository specific config directory.
|
||||
*
|
||||
* @return the repository specific config directory.
|
||||
*/
|
||||
private File getRepositoryConfigDirectory(Repository repository) {
|
||||
File baseDirectory = null;
|
||||
try {
|
||||
baseDirectory = repositoryLocationResolver.getConfigDirectory(repository);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
File configDirectory = new File(baseDirectory, StoreConstants.CONFIG_DIRECTORY_NAME);
|
||||
return getOrCreateFile(configDirectory);
|
||||
}
|
||||
|
||||
private File getOrCreateFile(File directory) {
|
||||
if (!directory.exists()) {
|
||||
IOUtil.mkdirs(directory);
|
||||
}
|
||||
return directory;
|
||||
}
|
||||
|
||||
private JAXBConfigurationStore getStore(Class type, String name, File configDirectory) {
|
||||
File configFile = new File(configDirectory, name.concat(StoreConstants.FILE_EXTENSION));
|
||||
LOG.debug("create store for {} at {}", type.getName(), configFile.getPath());
|
||||
return new JAXBConfigurationStore<>(type, configFile);
|
||||
public JAXBConfigurationStoreFactory(SCMContextProvider contextProvider, RepositoryLocationResolver repositoryLocationResolver) {
|
||||
super(contextProvider, repositoryLocationResolver, Store.CONFIG);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public JAXBConfigurationStore getStore(StoreParameters storeParameters) {
|
||||
try {
|
||||
return getStore(storeParameters.getType(), storeParameters.getName(),repositoryLocationResolver.getRepositoryDirectory(storeParameters.getRepository()));
|
||||
} catch (IOException e) {
|
||||
|
||||
throw new InternalRepositoryException(storeParameters.getRepository(),"Error on getting the store of the repository"+ storeParameters.getRepository().getNamespaceAndName());
|
||||
}
|
||||
return new JAXBConfigurationStore(storeParameters.getType(), getStoreLocation(storeParameters.getName().concat(StoreConstants.FILE_EXTENSION), storeParameters.getType(), storeParameters.getRepository()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,12 @@ import com.google.inject.Singleton;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.repository.RepositoryLocationResolver;
|
||||
import sonia.scm.security.KeyGenerator;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -49,26 +53,22 @@ import sonia.scm.security.KeyGenerator;
|
||||
*/
|
||||
@Singleton
|
||||
public class JAXBDataStoreFactory extends FileBasedStoreFactory
|
||||
implements DataStoreFactory
|
||||
{
|
||||
implements DataStoreFactory {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(JAXBDataStoreFactory.class);
|
||||
private static final String DIRECTORY_NAME = "data";
|
||||
private KeyGenerator keyGenerator;
|
||||
|
||||
@Inject
|
||||
public JAXBDataStoreFactory(RepositoryLocationResolver repositoryLocationResolver, KeyGenerator keyGenerator) {
|
||||
super(repositoryLocationResolver, DIRECTORY_NAME);
|
||||
public JAXBDataStoreFactory(SCMContextProvider contextProvider , RepositoryLocationResolver repositoryLocationResolver, KeyGenerator keyGenerator) {
|
||||
super(contextProvider, repositoryLocationResolver, Store.DATA);
|
||||
this.keyGenerator = keyGenerator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public DataStore getStore(StoreParameters storeParameters) {
|
||||
logger.debug("create new store for type {} with name {}", storeParameters.getType(), storeParameters.getName());
|
||||
if (storeParameters.getRepository() != null) {
|
||||
return new JAXBDataStore(keyGenerator, storeParameters.getType(), getDirectory(storeParameters.getName(), storeParameters.getRepository()));
|
||||
}
|
||||
return new JAXBDataStore(keyGenerator, storeParameters.getType(), getDirectory(storeParameters.getName()));
|
||||
File storeLocation = getStoreLocation(storeParameters);
|
||||
IOUtil.mkdirs(storeLocation);
|
||||
return new JAXBDataStore(keyGenerator, storeParameters.getType(), storeLocation);
|
||||
}
|
||||
}
|
||||
|
||||
49
scm-dao-xml/src/main/java/sonia/scm/store/Store.java
Normal file
49
scm-dao-xml/src/main/java/sonia/scm/store/Store.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package sonia.scm.store;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public enum Store {
|
||||
CONFIG("config"),
|
||||
DATA("data"),
|
||||
BLOB("blob");
|
||||
|
||||
private static final String GLOBAL_STORE_BASE_DIRECTORY = "var";
|
||||
|
||||
private String directory;
|
||||
|
||||
Store(String directory) {
|
||||
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the relkative store directory path to be stored in the repository root
|
||||
* <p>
|
||||
* The repository store directories are:
|
||||
* repo_base_dir/config/
|
||||
* repo_base_dir/blob/
|
||||
* repo_base_dir/data/
|
||||
*
|
||||
* @return the relative store directory path to be stored in the repository root
|
||||
*/
|
||||
public String getRepositoryStoreDirectory() {
|
||||
return directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the relative store directory path to be stored in the global root
|
||||
* <p>
|
||||
* The global store directories are:
|
||||
* base_dir/config/
|
||||
* base_dir/var/blob/
|
||||
* base_dir/var/data/
|
||||
*
|
||||
* @return the relative store directory path to be stored in the global root
|
||||
*/
|
||||
public String getGlobalStoreDirectory() {
|
||||
if (this.equals(CONFIG)) {
|
||||
return directory;
|
||||
}
|
||||
return GLOBAL_STORE_BASE_DIRECTORY + File.separator + directory;
|
||||
}
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public abstract class AbstractXmlDAO<I extends ModelObject,
|
||||
* the logger for XmlGroupDAO
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(XmlGroupDAO.class);
|
||||
LoggerFactory.getLogger(AbstractXmlDAO.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user