Example usage for org.eclipse.jgit.lib Repository close

List of usage examples for org.eclipse.jgit.lib Repository close

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository close.

Prototype

@Override
public void close() 

Source Link

Document

Decrement the use count, and maybe close resources.

Usage

From source file:com.gitblit.tickets.FileTicketService.java

License:Apache License

/**
 * Returns all the tickets in the repository. Querying tickets from the
 * repository requires deserializing all tickets. This is an  expensive
 * process and not recommended. Tickets are indexed by Lucene and queries
 * should be executed against that index.
 *
 * @param repository//w w w.j a v  a2 s.  com
 * @param filter
 *            optional filter to only return matching results
 * @return a list of tickets
 */
@Override
public List<TicketModel> getTickets(RepositoryModel repository, TicketFilter filter) {
    List<TicketModel> list = new ArrayList<TicketModel>();

    Repository db = repositoryManager.getRepository(repository.name);
    try {
        // Collect the set of all json files
        File dir = new File(db.getDirectory(), TICKETS_PATH);
        List<File> journals = findAll(dir, JOURNAL);

        // Deserialize each ticket and optionally filter out unwanted tickets
        for (File journal : journals) {
            String json = null;
            try {
                json = new String(FileUtils.readContent(journal), Constants.ENCODING);
            } catch (Exception e) {
                log.error(null, e);
            }
            if (StringUtils.isEmpty(json)) {
                // journal was touched but no changes were written
                continue;
            }
            try {
                // Reconstruct ticketId from the path
                // id/26/326/journal.json
                String path = FileUtils.getRelativePath(dir, journal);
                String tid = path.split("/")[1];
                long ticketId = Long.parseLong(tid);
                List<Change> changes = TicketSerializer.deserializeJournal(json);
                if (ArrayUtils.isEmpty(changes)) {
                    log.warn("Empty journal for {}:{}", repository, journal);
                    continue;
                }
                TicketModel ticket = TicketModel.buildTicket(changes);
                ticket.project = repository.projectPath;
                ticket.repository = repository.name;
                ticket.number = ticketId;

                // add the ticket, conditionally, to the list
                if (filter == null) {
                    list.add(ticket);
                } else {
                    if (filter.accept(ticket)) {
                        list.add(ticket);
                    }
                }
            } catch (Exception e) {
                log.error("failed to deserialize {}/{}\n{}",
                        new Object[] { repository, journal, e.getMessage() });
                log.error(null, e);
            }
        }

        // sort the tickets by creation
        Collections.sort(list);
        return list;
    } finally {
        db.close();
    }
}

From source file:com.gitblit.tickets.FileTicketService.java

License:Apache License

/**
 * Retrieves the specified attachment from a ticket.
 *
 * @param repository/*  w w w  .jav  a 2s.  c om*/
 * @param ticketId
 * @param filename
 * @return an attachment, if found, null otherwise
 */
@Override
public Attachment getAttachment(RepositoryModel repository, long ticketId, String filename) {
    if (ticketId <= 0L) {
        return null;
    }

    // deserialize the ticket model so that we have the attachment metadata
    TicketModel ticket = getTicket(repository, ticketId);
    Attachment attachment = ticket.getAttachment(filename);

    // attachment not found
    if (attachment == null) {
        return null;
    }

    // retrieve the attachment content
    Repository db = repositoryManager.getRepository(repository.name);
    try {
        String attachmentPath = toAttachmentPath(ticketId, attachment.name);
        File file = new File(db.getDirectory(), attachmentPath);
        if (file.exists()) {
            attachment.content = FileUtils.readContent(file);
            attachment.size = attachment.content.length;
        }
        return attachment;
    } finally {
        db.close();
    }
}

From source file:com.gitblit.tickets.FileTicketService.java

License:Apache License

/**
 * Deletes a ticket from the repository.
 *
 * @param ticket//from w  ww  .j  a  v  a 2s.c  o  m
 * @return true if successful
 */
@Override
protected synchronized boolean deleteTicketImpl(RepositoryModel repository, TicketModel ticket,
        String deletedBy) {
    if (ticket == null) {
        throw new RuntimeException("must specify a ticket!");
    }

    boolean success = false;
    Repository db = repositoryManager.getRepository(ticket.repository);
    try {
        String ticketPath = toTicketPath(ticket.number);
        File dir = new File(db.getDirectory(), ticketPath);
        if (dir.exists()) {
            success = FileUtils.delete(dir);
        }
        success = true;
    } finally {
        db.close();
    }
    return success;
}

From source file:com.gitblit.tickets.FileTicketService.java

License:Apache License

/**
 * Commit a ticket change to the repository.
 *
 * @param repository//from  w  w w  . j a  va2s  .  c o m
 * @param ticketId
 * @param change
 * @return true, if the change was committed
 */
@Override
protected synchronized boolean commitChangeImpl(RepositoryModel repository, long ticketId, Change change) {
    boolean success = false;

    Repository db = repositoryManager.getRepository(repository.name);
    try {
        List<Change> changes = getJournal(db, ticketId);
        changes.add(change);
        String journal = TicketSerializer.serializeJournal(changes).trim();

        String journalPath = toTicketPath(ticketId) + "/" + JOURNAL;
        File file = new File(db.getDirectory(), journalPath);
        file.getParentFile().mkdirs();
        FileUtils.writeContent(file, journal);
        success = true;
    } catch (Throwable t) {
        log.error(MessageFormat.format("Failed to commit ticket {0,number,0} to {1}", ticketId,
                db.getDirectory()), t);
    } finally {
        db.close();
    }
    return success;
}

