From 4e1f1e2dd465a339cd3bbec06f5ffd69d8b6c57a Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 19 Nov 2010 18:20:40 +0100 Subject: [PATCH] improve scm-hg-plugin --- .../api/rest/resources/HgConfigResource.java | 9 +- .../java/sonia/scm/repository/HgConfig.java | 35 +--- .../repository/HgInitialConfigBuilder.java | 179 ++++++++++++++++++ .../scm/repository/HgRepositoryHandler.java | 27 +++ .../java/sonia/scm/web/HgWebConfigWriter.java | 7 +- 5 files changed, 218 insertions(+), 39 deletions(-) create mode 100644 plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgInitialConfigBuilder.java diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/api/rest/resources/HgConfigResource.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/api/rest/resources/HgConfigResource.java index 779304b180..13675aa0af 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/api/rest/resources/HgConfigResource.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/api/rest/resources/HgConfigResource.java @@ -29,6 +29,8 @@ * */ + + package sonia.scm.api.rest.resources; //~--- non-JDK imports -------------------------------------------------------- @@ -107,7 +109,6 @@ public class HgConfigResource * * * @param uriInfo - * @param servletContext * @param config * * @return @@ -116,14 +117,12 @@ public class HgConfigResource */ @POST @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) - public Response setConfig(@Context UriInfo uriInfo, - @Context ServletContext servletContext, - HgConfig config) + public Response setConfig(@Context UriInfo uriInfo, HgConfig config) throws IOException { handler.setConfig(config); handler.storeConfig(); - new HgWebConfigWriter(config).write(servletContext); + new HgWebConfigWriter(config).write(); return Response.created(uriInfo.getRequestUri()).build(); } diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgConfig.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgConfig.java index 001e41862d..bd1fd60684 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgConfig.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgConfig.java @@ -29,12 +29,12 @@ * */ + + package sonia.scm.repository; //~--- JDK imports ------------------------------------------------------------ -import java.io.File; - import javax.xml.bind.annotation.XmlRootElement; /** @@ -42,7 +42,7 @@ import javax.xml.bind.annotation.XmlRootElement; * @author Sebastian Sdorra */ @XmlRootElement(name = "config") -public class HgConfig extends BasicRepositoryConfig +public class HgConfig extends SimpleRepositoryConfig { /** @@ -75,17 +75,6 @@ public class HgConfig extends BasicRepositoryConfig return pythonBinary; } - /** - * Method description - * - * - * @return - */ - public File getRepositoryDirectory() - { - return repositoryDirectory; - } - //~--- set methods ---------------------------------------------------------- /** @@ -110,25 +99,11 @@ public class HgConfig extends BasicRepositoryConfig this.pythonBinary = pythonBinary; } - /** - * Method description - * - * - * @param repositoryDirectory - */ - public void setRepositoryDirectory(File repositoryDirectory) - { - this.repositoryDirectory = repositoryDirectory; - } - //~--- fields --------------------------------------------------------------- /** Field description */ - private String hgBinary = "hg"; + private String hgBinary; /** Field description */ - private String pythonBinary = "python"; - - /** Field description */ - private File repositoryDirectory; + private String pythonBinary; } diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgInitialConfigBuilder.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgInitialConfigBuilder.java new file mode 100644 index 0000000000..474aab8027 --- /dev/null +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgInitialConfigBuilder.java @@ -0,0 +1,179 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * 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. + * 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. + * + * 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 + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * 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. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import sonia.scm.io.Command; +import sonia.scm.io.CommandResult; +import sonia.scm.io.SimpleCommand; +import sonia.scm.util.IOUtil; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; +import java.io.IOException; + +/** + * + * @author Sebastian Sdorra + */ +public class HgInitialConfigBuilder +{ + + /** Field description */ + public static final String DIRECTORY_REPOSITORY = "repositories"; + + /** Field description */ + private static final String[] PATH = new String[] + { + + // default path + "/usr/bin", + + // manually installed + "/usr/local/bin", + + // mac ports + "/opt/local/bin", + + // opencsw + "/opt/csw/bin" + }; + + /** the logger for HgInitialConfigBuilder */ + private static final Logger logger = + LoggerFactory.getLogger(HgInitialConfigBuilder.class); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param baseDirectory + */ + public HgInitialConfigBuilder(File baseDirectory) + { + this.baseDirectory = baseDirectory; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public HgConfig createInitialConfig() + { + File repoDirectory = new File( + baseDirectory, + DIRECTORY_REPOSITORY.concat(File.separator).concat( + HgRepositoryHandler.TYPE_NAME)); + + IOUtil.mkdirs(repoDirectory); + config.setRepositoryDirectory(repoDirectory); + config.setHgBinary(search("hg")); + config.setPythonBinary(search("python")); + + return config; + } + + /** + * TODO check for windows + * + * + * @param cmd + * + * @return + */ + private String search(String cmd) + { + String cmdPath = null; + + try + { + Command command = new SimpleCommand(cmd, "--version"); + CommandResult result = command.execute(); + + if (result.isSuccessfull()) + { + cmdPath = cmd; + } + } + catch (IOException ex) {} + + if (cmdPath == null) + { + for (String pathPart : PATH) + { + File file = new File(pathPart, cmd); + + if (file.exists()) + { + cmdPath = file.getAbsolutePath(); + + break; + } + } + } + + if (cmdPath != null) + { + if (logger.isInfoEnabled()) + { + logger.info("found {} at {}", cmd, cmdPath); + } + } + else if (logger.isWarnEnabled()) + { + logger.warn("could not find {}", cmd); + } + + return cmdPath; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private File baseDirectory; + + /** Field description */ + private HgConfig config = new HgConfig(); +} 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 2ff3bac258..fbe6e1f848 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 @@ -50,6 +50,7 @@ import sonia.scm.io.INIConfigurationWriter; import sonia.scm.io.INISection; import sonia.scm.util.IOUtil; import sonia.scm.util.Util; +import sonia.scm.web.HgWebConfigWriter; //~--- JDK imports ------------------------------------------------------------ @@ -62,6 +63,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; +import java.util.logging.Level; /** * @@ -154,6 +156,31 @@ public class HgRepositoryHandler extends AbstractRepositoryHandler } } + /** + * Method description + * + */ + @Override + public void loadConfig() + { + super.loadConfig(); + + if (config == null) + { + try + { + config = + new HgInitialConfigBuilder(baseDirectory).createInitialConfig(); + storeConfig(); + new HgWebConfigWriter(config).write(); + } + catch (IOException ex) + { + logger.error(ex.getMessage(), ex); + } + } + } + /** * Method description * diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgWebConfigWriter.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgWebConfigWriter.java index add8bfdfe7..0fac8f5ebc 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgWebConfigWriter.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgWebConfigWriter.java @@ -29,6 +29,8 @@ * */ + + package sonia.scm.web; //~--- non-JDK imports -------------------------------------------------------- @@ -49,8 +51,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import javax.servlet.ServletContext; - /** * * @author Sebastian Sdorra @@ -83,11 +83,10 @@ public class HgWebConfigWriter * Method description * * - * @param context * * @throws IOException */ - public void write(ServletContext context) throws IOException + public void write() throws IOException { File webConfigFile = new File(config.getRepositoryDirectory(), CONFIG_NAME);