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 99dce9040e..7acf0128af 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,8 +38,10 @@ package sonia.scm.api.rest.resources; import com.google.inject.Inject; import com.google.inject.Singleton; +import sonia.scm.SCMContext; import sonia.scm.cache.CacheManager; import sonia.scm.installer.HgInstallerFactory; +import sonia.scm.installer.HgPackage; import sonia.scm.installer.HgPackageReader; import sonia.scm.installer.HgPackages; import sonia.scm.repository.HgConfig; @@ -56,6 +58,7 @@ import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; @@ -135,6 +138,35 @@ public class HgConfigResource 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 ---------------------------------------------------------- /** diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/AbstractHgInstaller.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/AbstractHgInstaller.java index 71e4f7660c..032471d3c7 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/AbstractHgInstaller.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/AbstractHgInstaller.java @@ -77,4 +77,20 @@ public abstract class AbstractHgInstaller implements HgInstaller IOUtil.mkdirs(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(); + } } 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 1b106fdc58..641e46170c 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 @@ -43,6 +43,7 @@ import java.io.File; import java.io.IOException; 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; + /** + * Method description + * + * + * + * @param handler + * @param baseDirectory + * @param pkg + */ + public void installPackage(HgRepositoryHandler handler, File baseDirectory, + HgPackage pkg); + /** * Method description * diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgPackageInstaller.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgPackageInstaller.java new file mode 100644 index 0000000000..2360f7c814 --- /dev/null +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgPackageInstaller.java @@ -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; +} diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgPackageReader.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgPackageReader.java index 111e8c443c..0bb967bdea 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgPackageReader.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/installer/HgPackageReader.java @@ -91,6 +91,31 @@ public class HgPackageReader //~--- 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 * 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 779f4cc160..dd5809e37f 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 @@ -59,8 +59,6 @@ import sonia.scm.web.HgWebConfigWriter; import java.io.File; import java.io.IOException; -import javax.xml.bind.JAXBContext; - /** * * @author Sebastian Sdorra @@ -256,9 +254,4 @@ public class HgRepositoryHandler { return HgConfig.class; } - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - private JAXBContext changesetContext; }