1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.internal.storage.repository.git;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.io.File;
22  import java.util.concurrent.Executor;
23  import java.util.function.Supplier;
24  
25  import javax.annotation.Nullable;
26  
27  import com.linecorp.centraldogma.common.Author;
28  import com.linecorp.centraldogma.common.CentralDogmaException;
29  import com.linecorp.centraldogma.common.RepositoryExistsException;
30  import com.linecorp.centraldogma.common.RepositoryNotFoundException;
31  import com.linecorp.centraldogma.server.internal.storage.DirectoryBasedStorageManager;
32  import com.linecorp.centraldogma.server.internal.storage.repository.RepositoryCache;
33  import com.linecorp.centraldogma.server.storage.project.Project;
34  import com.linecorp.centraldogma.server.storage.repository.Repository;
35  import com.linecorp.centraldogma.server.storage.repository.RepositoryManager;
36  
37  public class GitRepositoryManager extends DirectoryBasedStorageManager<Repository>
38          implements RepositoryManager {
39  
40      private final Project parent;
41      private final Executor repositoryWorker;
42  
43      @Nullable
44      private final RepositoryCache cache;
45  
46      public GitRepositoryManager(Project parent, File rootDir, Executor repositoryWorker,
47                                  Executor purgeWorker, @Nullable RepositoryCache cache) {
48          super(rootDir, Repository.class, purgeWorker);
49          this.parent = requireNonNull(parent, "parent");
50          this.repositoryWorker = requireNonNull(repositoryWorker, "repositoryWorker");
51          this.cache = cache;
52          init();
53      }
54  
55      @Override
56      public Project parent() {
57          return parent;
58      }
59  
60      @Override
61      protected Repository openChild(File childDir) throws Exception {
62          return new GitRepository(parent, childDir, repositoryWorker, cache);
63      }
64  
65      @Override
66      protected Repository createChild(File childDir, Author author, long creationTimeMillis) throws Exception {
67          return new GitRepository(parent, childDir, repositoryWorker,
68                                   creationTimeMillis, author, cache);
69      }
70  
71      @Override
72      protected void closeChild(File childDir, Repository child,
73                                Supplier<CentralDogmaException> failureCauseSupplier) {
74          ((GitRepository) child).close(failureCauseSupplier);
75      }
76  
77      @Override
78      protected CentralDogmaException newStorageExistsException(String name) {
79          return new RepositoryExistsException(parent().name() + '/' + name);
80      }
81  
82      @Override
83      protected CentralDogmaException newStorageNotFoundException(String name) {
84          return new RepositoryNotFoundException(parent().name() + '/' + name);
85      }
86  }