James Moger
2015-12-08 f75535759570bbc4784ee8324b0d1b8dfb01766f
commit | author | age
bd0e83 1 /*
PM 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
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.NoSuchElementException;
23
24 /**
25  * A FilestoreModel represents a file stored outside a repository but referenced by the repository using a unique objectID
26  * 
27  * @author Paul Martin
28  *
29  */
30 public class FilestoreModel implements Serializable {
31
32     private static final long serialVersionUID = 1L;
33
34     public final String oid;
35     
36     private Long size;
37     private Status status;
38     
39     //Audit
40     private String stateChangedBy;
41     private Date stateChangedOn;
42
43     //Access Control
44     private List<String> repositories;
45     
46     public FilestoreModel(String id, long expectedSize, UserModel user, String repo) {
47         oid = id;
48         size = expectedSize;
49         status = Status.Upload_Pending;
50         stateChangedBy = user.getName();
51         stateChangedOn = new Date();
52         repositories = new ArrayList<String>();
53         repositories.add(repo);
54     }
55     
56     public synchronized long getSize() {
57         return size;
58     }
59     
60     public synchronized Status getStatus() {
61         return status;
62     }
63     
64     public synchronized String getChangedBy() {
65         return stateChangedBy;
66     }
67     
68     public synchronized Date getChangedOn() {
69         return stateChangedOn;
70     }
71     
72     public synchronized void setStatus(Status status, UserModel user) {
73         this.status = status;
74         stateChangedBy = user.getName();
75         stateChangedOn = new Date();
76     }
77     
78     public synchronized void reset(UserModel user, long size) {
79         status = Status.Upload_Pending;
80         stateChangedBy = user.getName();
81         stateChangedOn = new Date();
82         this.size = size;
83     }
84     
85     /*
86      *  Handles possible race condition with concurrent connections
87      *  @return true if action can proceed, false otherwise
88      */
89     public synchronized boolean actionUpload(UserModel user) {
90         if (status == Status.Upload_Pending) {
91             status = Status.Upload_In_Progress;
92             stateChangedBy = user.getName();
93             stateChangedOn = new Date();
94             return true;
95         }
96         
97         return false;
98     }
99     
100     public synchronized boolean isInErrorState() {
101         return (this.status.value < 0);
102     }
103     
104     public synchronized void addRepository(String repo) {
105         if (!repositories.contains(repo)) {
106             repositories.add(repo);
107         }    
108     }
109     
110     public synchronized void removeRepository(String repo) {
111         repositories.remove(repo);
112     }
113     
697905 114     public synchronized boolean isInRepositoryList(List<String> repoList) {
PM 115         for (String name : repositories) {
116             if (repoList.contains(name)) {
117                 return true;
118             }
119         }
120         return false;
121     }
122     
bd0e83 123     public static enum Status {
PM 124
125         Deleted(-30),
126         AuthenticationRequired(-20),
127         
128         Error_Unknown(-8),
129         Error_Unexpected_Stream_End(-7),
130         Error_Invalid_Oid(-6),
131         Error_Invalid_Size(-5),
132         Error_Hash_Mismatch(-4),
133         Error_Size_Mismatch(-3), 
134         Error_Exceeds_Size_Limit(-2),
135         Error_Unauthorized(-1),
136         //Negative values provide additional information and may be treated as 0 when not required
137         Unavailable(0),
138         Upload_Pending(1),
139         Upload_In_Progress(2),
140         Available(3);
141
142         final int value;
143
144         Status(int value) {
145             this.value = value;
146         }
147
148         public int getValue() {
149             return value;
150         }
151
152         @Override
153         public String toString() {
154             return name().toLowerCase().replace('_', ' ');
155         }
156
157         public static Status fromState(int state) {
158             for (Status s : values()) {
159                 if (s.getValue() == state) {
160                     return s;
161                 }
162             }
163             throw new NoSuchElementException(String.valueOf(state));
164         }
165     }
166
167 }
168