org.gitistics.test.RepositoryBuilderImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.gitistics.test.RepositoryBuilderImpl.java

Source

/*******************************************************************************
 * Copyright (C) 2013 Steven Rowlands
 * 
 * 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 3 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 org.gitistics.test;

import java.io.File;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;

public class RepositoryBuilderImpl implements RepositoryBuilder {

    private List<File> repositories = new ArrayList<File>();

    @SuppressWarnings("unchecked")
    public RepositoryWorkerReturn<Repository> open() {
        try {
            String tmpDir = System.getProperty("java.io.tmpdir");
            File dir = new File(tmpDir, UUID.randomUUID().toString().replaceAll("-", ""));
            Git.init().setDirectory(dir).setBare(false).call();
            repositories.add(dir);
            File file = new File(dir, ".git");
            Repository repository = new FileRepository(file);
            return (RepositoryWorkerReturn<Repository>) Proxy.newProxyInstance(this.getClass().getClassLoader(),
                    new Class[] { RepositoryWorkerReturn.class },
                    new ReturnHolder(new RepositoryWokerImpl(dir, repository), repository));
        } catch (Exception e) {
            throw new RuntimeException("Error creating git repository ", e);
        }
    }

    public RepositoryBuilder closeAll() {
        try {
            for (File f : repositories) {
                FileUtils.deleteDirectory(f);
            }
            repositories.clear();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return this;
    }

}