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.project;
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.ProjectExistsException;
30  import com.linecorp.centraldogma.common.ProjectNotFoundException;
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.project.ProjectManager;
35  
36  import io.micrometer.core.instrument.MeterRegistry;
37  
38  public class DefaultProjectManager extends DirectoryBasedStorageManager<Project> implements ProjectManager {
39  
40      private final Executor repositoryWorker;
41      @Nullable
42      private final RepositoryCache cache;
43  
44      public DefaultProjectManager(File rootDir, Executor repositoryWorker, Executor purgeWorker,
45                                   MeterRegistry meterRegistry, @Nullable String cacheSpec) {
46          super(rootDir, Project.class, purgeWorker);
47  
48          requireNonNull(meterRegistry, "meterRegistry");
49          requireNonNull(repositoryWorker, "repositoryWorker");
50  
51          this.repositoryWorker = repositoryWorker;
52          cache = cacheSpec != null ? new RepositoryCache(cacheSpec, meterRegistry) : null;
53  
54          init();
55      }
56  
57      @Override
58      public void close(Supplier<CentralDogmaException> failureCauseSupplier) {
59          super.close(failureCauseSupplier);
60          if (cache != null) {
61              cache.clear();
62          }
63      }
64  
65      @Override
66      protected Project openChild(File childDir) throws Exception {
67          return new DefaultProject(childDir, repositoryWorker, purgeWorker(), cache);
68      }
69  
70      @Override
71      protected Project createChild(File childDir, Author author, long creationTimeMillis) throws Exception {
72          return new DefaultProject(childDir, repositoryWorker, purgeWorker(), creationTimeMillis, author, cache);
73      }
74  
75      @Override
76      protected void closeChild(File childDir, Project child,
77                                Supplier<CentralDogmaException> failureCauseSupplier) {
78          final DefaultProject c = (DefaultProject) child;
79          c.repos.close(failureCauseSupplier);
80      }
81  
82      @Override
83      protected CentralDogmaException newStorageExistsException(String name) {
84          return new ProjectExistsException(name);
85      }
86  
87      @Override
88      protected CentralDogmaException newStorageNotFoundException(String name) {
89          return new ProjectNotFoundException(name);
90      }
91  }