From source file:com.gitblit.tickets.FileTicketService.java

License:Apache License

@Override
protected boolean deleteAllImpl(RepositoryModel repository) {
    Repository db = repositoryManager.getRepository(repository.name);
    if (db == null) {
        // the tickets no longer exist because the db no longer exists
        return true;
    }/*from   w ww  . j  a  va 2s .  c o  m*/
    try {
        File dir = new File(db.getDirectory(), TICKETS_PATH);
        return FileUtils.delete(dir);
    } catch (Exception e) {
        log.error(null, e);
    } finally {
        db.close();
    }
    return false;
}

From source file:com.gitblit.tickets.ITicketService.java

License:Apache License

/**
 * Returns the list of labels for the repository.
 *
 * @param repository//from ww  w .  j  a v a  2 s . com
 * @return the list of labels
 * @since 1.4.0
 */
public List<TicketLabel> getLabels(RepositoryModel repository) {
    String key = repository.name;
    if (labelsCache.containsKey(key)) {
        return labelsCache.get(key);
    }
    List<TicketLabel> list = new ArrayList<TicketLabel>();
    Repository db = repositoryManager.getRepository(repository.name);
    try {
        StoredConfig config = db.getConfig();
        Set<String> names = config.getSubsections(LABEL);
        for (String name : names) {
            TicketLabel label = new TicketLabel(name);
            label.color = config.getString(LABEL, name, COLOR);
            list.add(label);
        }
        labelsCache.put(key, Collections.unmodifiableList(list));
    } catch (Exception e) {
        log.error("invalid tickets settings for " + repository, e);
    } finally {
        db.close();
    }
    return list;
}

From source file:com.gitblit.tickets.ITicketService.java

License:Apache License

/**
 * Creates a label.//from   w w w  . j a v  a  2  s . c  om
 *
 * @param repository
 * @param milestone
 * @param createdBy
 * @return the label
 * @since 1.4.0
 */
public synchronized TicketLabel createLabel(RepositoryModel repository, String label, String createdBy) {
    TicketLabel lb = new TicketMilestone(label);
    Repository db = null;
    try {
        db = repositoryManager.getRepository(repository.name);
        StoredConfig config = db.getConfig();
        config.setString(LABEL, label, COLOR, lb.color);
        config.save();
    } catch (IOException e) {
        log.error("failed to create label " + label + " in " + repository, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return lb;
}

From source file:com.gitblit.tickets.ITicketService.java

License:Apache License

/**
 * Updates a label./*  w  w w  .ja v a2  s  .com*/
 *
 * @param repository
 * @param label
 * @param createdBy
 * @return true if the update was successful
 * @since 1.4.0
 */
public synchronized boolean updateLabel(RepositoryModel repository, TicketLabel label, String createdBy) {
    Repository db = null;
    try {
        db = repositoryManager.getRepository(repository.name);
        StoredConfig config = db.getConfig();
        config.setString(LABEL, label.name, COLOR, label.color);
        config.save();

        return true;
    } catch (IOException e) {
        log.error("failed to update label " + label + " in " + repository, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return false;
}

From source file:com.gitblit.tickets.ITicketService.java

License:Apache License

/**
 * Renames a label.//from   w ww . j  a v  a2s  .  co m
 *
 * @param repository
 * @param oldName
 * @param newName
 * @param createdBy
 * @return true if the rename was successful
 * @since 1.4.0
 */
public synchronized boolean renameLabel(RepositoryModel repository, String oldName, String newName,
        String createdBy) {
    if (StringUtils.isEmpty(newName)) {
        throw new IllegalArgumentException("new label can not be empty!");
    }
    Repository db = null;
    try {
        db = repositoryManager.getRepository(repository.name);
        TicketLabel label = getLabel(repository, oldName);
        StoredConfig config = db.getConfig();
        config.unsetSection(LABEL, oldName);
        config.setString(LABEL, newName, COLOR, label.color);
        config.save();

        for (QueryResult qr : label.tickets) {
            Change change = new Change(createdBy);
            change.unlabel(oldName);
            change.label(newName);
            updateTicket(repository, qr.number, change);
        }

        return true;
    } catch (IOException e) {
        log.error("failed to rename label " + oldName + " in " + repository, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return false;
}

From source file:com.gitblit.tickets.ITicketService.java

License:Apache License

/**
 * Deletes a label./*w  w w  . j av a2 s. c  om*/
 *
 * @param repository
 * @param label
 * @param createdBy
 * @return true if the delete was successful
 * @since 1.4.0
 */
public synchronized boolean deleteLabel(RepositoryModel repository, String label, String createdBy) {
    if (StringUtils.isEmpty(label)) {
        throw new IllegalArgumentException("label can not be empty!");
    }
    Repository db = null;
    try {
        db = repositoryManager.getRepository(repository.name);
        StoredConfig config = db.getConfig();
        config.unsetSection(LABEL, label);
        config.save();

        return true;
    } catch (IOException e) {
        log.error("failed to delete label " + label + " in " + repository, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return false;
}