From 65ce0d5820c34760460550cf6b7e6ea58e21c913 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 25 Apr 2011 13:18:06 +0200 Subject: [PATCH] added getPythonInstallations and getHgInstallations methods to HgConfigResource --- .../api/rest/resources/HgConfigResource.java | 40 +++++++++++ .../java/sonia/scm/installer/HgInstaller.java | 20 ++++++ .../sonia/scm/installer/UnixHgInstaller.java | 40 ++++++++++- .../scm/installer/WindowsHgInstaller.java | 71 ++++++++++++++++++- 4 files changed, 166 insertions(+), 5 deletions(-) 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 4ac5bc18e2..a8e06cd8e6 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 @@ -38,6 +38,7 @@ package sonia.scm.api.rest.resources; import com.google.inject.Inject; import com.google.inject.Singleton; +import sonia.scm.installer.HgInstallerFactory; import sonia.scm.repository.HgConfig; import sonia.scm.repository.HgRepositoryHandler; import sonia.scm.web.HgWebConfigWriter; @@ -46,12 +47,15 @@ import sonia.scm.web.HgWebConfigWriter; import java.io.IOException; +import java.util.List; + import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; +import javax.ws.rs.core.GenericEntity; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; @@ -121,6 +125,42 @@ public class HgConfigResource return config; } + /** + * Method description + * + * + * @return + */ + @GET + @Path("installations/hg") + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public GenericEntity> getHgInstallations() + { + List installations = + HgInstallerFactory.createInstaller().getHgInstallations(); + + return new GenericEntity>(installations) {} + ; + } + + /** + * Method description + * + * + * @return + */ + @GET + @Path("installations/python") + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public GenericEntity> getPythonInstallations() + { + List installations = + HgInstallerFactory.createInstaller().getPythonInstallations(); + + return new GenericEntity>(installations) {} + ; + } + //~--- set methods ---------------------------------------------------------- /** diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgInstaller.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgInstaller.java index 1a7b98a0ae..1b106fdc58 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgInstaller.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgInstaller.java @@ -42,6 +42,8 @@ import sonia.scm.repository.HgConfig; import java.io.File; import java.io.IOException; +import java.util.List; + /** * * @author Sebastian Sdorra @@ -72,4 +74,22 @@ public interface HgInstaller * @throws IOException */ public void update(File baseDirectory, HgConfig config) throws IOException; + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public List getHgInstallations(); + + /** + * Method description + * + * + * @return + */ + public List getPythonInstallations(); } diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/UnixHgInstaller.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/UnixHgInstaller.java index 6004ed91e6..e7d76bb2e1 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/UnixHgInstaller.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/UnixHgInstaller.java @@ -43,6 +43,8 @@ import sonia.scm.util.IOUtil; import java.io.File; import java.io.IOException; +import java.util.List; + /** * * @author Sebastian Sdorra @@ -50,6 +52,14 @@ import java.io.IOException; public class UnixHgInstaller extends AbstractHgInstaller { + /** Field description */ + public static final String COMMAND_HG = "hg"; + + /** Field description */ + public static final String COMMAND_PYTHON = "python"; + + //~--- methods -------------------------------------------------------------- + /** * Method description * @@ -64,8 +74,8 @@ public class UnixHgInstaller extends AbstractHgInstaller public void install(File baseDirectory, HgConfig config) throws IOException { super.install(baseDirectory, config); - config.setHgBinary(IOUtil.search("hg")); - config.setPythonBinary(IOUtil.search("python")); + config.setHgBinary(IOUtil.search(COMMAND_HG)); + config.setPythonBinary(IOUtil.search(COMMAND_PYTHON)); } /** @@ -82,4 +92,30 @@ public class UnixHgInstaller extends AbstractHgInstaller // do nothing } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public List getHgInstallations() + { + return IOUtil.searchAll(COMMAND_HG); + } + + /** + * Method description + * + * + * @return + */ + @Override + public List getPythonInstallations() + { + return IOUtil.searchAll(COMMAND_PYTHON); + } } diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/WindowsHgInstaller.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/WindowsHgInstaller.java index 9deadbb33a..fe9e02310b 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/WindowsHgInstaller.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/WindowsHgInstaller.java @@ -49,6 +49,9 @@ import java.io.File; import java.io.FilenameFilter; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + /** * * @author Sebastian Sdorra @@ -87,8 +90,12 @@ public class WindowsHgInstaller extends AbstractHgInstaller }; /** Field description */ - private static final String REGISTRY_PYTHON = - "HKEY_CLASSES_ROOT\\Python.File\\shell\\open\\command"; + private static final String[] REGISTRY_PYTHON = new String[] + { + + // .py files + "HKEY_CLASSES_ROOT\\Python.File\\shell\\open\\command" + }; /** the logger for WindowsHgInstaller */ private static final Logger logger = @@ -141,6 +148,34 @@ public class WindowsHgInstaller extends AbstractHgInstaller @Override public void update(File baseDirectory, HgConfig config) {} + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public List getHgInstallations() + { + return getInstallations(REGISTRY_HG); + } + + /** + * Method description + * + * + * @return + */ + @Override + public List getPythonInstallations() + { + return getInstallations(REGISTRY_PYTHON); + } + + //~--- methods -------------------------------------------------------------- + /** * Method description * @@ -235,6 +270,36 @@ public class WindowsHgInstaller extends AbstractHgInstaller //~--- get methods ---------------------------------------------------------- + /** + * Method description + * + * + * @param registryKeys + * + * @return + */ + private List getInstallations(String[] registryKeys) + { + List installations = new ArrayList(); + + for (String registryKey : registryKeys) + { + String path = RegistryUtil.getRegistryValue(registryKey); + + if (path != null) + { + File file = new File(path); + + if (!file.exists()) + { + installations.add(path); + } + } + } + + return installations; + } + /** * Method description * @@ -310,7 +375,7 @@ public class WindowsHgInstaller extends AbstractHgInstaller */ private String getPythonBinary() { - String python = RegistryUtil.getRegistryValue(REGISTRY_PYTHON); + String python = RegistryUtil.getRegistryValue(REGISTRY_PYTHON[0]); if (python == null) {