diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java index 313b257ea0..4115c4d7a6 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java @@ -9,14 +9,13 @@ package sonia.scm.repository; //~--- non-JDK imports -------------------------------------------------------- -import sonia.scm.SCMContextProvider; +import sonia.scm.ConfigurationException; import sonia.scm.io.CommandResult; import sonia.scm.io.ExtendedCommand; import sonia.scm.io.INIConfiguration; import sonia.scm.io.INIConfigurationReader; import sonia.scm.io.INIConfigurationWriter; import sonia.scm.io.INISection; -import sonia.scm.util.AssertUtil; import sonia.scm.util.Util; //~--- JDK imports ------------------------------------------------------------ @@ -30,17 +29,14 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; -import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; -import javax.xml.bind.JAXB; - /** * * @author Sebastian Sdorra */ -public class HgRepositoryHandler implements RepositoryHandler +public class HgRepositoryHandler extends AbstractRepositoryHandler { /** Field description */ @@ -67,15 +63,6 @@ public class HgRepositoryHandler implements RepositoryHandler //~--- methods -------------------------------------------------------------- - /** - * Method description - * - * - * @throws IOException - */ - @Override - public void close() throws IOException {} - /** * Method description * @@ -89,9 +76,7 @@ public class HgRepositoryHandler implements RepositoryHandler public void create(Repository repository) throws RepositoryException, IOException { - repository.setId(UUID.randomUUID().toString()); - repository.setUrl(buildUrl(repository.getName())); - repository.setCreationDate(new Date()); + initNewRepository(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 * @@ -218,15 +175,6 @@ public class HgRepositoryHandler implements RepositoryHandler } } - /** - * Method description - * - */ - public void storeConfig() - { - JAXB.marshal(config, configFile); - } - //~--- get methods ---------------------------------------------------------- /** @@ -241,7 +189,7 @@ public class HgRepositoryHandler implements RepositoryHandler public Repository get(String id) { Repository repository = null; - File[] directories = config.getRepositoryDirectory().listFiles(); + File[] directories = getRepositoryDirectory().listFiles(); for (File directory : directories) { @@ -278,7 +226,7 @@ public class HgRepositoryHandler implements RepositoryHandler public Collection getAll() { List repositories = new ArrayList(); - String[] repositoryNames = config.getRepositoryDirectory().list(); + String[] repositoryNames = getRepositoryDirectory().list(); if ((repositoryNames != null) && (repositoryNames.length > 0)) { @@ -299,17 +247,6 @@ public class HgRepositoryHandler implements RepositoryHandler return repositories; } - /** - * Method description - * - * - * @return - */ - public HgConfig getConfig() - { - return config; - } - /** * Method description * @@ -322,6 +259,36 @@ public class HgRepositoryHandler implements RepositoryHandler 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 * @@ -329,22 +296,9 @@ public class HgRepositoryHandler implements RepositoryHandler * @return */ @Override - public boolean isConfigured() + protected Class getConfigClass() { - return config != null; - } - - //~--- set methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @param config - */ - public void setConfig(HgConfig config) - { - this.config = config; + return HgConfig.class; } //~--- methods -------------------------------------------------------------- @@ -387,7 +341,7 @@ public class HgRepositoryHandler implements RepositoryHandler repository = new Repository(); repository.setType(TYPE_NAME); repository.setName(name); - repository.setUrl(buildUrl(name)); + repository.setUrl(buildUrl(repository)); loadRepository(repository, hgDirectory); } } @@ -395,31 +349,6 @@ public class HgRepositoryHandler implements RepositoryHandler 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 * @@ -582,11 +511,27 @@ public class HgRepositoryHandler implements RepositoryHandler return new File(config.getRepositoryDirectory(), repository.getName()); } - //~--- fields --------------------------------------------------------------- + /** + * Method description + * + * + * @return + */ + private File getRepositoryDirectory() + { + File directory = null; - /** Field description */ - private HgConfig config; + if (isConfigured()) + { + directory = config.getRepositoryDirectory(); - /** Field description */ - private File configFile; + if (!directory.exists() &&!directory.mkdirs()) + { + throw new ConfigurationException( + "could not create directory ".concat(directory.getPath())); + } + } + + return directory; + } } diff --git a/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java new file mode 100644 index 0000000000..d8c66aab57 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java @@ -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 + */ +public abstract class AbstractRepositoryHandler implements RepositoryHandler +{ + + /** + * Method description + * + * + * @param repository + * + * @return + */ + protected abstract String buildUrl(Repository repository); + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + protected abstract Class 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; +}