mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 02:31:14 +01:00
refactor store api
This commit is contained in:
@@ -39,9 +39,10 @@ import com.google.inject.Singleton;
|
||||
|
||||
import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupDAO;
|
||||
import sonia.scm.store.StoreFactory;
|
||||
import sonia.scm.xml.AbstractXmlDAO;
|
||||
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -63,7 +64,7 @@ public class XmlGroupDAO extends AbstractXmlDAO<Group, XmlGroupDatabase>
|
||||
* @param storeFactory
|
||||
*/
|
||||
@Inject
|
||||
public XmlGroupDAO(StoreFactory storeFactory)
|
||||
public XmlGroupDAO(ConfigurationStoreFactory storeFactory)
|
||||
{
|
||||
super(storeFactory.getStore(XmlGroupDatabase.class, STORE_NAME));
|
||||
}
|
||||
|
||||
@@ -39,8 +39,8 @@ import com.google.inject.Singleton;
|
||||
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryDAO;
|
||||
import sonia.scm.store.StoreFactory;
|
||||
import sonia.scm.xml.AbstractXmlDAO;
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -64,7 +64,7 @@ public class XmlRepositoryDAO
|
||||
* @param storeFactory
|
||||
*/
|
||||
@Inject
|
||||
public XmlRepositoryDAO(StoreFactory storeFactory)
|
||||
public XmlRepositoryDAO(ConfigurationStoreFactory storeFactory)
|
||||
{
|
||||
super(storeFactory.getStore(XmlRepositoryDatabase.class, STORE_NAME));
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ import java.io.File;
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class FileBasedStore<T> implements StoreBase<T>
|
||||
public abstract class FileBasedStore<T> implements MultiEntryStore<T>
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,12 +28,9 @@
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.store;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -41,75 +38,52 @@ import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
*
|
||||
* Abstract store factory for file based stores.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class FileBasedStoreFactory
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static final String BASE_DIRECTORY = "var";
|
||||
public abstract class FileBasedStoreFactory {
|
||||
|
||||
/**
|
||||
* the logger for FileBasedStoreFactory
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(FileBasedStoreFactory.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FileBasedStoreFactory.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
private static final String BASE_DIRECTORY = "var";
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @param dataDirectoryName
|
||||
*/
|
||||
public FileBasedStoreFactory(SCMContextProvider context,
|
||||
String dataDirectoryName)
|
||||
{
|
||||
private final SCMContextProvider context;
|
||||
|
||||
private final String dataDirectoryName;
|
||||
|
||||
private File dataDirectory;
|
||||
|
||||
protected FileBasedStoreFactory(SCMContextProvider context,
|
||||
String dataDirectoryName) {
|
||||
this.context = context;
|
||||
this.dataDirectoryName = dataDirectoryName;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
* Returns data directory for given name.
|
||||
*
|
||||
* @param name name of data directory
|
||||
*
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
* @return data directory
|
||||
*/
|
||||
protected File getDirectory(String name)
|
||||
{
|
||||
if (dataDirectory == null)
|
||||
{
|
||||
protected File getDirectory(String name) {
|
||||
if (dataDirectory == null) {
|
||||
dataDirectory = new File(context.getBaseDirectory(),
|
||||
BASE_DIRECTORY.concat(File.separator).concat(dataDirectoryName));
|
||||
logger.debug("create data directory {}", dataDirectory);
|
||||
LOG.debug("create data directory {}", dataDirectory);
|
||||
}
|
||||
|
||||
File storeDirectory = new File(dataDirectory, name);
|
||||
|
||||
IOUtil.mkdirs(storeDirectory);
|
||||
|
||||
return storeDirectory;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private final SCMContextProvider context;
|
||||
|
||||
/** Field description */
|
||||
private File dataDirectory;
|
||||
|
||||
/** Field description */
|
||||
private final String dataDirectoryName;
|
||||
}
|
||||
|
||||
@@ -28,12 +28,9 @@
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.store;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -43,87 +40,39 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* File base implementation of {@link Blob}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public final class FileBlob implements Blob
|
||||
{
|
||||
public final class FileBlob implements Blob {
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param id
|
||||
* @param file
|
||||
*/
|
||||
public FileBlob(String id, File file)
|
||||
{
|
||||
private final String id;
|
||||
private final File file;
|
||||
|
||||
FileBlob(String id, File file) {
|
||||
this.id = id;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void commit() throws IOException
|
||||
{
|
||||
public void commit() throws IOException {
|
||||
|
||||
// nothing todo
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String getId()
|
||||
{
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
@Override
|
||||
public InputStream getInputStream() throws FileNotFoundException
|
||||
{
|
||||
public InputStream getInputStream() throws FileNotFoundException {
|
||||
return new FileInputStream(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public OutputStream getOutputStream() throws IOException
|
||||
{
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return new FileOutputStream(file);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private final File file;
|
||||
|
||||
/** Field description */
|
||||
private final String id;
|
||||
}
|
||||
|
||||
@@ -28,12 +28,9 @@
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.store;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
@@ -45,147 +42,86 @@ import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.security.KeyGenerator;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* File based implementation of {@link BlobStore}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class FileBlobStore extends FileBasedStore<Blob> implements BlobStore
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static final String SUFFIX = ".blob";
|
||||
public class FileBlobStore extends FileBasedStore<Blob> implements BlobStore {
|
||||
|
||||
/**
|
||||
* the logger for FileBlobStore
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(FileBlobStore.class);
|
||||
private static final Logger LOG
|
||||
= LoggerFactory.getLogger(FileBlobStore.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
private static final String SUFFIX = ".blob";
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param keyGenerator
|
||||
* @param directory
|
||||
*/
|
||||
public FileBlobStore(KeyGenerator keyGenerator, File directory)
|
||||
{
|
||||
private final KeyGenerator keyGenerator;
|
||||
|
||||
FileBlobStore(KeyGenerator keyGenerator, File directory) {
|
||||
super(directory, SUFFIX);
|
||||
this.keyGenerator = keyGenerator;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Blob create()
|
||||
{
|
||||
public Blob create() {
|
||||
return create(keyGenerator.createKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param id
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Blob create(String id)
|
||||
{
|
||||
public Blob create(String id) {
|
||||
Preconditions.checkArgument(!Strings.isNullOrEmpty(id),
|
||||
"id argument is required");
|
||||
logger.debug("create new blob with id {}", id);
|
||||
LOG.debug("create new blob with id {}", id);
|
||||
|
||||
File file = getFile(id);
|
||||
|
||||
try
|
||||
{
|
||||
if (file.exists())
|
||||
{
|
||||
try {
|
||||
if (file.exists()) {
|
||||
throw new EntryAlreadyExistsStoreException(
|
||||
"blob with id ".concat(id).concat(" allready exists"));
|
||||
}
|
||||
else if (!file.createNewFile())
|
||||
{
|
||||
else if (!file.createNewFile()) {
|
||||
throw new StoreException("could not create blob for id ".concat(id));
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
catch (IOException ex) {
|
||||
throw new StoreException("could not create blob for id ".concat(id), ex);
|
||||
}
|
||||
|
||||
return new FileBlob(id, file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param blob
|
||||
*/
|
||||
@Override
|
||||
public void remove(Blob blob)
|
||||
{
|
||||
public void remove(Blob blob) {
|
||||
Preconditions.checkNotNull(blob, "blob argument is required");
|
||||
remove(blob.getId());
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Blob> getAll()
|
||||
{
|
||||
logger.trace("get all items from data store");
|
||||
public List<Blob> getAll() {
|
||||
LOG.trace("get all items from data store");
|
||||
|
||||
Builder<Blob> builder = ImmutableList.builder();
|
||||
|
||||
for (File file : directory.listFiles())
|
||||
{
|
||||
for (File file : directory.listFiles()) {
|
||||
builder.add(read(file));
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param file
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected FileBlob read(File file)
|
||||
{
|
||||
protected FileBlob read(File file) {
|
||||
FileBlob blob = null;
|
||||
|
||||
if (file.exists())
|
||||
{
|
||||
if (file.exists()) {
|
||||
String id = getId(file);
|
||||
|
||||
blob = new FileBlob(id, file);
|
||||
@@ -194,8 +130,4 @@ public class FileBlobStore extends FileBasedStore<Blob> implements BlobStore
|
||||
return blob;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** key generator */
|
||||
private final KeyGenerator keyGenerator;
|
||||
}
|
||||
|
||||
@@ -28,12 +28,9 @@
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.store;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
@@ -44,60 +41,40 @@ import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.security.KeyGenerator;
|
||||
|
||||
/**
|
||||
* File based store factory.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class FileBlobStoreFactory extends FileBasedStoreFactory
|
||||
implements BlobStoreFactory
|
||||
{
|
||||
public class FileBlobStoreFactory extends FileBasedStoreFactory implements BlobStoreFactory {
|
||||
|
||||
/** Field description */
|
||||
private static final String DIRECTORY_NAME = "blob";
|
||||
|
||||
/**
|
||||
* the logger for FileBlobStoreFactory
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(FileBlobStoreFactory.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FileBlobStoreFactory.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
private final KeyGenerator keyGenerator;
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
* Constructs a new instance.
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @param keyGenerator
|
||||
* @param context scm context
|
||||
* @param keyGenerator key generator
|
||||
*/
|
||||
@Inject
|
||||
public FileBlobStoreFactory(SCMContextProvider context,
|
||||
KeyGenerator keyGenerator)
|
||||
{
|
||||
KeyGenerator keyGenerator) {
|
||||
super(context, DIRECTORY_NAME);
|
||||
this.keyGenerator = keyGenerator;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public BlobStore getBlobStore(String name)
|
||||
{
|
||||
logger.debug("create new blob with name {}", name);
|
||||
public BlobStore getBlobStore(String name) {
|
||||
LOG.debug("create new blob with name {}", name);
|
||||
|
||||
return new FileBlobStore(keyGenerator, getDirectory(name));
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** key generator */
|
||||
private final KeyGenerator keyGenerator;
|
||||
}
|
||||
|
||||
@@ -80,8 +80,7 @@ import javax.xml.stream.XMLStreamWriter;
|
||||
*
|
||||
* @param <V>
|
||||
*/
|
||||
public class JAXBConfigurationEntryStore<V>
|
||||
implements ConfigurationEntryStore<V>
|
||||
public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V>
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -28,18 +28,13 @@
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.store;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
@@ -47,121 +42,81 @@ import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
|
||||
/**
|
||||
* JAXB implementation of {@link ConfigurationStore}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class JAXBStore<T> extends AbstractStore<T>
|
||||
{
|
||||
|
||||
/** the logger for JAXBStore */
|
||||
private static final Logger logger = LoggerFactory.getLogger(JAXBStore.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
public class JAXBConfigurationStore<T> extends AbstractStore<T> {
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
* @param configFile
|
||||
* the logger for JAXBConfigurationStore
|
||||
*/
|
||||
public JAXBStore(Class<T> type, File configFile)
|
||||
{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JAXBConfigurationStore.class);
|
||||
|
||||
private Class<T> type;
|
||||
|
||||
private File configFile;
|
||||
|
||||
private JAXBContext context;
|
||||
|
||||
JAXBConfigurationStore(Class<T> type, File configFile) {
|
||||
this.type = type;
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
context = JAXBContext.newInstance(type);
|
||||
this.configFile = configFile;
|
||||
}
|
||||
catch (JAXBException ex)
|
||||
{
|
||||
throw new StoreException(ex);
|
||||
catch (JAXBException ex) {
|
||||
throw new StoreException("failed to create jaxb context", ex);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
* Returns type of stored object.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
* @return type
|
||||
*/
|
||||
public Class<T> getType()
|
||||
{
|
||||
public Class<T> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T readObject()
|
||||
{
|
||||
logger.debug("load {} from store {}", type, configFile);
|
||||
protected T readObject() {
|
||||
LOG.debug("load {} from store {}", type, configFile);
|
||||
|
||||
T result = null;
|
||||
|
||||
if (configFile.exists())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (configFile.exists()) {
|
||||
try {
|
||||
result = (T) context.createUnmarshaller().unmarshal(configFile);
|
||||
}
|
||||
catch (JAXBException ex)
|
||||
{
|
||||
throw new StoreException(ex);
|
||||
catch (JAXBException ex) {
|
||||
throw new StoreException("failed to unmarshall object", ex);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param object
|
||||
*/
|
||||
@Override
|
||||
protected void writeObject(T object)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("store {} to {}", object.getClass().getName(),
|
||||
protected void writeObject(T object) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("store {} to {}", object.getClass().getName(),
|
||||
configFile.getPath());
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
Marshaller marshaller = context.createMarshaller();
|
||||
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
marshaller.marshal(object, configFile);
|
||||
}
|
||||
catch (JAXBException ex)
|
||||
{
|
||||
throw new StoreException(ex);
|
||||
catch (JAXBException ex) {
|
||||
throw new StoreException("failed to marshall object", ex);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private File configFile;
|
||||
|
||||
/** Field description */
|
||||
private JAXBContext context;
|
||||
|
||||
/** Field description */
|
||||
private Class<T> type;
|
||||
}
|
||||
@@ -28,13 +28,9 @@
|
||||
* 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;
|
||||
@@ -43,86 +39,48 @@ import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* JAXB implementation of {@link JAXBConfigurationStoreFactory}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class JAXBStoreFactory implements StoreFactory
|
||||
{
|
||||
|
||||
/** the logger for JAXBStoreFactory */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(JAXBStoreFactory.class);
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
public class JAXBConfigurationStoreFactory implements ConfigurationStoreFactory {
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* the logger for JAXBConfigurationStoreFactory
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JAXBConfigurationStoreFactory.class);
|
||||
|
||||
// do nothing
|
||||
}
|
||||
private final File configDirectory;
|
||||
|
||||
/**
|
||||
* Method description
|
||||
* Constructs a new instance.
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @param context scm context
|
||||
*/
|
||||
@Override
|
||||
public void init(SCMContextProvider context)
|
||||
{
|
||||
configDirectory = new File(context.getBaseDirectory(),
|
||||
StoreConstants.CONFIGDIRECTORY_NAME);
|
||||
@Inject
|
||||
public JAXBConfigurationStoreFactory(SCMContextProvider context) {
|
||||
configDirectory = new File(context.getBaseDirectory(), StoreConstants.CONFIGDIRECTORY_NAME);
|
||||
IOUtil.mkdirs(configDirectory);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
* @param name
|
||||
* @param <T>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> JAXBStore<T> getStore(Class<T> type, String name)
|
||||
{
|
||||
if (configDirectory == null)
|
||||
{
|
||||
public <T> JAXBConfigurationStore<T> getStore(Class<T> type, String name) {
|
||||
if (configDirectory == null) {
|
||||
throw new IllegalStateException("store factory is not initialized");
|
||||
}
|
||||
|
||||
File configFile = new File(configDirectory,
|
||||
name.concat(StoreConstants.FILE_EXTENSION));
|
||||
File configFile = new File(configDirectory, name.concat(StoreConstants.FILE_EXTENSION));
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("create store for {} at {}", type.getName(),
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("create store for {} at {}", type.getName(),
|
||||
configFile.getPath());
|
||||
}
|
||||
|
||||
return new JAXBStore<T>(type, configFile);
|
||||
return new JAXBConfigurationStore<>(type, configFile);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private File configDirectory;
|
||||
}
|
||||
@@ -28,12 +28,9 @@
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.store;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableMap.Builder;
|
||||
|
||||
@@ -43,7 +40,6 @@ import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.security.KeyGenerator;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -53,87 +49,57 @@ import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
|
||||
/**
|
||||
* Jaxb implementation of {@link DataStore}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*
|
||||
* @param <T>
|
||||
* @param <T> type of stored data.
|
||||
*/
|
||||
public class JAXBDataStore<T> extends FileBasedStore<T> implements DataStore<T>
|
||||
{
|
||||
public class JAXBDataStore<T> extends FileBasedStore<T> implements DataStore<T> {
|
||||
|
||||
/**
|
||||
* the logger for JAXBDataStore
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(JAXBDataStore.class);
|
||||
private static final Logger LOG
|
||||
= LoggerFactory.getLogger(JAXBDataStore.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
private final JAXBContext context;
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
* @param keyGenerator
|
||||
* @param directory
|
||||
*/
|
||||
public JAXBDataStore(KeyGenerator keyGenerator, Class<T> type, File directory)
|
||||
{
|
||||
private final KeyGenerator keyGenerator;
|
||||
|
||||
JAXBDataStore(KeyGenerator keyGenerator, Class<T> type, File directory) {
|
||||
super(directory, StoreConstants.FILE_EXTENSION);
|
||||
this.keyGenerator = keyGenerator;
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
context = JAXBContext.newInstance(type);
|
||||
this.directory = directory;
|
||||
}
|
||||
catch (JAXBException ex)
|
||||
{
|
||||
throw new StoreException(ex);
|
||||
catch (JAXBException ex) {
|
||||
throw new StoreException("failed to create jaxb context", ex);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param id
|
||||
* @param item
|
||||
*/
|
||||
@Override
|
||||
public void put(String id, T item)
|
||||
{
|
||||
logger.debug("put item {} to store", id);
|
||||
public void put(String id, T item) {
|
||||
LOG.debug("put item {} to store", id);
|
||||
|
||||
File file = getFile(id);
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
Marshaller marshaller = context.createMarshaller();
|
||||
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
marshaller.marshal(item, file);
|
||||
}
|
||||
catch (JAXBException ex)
|
||||
{
|
||||
catch (JAXBException ex) {
|
||||
throw new StoreException("could not write object with id ".concat(id),
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param item
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String put(T item)
|
||||
{
|
||||
public String put(T item) {
|
||||
String key = keyGenerator.createKey();
|
||||
|
||||
put(key, item);
|
||||
@@ -141,55 +107,31 @@ public class JAXBDataStore<T> extends FileBasedStore<T> implements DataStore<T>
|
||||
return key;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, T> getAll()
|
||||
{
|
||||
logger.trace("get all items from data store");
|
||||
public Map<String, T> getAll() {
|
||||
LOG.trace("get all items from data store");
|
||||
|
||||
Builder<String, T> builder = ImmutableMap.builder();
|
||||
|
||||
for (File file : directory.listFiles())
|
||||
{
|
||||
for (File file : directory.listFiles()) {
|
||||
builder.put(getId(file), read(file));
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param file
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T read(File file)
|
||||
{
|
||||
protected T read(File file) {
|
||||
T item = null;
|
||||
|
||||
if (file.exists())
|
||||
{
|
||||
logger.trace("try to read {}", file);
|
||||
if (file.exists()) {
|
||||
LOG.trace("try to read {}", file);
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
item = (T) context.createUnmarshaller().unmarshal(file);
|
||||
}
|
||||
catch (JAXBException ex)
|
||||
{
|
||||
catch (JAXBException ex) {
|
||||
throw new StoreException(
|
||||
"could not read object ".concat(file.getPath()), ex);
|
||||
}
|
||||
@@ -197,12 +139,4 @@ public class JAXBDataStore<T> extends FileBasedStore<T> implements DataStore<T>
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private JAXBContext context;
|
||||
|
||||
/** Field description */
|
||||
private final KeyGenerator keyGenerator;
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ public class JAXBDataStoreFactory extends FileBasedStoreFactory
|
||||
{
|
||||
logger.debug("create new store for type {} with name {}", type, name);
|
||||
|
||||
return new JAXBDataStore<T>(keyGenerator, type, getDirectory(name));
|
||||
return new JAXBDataStore<>(keyGenerator, type, getDirectory(name));
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
@@ -33,7 +33,8 @@
|
||||
package sonia.scm.store;
|
||||
|
||||
/**
|
||||
*
|
||||
* Store constants for xml implementations.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public interface StoreConstants
|
||||
|
||||
@@ -37,10 +37,10 @@ package sonia.scm.user.xml;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import sonia.scm.store.StoreFactory;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserDAO;
|
||||
import sonia.scm.xml.AbstractXmlDAO;
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -63,7 +63,7 @@ public class XmlUserDAO extends AbstractXmlDAO<User, XmlUserDatabase>
|
||||
* @param storeFactory
|
||||
*/
|
||||
@Inject
|
||||
public XmlUserDAO(StoreFactory storeFactory)
|
||||
public XmlUserDAO(ConfigurationStoreFactory storeFactory)
|
||||
{
|
||||
super(storeFactory.getStore(XmlUserDatabase.class, STORE_NAME));
|
||||
}
|
||||
|
||||
@@ -41,11 +41,11 @@ import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.GenericDAO;
|
||||
import sonia.scm.ModelObject;
|
||||
import sonia.scm.group.xml.XmlGroupDAO;
|
||||
import sonia.scm.store.Store;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Collection;
|
||||
import sonia.scm.store.ConfigurationStore;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -76,7 +76,7 @@ public abstract class AbstractXmlDAO<I extends ModelObject,
|
||||
*
|
||||
* @param store
|
||||
*/
|
||||
public AbstractXmlDAO(Store<T> store)
|
||||
public AbstractXmlDAO(ConfigurationStore<T> store)
|
||||
{
|
||||
this.store = store;
|
||||
db = store.get();
|
||||
@@ -290,7 +290,7 @@ public abstract class AbstractXmlDAO<I extends ModelObject,
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private final Store<T> store;
|
||||
private final ConfigurationStore<T> store;
|
||||
|
||||
/** Field description */
|
||||
protected T db;
|
||||
|
||||
Reference in New Issue
Block a user