Example usage for org.eclipse.jgit.internal.storage.file LockFile lock

List of usage examples for org.eclipse.jgit.internal.storage.file LockFile lock

Introduction

In this page you can find the example usage for org.eclipse.jgit.internal.storage.file LockFile lock.

Prototype

public boolean lock() throws IOException 

Source Link

Document

Try to establish the lock.

Usage

From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.GitVcsSupportTest.java

License:Apache License

public void collecting_changes_should_not_block_IDE_requests() throws Exception {
    String classpath = myConfigBuilder.build().getFetchClasspath() + File.pathSeparator
            + ClasspathUtil.composeClasspath(new Class[] { MockFetcher.class }, null, null);
    myConfigBuilder.setSeparateProcessForFetch(true).setFetchClasspath(classpath)
            .setFetcherClassName(MockFetcher.class.getName());

    final GitVcsSupport support = getSupport();

    final VcsRootImpl root = (VcsRootImpl) getRoot("master");
    final File customRootDir = new File(myTmpDir, "custom-dir");
    root.addProperty(Constants.PATH, customRootDir.getAbsolutePath());
    final String repositoryUrl = root.getProperty(Constants.FETCH_URL);

    final AtomicInteger succeedIDERequestCount = new AtomicInteger(0);
    final AtomicBoolean fetchBlocksIDERequests = new AtomicBoolean(false);
    final List<Exception> errors = Collections.synchronizedList(new ArrayList<Exception>());

    Runnable longFetch = new Runnable() {
        public void run() {
            try {
                support.collectChanges(root, VERSION_TEST_HEAD, MERGE_VERSION, new CheckoutRules(""));
                fetchBlocksIDERequests.set(succeedIDERequestCount.get() == 0);
            } catch (VcsException e) {
                errors.add(e);/*from   w  w w  .j  ava  2s  .  c  om*/
            }
        }
    };

    Runnable requestsFromIDE = new Runnable() {
        public void run() {
            while (!new File(customRootDir, "mock.lock").exists()) {//wait for fetch to begin (MockFetcher holds a lock during fetch)
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            LockFile lock = new LockFile(new File(customRootDir.getAbsolutePath(), "mock"), FS.DETECTED);
            try {
                while (true) {//do mapFullPath while fetch is executed (we cannot acquire a lock while it is executed)
                    if (!lock.lock()) {
                        support.mapFullPath(new VcsRootEntry(root, new CheckoutRules("")),
                                GitUtils.versionRevision(VERSION_TEST_HEAD) + "|" + repositoryUrl
                                        + "|readme.txt");
                        succeedIDERequestCount.incrementAndGet();
                    } else {
                        lock.unlock();
                        break;
                    }
                }
            } catch (Exception e) {
                errors.add(e);
            }
        }
    };

    Thread fetch = new Thread(longFetch);
    Thread ideRequests = new Thread(requestsFromIDE);
    fetch.start();
    ideRequests.start();
    fetch.join();
    ideRequests.join();
    if (!errors.isEmpty()) {
        fail("Errors in readers thread", errors.get(0));
    }

    assertFalse(fetchBlocksIDERequests.get());
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.MockFetcher.java

License:Apache License

public static void main(String... args) throws Exception {
    String repositoryPath = new File(".").getAbsolutePath();
    LockFile lock = new LockFile(new File(repositoryPath, "mock"), FS.DETECTED);
    lock.lock();
    try {// w  ww  .  j a v  a  2  s  .  c  o  m
        Thread.sleep(10000);
        Fetcher.main(args);
    } finally {
        lock.unlock();
    }
}

From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepository.java

License:Open Source License

@Override
public void lockItem(String site, String path) {
    Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : SANDBOX);

    synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : SANDBOX)) {
        try (TreeWalk tw = new TreeWalk(repo)) {
            RevTree tree = helper.getTreeForLastCommit(repo);
            tw.addTree(tree); // tree 0
            tw.setRecursive(false);//from  w ww . j  ava 2  s. co m
            tw.setFilter(PathFilter.create(path));

            if (!tw.next()) {
                return;
            }

            File repoRoot = repo.getWorkTree();
            Paths.get(repoRoot.getPath(), tw.getPathString());
            File file = new File(tw.getPathString());
            LockFile lock = new LockFile(file);
            lock.lock();

            tw.close();

        } catch (IOException e) {
            logger.error("Error while locking file for site: " + site + " path: " + path, e);
        }
    }
}