de.egore911.versioning.deployer.performer.PerformCheckout.java Source code

Java tutorial

Introduction

Here is the source code for de.egore911.versioning.deployer.performer.PerformCheckout.java

Source

/*
 * Copyright 2013  Christoph Brill <egore911@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.egore911.versioning.deployer.performer;

import java.io.File;
import java.io.IOException;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc2.SvnCheckout;
import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
import org.tmatesoft.svn.core.wc2.SvnTarget;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Christoph Brill &lt;egore911@gmail.com&gt;
 */
public class PerformCheckout {

    private static final Logger LOG = LoggerFactory.getLogger(PerformCheckout.class);

    private final XPathExpression checkoutXpath;
    private final XPathExpression targetXpath;
    private final XPathExpression gitXpath;
    private final XPathExpression svnXpath;

    public PerformCheckout(XPath xPath) throws XPathExpressionException {
        checkoutXpath = xPath.compile("checkout");
        targetXpath = xPath.compile("target/text()");
        gitXpath = xPath.compile("git/text()");
        svnXpath = xPath.compile("svn/text()");
    }

    public void perform(Node serverDeploymentsDeploymentNode) throws XPathExpressionException {
        NodeList checkoutOperations = (NodeList) checkoutXpath.evaluate(serverDeploymentsDeploymentNode,
                XPathConstants.NODESET);
        for (int j = 0; j < checkoutOperations.getLength(); j++) {
            Node checkoutOperation = checkoutOperations.item(j);
            String target = (String) targetXpath.evaluate(checkoutOperation, XPathConstants.STRING);
            String git = (String) gitXpath.evaluate(checkoutOperation, XPathConstants.STRING);
            if (StringUtils.isNotEmpty(git)) {
                performGit(target, git);
            } else {
                String svn = (String) svnXpath.evaluate(checkoutOperation, XPathConstants.STRING);
                if (StringUtils.isNotEmpty(svn)) {
                    performSvn(target, svn);
                } else {
                    LOG.warn("Neither SVN nor git used for server configuration");
                }
            }
        }
    }

    private static void performGit(String target, String url) {
        String tmp = target + File.separatorChar + "checkout";
        try {
            File tmpDir = new File(tmp);
            try {
                if (!new File(tmp + "/.git").exists()) {
                    Git.cloneRepository().setURI(url).setDirectory(tmpDir).call();
                } else {
                    FileRepository fileRepository = new FileRepository(tmp + "/.git");
                    Git git = new Git(fileRepository);
                    git.pull().setRebase(true).call();
                }
                FileUtils.copyDirectory(tmpDir, new File(target));
            } finally {
                FileUtils.deleteDirectory(tmpDir);
            }
        } catch (GitAPIException | IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    private static void performSvn(String target, String url) {
        String tmp = target + File.separatorChar + "checkout";
        try {
            File tmpDir = new File(tmp);
            SVNURL svnurl = SVNURL.parseURIEncoded(url);
            SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
            try {
                SvnCheckout checkout = svnOperationFactory.createCheckout();
                checkout.setSingleTarget(SvnTarget.fromFile(tmpDir));
                checkout.setSource(SvnTarget.fromURL(svnurl));
                checkout.run();
            } finally {
                svnOperationFactory.dispose();
            }
            FileUtils.copyDirectory(tmpDir, new File(target));
            FileUtils.deleteDirectory(tmpDir);
        } catch (SVNException | IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }

}