added search method to IOUtil

This commit is contained in:
Sebastian Sdorra
2011-01-16 19:20:30 +01:00
parent c643dcfcbe
commit 6c98251b10
4 changed files with 174 additions and 135 deletions

View File

@@ -29,27 +29,20 @@
*
*/
package sonia.scm.installer;
//~--- 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.repository.HgConfig;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.util.IOUtil;
import sonia.scm.util.SystemUtil;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
*
@@ -61,10 +54,6 @@ public abstract class AbstractHgInstaller implements HgInstaller
/** Field description */
public static final String DIRECTORY_REPOSITORY = "repositories";
/** the logger for AbstractHgInstaller */
private static final Logger logger = LoggerFactory
.getLogger(AbstractHgInstaller.class);
//~--- constructors ---------------------------------------------------------
/**
@@ -91,104 +80,15 @@ public abstract class AbstractHgInstaller implements HgInstaller
@Override
public void install(HgConfig config) throws IOException
{
File repoDirectory = new File(baseDirectory, DIRECTORY_REPOSITORY.concat(
File.separator).concat(HgRepositoryHandler.TYPE_NAME));
File repoDirectory = new File(
baseDirectory,
DIRECTORY_REPOSITORY.concat(File.separator).concat(
HgRepositoryHandler.TYPE_NAME));
IOUtil.mkdirs(repoDirectory);
config.setRepositoryDirectory(repoDirectory);
}
/**
* TODO check for windows
*
*
*
* @param path
* @param cmd
*
* @return
*/
protected String search(String[] path, 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)
{
List<String> extensions = getExecutableSearchExtensions();
File file = findFileByExtension(pathPart, cmd, extensions);
if (file != null)
{
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;
}
/**
* Returns a list of file extensions to use when searching for executables.
* The list is in priority order, with the highest priority first.
*/
protected List<String> getExecutableSearchExtensions()
{
List<String> extensions;
if (SystemUtil.isWindows())
{
extensions = Arrays.asList(".exe");
}
else
{
extensions = Arrays.asList("");
}
return extensions;
}
private File findFileByExtension(String parentPath, String cmd,
List<String> potentialExtensions)
{
File file = null;
for (String potentialExtension : potentialExtensions)
{
String fileName = cmd.concat(potentialExtension);
File potentialFile = new File(parentPath, fileName);
if (potentialFile.exists())
{
file = potentialFile;
break;
}
}
return file;
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -35,10 +35,8 @@ package sonia.scm.installer;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.HgConfig;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
@@ -52,29 +50,6 @@ import java.io.IOException;
public class UnixHgInstaller extends AbstractHgInstaller
{
/** 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 UnixHgInstaller */
private static final Logger logger =
LoggerFactory.getLogger(UnixHgInstaller.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
@@ -100,8 +75,8 @@ public class UnixHgInstaller extends AbstractHgInstaller
public void install(HgConfig config) throws IOException
{
super.install(config);
config.setHgBinary(search(PATH, "hg"));
config.setPythonBinary(search(PATH, "python"));
config.setHgBinary(IOUtil.search("hg"));
config.setPythonBinary(IOUtil.search("python"));
}
/**

View File

@@ -320,7 +320,7 @@ public class WindowsHgInstaller extends AbstractHgInstaller
if (python == null)
{
python = search(new String[0], "python");
python = IOUtil.search(new String[0], "python");
}
return python;