mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 08:55:44 +01:00
added AbstractRepositoryHandler to scm.core
This commit is contained in:
@@ -9,14 +9,13 @@ package sonia.scm.repository;
|
|||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import sonia.scm.SCMContextProvider;
|
import sonia.scm.ConfigurationException;
|
||||||
import sonia.scm.io.CommandResult;
|
import sonia.scm.io.CommandResult;
|
||||||
import sonia.scm.io.ExtendedCommand;
|
import sonia.scm.io.ExtendedCommand;
|
||||||
import sonia.scm.io.INIConfiguration;
|
import sonia.scm.io.INIConfiguration;
|
||||||
import sonia.scm.io.INIConfigurationReader;
|
import sonia.scm.io.INIConfigurationReader;
|
||||||
import sonia.scm.io.INIConfigurationWriter;
|
import sonia.scm.io.INIConfigurationWriter;
|
||||||
import sonia.scm.io.INISection;
|
import sonia.scm.io.INISection;
|
||||||
import sonia.scm.util.AssertUtil;
|
|
||||||
import sonia.scm.util.Util;
|
import sonia.scm.util.Util;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
@@ -30,17 +29,14 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.xml.bind.JAXB;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
public class HgRepositoryHandler implements RepositoryHandler
|
public class HgRepositoryHandler extends AbstractRepositoryHandler<HgConfig>
|
||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
@@ -67,15 +63,6 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -89,9 +76,7 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
public void create(Repository repository)
|
public void create(Repository repository)
|
||||||
throws RepositoryException, IOException
|
throws RepositoryException, IOException
|
||||||
{
|
{
|
||||||
repository.setId(UUID.randomUUID().toString());
|
initNewRepository(repository);
|
||||||
repository.setUrl(buildUrl(repository.getName()));
|
|
||||||
repository.setCreationDate(new Date());
|
|
||||||
|
|
||||||
File directory = getDirectory(repository);
|
File directory = getDirectory(repository);
|
||||||
|
|
||||||
@@ -146,34 +131,6 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param context
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void init(SCMContextProvider context)
|
|
||||||
{
|
|
||||||
File baseDirectory = context.getBaseDirectory();
|
|
||||||
|
|
||||||
AssertUtil.assertIsNotNull(baseDirectory);
|
|
||||||
configFile = new File(baseDirectory, CONFIG_FILE);
|
|
||||||
loadConfig();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public void loadConfig()
|
|
||||||
{
|
|
||||||
if (configFile.exists())
|
|
||||||
{
|
|
||||||
config = JAXB.unmarshal(configFile, HgConfig.class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -218,15 +175,6 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public void storeConfig()
|
|
||||||
{
|
|
||||||
JAXB.marshal(config, configFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -241,7 +189,7 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
public Repository get(String id)
|
public Repository get(String id)
|
||||||
{
|
{
|
||||||
Repository repository = null;
|
Repository repository = null;
|
||||||
File[] directories = config.getRepositoryDirectory().listFiles();
|
File[] directories = getRepositoryDirectory().listFiles();
|
||||||
|
|
||||||
for (File directory : directories)
|
for (File directory : directories)
|
||||||
{
|
{
|
||||||
@@ -278,7 +226,7 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
public Collection<Repository> getAll()
|
public Collection<Repository> getAll()
|
||||||
{
|
{
|
||||||
List<Repository> repositories = new ArrayList<Repository>();
|
List<Repository> repositories = new ArrayList<Repository>();
|
||||||
String[] repositoryNames = config.getRepositoryDirectory().list();
|
String[] repositoryNames = getRepositoryDirectory().list();
|
||||||
|
|
||||||
if ((repositoryNames != null) && (repositoryNames.length > 0))
|
if ((repositoryNames != null) && (repositoryNames.length > 0))
|
||||||
{
|
{
|
||||||
@@ -299,17 +247,6 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
return repositories;
|
return repositories;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public HgConfig getConfig()
|
|
||||||
{
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -322,6 +259,36 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
return TYPE;
|
return TYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param repository
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected String buildUrl(Repository repository)
|
||||||
|
{
|
||||||
|
String url = config.getBaseUrl();
|
||||||
|
|
||||||
|
if (Util.isNotEmpty(url))
|
||||||
|
{
|
||||||
|
if (!url.endsWith("/"))
|
||||||
|
{
|
||||||
|
url = url.concat("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
url = url.concat(repository.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -329,22 +296,9 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean isConfigured()
|
protected Class<HgConfig> getConfigClass()
|
||||||
{
|
{
|
||||||
return config != null;
|
return HgConfig.class;
|
||||||
}
|
|
||||||
|
|
||||||
//~--- set methods ----------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param config
|
|
||||||
*/
|
|
||||||
public void setConfig(HgConfig config)
|
|
||||||
{
|
|
||||||
this.config = config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
@@ -387,7 +341,7 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
repository = new Repository();
|
repository = new Repository();
|
||||||
repository.setType(TYPE_NAME);
|
repository.setType(TYPE_NAME);
|
||||||
repository.setName(name);
|
repository.setName(name);
|
||||||
repository.setUrl(buildUrl(name));
|
repository.setUrl(buildUrl(repository));
|
||||||
loadRepository(repository, hgDirectory);
|
loadRepository(repository, hgDirectory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -395,31 +349,6 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
return repository;
|
return repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param name
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private String buildUrl(String name)
|
|
||||||
{
|
|
||||||
String url = config.getBaseUrl();
|
|
||||||
|
|
||||||
if (Util.isNotEmpty(url))
|
|
||||||
{
|
|
||||||
if (!url.endsWith("/"))
|
|
||||||
{
|
|
||||||
url = url.concat("/");
|
|
||||||
}
|
|
||||||
|
|
||||||
url = url.concat(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -582,11 +511,27 @@ public class HgRepositoryHandler implements RepositoryHandler
|
|||||||
return new File(config.getRepositoryDirectory(), repository.getName());
|
return new File(config.getRepositoryDirectory(), repository.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private File getRepositoryDirectory()
|
||||||
|
{
|
||||||
|
File directory = null;
|
||||||
|
|
||||||
/** Field description */
|
if (isConfigured())
|
||||||
private HgConfig config;
|
{
|
||||||
|
directory = config.getRepositoryDirectory();
|
||||||
|
|
||||||
/** Field description */
|
if (!directory.exists() &&!directory.mkdirs())
|
||||||
private File configFile;
|
{
|
||||||
|
throw new ConfigurationException(
|
||||||
|
"could not create directory ".concat(directory.getPath()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,169 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import sonia.scm.SCMContextProvider;
|
||||||
|
|
||||||
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXB;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sebastian Sdorra
|
||||||
|
*
|
||||||
|
* @param <C>
|
||||||
|
*/
|
||||||
|
public abstract class AbstractRepositoryHandler<C> implements RepositoryHandler
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param repository
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected abstract String buildUrl(Repository repository);
|
||||||
|
|
||||||
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected abstract Class<C> getConfigClass();
|
||||||
|
|
||||||
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException
|
||||||
|
{
|
||||||
|
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param context
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void init(SCMContextProvider context)
|
||||||
|
{
|
||||||
|
String name = getType().getName();
|
||||||
|
|
||||||
|
configFile =
|
||||||
|
new File(context.getBaseDirectory(),
|
||||||
|
"config".concat(File.separator).concat(name).concat(".xml"));
|
||||||
|
loadConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void loadConfig()
|
||||||
|
{
|
||||||
|
if (configFile.exists())
|
||||||
|
{
|
||||||
|
config = JAXB.unmarshal(configFile, getConfigClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void storeConfig()
|
||||||
|
{
|
||||||
|
if (config != null)
|
||||||
|
{
|
||||||
|
JAXB.marshal(config, configFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public C getConfig()
|
||||||
|
{
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isConfigured()
|
||||||
|
{
|
||||||
|
return config != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- set methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param config
|
||||||
|
*/
|
||||||
|
public void setConfig(C config)
|
||||||
|
{
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param repository
|
||||||
|
*/
|
||||||
|
protected void initNewRepository(Repository repository)
|
||||||
|
{
|
||||||
|
repository.setId(UUID.randomUUID().toString());
|
||||||
|
repository.setUrl(buildUrl(repository));
|
||||||
|
repository.setCreationDate(new Date());
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
protected C config;
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
protected File configFile;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user