You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilestoreModel.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2015 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.models;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.NoSuchElementException;
  22. /**
  23. * A FilestoreModel represents a file stored outside a repository but referenced by the repository using a unique objectID
  24. *
  25. * @author Paul Martin
  26. *
  27. */
  28. public class FilestoreModel implements Serializable {
  29. private static final long serialVersionUID = 1L;
  30. public final String oid;
  31. private Long size;
  32. private Status status;
  33. //Audit
  34. private String stateChangedBy;
  35. private Date stateChangedOn;
  36. //Access Control
  37. private List<String> repositories;
  38. public FilestoreModel(String id, long expectedSize, UserModel user, String repo) {
  39. oid = id;
  40. size = expectedSize;
  41. status = Status.Upload_Pending;
  42. stateChangedBy = user.getName();
  43. stateChangedOn = new Date();
  44. repositories = new ArrayList<String>();
  45. repositories.add(repo);
  46. }
  47. public synchronized long getSize() {
  48. return size;
  49. }
  50. public synchronized Status getStatus() {
  51. return status;
  52. }
  53. public synchronized String getChangedBy() {
  54. return stateChangedBy;
  55. }
  56. public synchronized Date getChangedOn() {
  57. return stateChangedOn;
  58. }
  59. public synchronized void setStatus(Status status, UserModel user) {
  60. this.status = status;
  61. stateChangedBy = user.getName();
  62. stateChangedOn = new Date();
  63. }
  64. public synchronized void reset(UserModel user, long size) {
  65. status = Status.Upload_Pending;
  66. stateChangedBy = user.getName();
  67. stateChangedOn = new Date();
  68. this.size = size;
  69. }
  70. /*
  71. * Handles possible race condition with concurrent connections
  72. * @return true if action can proceed, false otherwise
  73. */
  74. public synchronized boolean actionUpload(UserModel user) {
  75. if (status == Status.Upload_Pending) {
  76. status = Status.Upload_In_Progress;
  77. stateChangedBy = user.getName();
  78. stateChangedOn = new Date();
  79. return true;
  80. }
  81. return false;
  82. }
  83. public synchronized boolean isInErrorState() {
  84. return (this.status.value < 0);
  85. }
  86. public synchronized void addRepository(String repo) {
  87. if (!repositories.contains(repo)) {
  88. repositories.add(repo);
  89. }
  90. }
  91. public synchronized void removeRepository(String repo) {
  92. repositories.remove(repo);
  93. }
  94. public synchronized boolean isInRepositoryList(List<String> repoList) {
  95. for (String name : repositories) {
  96. if (repoList.contains(name)) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. public static enum Status {
  103. Deleted(-30),
  104. AuthenticationRequired(-20),
  105. Error_Unknown(-8),
  106. Error_Unexpected_Stream_End(-7),
  107. Error_Invalid_Oid(-6),
  108. Error_Invalid_Size(-5),
  109. Error_Hash_Mismatch(-4),
  110. Error_Size_Mismatch(-3),
  111. Error_Exceeds_Size_Limit(-2),
  112. Error_Unauthorized(-1),
  113. //Negative values provide additional information and may be treated as 0 when not required
  114. Unavailable(0),
  115. Upload_Pending(1),
  116. Upload_In_Progress(2),
  117. Available(3);
  118. final int value;
  119. Status(int value) {
  120. this.value = value;
  121. }
  122. public int getValue() {
  123. return value;
  124. }
  125. @Override
  126. public String toString() {
  127. return name().toLowerCase().replace('_', ' ');
  128. }
  129. public static Status fromState(int state) {
  130. for (Status s : values()) {
  131. if (s.getValue() == state) {
  132. return s;
  133. }
  134. }
  135. throw new NoSuchElementException(String.valueOf(state));
  136. }
  137. }
  138. }