added HgPackageInstaller

This commit is contained in:
Sebastian Sdorra
2011-04-27 17:26:34 +02:00
parent 58baccc18b
commit 1cb6fb48e0
6 changed files with 302 additions and 7 deletions

View File

@@ -38,8 +38,10 @@ package sonia.scm.api.rest.resources;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import sonia.scm.SCMContext;
import sonia.scm.cache.CacheManager; import sonia.scm.cache.CacheManager;
import sonia.scm.installer.HgInstallerFactory; import sonia.scm.installer.HgInstallerFactory;
import sonia.scm.installer.HgPackage;
import sonia.scm.installer.HgPackageReader; import sonia.scm.installer.HgPackageReader;
import sonia.scm.installer.HgPackages; import sonia.scm.installer.HgPackages;
import sonia.scm.repository.HgConfig; import sonia.scm.repository.HgConfig;
@@ -56,6 +58,7 @@ import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@@ -135,6 +138,35 @@ public class HgConfigResource
return handler.getConfig(); return handler.getConfig();
} }
/**
* Method description
*
*
*
* @param id
* @return
*/
@POST
@Path("packages/{pkgId}")
public Response installPackage(@PathParam("pkgId") String id)
{
Response response = null;
HgPackage pkg = pkgReader.getPackage(id);
if (pkg != null)
{
HgInstallerFactory.createInstaller().installPackage(handler,
SCMContext.getContext().getBaseDirectory(), pkg);
response = Response.noContent().build();
}
else
{
response = Response.status(Response.Status.NOT_FOUND).build();
}
return response;
}
//~--- get methods ---------------------------------------------------------- //~--- get methods ----------------------------------------------------------
/** /**

View File

@@ -77,4 +77,20 @@ public abstract class AbstractHgInstaller implements HgInstaller
IOUtil.mkdirs(repoDirectory); IOUtil.mkdirs(repoDirectory);
config.setRepositoryDirectory(repoDirectory); config.setRepositoryDirectory(repoDirectory);
} }
/**
* Method description
*
*
*
* @param handler
* @param baseDirectory
* @param pkg
*/
@Override
public void installPackage(HgRepositoryHandler handler, File baseDirectory,
HgPackage pkg)
{
new HgPackageInstaller(handler, baseDirectory, pkg).run();
}
} }

View File

@@ -43,6 +43,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import sonia.scm.repository.HgRepositoryHandler;
/** /**
* *
@@ -63,6 +64,18 @@ public interface HgInstaller
*/ */
public void install(File baseDirectory, HgConfig config) throws IOException; public void install(File baseDirectory, HgConfig config) throws IOException;
/**
* Method description
*
*
*
* @param handler
* @param baseDirectory
* @param pkg
*/
public void installPackage(HgRepositoryHandler handler, File baseDirectory,
HgPackage pkg);
/** /**
* Method description * Method description
* *

View File

@@ -0,0 +1,216 @@
/**
* 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.installer;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.io.ZipUnArchiver;
import sonia.scm.repository.HgConfig;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.MessageFormat;
/**
*
* @author Sebastian Sdorra
*/
public class HgPackageInstaller implements Runnable
{
/** the logger for HgPackageInstaller */
private static final Logger logger =
LoggerFactory.getLogger(HgPackageInstaller.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
*
* @param handler
* @param baseDirectory
* @param pkg
*/
public HgPackageInstaller(HgRepositoryHandler handler, File baseDirectory,
HgPackage pkg)
{
this.handler = handler;
this.baseDirectory = baseDirectory;
this.pkg = pkg;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
@Override
public void run()
{
File downloadedFile = downloadFile();
if ((downloadedFile != null) && downloadedFile.exists())
{
File directory = extractPackage(downloadedFile);
updateConfig(directory);
}
}
/**
* Method description
*
*
* @return
*/
private File downloadFile()
{
File file = null;
InputStream input = null;
OutputStream output = null;
try
{
file = File.createTempFile("scm-hg-", ".pkg");
if (logger.isDebugEnabled())
{
logger.debug("download package to {}", file.getAbsolutePath());
}
input = new URL(pkg.getUrl()).openStream();
output = new FileOutputStream(file);
IOUtil.copy(input, output);
}
catch (IOException ex)
{
logger.error("could not downlaod file ".concat(pkg.getUrl()), ex);
}
finally
{
IOUtil.close(input);
IOUtil.close(output);
}
return file;
}
/**
* Method description
*
*
* @param file
*
* @return
*/
private File extractPackage(File file)
{
File directory = new File(baseDirectory,
"pkg".concat(File.separator).concat(pkg.getId()));
IOUtil.mkdirs(directory);
try
{
IOUtil.extract(file, directory, ZipUnArchiver.EXTENSION);
}
catch (IOException ex)
{
directory = null;
logger.error("could not extract pacakge ".concat(pkg.getId()), ex);
}
finally
{
// delete temp file
try
{
IOUtil.delete(file, true);
}
catch (IOException ex)
{
logger.error(ex.getMessage(), ex);
}
}
return directory;
}
/**
* Method description
*
*
* @param directory
*/
private void updateConfig(File directory)
{
String path = directory.getAbsolutePath();
HgConfig template = pkg.getHgConfigTemplate();
HgConfig config = handler.getConfig();
config.setHgBinary(MessageFormat.format(template.getHgBinary(), path));
config.setPythonBinary(MessageFormat.format(template.getPythonBinary(),
path));
config.setPythonPath(MessageFormat.format(template.getPythonPath(), path));
config.setUseOptimizedBytecode(template.isUseOptimizedBytecode());
handler.storeConfig();
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private File baseDirectory;
/** Field description */
private HgRepositoryHandler handler;
/** Field description */
private HgPackage pkg;
}

View File

@@ -91,6 +91,31 @@ public class HgPackageReader
//~--- get methods ---------------------------------------------------------- //~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param id
*
* @return
*/
public HgPackage getPackage(String id)
{
HgPackage pkg = null;
for (HgPackage p : getPackages())
{
if (id.equals(p.getId()))
{
pkg = p;
break;
}
}
return pkg;
}
/** /**
* Method description * Method description
* *

View File

@@ -59,8 +59,6 @@ import sonia.scm.web.HgWebConfigWriter;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import javax.xml.bind.JAXBContext;
/** /**
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
@@ -256,9 +254,4 @@ public class HgRepositoryHandler
{ {
return HgConfig.class; return HgConfig.class;
} }
//~--- fields ---------------------------------------------------------------
/** Field description */
private JAXBContext changesetContext;
} }