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

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

Introduction

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

Prototype

public OutputStream getOutputStream() 

Source Link

Document

Obtain the direct output stream for this lock.

Usage

From source file:com.google.gerrit.pgm.init.api.InitUtil.java

License:Apache License

public static void copy(Path dst, byte[] buf) throws FileNotFoundException, IOException {
    // If the file already has the content we want to put there,
    // don't attempt to overwrite the file.
    ////from   ww w. jav  a  2 s.  c  o  m
    try (InputStream in = Files.newInputStream(dst)) {
        if (Arrays.equals(buf, ByteStreams.toByteArray(in))) {
            return;
        }
    } catch (NoSuchFileException notFound) {
        // Fall through and write the file.
    }

    Files.createDirectories(dst.getParent());
    LockFile lf = new LockFile(dst.toFile(), FS.DETECTED);
    if (!lf.lock()) {
        throw new IOException("Cannot lock " + dst);
    }
    try {
        try (InputStream in = new ByteArrayInputStream(buf); OutputStream out = lf.getOutputStream()) {
            ByteStreams.copy(in, out);
        }
        if (!lf.commit()) {
            throw new IOException("Cannot commit " + dst);
        }
    } finally {
        lf.unlock();
    }
}

From source file:com.google.gerrit.pgm.init.InitContainer.java

License:Apache License

@Override
public void run() throws FileNotFoundException, IOException {
    ui.header("Container Process");

    container.string("Run as", "user", username());
    container.string("Java runtime", "javaHome", javaHome());

    Path myWar;//from   w  ww  . j  a va  2s  .c  o  m
    try {
        myWar = GerritLauncher.getDistributionArchive().toPath();
    } catch (FileNotFoundException e) {
        System.err.println("warn: Cannot find distribution archive (e.g. gerrit.war)");
        myWar = null;
    }

    String path = container.get("war");
    if (path != null) {
        path = container.string("Gerrit runtime", "war",
                myWar != null ? myWar.toAbsolutePath().toString() : null);
        if (path == null || path.isEmpty()) {
            throw die("container.war is required");
        }

    } else if (myWar != null) {
        final boolean copy;
        final Path siteWar = site.gerrit_war;
        if (Files.exists(siteWar)) {
            copy = ui.yesno(true, "Upgrade %s", siteWar);
        } else {
            copy = ui.yesno(true, "Copy %s to %s", myWar.getFileName(), siteWar);
            if (copy) {
                container.unset("war");
            } else {
                container.set("war", myWar.toAbsolutePath().toString());
            }
        }
        if (copy) {
            if (!ui.isBatch()) {
                System.err.format("Copying %s to %s", myWar.getFileName(), siteWar);
                System.err.println();
            }

            try (InputStream in = Files.newInputStream(myWar)) {
                Files.createDirectories(siteWar.getParent());

                LockFile lf = new LockFile(siteWar.toFile(), FS.DETECTED);
                if (!lf.lock()) {
                    throw new IOException("Cannot lock " + siteWar);
                }
                try {
                    try (OutputStream out = lf.getOutputStream()) {
                        ByteStreams.copy(in, out);
                    }
                    if (!lf.commit()) {
                        throw new IOException("Cannot commit " + siteWar);
                    }
                } finally {
                    lf.unlock();
                }
            }
        }
    }
}

From source file:com.google.gerrit.pgm.ProtoGen.java

License:Apache License

@Override
public int run() throws Exception {
    LockFile lock = new LockFile(file.getAbsoluteFile(), FS.DETECTED);
    if (!lock.lock()) {
        throw die("Cannot lock " + file);
    }//from   w  w w  . j  a v a  2 s  . c o  m
    try {
        JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class);
        try (OutputStream o = lock.getOutputStream();
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(o, "UTF-8")))) {
            String header;
            try (InputStream in = getClass().getResourceAsStream("ProtoGenHeader.txt")) {
                ByteBuffer buf = IO.readWholeStream(in, 1024);
                int ptr = buf.arrayOffset() + buf.position();
                int len = buf.remaining();
                header = new String(buf.array(), ptr, len, "UTF-8");
            }

            String version = com.google.gerrit.common.Version.getVersion();
            out.write(header.replace("@@VERSION@@", version));
            jsm.generateProto(out);
            out.flush();
        }
        if (!lock.commit()) {
            throw die("Could not write to " + file);
        }
    } finally {
        lock.unlock();
    }
    System.out.println("Created " + file.getPath());
    return 0;
}

From source file:com.googlesource.gerrit.plugins.lfs.locks.LfsProjectLocks.java

License:Apache License

LfsLock createLock(CurrentUser user, LfsCreateLockInput input) throws LfsException {
    log.atFine().log("Create lock for %s in project %s", input.path, project);
    String lockId = toLockId.apply(input.path);
    LfsLock lock = locks.getIfPresent(lockId);
    if (lock != null) {
        throw new LfsLockExistsException(lock);
    }/*  w  ww .  j  a va 2 s .c  o m*/

    lock = new LfsLock(lockId, input.path, LfsDateTime.now(), new LfsLockOwner(user.getUserName().get()));
    LockFile fileLock = new LockFile(locksPath.resolve(lockId).toFile());
    try {
        if (!fileLock.lock()) {
            log.atWarning().log("Cannot lock path [%s] in project %s", input.path, project);
            throw new LfsLockExistsException(lock);
        }
    } catch (IOException e) {
        String error = String.format("Locking path [%s] in project %s failed with error %s", input.path,
                project, e.getMessage());
        log.atWarning().log(error);
        throw new LfsException(error);
    }

    try {
        try (OutputStreamWriter out = new OutputStreamWriter(fileLock.getOutputStream())) {
            gson.toJson(lock, out);
        } catch (IOException e) {
            String error = String.format("Locking path [%s] in project %s failed during write with error %s",
                    input.path, project, e.getMessage());
            log.atWarning().log(error);
            throw new LfsException(error);
        }
        if (!fileLock.commit()) {
            String error = String.format("Committing lock to path [%s] in project %s failed", input.path,
                    project);
            log.atWarning().log(error);
            throw new LfsException(error);
        }
        // put lock object to cache while file lock is being hold so that
        // there is no chance that other process performs lock operation
        // in the meantime (either cache returns with existing object or
        // LockFile.lock fails on locking attempt)
        locks.put(lockId, lock);
    } finally {
        fileLock.unlock();
    }

    return lock;
}

From source file:com.microsoft.gittf.core.tasks.FetchTask.java

License:Open Source License

private boolean writeFetchHead(final ObjectId commitID, final int changesetID) throws IOException {
    Ref fetchHeadRef = repository.getRef(Constants.FETCH_HEAD);
    boolean referencesEqual = fetchHeadRef == null ? false : fetchHeadRef.getObjectId().equals(commitID);

    if (referencesEqual) {
        return false;
    }// w  w  w  .  j av  a 2  s  .c o  m

    final File refFile = new File(repository.getDirectory(), Constants.FETCH_HEAD);
    final LockFile lockFile = new LockFile(refFile, repository.getFS());

    if (lockFile.lock()) {
        try {
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(lockFile.getOutputStream(), Charset.forName("UTF-8"))); //$NON-NLS-1$

            try {
                writer.append(MessageFormat.format("{0}\t\t{1}", commitID.getName(), //$NON-NLS-1$
                        Messages.formatString("FetchTask.RefLogFormat", //$NON-NLS-1$
                                Integer.toString(changesetID))));
            } finally {
                writer.close();
            }

            lockFile.commit();
        } finally {
            lockFile.unlock();
        }
    }

    return true;
}