merge + refactor getStoreDirectory

This commit is contained in:
Mohamed Karray
2018-11-28 15:14:49 +01:00
98 changed files with 1474 additions and 1534 deletions

View File

@@ -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());
}
}

View File

@@ -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);
}
}

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,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());
}
}

View File

@@ -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()));
}
}

View File

@@ -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);
}
}

View 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;
}
}