123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- /*
- * Copyright 2014 gitblit.com.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.gitblit.tickets;
-
- import java.io.File;
- import java.io.IOException;
- import java.text.MessageFormat;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.atomic.AtomicLong;
-
- import javax.inject.Inject;
-
- import org.eclipse.jgit.lib.Repository;
-
- import com.gitblit.Constants;
- import com.gitblit.manager.INotificationManager;
- import com.gitblit.manager.IRepositoryManager;
- import com.gitblit.manager.IRuntimeManager;
- import com.gitblit.manager.IUserManager;
- import com.gitblit.models.RepositoryModel;
- import com.gitblit.models.TicketModel;
- import com.gitblit.models.TicketModel.Attachment;
- import com.gitblit.models.TicketModel.Change;
- import com.gitblit.utils.ArrayUtils;
- import com.gitblit.utils.FileUtils;
- import com.gitblit.utils.StringUtils;
-
- /**
- * Implementation of a ticket service based on a directory within the repository.
- * All tickets are serialized as a list of JSON changes and persisted in a hashed
- * directory structure, similar to the standard git loose object structure.
- *
- * @author James Moger
- *
- */
- public class FileTicketService extends ITicketService {
-
- private static final String JOURNAL = "journal.json";
-
- private static final String TICKETS_PATH = "tickets/";
-
- private final Map<String, AtomicLong> lastAssignedId;
-
- @Inject
- public FileTicketService(
- IRuntimeManager runtimeManager,
- INotificationManager notificationManager,
- IUserManager userManager,
- IRepositoryManager repositoryManager) {
-
- super(runtimeManager,
- notificationManager,
- userManager,
- repositoryManager);
-
- lastAssignedId = new ConcurrentHashMap<String, AtomicLong>();
- }
-
- @Override
- public FileTicketService start() {
- return this;
- }
-
- @Override
- protected void resetCachesImpl() {
- lastAssignedId.clear();
- }
-
- @Override
- protected void resetCachesImpl(RepositoryModel repository) {
- if (lastAssignedId.containsKey(repository.name)) {
- lastAssignedId.get(repository.name).set(0);
- }
- }
-
- @Override
- protected void close() {
- }
-
- /**
- * Returns the ticket path. This follows the same scheme as Git's object
- * store path where the first two characters of the hash id are the root
- * folder with the remaining characters as a subfolder within that folder.
- *
- * @param ticketId
- * @return the root path of the ticket content in the ticket directory
- */
- private String toTicketPath(long ticketId) {
- StringBuilder sb = new StringBuilder();
- sb.append(TICKETS_PATH);
- long m = ticketId % 100L;
- if (m < 10) {
- sb.append('0');
- }
- sb.append(m);
- sb.append('/');
- sb.append(ticketId);
- return sb.toString();
- }
-
- /**
- * Returns the path to the attachment for the specified ticket.
- *
- * @param ticketId
- * @param filename
- * @return the path to the specified attachment
- */
- private String toAttachmentPath(long ticketId, String filename) {
- return toTicketPath(ticketId) + "/attachments/" + filename;
- }
-
- /**
- * Ensures that we have a ticket for this ticket id.
- *
- * @param repository
- * @param ticketId
- * @return true if the ticket exists
- */
- @Override
- public boolean hasTicket(RepositoryModel repository, long ticketId) {
- boolean hasTicket = false;
- Repository db = repositoryManager.getRepository(repository.name);
- try {
- String journalPath = toTicketPath(ticketId) + "/" + JOURNAL;
- hasTicket = new File(db.getDirectory(), journalPath).exists();
- } finally {
- db.close();
- }
- return hasTicket;
- }
-
- /**
- * Assigns a new ticket id.
- *
- * @param repository
- * @return a new long id
- */
- @Override
- public synchronized long assignNewId(RepositoryModel repository) {
- long newId = 0L;
- Repository db = repositoryManager.getRepository(repository.name);
- try {
- if (!lastAssignedId.containsKey(repository.name)) {
- lastAssignedId.put(repository.name, new AtomicLong(0));
- }
- AtomicLong lastId = lastAssignedId.get(repository.name);
- if (lastId.get() <= 0) {
- // identify current highest ticket id by scanning the paths in the tip tree
- File dir = new File(db.getDirectory(), TICKETS_PATH);
- dir.mkdirs();
- List<File> journals = findAll(dir, JOURNAL);
- for (File journal : journals) {
- // 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);
- if (ticketId > lastId.get()) {
- lastId.set(ticketId);
- }
- }
- }
-
- // assign the id and touch an empty journal to hold it's place
- newId = lastId.incrementAndGet();
- String journalPath = toTicketPath(newId) + "/" + JOURNAL;
- File journal = new File(db.getDirectory(), journalPath);
- journal.getParentFile().mkdirs();
- journal.createNewFile();
- } catch (IOException e) {
- log.error("failed to assign ticket id", e);
- return 0L;
- } finally {
- db.close();
- }
- return newId;
- }
-
- /**
- * 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
- * @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();
- }
- }
-
- private List<File> findAll(File dir, String filename) {
- List<File> list = new ArrayList<File>();
- for (File file : dir.listFiles()) {
- if (file.isDirectory()) {
- list.addAll(findAll(file, filename));
- } else if (file.isFile()) {
- if (file.getName().equals(filename)) {
- list.add(file);
- }
- }
- }
- return list;
- }
-
- /**
- * Retrieves the ticket from the repository by first looking-up the changeId
- * associated with the ticketId.
- *
- * @param repository
- * @param ticketId
- * @return a ticket, if it exists, otherwise null
- */
- @Override
- protected TicketModel getTicketImpl(RepositoryModel repository, long ticketId) {
- Repository db = repositoryManager.getRepository(repository.name);
- try {
- List<Change> changes = getJournal(db, ticketId);
- if (ArrayUtils.isEmpty(changes)) {
- log.warn("Empty journal for {}:{}", repository, ticketId);
- return null;
- }
- TicketModel ticket = TicketModel.buildTicket(changes);
- if (ticket != null) {
- ticket.project = repository.projectPath;
- ticket.repository = repository.name;
- ticket.number = ticketId;
- }
- return ticket;
- } finally {
- db.close();
- }
- }
-
- /**
- * Returns the journal for the specified ticket.
- *
- * @param db
- * @param ticketId
- * @return a list of changes
- */
- private List<Change> getJournal(Repository db, long ticketId) {
- if (ticketId <= 0L) {
- return new ArrayList<Change>();
- }
-
- String journalPath = toTicketPath(ticketId) + "/" + JOURNAL;
- File journal = new File(db.getDirectory(), journalPath);
- if (!journal.exists()) {
- return new ArrayList<Change>();
- }
-
- String json = null;
- try {
- json = new String(FileUtils.readContent(journal), Constants.ENCODING);
- } catch (Exception e) {
- log.error(null, e);
- }
- if (StringUtils.isEmpty(json)) {
- return new ArrayList<Change>();
- }
- List<Change> list = TicketSerializer.deserializeJournal(json);
- return list;
- }
-
- @Override
- public boolean supportsAttachments() {
- return true;
- }
-
- /**
- * Retrieves the specified attachment from a ticket.
- *
- * @param repository
- * @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();
- }
- }
-
- /**
- * Deletes a ticket from the repository.
- *
- * @param ticket
- * @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;
- }
-
- /**
- * Commit a ticket change to the repository.
- *
- * @param repository
- * @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;
- }
-
- @Override
- protected boolean deleteAllImpl(RepositoryModel repository) {
- Repository db = repositoryManager.getRepository(repository.name);
- 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;
- }
-
- @Override
- protected boolean renameImpl(RepositoryModel oldRepository, RepositoryModel newRepository) {
- return true;
- }
-
- @Override
- public String toString() {
- return getClass().getSimpleName();
- }
- }
|